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