[jboss-cvs] JBossAS SVN: r108136 - in projects/ejb3/trunk/core/src: test/java/org/jboss/ejb3/core/test and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Sep 14 06:43:32 EDT 2010


Author: jaikiran
Date: 2010-09-14 06:43:32 -0400 (Tue, 14 Sep 2010)
New Revision: 108136

Added:
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/SimpleSLSB.java
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/unit/
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/unit/EnvEntryTypeTestCase.java
Modified:
   projects/ejb3/trunk/core/src/main/java/org/jboss/injection/EnvEntryEncInjector.java
   projects/ejb3/trunk/core/src/main/java/org/jboss/injection/ResourceHandler.java
Log:
EJBTHREE-2168 env-entry-type should be inferred from injection-target (if present) in absence of explicit env-entry-type

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/injection/EnvEntryEncInjector.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/injection/EnvEntryEncInjector.java	2010-09-14 07:24:06 UTC (rev 108135)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/injection/EnvEntryEncInjector.java	2010-09-14 10:43:32 UTC (rev 108136)
@@ -21,6 +21,7 @@
  */
 package org.jboss.injection;
 
+import org.jboss.ejb3.common.classloader.util.PrimitiveClassLoadingUtil;
 import org.jboss.logging.Logger;
 import org.jboss.util.naming.Util;
 
@@ -52,7 +53,7 @@
       {
          Util.rebind(container.getEnc(),
                  name,
-                 getEnvEntryValue());
+                 getEnvEntryValue(container.getClassloader()));
       }
       catch (Exception e)
       {
@@ -60,35 +61,43 @@
       }
    }
 
-
+   @Deprecated
    protected Object getEnvEntryValue() throws ClassNotFoundException
    {
-      Class type = Thread.currentThread().getContextClassLoader().loadClass(entryType);
+      ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+      return this.getEnvEntryValue(tccl);
+   }
+   
+   private Object getEnvEntryValue(ClassLoader cl) throws ClassNotFoundException
+   {
+      // Use PrimitiveClassLoadingUtil since the type might be primitive
+      Class<?> type = PrimitiveClassLoadingUtil.loadClass(entryType, cl);
+      
       if (type == String.class)
       {
          return value;
       }
-      else if (type == Integer.class)
+      else if (type == Integer.class || type == int.class)
       {
          return new Integer(value);
       }
-      else if (type == Long.class)
+      else if (type == Long.class || type == long.class)
       {
          return new Long(value);
       }
-      else if (type == Double.class)
+      else if (type == Double.class || type == double.class)
       {
          return new Double(value);
       }
-      else if (type == Float.class)
+      else if (type == Float.class || type == float.class)
       {
          return new Float(value);
       }
-      else if (type == Byte.class)
+      else if (type == Byte.class || type == byte.class)
       {
          return new Byte(value);
       }
-      else if (type == Character.class)
+      else if (type == Character.class || type == char.class)
       {
          String input = value;
          if (input == null || input.length() == 0)
@@ -104,11 +113,11 @@
             return new Character(input.charAt(0));
          }
       }
-      else if (type == Short.class)
+      else if (type == Short.class || type == short.class)
       {
          return new Short(value);
       }
-      else if (type == Boolean.class)
+      else if (type == Boolean.class || type == boolean.class)
       {
          return new Boolean(value);
       }

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/injection/ResourceHandler.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/injection/ResourceHandler.java	2010-09-14 07:24:06 UTC (rev 108135)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/injection/ResourceHandler.java	2010-09-14 10:43:32 UTC (rev 108136)
@@ -28,6 +28,7 @@
 import java.net.URL;
 import java.util.Collection;
 import java.util.Map;
+import java.util.Set;
 
 import javax.annotation.Resource;
 import javax.annotation.Resources;
@@ -45,6 +46,7 @@
 import org.jboss.metadata.javaee.spec.MessageDestinationReferenceMetaData;
 import org.jboss.metadata.javaee.spec.RemoteEnvironment;
 import org.jboss.metadata.javaee.spec.ResourceEnvironmentReferenceMetaData;
+import org.jboss.metadata.javaee.spec.ResourceInjectionTargetMetaData;
 import org.jboss.metadata.javaee.spec.ResourceReferenceMetaData;
 import org.jboss.reflect.plugins.ValueConvertor;
 import org.omg.CORBA.ORB;
