[jboss-cvs] JBossAS SVN: r73107 - in projects/ejb3/trunk/core/src/main/java/org/jboss: injection and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed May 7 08:36:26 EDT 2008


Author: wolfc
Date: 2008-05-07 08:36:26 -0400 (Wed, 07 May 2008)
New Revision: 73107

Modified:
   projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/JndiUtil.java
   projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiFieldInjector.java
   projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiMethodInjector.java
   projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiPropertyInjector.java
Log:
EJBTHREE-1342: cleaned up redundant and improved error message

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/JndiUtil.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/JndiUtil.java	2008-05-07 11:36:57 UTC (rev 73106)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/JndiUtil.java	2008-05-07 12:36:26 UTC (rev 73107)
@@ -53,6 +53,26 @@
       
       return object;
    }
+
+   public static Object lookupLink(Context jndiContext, String binding)
+      throws NamingException
+   {
+      Object object = null;
+   
+      try
+      {
+         object = jndiContext.lookupLink(binding);
+      }
+      catch (NameNotFoundException e)
+      {
+         Context haCtx = InitialContextFactory.getHAContext(jndiContext);
+         if(haCtx == null)
+            throw e;
+         object = haCtx.lookupLink(binding);
+      }
+      
+      return object;
+   }
 }
 
 

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiFieldInjector.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiFieldInjector.java	2008-05-07 11:36:57 UTC (rev 73106)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiFieldInjector.java	2008-05-07 12:36:26 UTC (rev 73107)
@@ -22,14 +22,12 @@
 package org.jboss.injection;
 
 import java.lang.reflect.Field;
+
 import javax.naming.Context;
-import javax.naming.NamingException;
 
+import org.jboss.injection.lang.reflect.FieldBeanProperty;
 import org.jboss.logging.Logger;
 
-import org.jboss.ejb3.BeanContext;
-import org.jboss.ejb3.JndiUtil;
-
 /**
  * Comment
  *
@@ -37,93 +35,17 @@
  * @version $Revision$
  * @deprecated  use JndiPropertyInjector
  */
-public class JndiFieldInjector implements Injector, PojoInjector
+public class JndiFieldInjector extends JndiPropertyInjector
 {
    private static final Logger log = Logger.getLogger(JndiFieldInjector.class);
    
-   private Field field;
-   private String jndiName;
-   private Context ctx;
-
    public JndiFieldInjector(Field field, String jndiName, Context ctx)
    {
-      this.field = field;
-      this.field.setAccessible(true);
-      this.jndiName = jndiName;
-      this.ctx = ctx;
+      super(new FieldBeanProperty(field), jndiName, ctx);
    }
 
    public JndiFieldInjector(Field field, Context ctx)
    {
       this(field, field.getName(), ctx);
    }
-
-   public void inject(BeanContext bctx)
-   {
-      inject(bctx, bctx.getInstance());
-   }
-
-   public Class getInjectionClass()
-   {
-      return field.getType();
-   }
-
-   public Field getField()
-   {
-      return field;
-   }
-
-   protected Object lookup(String jndiName, Class field)
-   {
-      Object dependency = null;
-
-      try
-      {
-         dependency = JndiUtil.lookup(ctx, jndiName);
-      }
-      catch (NamingException e)
-      {
-         e.printStackTrace();
-         throw new RuntimeException("Unable to inject jndi dependency: " + jndiName + " into field " + field, e);
-      }
-      
-      return dependency;
-   }
-   
-   public void inject(BeanContext bctx, Object instance)
-   {
-      inject(instance);
-   }
-
-   public void inject(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);
-      }
-   }
-   
-   public String toString()
-   {
-      return super.toString() + "{field=" + field + ",jndiName=" + jndiName + "}";
-   }
 }

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiMethodInjector.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiMethodInjector.java	2008-05-07 11:36:57 UTC (rev 73106)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiMethodInjector.java	2008-05-07 12:36:26 UTC (rev 73107)
@@ -21,12 +21,11 @@
  */
 package org.jboss.injection;
 
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+
 import javax.naming.Context;
