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.Field ; 14 15 22 public class JndiFieldInjector implements Injector 23 { 24 private Field field; 25 private String jndiName; 26 private Context ctx; 27 28 public JndiFieldInjector(Field field, String jndiName, Context ctx) 29 { 30 this.field = field; 31 this.field.setAccessible(true); 32 this.jndiName = jndiName; 33 this.ctx = ctx; 34 } 35 36 public JndiFieldInjector(Field field, Context ctx) 37 { 38 this(field, field.getName(), ctx); 39 } 40 41 public void inject(BeanContext bctx) 42 { 43 Object dependency = null; 44 try 45 { 46 dependency = ctx.lookup(jndiName); 47 } 48 catch (NamingException e) 49 { 50 throw new RuntimeException ("Unable to @Inject jndi dependency: " + jndiName + " into field " + field + " of class " + field.getDeclaringClass().getName(), e); 51 } 52 try 53 { 54 field.set(bctx.getInstance(), dependency); 55 } 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 field: " + field.toString() + " for type: " + type, e); } 62 catch (IllegalAccessException e) 63 { 64 throw new RuntimeException (e); } 66 } 67 } 68 | Popular Tags |