KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > annotations > helper > bean > TransactionResolver


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: TransactionResolver.java 817 2006-07-04 12:05:43Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.annotations.helper.bean;
27
28 import static javax.ejb.TransactionManagementType.BEAN JavaDoc;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.ejb.TransactionAttributeType JavaDoc;
34 import javax.ejb.TransactionManagementType JavaDoc;
35
36 import org.objectweb.asm.Type;
37 import org.objectweb.easybeans.deployment.annotations.JClassInterceptor;
38 import org.objectweb.easybeans.deployment.annotations.JMethod;
39 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
40 import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
41 import org.objectweb.easybeans.persistence.interceptors.NoTxMethodCallOnlyEntityManagerInterceptor;
42 import org.objectweb.easybeans.transaction.interceptors.BMTStatefulTransactionInterceptor;
43 import org.objectweb.easybeans.transaction.interceptors.BMTStatelessTransactionInterceptor;
44 import org.objectweb.easybeans.transaction.interceptors.BMTTransactionInterceptor;
45 import org.objectweb.easybeans.transaction.interceptors.CMTMandatoryTransactionInterceptor;
46 import org.objectweb.easybeans.transaction.interceptors.CMTNeverTransactionInterceptor;
47 import org.objectweb.easybeans.transaction.interceptors.CMTNotSupportedTransactionInterceptor;
48 import org.objectweb.easybeans.transaction.interceptors.CMTRequiredTransactionInterceptor;
49 import org.objectweb.easybeans.transaction.interceptors.CMTRequiresNewTransactionInterceptor;
50 import org.objectweb.easybeans.transaction.interceptors.CMTSupportsTransactionInterceptor;
51 import org.objectweb.easybeans.transaction.interceptors.ListenerSessionSynchronizationInterceptor;
52
53 /**
54  * This class adds the interceptor for transaction on a given method.
55  * @author Florent Benoit
56  */

