KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > transaction > interceptors > CMTRequiredTransactionInterceptor


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: CMTRequiredTransactionInterceptor.java 942 2006-07-26 09:17:13Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.transaction.interceptors;
27
28 import static javax.transaction.Status.STATUS_ACTIVE JavaDoc;
29 import static javax.transaction.Status.STATUS_MARKED_ROLLBACK JavaDoc;
30
31 import javax.ejb.EJBException JavaDoc;
32 import javax.ejb.TransactionRolledbackLocalException JavaDoc;
33 import javax.transaction.NotSupportedException JavaDoc;
34 import javax.transaction.RollbackException JavaDoc;
35 import javax.transaction.SystemException JavaDoc;
36 import javax.transaction.Transaction JavaDoc;
37
38 import org.objectweb.easybeans.api.EasyBeansInvocationContext;
39 import org.objectweb.easybeans.log.JLog;
40 import org.objectweb.easybeans.log.JLogFactory;
41
42 /**
43  * Defines an interceptor for method using the REQUIRED attribute.
44  * @author Florent Benoit
45  */

46 public class CMTRequiredTransactionInterceptor extends AbsTransactionInterceptor {
47
48     /**
49      * Logger.
50      */

51     private JLog logger = JLogFactory.getLog(CMTRequiredTransactionInterceptor.class);
52
53     /**
54      * Constructor.<br>
55      * Acquire the transaction manager.
56      */

57     public CMTRequiredTransactionInterceptor() {
58         super();
59     }
60
61     /**
62      * Execute transaction as specified with the REQUIRED attribute.
63      * @param invocationContext
64      * context with useful attributes on the current invocation
65      * @return result of the next invocation (to chain interceptors)
66      * @throws Exception
67      * if interceptor fails
68      * @see <a HREF="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0
69      * specification ?12.6.2.2</a>
70      */

71     @Override JavaDoc
72     public Object JavaDoc intercept(final EasyBeansInvocationContext invocationContext) throws Exception JavaDoc {
73         logger.debug("Calling Required TX interceptor");
74
75         // Get current transaction
76
Transaction JavaDoc transaction;
77         try {
78             transaction = getTransactionManager().getTransaction();
79         } catch (SystemException JavaDoc se) {
80             throw new EJBException JavaDoc("Cannot get the current transaction on transaction manager.", se);
81         }
82
83         logger.debug("Transaction found = {0}", transaction);
84
85         /*
86          * If the client invokes the enterprise bean's method while the client
87          * is not associated with a transaction context, the container
88          * automatically starts a new transaction before delegating a method
89          * call to the enterprise bean business method.
90          */

91         boolean startedTransaction = false;
92         if (transaction == null) {
93             try {
94                 getTransactionManager().begin();
95                 startedTransaction = true;
96             } catch (NotSupportedException JavaDoc nse) {
97                 throw new EJBException JavaDoc("Transaction Manager implementation does not support nested transactions.", nse);
98             } catch (SystemException JavaDoc se) {
99                 throw new EJBException JavaDoc("Cannot call begin() on the transaction manager.", se);
100             }
101         }
102         // else
103
/*
104          * If a client invokes the enterprise bean's method while the client is
105          * associated with a transaction context, the container invokes the
106          * enterprise bean's method in the client's transaction context.
107          */

108         boolean gotBusinessException = false;
109         try {
110             return invocationContext.proceed();
111
112         } catch (Exception JavaDoc e) {
113             gotBusinessException = true;
114
115             // Chapter 14.3.1
116
// Runs in the context of the client
117
if (!startedTransaction) {
118                 handleContextClientTransaction(invocationContext, e);
119             } else {
120                 // Container's transaction
121
handleContextContainerTransaction(invocationContext, e);
122             }
123             // Shouldn't come here
124
return null;
125         } finally {
126             if (!gotBusinessException) {
127
128                 // only do some operations if transaction has been started
129
// before
130
// invoking the method.
131
if (startedTransaction) {
132
133                     // sanity check.
134
Transaction JavaDoc transactionAfter = null;
135                     try {
136                         transactionAfter = getTransactionManager().getTransaction();
137                     } catch (SystemException JavaDoc se) {
138                         throw new EJBException JavaDoc("Cannot get the current transaction on transaction manager.", se);
139                     }
140
141                     if (transactionAfter == null) {
142                         throw new RuntimeException JavaDoc("Transaction disappeared.");
143                     }
144
145                     /*
146                      * The container attempts to commit the transaction when the
147                      * business method has completed. The container performs the
148                      * commit protocol before the method result is sent to the
149                      * client.
150                      */

151                     try {
152                         switch (getTransactionManager().getStatus()) {
153                         case STATUS_ACTIVE:
154                             getTransactionManager().commit();
155                             break;
156                         case STATUS_MARKED_ROLLBACK:
157                             getTransactionManager().rollback();
158                             break;
159                         default:
160                             throw new RuntimeException JavaDoc("Unexpected transaction status" + getTransactionManager().getStatus());
161                         }
162                     } catch (RollbackException JavaDoc e) {
163                         throw new TransactionRolledbackLocalException JavaDoc("Could not commit transaction", e);
164                     } catch (Exception JavaDoc e) {
165                         throw new EJBException JavaDoc("Container exception", e);
166                     }
167                 }
168             }
169         }
170
171     }
172
173 }
174
Popular Tags