1 22 package org.jboss.injection; 23 24 import org.jboss.aop.Advisor; 25 import org.jboss.ejb3.BeanContext; 26 import org.jboss.ejb3.Container; 27 import org.jboss.ejb3.tx.TxUtil; 28 import org.jboss.ejb3.tx.UserTransactionImpl; 29 30 import javax.ejb.TransactionManagementType ; 31 import javax.transaction.UserTransaction ; 32 import java.lang.reflect.InvocationTargetException ; 33 import java.lang.reflect.Method ; 34 35 41 public class UserTransactionMethodInjector implements Injector 42 { 43 private Method setMethod; 44 45 public UserTransactionMethodInjector(Method setMethod, InjectionContainer container) 46 { 47 if (container instanceof Container) 48 { 49 TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) container)); 50 if (type != TransactionManagementType.BEAN) 51 throw new IllegalStateException ("Container " + ((Container) container).getEjbName() + ": it is illegal to inject UserTransaction into a CMT bean"); 52 } 53 this.setMethod = setMethod; 54 setMethod.setAccessible(true); 55 } 56 57 public void inject(BeanContext ctx) 58 { 59 Object instance = ctx.getInstance(); 60 inject(instance); 61 } 62 63 public void inject(Object instance) 64 { 65 UserTransaction ut = new UserTransactionImpl(); 66 Object [] args = {ut}; 67 try 68 { 69 setMethod.invoke(instance, args); 70 } 71 catch (IllegalAccessException e) 72 { 73 throw new RuntimeException (e); } 75 catch (IllegalArgumentException e) 76 { 77 throw new RuntimeException ("Failed in setting EntityManager on setter method: " + setMethod.toString()); 78 } 79 catch (InvocationTargetException e) 80 { 81 throw new RuntimeException (e.getCause()); } 83 } 84 85 public Class getInjectionClass() 86 { 87 return setMethod.getParameterTypes()[0]; 88 } 89 } 90 | Popular Tags |