1 22 package org.jboss.ejb3.tx; 23 24 import java.lang.reflect.Method ; 25 import javax.ejb.TransactionAttribute ; 26 import javax.ejb.TransactionAttributeType ; 27 import javax.ejb.TransactionManagementType ; 28 import org.jboss.annotation.ejb.TransactionTimeout; 29 import org.jboss.aop.Advisor; 30 import org.jboss.aop.joinpoint.Joinpoint; 31 import org.jboss.aop.joinpoint.MethodJoinpoint; 32 import org.jboss.aspects.tx.TxInterceptor; 33 import org.jboss.logging.Logger; 34 import org.jboss.tm.TransactionManagerLocator; 35 import org.jboss.tm.TxManager; 36 import org.jboss.ejb3.stateful.StatefulContainer; 37 import org.jboss.ejb3.stateless.StatelessContainer; 38 39 45 public class TxInterceptorFactory extends org.jboss.aspects.tx.TxInterceptorFactory 46 { 47 private static final Logger log = Logger 48 .getLogger(TxInterceptorFactory.class); 49 50 protected String resolveTxType(Advisor advisor, Joinpoint jp) 51 { 52 Method method = ((MethodJoinpoint) jp).getMethod(); 53 TransactionAttribute tx = (TransactionAttribute ) advisor.resolveAnnotation(method, TransactionAttribute .class); 54 55 if (tx == null) 56 tx = (TransactionAttribute ) advisor.resolveAnnotation(TransactionAttribute .class); 57 58 String value = "REQUIRED"; 59 if (tx != null) 60 { 61 TransactionAttributeType type = tx.value(); 62 63 if (type == null) 64 { 65 value = "REQUIRED"; 66 } 67 else if (type == TransactionAttributeType.NOT_SUPPORTED) 68 { 69 value = "NOTSUPPORTED"; 70 } 71 else if (type == TransactionAttributeType.REQUIRES_NEW) 72 { 73 value = "REQUIRESNEW"; 74 } 75 else 76 { 77 value = type.name(); 78 } 79 } 80 81 return value; 82 } 83 84 protected int resolveTransactionTimeout(Advisor advisor, Method method) 85 { 86 TransactionTimeout annotation = (TransactionTimeout)advisor.resolveAnnotation(method, TransactionTimeout.class); 87 if (annotation != null) 88 { 89 return annotation.value(); 90 } 91 92 return -1; 93 } 94 95 protected void initializePolicy() 96 { 97 policy = new Ejb3TxPolicy(); 98 } 99 100 public Object createPerJoinpoint(Advisor advisor, Joinpoint jp) 101 { 102 TransactionManagementType type = TxUtil.getTransactionManagementType(advisor); 104 if (type == TransactionManagementType.BEAN) 105 return new BMTInterceptor(TxUtil.getTransactionManager(), !(advisor instanceof StatefulContainer)); 106 107 Method method = ((MethodJoinpoint) jp).getMethod(); 108 int timeout = resolveTransactionTimeout(advisor, method); 109 110 if (policy == null); 111 super.initialize(); 112 113 String txType = resolveTxType(advisor, jp).toUpperCase(); 114 if (txType.equals("REQUIRED")) 115 { 116 return new TxInterceptor.Required(TxUtil.getTransactionManager(), policy, timeout); 117 } 118 else if (txType.equals("REQUIRESNEW")) 119 { 120 return new TxInterceptor.RequiresNew(TxUtil.getTransactionManager(), policy, timeout); 121 } 122 else 123 { 124 return super.createPerJoinpoint(advisor, jp); 125 } 126 } 127 128 129 public String getName() 130 { 131 return getClass().getName(); 132 } 133 } 134 | Popular Tags |