1 22 package org.jboss.injection; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import org.jboss.ejb3.BeanContext; 27 import org.jboss.logging.Logger; 28 29 35 public class EntityManagerFactoryMethodInjector implements Injector, PojoInjector 36 { 37 private static final Logger log = Logger.getLogger(EntityManagerFactoryMethodInjector.class); 38 private Method setMethod; 39 private Object factory; 40 41 public EntityManagerFactoryMethodInjector(Method setMethod, Object factory) 42 { 43 this.setMethod = setMethod; 44 setMethod.setAccessible(true); 45 this.factory = factory; 46 } 47 48 public void inject(BeanContext ctx) 49 { 50 inject(ctx, ctx.getInstance()); 51 } 52 53 public void inject(BeanContext ctx, Object instance) 54 { 55 inject(instance); 56 } 57 58 public void inject(Object instance) 59 { 60 try 61 { 62 Object [] args = {factory}; 63 setMethod.invoke(instance, args); 64 } 65 catch (IllegalAccessException e) 66 { 67 throw new RuntimeException (e); } 69 catch (IllegalArgumentException e) 70 { 71 throw new RuntimeException ("Failed in setting EntityManager on setter method: " + setMethod.toString()); 72 } 73 catch (InvocationTargetException e) 74 { 75 throw new RuntimeException (e.getCause()); } 77 } 78 79 public Class getInjectionClass() 80 { 81 return setMethod.getParameterTypes()[0]; 82 } 83 } 84 | Popular Tags |