-import javax.naming.NamingException;
-import org.jboss.ejb3.BeanContext;
-import org.jboss.ejb3.JndiUtil;
+
+import org.jboss.injection.lang.reflect.MethodBeanProperty;
 import org.jboss.logging.Logger;
 
 /**
@@ -36,76 +35,13 @@
  * @version $Revision$
  * @deprecated use JndiPropertyInjector
  */
-public class JndiMethodInjector implements Injector, PojoInjector
+public class JndiMethodInjector extends JndiPropertyInjector
 {
    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(JndiMethodInjector.class);
    
-   private Method setMethod;
-   private String jndiName;
-   private Context ctx;
-
    public JndiMethodInjector(Method setMethod, String jndiName, Context ctx)
    {
-      this.setMethod = setMethod;
-      setMethod.setAccessible(true);
-      this.jndiName = jndiName;
-      this.ctx = ctx;
+      super(new MethodBeanProperty(setMethod), jndiName, ctx);
    }
-
-   public void inject(BeanContext bctx)
-   {
-      inject(bctx, bctx.getInstance());
-   }
-   
-   public Class getInjectionClass()
-   {
-      return setMethod.getParameterTypes()[0];
-   }
-   
-   protected Object lookup(String jndiName, Class param)
-   {
-      Object dependency = null;
-      
-      try
-      {
-         dependency = JndiUtil.lookup(ctx, jndiName);
-      }
-      catch (NamingException e)
-      {
-         e.printStackTrace();
-         throw new RuntimeException("Unable to @Inject jndi dependency: " + jndiName + " into method " + setMethod, e);
-      }
-      return dependency;
-   }
-   
-   public void inject(BeanContext bctx, Object instance)
-   {
-      inject(instance);
-   }
-
-   public void inject(Object instance)
-   {
-      Object dependency = lookup(jndiName, setMethod.getParameterTypes()[0]);
-
-      Object[] args = {dependency};
-      try
-      {
-         setMethod.invoke(instance, args);
-      }
-      catch (IllegalAccessException e)
-      {
-         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
-      }
-      catch (IllegalArgumentException e)
-      {
-         String type = "UNKNOWN";
-         if (dependency != null) type = dependency.getClass().getName();
-         throw new RuntimeException("Non matching type for @Inject of setter: " + setMethod + " for type: " + type, e);  //To change body of catch statement use Options | File Templates.
-      }
-      catch (InvocationTargetException e)
-      {
-         throw new RuntimeException(e.getCause());  //To change body of catch statement use Options | File Templates.
-      }
-   }
 }

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiPropertyInjector.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiPropertyInjector.java	2008-05-07 11:36:57 UTC (rev 73106)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/injection/JndiPropertyInjector.java	2008-05-07 12:36:26 UTC (rev 73107)
@@ -21,7 +21,10 @@
  */
 package org.jboss.injection;
 
+import java.util.Arrays;
+
 import javax.naming.Context;
+import javax.naming.LinkRef;
 import javax.naming.NamingException;
 
 import org.jboss.ejb3.BeanContext;
@@ -88,6 +91,29 @@
    {
       Object value = lookup(jndiName);
       log.trace("injecting " + value + " from " + jndiName + " into " + property + " of " + instance);
-      property.set(instance, value);
+      try
+      {
+         property.set(instance, value);
+      }
+      catch(IllegalArgumentException e)
+      {
+         // We found something to inject, but it happened to be the wrong thing
+         String realJndiName;
+         try
+         {
+            // TODO: check whether it's a real link beforehand
+            Object link = JndiUtil.lookupLink(ctx, jndiName);
+            realJndiName = jndiName + (link instanceof LinkRef ? " (link -> " + ((LinkRef) link).getLinkName() + ")" : "");
+         }
+         catch(NamingException ne)
+         {
+            log.trace("Failed to obtain the real JNDI name", ne);
+            realJndiName = jndiName;
+         }
+         Class<?> interfaces[] = value.getClass().getInterfaces();
+         String interfacesStr = (interfaces.length > 0 ? " (implements " + Arrays.toString(interfaces) + ")" : "");
+         String msg = "failed to inject " + value + interfacesStr + " from " + realJndiName + " into " + property + " of " + instance;
+         throw new IllegalArgumentException(msg, e);
+      }
    }
 }




More information about the jboss-cvs-commits mailing list