@@ -101,7 +103,12 @@
          InjectionUtil.injectionTarget(encName, envEntry, container, container.getEncInjections());
          if (container.getEncInjectors().containsKey(encName)) continue;
          log.trace("adding env-entry injector " + encName);
-         container.getEncInjectors().put(encName, new EnvEntryEncInjector(encName, envEntry.getType(), envEntry.getValue()));
+         String envEntryType = getEnvEntryType(container, envEntry);
+         if (envEntryType == null)
+         {
+            throw new RuntimeException("env-entry-type is not specified for env-entry named " + envEntry.getName());
+         }
+         container.getEncInjectors().put(encName, new EnvEntryEncInjector(encName, envEntryType, envEntry.getValue()));
       }
    }
 
@@ -269,6 +276,55 @@
          InjectionUtil.injectionTarget(encName, envRef, container, container.getEncInjections());
       }
    }
+   
+   /**
+    * Returns the env-entry-type for the passed {@link EnvironmentEntryMetaData}.
+    * <p>
+    *   If the passed env-entry has the env-entry-type explicitly specified, then
+    *   that value is returned. Else, this method checks for the presence of any
+    *   injection targets for this env-entry. If there's a injection target, then
+    *   the env-entry-type is deduced based on the field/method of the injection target. 
+    * </p>
+    * <p>
+    *   This method returns null if the env-entry-type isn't explicitly specified and 
+    *   if the env-entry-type could not be deduced from the injection targets of this
+    *   env-entry.
+    * </p>
+    * @param container The container to which this env-entry belongs
+    * @param envEntry The env-entry metadata
+    * @return
+    */
+   private static String getEnvEntryType(InjectionContainer container, EnvironmentEntryMetaData envEntry)
+   {
+      // first check whether the type is explicitly specified
+      String explicitType = envEntry.getType();
+      if (explicitType != null)
+      {
+         return explicitType;
+      }
+      // check if this env-entry has an injection-target. If yes, then 
+      // fetch the type from the injection target's field/method type
+      Set<ResourceInjectionTargetMetaData> injectionTargets = envEntry.getInjectionTargets();
+      if (injectionTargets == null || injectionTargets.isEmpty())
+      {
+         return null;
+      }
+      // even if there are multiple injection targets, we just check for the first one
+      // TODO: Check for injection targets and if there's a mismatch in the types between
+      // different injection targets, then throw an error
+      ResourceInjectionTargetMetaData injectionTarget = injectionTargets.iterator().next();
+      AccessibleObject accessibleObject = InjectionUtil.findInjectionTarget(container.getClassloader(), injectionTarget);
+      if (accessibleObject instanceof Field)
+      {
+         return ((Field) accessibleObject).getType().getName();
+      }
+      else if (accessibleObject instanceof Method)
+      {
+         return ((Method) accessibleObject).getParameterTypes()[0].getName();
+      }
+      
+      return null;
+   }
 
    public void loadXml(X xml, InjectionContainer container)
    {

Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/SimpleSLSB.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/SimpleSLSB.java	                        (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/SimpleSLSB.java	2010-09-14 10:43:32 UTC (rev 108136)
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree2168;
+
+import javax.ejb.Stateless;
+
+/**
+ * SimpleSLSB
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+ at Stateless
+public class SimpleSLSB
+{
+
+   // Note: Don't change this field name, the EnvEntryTypeTestCase depends on the field name string
+   private String someEnvEntryField;
+
+   // Note: Don't change this field name, the EnvEntryTypeTestCase depends on the field name string
+   private char somePrimitiveField;
+
+   // Note: Don't change method name since the EnvEntryTypeTestCase depends on this method name string
+   public void setSomeOtherEnvEntry(Boolean test)
+   {
+      // do nothing
+   }
+   
+   // Note: Don't change method name since the EnvEntryTypeTestCase depends on this method name string
+   public void setSomeMethodWithPrimitiveParam(int test)
+   {
+      // do nothing
+   }
+}

Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/unit/EnvEntryTypeTestCase.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/unit/EnvEntryTypeTestCase.java	                        (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree2168/unit/EnvEntryTypeTestCase.java	2010-09-14 10:43:32 UTC (rev 108136)
@@ -0,0 +1,232 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree2168.unit;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import junit.framework.Assert;
+
+import org.jboss.ejb3.core.test.ejbthree2168.SimpleSLSB;
+import org.jboss.injection.EncInjector;
+import org.jboss.injection.InjectionContainer;
+import org.jboss.injection.ResourceHandler;
+import org.jboss.metadata.ejb.jboss.JBossEnvironmentRefsGroupMetaData;
+import org.jboss.metadata.ejb.jboss.JBossSessionBeanMetaData;
+import org.jboss.metadata.javaee.spec.EnvironmentEntriesMetaData;
+import org.jboss.metadata.javaee.spec.EnvironmentEntryMetaData;
+import org.jboss.metadata.javaee.spec.ResourceInjectionTargetMetaData;
+import org.jnp.server.SingletonNamingServer;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Tests that if env-entry-type isn't explicitly specified on a env-entry, then
+ * the env-entry-type is inferred from the injection target (if any)
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+public class EnvEntryTypeTestCase
+{
+
+   private static SingletonNamingServer namingServer;
+
+   @BeforeClass
+   public static void beforeClass() throws Exception
+   {
+      // startup the local naming server
+      bootupNamingServer();
+   }
+
+   @AfterClass
+   public static void afterClass()
+   {
+      if (namingServer != null)
+      {
+         namingServer.destroy();
+      }
+   }
+
+   private static void bootupNamingServer() throws Exception
+   {
+      namingServer = new SingletonNamingServer();
+
+   }
+
+   /**
+    * Test a env-entry without an explicit env-entry-type, with a injection target pointing
+    * to a {@link Field}
+    * 
+    * @throws Exception
+    */
+   @Test
+   public void testEnvEntryWithoutExplicitTypeForFieldInjection() throws Exception
+   {
+
+      // create the env-entry
+      EnvironmentEntryMetaData envEntryForField = new EnvironmentEntryMetaData();
+      envEntryForField.setEnvEntryName("testField");
+      envEntryForField.setValue("test");
+
+      // the injection target
+      ResourceInjectionTargetMetaData injectionTargetForField = new ResourceInjectionTargetMetaData();
+      injectionTargetForField.setInjectionTargetClass(SimpleSLSB.class.getName());
+      injectionTargetForField.setInjectionTargetName("someEnvEntryField");
+
+      // setup the env-entry to use the injection target
+      envEntryForField.setInjectionTargets(Collections.singleton(injectionTargetForField));
+
+      // create env-entry for a injection target field of type primitive   
+      EnvironmentEntryMetaData envEntryForPrimitiveField = new EnvironmentEntryMetaData();
+      envEntryForPrimitiveField.setEnvEntryName("testFieldWithPrimitive");
+      envEntryForPrimitiveField.setValue("c");
+      
+      // the injection target for a primitive field
+      ResourceInjectionTargetMetaData injectionTargetForPrimitiveField = new ResourceInjectionTargetMetaData();
+      injectionTargetForPrimitiveField.setInjectionTargetClass(SimpleSLSB.class.getName());
+      injectionTargetForPrimitiveField.setInjectionTargetName("somePrimitiveField");
+
+      // setup the env-entry to use the injection target
+      envEntryForPrimitiveField.setInjectionTargets(Collections.singleton(injectionTargetForPrimitiveField));
+
+
+      // a mock container
+      InjectionContainer mockInjectionContainer = mock(InjectionContainer.class);
+      ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+      Map<String, EncInjector> encInjectors = new HashMap<String, EncInjector>();
+      // setup the mocks
+      when(mockInjectionContainer.getClassloader()).thenReturn(tccl);
+      when(mockInjectionContainer.getEncInjectors()).thenReturn(encInjectors);
+      when(mockInjectionContainer.getEnc()).thenReturn(this.getLocalContext());
+
+      // bean metadata
+      JBossSessionBeanMetaData sessionBean = new JBossSessionBeanMetaData();
+      JBossEnvironmentRefsGroupMetaData envRefGroup = new JBossEnvironmentRefsGroupMetaData();
+      EnvironmentEntriesMetaData envEntries = new EnvironmentEntriesMetaData();
+      // add the env-entry to the env entries collection
+      envEntries.add(envEntryForField);
+      envEntries.add(envEntryForPrimitiveField);
+      envRefGroup.setEnvironmentEntries(envEntries);
+      sessionBean.setEnvironmentRefsGroup(envRefGroup);
+
+      // process using a ResourceHandler
+      ResourceHandler<JBossSessionBeanMetaData> resourceHandler = new ResourceHandler<JBossSessionBeanMetaData>();
+      resourceHandler.loadXml(sessionBean, mockInjectionContainer);
+
+      // Test
+      Assert.assertEquals("Unexpected number of EncInjectors", 2, encInjectors.size());
+      for (EncInjector encInjector : encInjectors.values())
+      {
+         // run the ENC injections
+         encInjector.inject(mockInjectionContainer);
+      }
+   }
+
+   /**
+    * Test a env-entry without an explicit env-entry-type, with a injection target pointing
+    * to a {@link Method}
+    * 
+    * @throws Exception
+    */
+   @Test
+   public void testEnvEntryWithoutExplicitTypeForMethodInjection() throws Exception
+   {
+
+      // create the env-entry
+      EnvironmentEntryMetaData envEntryForMethod = new EnvironmentEntryMetaData();
+      envEntryForMethod.setEnvEntryName("testMethod");
+      envEntryForMethod.setValue("true");
+
+      // the injection target
+      ResourceInjectionTargetMetaData injectionTargetForMethod = new ResourceInjectionTargetMetaData();
+      injectionTargetForMethod.setInjectionTargetClass(SimpleSLSB.class.getName());
+      injectionTargetForMethod.setInjectionTargetName("setSomeOtherEnvEntry");
+
+      // setup the env-entry to use the injection target
+      envEntryForMethod.setInjectionTargets(Collections.singleton(injectionTargetForMethod));
+
+      // create env-entry for a injection target method with primitive params  
+      EnvironmentEntryMetaData envEntryForPrimitiveMethod = new EnvironmentEntryMetaData();
+      envEntryForPrimitiveMethod.setEnvEntryName("testMethodWithPrimitive");
+      envEntryForPrimitiveMethod.setValue("3");
+
+      // the injection target for a primitive field
+      ResourceInjectionTargetMetaData injectionTargetForMethodWithPrimitiveParam = new ResourceInjectionTargetMetaData();
+      injectionTargetForMethodWithPrimitiveParam.setInjectionTargetClass(SimpleSLSB.class.getName());
+      injectionTargetForMethodWithPrimitiveParam.setInjectionTargetName("setSomeMethodWithPrimitiveParam");
+
+      // setup the env-entry to use the injection target
+      envEntryForPrimitiveMethod.setInjectionTargets(Collections.singleton(injectionTargetForMethodWithPrimitiveParam));
+
+      // mock container
+      InjectionContainer mockInjectionContainer = mock(InjectionContainer.class);
+      ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+      Map<String, EncInjector> encInjectors = new HashMap<String, EncInjector>();
+      // setup the mocks
+      when(mockInjectionContainer.getClassloader()).thenReturn(tccl);
+      when(mockInjectionContainer.getEncInjectors()).thenReturn(encInjectors);
+      when(mockInjectionContainer.getEnc()).thenReturn(this.getLocalContext());
+
+      // bean metadata
+      JBossSessionBeanMetaData sessionBean = new JBossSessionBeanMetaData();
+      JBossEnvironmentRefsGroupMetaData envRefGroup = new JBossEnvironmentRefsGroupMetaData();
+      EnvironmentEntriesMetaData envEntries = new EnvironmentEntriesMetaData();
+      envEntries.add(envEntryForMethod);
+      envEntries.add(envEntryForPrimitiveMethod);
+      envRefGroup.setEnvironmentEntries(envEntries);
+      sessionBean.setEnvironmentRefsGroup(envRefGroup);
+
+      // process using ResourceHandler
+      ResourceHandler<JBossSessionBeanMetaData> resourceHandler = new ResourceHandler<JBossSessionBeanMetaData>();
+      resourceHandler.loadXml(sessionBean, mockInjectionContainer);
+
+      // Test
+      Assert.assertEquals("Unexpected number of EncInjectors", 2, encInjectors.size());
+      for (EncInjector encInjector : encInjectors.values())
+      {
+         // run the ENC injectors
+         encInjector.inject(mockInjectionContainer);
+      }
+   }
+
+   private Context getLocalContext() throws NamingException
+   {
+      Properties props = new Properties();
+      props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.LocalOnlyContextFactory");
+      return new InitialContext(props);
+   }
+}



More information about the jboss-cvs-commits mailing list