57 public final class TransactionResolver {
58
59     /**
60      * Signature of EasyBeans interceptors.
61      */

62     private static final JMethod EASYBEANS_INTERCEPTOR = new JMethod(0, "intercept",
63             "(Lorg/objectweb/easybeans/api/EasyBeansInvocationContext;)Ljava/lang/Object;", null,
64             new String JavaDoc[] {"java/lang/Exception"});
65
66     /**
67      * CMT Required transaction interceptor.
68      */

69     private static final String JavaDoc CMT_REQUIRED_INTERCEPTOR = Type
70             .getInternalName(CMTRequiredTransactionInterceptor.class);
71
72     /**
73      * CMT Mandatory transaction interceptor.
74      */

75     private static final String JavaDoc CMT_MANDATORY_INTERCEPTOR = Type
76             .getInternalName(CMTMandatoryTransactionInterceptor.class);
77
78     /**
79      * CMT Never transaction interceptor.
80      */

81     private static final String JavaDoc CMT_NEVER_INTERCEPTOR = Type.getInternalName(CMTNeverTransactionInterceptor.class);
82
83     /**
84      * CMT NotSupported transaction interceptor.
85      */

86     private static final String JavaDoc CMT_NOT_SUPPORTED_INTERCEPTOR = Type
87             .getInternalName(CMTNotSupportedTransactionInterceptor.class);
88
89     /**
90      * CMT Supports transaction interceptor.
91      */

92     private static final String JavaDoc CMT_SUPPORTS_INTERCEPTOR = Type
93             .getInternalName(CMTSupportsTransactionInterceptor.class);
94
95     /**
96      * CMT RequiresNew transaction interceptor.
97      */

98     private static final String JavaDoc CMT_REQUIRES_NEW_INTERCEPTOR = Type
99             .getInternalName(CMTRequiresNewTransactionInterceptor.class);
100
101     /**
102      * BMT transaction interceptor.
103      */

104     private static final String JavaDoc BMT_INTERCEPTOR = Type.getInternalName(BMTTransactionInterceptor.class);
105
106     /**
107      * BMT stateful transaction interceptor.
108      */

109     private static final String JavaDoc BMT_STATEFUL_INTERCEPTOR = Type.getInternalName(BMTStatefulTransactionInterceptor.class);
110
111     /**
112      * BMT stateless transaction interceptor.
113      */

114     private static final String JavaDoc BMT_STATELESS_INTERCEPTOR = Type.getInternalName(BMTStatelessTransactionInterceptor.class);
115
116     /**
117      * ListenerSessionSynchronizationInterceptor transaction interceptor.
118      */

119     private static final String JavaDoc LISTENER_SESSION_SYNCHRO_INTERCEPTOR = Type
120             .getInternalName(ListenerSessionSynchronizationInterceptor.class);
121
122     /**
123      * Interceptor used for transaction-scoped persistence context (when there is no transaction).
124      */

125     private static final String JavaDoc NOTX_TRANSACTION_SCOPED_INTERCEPTOR = Type
126             .getInternalName(NoTxMethodCallOnlyEntityManagerInterceptor.class);
127
128
129     /**
130      * javax.ejb.SessionSynchronization interface.
131      */

132     private static final String JavaDoc SESSION_SYNCHRONIZATION_INTERFACE = "javax/ejb/SessionSynchronization";
133
134     /**
135      * Helper class, no public constructor.
136      */

137     private TransactionResolver() {
138     }
139
140     /**
141      * Adds the right transaction interceptor depending of the transactional
142      * attribute set by the user.
143      * @param bean the given bean on which set the transactional interceptor.
144      */

145     public static void resolve(final ClassAnnotationMetadata bean) {
146         TransactionAttributeType JavaDoc beanTxType = bean.getTransactionAttributeType();
147         TransactionManagementType JavaDoc beanTxManaged = bean.getTransactionManagementType();
148
149
150         // Checks if Synchronization is needed for this stateful bean
151
boolean addSynchro = false;
152         if (bean.isStateful()) {
153                 String JavaDoc[] interfaces = bean.getInterfaces();
154                 if (interfaces != null) {
155                         for (String JavaDoc itf : interfaces) {
156                             if (SESSION_SYNCHRONIZATION_INTERFACE.equals(itf)) {
157                                 addSynchro = true;
158                                 break;
159                             }
160                         }
161                 }
162
163         }
164
165         for (MethodAnnotationMetadata method : bean.getMethodAnnotationMetadataCollection()) {
166
167             List JavaDoc<JClassInterceptor> interceptors = method.getInterceptors();
168             if (interceptors == null) {
169                 interceptors = new ArrayList JavaDoc<JClassInterceptor>();
170             }
171
172             // Bean managed or container managed ?
173
if (beanTxManaged.equals(BEAN)) {
174                 // BMT
175
if (bean.isStateful()) {
176                     interceptors.add(new JClassInterceptor(BMT_STATEFUL_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
177                 } else if (bean.isStateless()){
178                     interceptors.add(new JClassInterceptor(BMT_STATELESS_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
179                 } else {
180                     interceptors.add(new JClassInterceptor(BMT_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
181                 }
182                 // Add interceptor for EntityManager
183
interceptors.add(new JClassInterceptor(NOTX_TRANSACTION_SCOPED_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
184             } else {
185                 // CMT
186
TransactionAttributeType JavaDoc methodTx = method.getTransactionAttributeType();
187
188                 // Set method tx attribute to the class tx attribute if none was
189
// set.
190
if (methodTx == null) {
191                     if (!method.isInherited()) {
192                         methodTx = beanTxType;
193                     } else {
194                         // inherited method, take value of the original class
195
methodTx = method.getOriginalClassAnnotationMetadata().getTransactionAttributeType();
196                     }
197                 }
198
199                 switch (methodTx) {
200                     case MANDATORY:
201                         interceptors.add(new JClassInterceptor(CMT_MANDATORY_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
202                         break;
203                     case NEVER:
204                         interceptors.add(new JClassInterceptor(CMT_NEVER_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
205                         interceptors.add(new JClassInterceptor(NOTX_TRANSACTION_SCOPED_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
206                         break;
207                     case NOT_SUPPORTED:
208                         interceptors.add(new JClassInterceptor(CMT_NOT_SUPPORTED_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
209                         interceptors.add(new JClassInterceptor(NOTX_TRANSACTION_SCOPED_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
210                         break;
211                     case REQUIRED:
212                         interceptors.add(new JClassInterceptor(CMT_REQUIRED_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
213                         break;
214                     case REQUIRES_NEW:
215                         interceptors.add(new JClassInterceptor(CMT_REQUIRES_NEW_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
216                         break;
217                     case SUPPORTS:
218                         interceptors.add(new JClassInterceptor(CMT_SUPPORTS_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
219                         interceptors.add(new JClassInterceptor(NOTX_TRANSACTION_SCOPED_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
220                         break;
221                     default:
222                         throw new IllegalStateException JavaDoc("Invalid tx attribute on method '" + method.getMethodName()
223                                 + "', value = '" + methodTx + "'.");
224                 }
225
226                 // Add listener interceptor for stateul bean only if the bean implements SessionSynchronization interface
227
if (addSynchro) {
228                     interceptors.add(new JClassInterceptor(LISTENER_SESSION_SYNCHRO_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
229                 }
230                 // End CMT
231
}
232             method.setInterceptors(interceptors);
233         }
234     }
235 }
236
Popular Tags