1 22 package org.jboss.aspects.tx; 23 24 import org.jboss.aop.annotation.AnnotationElement; 25 import org.jboss.aop.joinpoint.ConstructorInvocation; 26 import org.jboss.aop.joinpoint.FieldReadInvocation; 27 import org.jboss.aop.joinpoint.FieldWriteInvocation; 28 import org.jboss.aspects.Injected; 29 import org.jboss.tm.TransactionManagerLocator; 30 31 import javax.transaction.TransactionManager ; 32 import java.lang.reflect.Method ; 33 34 41 public class TransactionManagerInjector 42 { 43 public Object access(FieldReadInvocation invocation) throws Throwable 44 { 45 return TransactionManagerLocator.getInstance().locate(); 46 } 47 48 public Object access(FieldWriteInvocation invocation) throws Throwable 49 { 50 throw new RuntimeException ("It is illegal to set an injected TransactionManager field."); 51 } 52 53 public Object allocation(ConstructorInvocation invocation) throws Throwable 54 { 55 Object obj = invocation.invokeNext(); 56 57 try 58 { 59 Object [] arg = {TransactionManagerLocator.getInstance().locate()}; 60 Method [] methods = obj.getClass().getMethods(); 61 for (int i = 0; i < methods.length; i++) 62 { 63 if (methods[i].getParameterTypes().length == 1) 64 { 65 if (methods[i].getParameterTypes()[0].equals(TransactionManager .class)) 66 { 67 if (AnnotationElement.isAnyAnnotationPresent(methods[i], Injected.class)) 68 { 69 methods[i].invoke(obj, arg); 70 } 71 } 72 } 73 } 74 } 75 catch (Exception e) 76 { 77 throw new RuntimeException (e); } 79 return obj; 80 } 81 } 82 | Popular Tags |