FileDocCategorySizeDatePackage
JndiFieldInjector.javaAPI DocJBoss 4.2.14072Fri Jul 13 20:53:46 BST 2007org.jboss.injection

JndiFieldInjector

public class JndiFieldInjector extends Object implements Injector, PojoInjector
Comment
author
Bill Burke
version
$Revision: 60233 $
deprecated
use JndiPropertyInjector

Fields Summary
private static final Logger
log
private Field
field
private String
jndiName
private Context
ctx
Constructors Summary
public JndiFieldInjector(Field field, String jndiName, Context ctx)


         
   
      this.field = field;
      this.field.setAccessible(true);
      this.jndiName = jndiName;
      this.ctx = ctx;
   
public JndiFieldInjector(Field field, Context ctx)

      this(field, field.getName(), ctx);
   
Methods Summary
public java.lang.reflect.FieldgetField()

      return field;
   
public java.lang.ClassgetInjectionClass()

      return field.getType();
   
public voidinject(org.jboss.ejb3.BeanContext bctx)

      inject(bctx, bctx.getInstance());
   
public voidinject(org.jboss.ejb3.BeanContext bctx, java.lang.Object instance)

      inject(instance);
   
public voidinject(java.lang.Object instance)

      
      Object dependency = lookup(jndiName, field.getType());
      
      try
      {
         field.set(instance, dependency);
      }
      catch (IllegalArgumentException e)
      {
         String type = "UNKNOWN";
         String interfaces = "";
         if (dependency != null)
         {
            type = dependency.getClass().getName();
            Class[] intfs = dependency.getClass().getInterfaces();
            for (Class intf : intfs) interfaces += ", " + intf.getName();
         }
         throw new RuntimeException("Non matching type for inject of field: " + field + " for type: " + type + " of jndiName " + jndiName + "\nintfs: " + interfaces, e);
      }
      catch (IllegalAccessException e)
      {
         throw new RuntimeException(e);
      }
   
protected java.lang.Objectlookup(java.lang.String jndiName, java.lang.Class field)

      Object dependency = null;

      try
      {
         dependency = ctx.lookup(jndiName);

         if (dependency instanceof javax.xml.rpc.Service && !field.isAssignableFrom(javax.xml.rpc.Service.class))
         {
            javax.xml.rpc.Service service = (javax.xml.rpc.Service)dependency;
            dependency = service.getPort(field);
         }
      }
      catch (NamingException e)
      {
         e.printStackTrace();
         throw new RuntimeException("Unable to inject jndi dependency: " + jndiName + " into field " + field, e);
      }
      catch (javax.xml.rpc.ServiceException e)
      {
         e.printStackTrace();
         throw new RuntimeException("Unable to inject jndi webservice dependency: " + jndiName + " into field " + field, e);
      }
      
      return dependency;
   
public java.lang.StringtoString()

      return super.toString() + "{field=" + field + ",jndiName=" + jndiName + "}";