Commit acdef6815f24637f5611925cbd2735d51a797117

Authored by Cleverson Sacramento
1 parent 6a702c16
Exists in master

FWK-207: Criação dos eventos AfterTransactionBegin e

BeforeTransactionComplete 

Task-Url: https://demoiselle.atlassian.net/browse/FWK-207
impl/core/src/main/java/br/gov/frameworkdemoiselle/transaction/AfterTransactionBegin.java 0 → 100644
... ... @@ -0,0 +1,48 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.transaction;
  38 +
  39 +import java.io.Serializable;
  40 +
  41 +/**
  42 + * Event that is fired after the transaction has began.
  43 + *
  44 + * @author SERPRO
  45 + */
  46 +public interface AfterTransactionBegin extends Serializable {
  47 +
  48 +}
... ...
impl/core/src/main/java/br/gov/frameworkdemoiselle/transaction/BeforeTransactionComplete.java 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.transaction;
  38 +
  39 +import java.io.Serializable;
  40 +
  41 +/**
  42 + * Event that is fired before the transaction be committed or rolled back.
  43 + *
  44 + * @author SERPRO
  45 + */
  46 +public interface BeforeTransactionComplete extends Serializable {
  47 +
  48 + boolean isMarkedRollback();
  49 +}
... ...
impl/core/src/main/java/br/gov/frameworkdemoiselle/transaction/TransactionalInterceptor.java
... ... @@ -110,10 +110,10 @@ public class TransactionalInterceptor implements Serializable {
110 110 */
111 111 @AroundInvoke
112 112 public Object manage(final InvocationContext ic) throws Exception {
113   - initiate();
114   -
115 113 Object result = null;
  114 +
116 115 try {
  116 + initiate();
117 117 getLogger().finer(getBundle().getString("transactional-execution", ic.getMethod().toGenericString()));
118 118 result = ic.proceed();
119 119  
... ... @@ -135,6 +135,7 @@ public class TransactionalInterceptor implements Serializable {
135 135 transaction.begin();
136 136 getTransactionInfo().markAsOwner();
137 137 getLogger().fine(getBundle().getString("begin-transaction"));
  138 + fireAfterTransactionBegin();
138 139 }
139 140  
140 141 getTransactionInfo().incrementCounter();
... ... @@ -152,31 +153,23 @@ public class TransactionalInterceptor implements Serializable {
152 153 }
153 154  
154 155 if (rollback) {
155   - transaction.setRollbackOnly();
156   - getLogger().fine(getBundle().getString("transaction-marked-rollback", cause.getMessage()));
  156 + setRollbackOnly(transaction, cause);
157 157 }
158 158 }
159 159 }
160 160  
161   - private void complete() {
  161 + private void setRollbackOnly(Transaction transaction, Exception cause) {
  162 + transaction.setRollbackOnly();
  163 + getLogger().fine(getBundle().getString("transaction-marked-rollback", cause.getMessage()));
  164 + }
  165 +
  166 + private void complete() throws Exception {
162 167 Transaction transaction = getTransactionContext().getCurrentTransaction();
163 168 getTransactionInfo().decrementCounter();
164 169  
165 170 if (getTransactionInfo().getCounter() == 0 && transaction.isActive()) {
166   -
167 171 if (getTransactionInfo().isOwner()) {
168   - if (transaction.isMarkedRollback()) {
169   - transaction.rollback();
170   - getTransactionInfo().clear();
171   -
172   - getLogger().fine(getBundle().getString("transaction-rolledback"));
173   -
174   - } else {
175   - transaction.commit();
176   - getTransactionInfo().clear();
177   -
178   - getLogger().fine(getBundle().getString("transaction-commited"));
179   - }
  172 + complete(transaction);
180 173 }
181 174  
182 175 } else if (getTransactionInfo().getCounter() == 0 && !transaction.isActive()) {
... ... @@ -184,6 +177,47 @@ public class TransactionalInterceptor implements Serializable {
184 177 }
185 178 }
186 179  
  180 + private void complete(Transaction transaction) throws Exception {
  181 + try {
  182 + fireBeforeTransactionComplete(transaction.isMarkedRollback());
  183 +
  184 + } catch (Exception cause) {
  185 + setRollbackOnly(transaction, cause);
  186 + throw cause;
  187 +
  188 + } finally {
  189 + if (transaction.isMarkedRollback()) {
  190 + transaction.rollback();
  191 + getTransactionInfo().clear();
  192 + getLogger().fine(getBundle().getString("transaction-rolledback"));
  193 +
  194 + } else {
  195 + transaction.commit();
  196 + getTransactionInfo().clear();
  197 + getLogger().fine(getBundle().getString("transaction-commited"));
  198 + }
  199 + }
  200 + }
  201 +
  202 + private void fireAfterTransactionBegin() {
  203 + Beans.getBeanManager().fireEvent(new AfterTransactionBegin() {
  204 +
  205 + private static final long serialVersionUID = 1L;
  206 + });
  207 + }
  208 +
  209 + private void fireBeforeTransactionComplete(final boolean markedRollback) {
  210 + Beans.getBeanManager().fireEvent(new BeforeTransactionComplete() {
  211 +
  212 + private static final long serialVersionUID = 1L;
  213 +
  214 + @Override
  215 + public boolean isMarkedRollback() {
  216 + return markedRollback;
  217 + }
  218 + });
  219 + }
  220 +
187 221 private ResourceBundle getBundle() {
188 222 if (bundle == null) {
189 223 bundle = Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-core-bundle"));
... ...