1 25 26 package org.objectweb.easybeans.deployment.annotations.helper.bean; 27 28 import static javax.ejb.TransactionManagementType.BEAN ; 29 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 import javax.ejb.TransactionAttributeType ; 34 import javax.ejb.TransactionManagementType ; 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 57 public final class TransactionResolver { 58 59 62 private static final JMethod EASYBEANS_INTERCEPTOR = new JMethod(0, "intercept", 63 "(Lorg/objectweb/easybeans/api/EasyBeansInvocationContext;)Ljava/lang/Object;", null, 64 new String [] {"java/lang/Exception"}); 65 66 69 private static final String CMT_REQUIRED_INTERCEPTOR = Type 70 .getInternalName(CMTRequiredTransactionInterceptor.class); 71 72 75 private static final String CMT_MANDATORY_INTERCEPTOR = Type 76 .getInternalName(CMTMandatoryTransactionInterceptor.class); 77 78 81 private static final String CMT_NEVER_INTERCEPTOR = Type.getInternalName(CMTNeverTransactionInterceptor.class); 82 83 86 private static final String CMT_NOT_SUPPORTED_INTERCEPTOR = Type 87 .getInternalName(CMTNotSupportedTransactionInterceptor.class); 88 89 92 private static final String CMT_SUPPORTS_INTERCEPTOR = Type 93 .getInternalName(CMTSupportsTransactionInterceptor.class); 94 95 98 private static final String CMT_REQUIRES_NEW_INTERCEPTOR = Type 99 .getInternalName(CMTRequiresNewTransactionInterceptor.class); 100 101 104 private static final String BMT_INTERCEPTOR = Type.getInternalName(BMTTransactionInterceptor.class); 105 106 109 private static final String BMT_STATEFUL_INTERCEPTOR = Type.getInternalName(BMTStatefulTransactionInterceptor.class); 110 111 114 private static final String BMT_STATELESS_INTERCEPTOR = Type.getInternalName(BMTStatelessTransactionInterceptor.class); 115 116 119 private static final String LISTENER_SESSION_SYNCHRO_INTERCEPTOR = Type 120 .getInternalName(ListenerSessionSynchronizationInterceptor.class); 121 122 125 private static final String NOTX_TRANSACTION_SCOPED_INTERCEPTOR = Type 126 .getInternalName(NoTxMethodCallOnlyEntityManagerInterceptor.class); 127 128 129 132 private static final String SESSION_SYNCHRONIZATION_INTERFACE = "javax/ejb/SessionSynchronization"; 133 134 137 private TransactionResolver() { 138 } 139 140 145 public static void resolve(final ClassAnnotationMetadata bean) { 146 TransactionAttributeType beanTxType = bean.getTransactionAttributeType(); 147 TransactionManagementType beanTxManaged = bean.getTransactionManagementType(); 148 149 150 boolean addSynchro = false; 152 if (bean.isStateful()) { 153 String [] interfaces = bean.getInterfaces(); 154 if (interfaces != null) { 155 for (String 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 <JClassInterceptor> interceptors = method.getInterceptors(); 168 if (interceptors == null) { 169 interceptors = new ArrayList <JClassInterceptor>(); 170 } 171 172 if (beanTxManaged.equals(BEAN)) { 174 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 interceptors.add(new JClassInterceptor(NOTX_TRANSACTION_SCOPED_INTERCEPTOR, EASYBEANS_INTERCEPTOR)); 184 } else { 185 TransactionAttributeType methodTx = method.getTransactionAttributeType(); 187 188 if (methodTx == null) { 191 if (!method.isInherited()) { 192 methodTx = beanTxType; 193 } else { 194 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 ("Invalid tx attribute on method '" + method.getMethodName() 223 + "', value = '" + methodTx + "'."); 224 } 225 226 if (addSynchro) { 228 interceptors.add(new JClassInterceptor(LISTENER_SESSION_SYNCHRO_INTERCEPTOR, EASYBEANS_INTERCEPTOR)); 229 } 230 } 232 method.setInterceptors(interceptors); 233 } 234 } 235 } 236 | Popular Tags |