1 7 package org.jboss.ejb3.injection; 8 9 import org.jboss.ejb3.BeanContext; 10 11 import javax.naming.Context ; 12 import javax.naming.NamingException ; 13 import java.lang.reflect.InvocationTargetException ; 14 import java.lang.reflect.Method ; 15 16 23 public class JndiMethodInjector implements Injector 24 { 25 private Method setMethod; 26 private String jndiName; 27 private Context ctx; 28 29 public JndiMethodInjector(Method setMethod, String jndiName, Context ctx) 30 { 31 this.setMethod = setMethod; 32 this.jndiName = jndiName; 33 this.ctx = ctx; 34 } 35 36 public void inject(BeanContext bctx) 37 { 38 Object dependency = null; 39 try 40 { 41 dependency = ctx.lookup(jndiName); 42 } 43 catch (NamingException e) 44 { 45 throw new RuntimeException ("Unable to @Inject jndi dependency: " + jndiName + " into method " + setMethod, e); 46 } 47 Object [] args = {dependency}; 48 try 49 { 50 setMethod.invoke(bctx.getInstance(), args); 51 } 52 catch (IllegalAccessException e) 53 { 54 throw new RuntimeException (e); } 56 catch (IllegalArgumentException e) 57 { 58 String type = "UNKNOWN"; 59 if (dependency != null) type = dependency.getClass().getName(); 60 throw new RuntimeException ("Non matching type for @Inject of setter: " + setMethod + " for type: " + type, e); } 62 catch (InvocationTargetException e) 63 { 64 throw new RuntimeException (e.getCause()); } 66 } 67 } 68 | Popular Tags |