1 22 package org.jboss.aspects.tx; 23 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Field ; 27 import java.util.HashMap ; 28 import org.jboss.aop.Advisor; 29 import org.jboss.aop.InstanceAdvisor; 30 import org.jboss.aop.joinpoint.ConstructorJoinpoint; 31 import org.jboss.aop.joinpoint.Joinpoint; 32 import org.jboss.aop.joinpoint.MethodJoinpoint; 33 import org.jboss.aop.joinpoint.FieldJoinpoint; 34 import org.jboss.tm.TransactionManagerLocator; 35 36 42 public class TxInterceptorFactory implements org.jboss.aop.advice.AspectFactory 43 { 44 protected TxPolicy policy; 45 protected HashMap nameMap = new HashMap (); 46 47 protected void initializePolicy() 48 { 49 policy = new TxPolicy(); 50 } 51 52 public void initialize() 53 { 54 if (policy != null) return; 55 initializePolicy(); 56 nameMap.put("NEVER", new TxInterceptor.Never(TransactionManagerLocator.getInstance().locate(), policy)); 57 nameMap.put("NOTSUPPORTED", new TxInterceptor.NotSupported(TransactionManagerLocator.getInstance().locate(), policy)); 58 nameMap.put("SUPPORTS", new TxInterceptor.Supports(TransactionManagerLocator.getInstance().locate(), policy)); 59 nameMap.put("REQUIRED", new TxInterceptor.Required(TransactionManagerLocator.getInstance().locate(), policy)); 60 nameMap.put("REQUIRESNEW", new TxInterceptor.RequiresNew(TransactionManagerLocator.getInstance().locate(), policy)); 61 nameMap.put("MANDATORY", new TxInterceptor.Mandatory(TransactionManagerLocator.getInstance().locate(), policy)); 62 } 63 64 protected String resolveTxType(Advisor advisor, Joinpoint jp) 65 { 66 if (jp instanceof ConstructorJoinpoint) 67 { 68 Constructor con = ((ConstructorJoinpoint)jp).getConstructor(); 69 String txType = (String )advisor.getConstructorMetaData().getConstructorMetaData(con, "transaction", "trans-attribute"); 70 if (txType != null) return txType; 71 72 txType = (String )advisor.getDefaultMetaData().getMetaData("transaction", "trans-attribute"); 73 if (txType != null) return txType; 74 75 Tx tx = (Tx)advisor.resolveAnnotation(con, Tx.class); 76 if (tx == null) 77 { 78 tx = (Tx)advisor.resolveAnnotation(Tx.class); 79 } 80 if (tx == null) return "REQUIRED"; 81 return tx.value().name(); 82 } 83 else if (jp instanceof MethodJoinpoint) 84 { 85 Method con = ((MethodJoinpoint)jp).getMethod(); 86 String txType = (String )advisor.getMethodMetaData().getMethodMetaData(con, "transaction", "trans-attribute"); 87 if (txType != null) return txType; 88 89 txType = (String )advisor.getDefaultMetaData().getMetaData("transaction", "trans-attribute"); 90 if (txType != null) return txType; 91 92 Tx tx = (Tx)advisor.resolveAnnotation(con, Tx.class); 93 if (tx == null) 94 { 95 tx = (Tx)advisor.resolveAnnotation(Tx.class); 96 } 97 if (tx == null) return "REQUIRED"; 98 return tx.value().name(); 99 } 100 else 101 { 102 Field con = ((FieldJoinpoint)jp).getField(); 103 String txType = (String )advisor.getFieldMetaData().getFieldMetaData(con, "transaction", "trans-attribute"); 104 if (txType != null) return txType; 105 106 txType = (String )advisor.getDefaultMetaData().getMetaData("transaction", "trans-attribute"); 107 if (txType != null) return txType; 108 109 Tx tx = (Tx)advisor.resolveAnnotation(con, Tx.class); 110 if (tx == null) 111 { 112 tx = (Tx)advisor.resolveAnnotation(Tx.class); 113 } 114 if (tx == null) return "REQUIRED"; 115 return tx.value().name(); 116 } 117 } 118 119 120 public Object createPerJoinpoint(Advisor advisor, Joinpoint jp) 121 { 122 initialize(); 123 String txType = resolveTxType(advisor, jp); 124 Object rtn = nameMap.get(txType.toUpperCase()); 125 if (rtn == null) throw new RuntimeException ("TX TYPE was null for: " + txType); 126 return rtn; 127 } 128 129 public Object createPerVM() 130 { 131 throw new IllegalStateException ("Scope not allowed"); 132 } 133 134 public Object createPerClass(Advisor advisor) 135 { 136 throw new IllegalStateException ("Scope not allowed"); 137 } 138 139 public Object createPerInstance(Advisor advisor, InstanceAdvisor instanceAdvisor) 140 { 141 throw new IllegalStateException ("Scope not allowed"); 142 } 143 144 public Object createPerJoinpoint(Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp) 145 { 146 throw new IllegalStateException ("Scope not allowed"); 147 } 148 149 public String getName() 150 { 151 return getClass().getName(); 152 } 153 } 154 | Popular Tags |