openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r742632 - in /incubator/openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/config/ main/java/org/apache/webbeans/inject/ main/java/org/apache/webbeans/util/ test/java/org/apache/webbeans/test/component/resource/ test/java/o...
Date Mon, 09 Feb 2009 17:12:18 GMT
Author: struberg
Date: Mon Feb  9 17:12:17 2009
New Revision: 742632

URL: http://svn.apache.org/viewvc?rev=742632&view=rev
Log:
OWB-74 OWB-71 add Resource injection for methods
e.g.:
@PersistenceUnit public void setEmf(EntityManagerFactory emf) {...

Modified:
    incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java
    incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java
    incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/InjectableMethods.java
    incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
    incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java?rev=742632&r1=742631&r2=742632&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java
Mon Feb  9 17:12:17 2009
@@ -587,38 +587,82 @@
         Asserts.assertNotNull(component, "component parameter can not be null");
 
         Class<T> clazz = component.getReturnType();
-        Method[] methods = AnnotationUtil.getMethodsWithAnnotation(clazz, Initializer.class);
-        if (methods.length != 0)
+        Method[] methods = clazz.getDeclaredMethods();
+        for (Method method : methods)
         {
-            for (Method method : methods)
+            boolean isInitializer = AnnotationUtil.isMethodHasAnnotation(method, Initializer.class);
+            boolean isResource    = AnnotationUtil.isMethodHasResourceAnnotation(method);
+            
+            if (isInitializer && isResource)
+            {
+                throw new WebBeansConfigurationException("Found Initializer and resource
injection at the same time for the method : " 
+                                                         + method.getName() + " in class
: " + clazz.getName());
+            }
+            
+            if (isInitializer)
+            {
+                checkForInjectedInitializerMethod(component, clazz, method);
+            }
+            else if (isResource)
+            {
+                checkForValidResourceMethod(component, clazz, method);
+            }
+            else
+            {
+                continue;
+            }
+         
+            if (!Modifier.isStatic(method.getModifiers()))
             {
-                Annotation[][] anns = method.getParameterAnnotations();
-                Type[] type = method.getGenericParameterTypes();
-                for (int i = 0; i < anns.length; i++)
-                {
-                    Annotation[] a = anns[i];
-                    Type t = type[i];
-                    WebBeansUtil.checkForNewBindingForDeployment(t, clazz, method.getName(),
a);
-                }
-
-                if (method.getAnnotation(Produces.class) == null)
-                {
-                    WebBeansUtil.checkInjectedMethodParameterConditions(method, clazz);
-                    if (!Modifier.isStatic(method.getModifiers()))
-                    {
-                        component.addInjectedMethod(method);
-                        addMethodInjectionPointMetaData(component, method);
-                    }
-
-                }
-                else
-                {
-                    throw new WebBeansConfigurationException("Initializer method : " + method.getName()
+ " in class : " + clazz.getName() + " can not be annotated with @Produces or @Destructor");
-                }
+                component.addInjectedMethod(method);
+                addMethodInjectionPointMetaData(component, method);
             }
+
         }
     }
 
+    /**
+     * add the definitions for a &#x0040;Initializer method.
+     */
+    private static <T> void checkForInjectedInitializerMethod(ComponentImpl<T>
component, Class<T> clazz, Method method)
+    {
+        Annotation[][] anns = method.getParameterAnnotations();
+        Type[] type = method.getGenericParameterTypes();
+        for (int i = 0; i < anns.length; i++)
+        {
+            Annotation[] a = anns[i];
+            Type t = type[i];
+            WebBeansUtil.checkForNewBindingForDeployment(t, clazz, method.getName(), a);
+        }
+
+        if (method.getAnnotation(Produces.class) == null)
+        {
+            WebBeansUtil.checkInjectedMethodParameterConditions(method, clazz);
+        }
+        else
+        {
+            throw new WebBeansConfigurationException("Initializer method : " + method.getName()
+ " in class : " + clazz.getName() 
+                                                     + " can not be annotated with @Produces
or @Destructor");
+        }
+    }
+
+    /**
+     * add the definitions for a &#x0040;Initializer method.
+     */
+    private static <T> void checkForValidResourceMethod(ComponentImpl<T> component,
Class<T> clazz, Method method)
+    {
+        Class<?>[] parameterTypes = method.getParameterTypes();
+        if (parameterTypes == null || parameterTypes.length != 1)
+        {
+            throw new WebBeansConfigurationException("Resource method : " + method.getName()
+ " in class : " + clazz.getName() 
+                                                     + " must only have exactly 1 parameter
with a valid resource type");
+        }
+        
+        Annotation[] anns = method.getAnnotations();
+        WebBeansUtil.checkForValidResources(parameterTypes[0], clazz, method.getName(), anns);
+    }
+
+    
     public static void defineSimpleWebBeanInterceptorStack(AbstractComponent<?> component)
     {
         Asserts.assertNotNull(component, "component parameter can no be null");

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java?rev=742632&r1=742631&r2=742632&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AbstractInjectable.java
Mon Feb  9 17:12:17 2009
@@ -119,17 +119,13 @@
 
     }
 
-    private boolean isResource(Annotation... annotations)
+    /**
+     * check if any of the given resources is a resource annotation
+     * @see AnnotationUtil#isResourceAnnotation(Class)
+     */
+    protected boolean isResource(Annotation... annotations)
     {
-        for (Annotation anno : annotations)
-        {
-            if (AnnotationUtil.isResourceAnnotation(anno.annotationType()))
-            {
-                return true;
-            }
-        }
-        
-        return false;
+        return AnnotationUtil.hasResourceAnnotation(annotations);
     }
 
     private boolean isNewBinding(Annotation... annotations)

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/InjectableMethods.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/InjectableMethods.java?rev=742632&r1=742631&r2=742632&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/InjectableMethods.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/InjectableMethods.java
Mon Feb  9 17:12:17 2009
@@ -55,44 +55,57 @@
     public T doInjection()
     {
         Type[] types = m.getGenericParameterTypes();
-        Annotation[][] annots = m.getParameterAnnotations();
         List<Object> list = new ArrayList<Object>();
-        if (types.length > 0)
+        
+        Annotation[] methodAnnots = m.getAnnotations();
+        if (isResource(methodAnnots))
         {
-            int i = 0;
-            for (Type type : types)
+            // if the method itself is resource annotated, e.g. @PersistenceUnit
+            Type[] args = new Type[0];
+            Class<?> clazz = (Class<?>) types[0];
+            list.add(inject(clazz, args, methodAnnots));
+        }
+        else 
+        {
+            // otherwise we inject the method parameters as usual
+            Annotation[][] annots = m.getParameterAnnotations();
+            if (types.length > 0)
             {
-                Annotation[] annot = annots[i];
-                if (annot.length == 0)
-                {
-                    annot = new Annotation[1];
-                    annot[0] = new CurrentLiteral();
-                }
-
-                Type[] args = new Type[0];
-                Class<?> clazz = null;
-                if (type instanceof ParameterizedType)
-                {
-                    ParameterizedType pt = (ParameterizedType) type;
-
-                    checkParametrizedTypeForInjectionPoint(pt);
-                    args = pt.getActualTypeArguments();
-
-                    clazz = (Class<?>) pt.getRawType();
-                }
-                else
+                int i = 0;
+                for (Type type : types)
                 {
-                    clazz = (Class<?>) type;
+                    Annotation[] annot = annots[i];
+                    if (annot.length == 0)
+                    {
+                        annot = new Annotation[1];
+                        annot[0] = new CurrentLiteral();
+                    }
+    
+                    Type[] args = new Type[0];
+                    Class<?> clazz = null;
+                    if (type instanceof ParameterizedType)
+                    {
+                        ParameterizedType pt = (ParameterizedType) type;
+    
+                        checkParametrizedTypeForInjectionPoint(pt);
+                        args = pt.getActualTypeArguments();
+    
+                        clazz = (Class<?>) pt.getRawType();
+                    }
+                    else
+                    {
+                        clazz = (Class<?>) type;
+                    }
+    
+                    list.add(inject(clazz, args, AnnotationUtil.getBindingAnnotations(annot)));
+    
+                    i++;
+    
                 }
-
-                list.add(inject(clazz, args, AnnotationUtil.getBindingAnnotations(annot)));
-
-                i++;
-
+    
             }
-
         }
-
+        
         try
         {
             if (!ClassUtil.isPublic(m.getModifiers()))

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java?rev=742632&r1=742631&r2=742632&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
Mon Feb  9 17:12:17 2009
@@ -79,6 +79,20 @@
     }
 
     /**
+     * Check if a resource annotation exist on the method.
+     * 
+     * @param method method
+     * @return <code>true</code> if any resource annotation exists for the given
method
+     */
+    public static boolean isMethodHasResourceAnnotation(Method method)
+    {
+        Asserts.assertNotNull(method, "Method argument can not be null");
+        
+        Annotation[] anns = method.getDeclaredAnnotations();
+        return hasResourceAnnotation(anns);
+    }
+    
+    /**
      * Check given annotation exist in the any parameter of the given method.
      * Return true if exist false otherwise.
      * 
@@ -722,6 +736,23 @@
     }
 
     /**
+     * check if any of the given resources is a resource annotation
+     * @see AnnotationUtil#isResourceAnnotation(Class)
+     */
+    public static boolean hasResourceAnnotation(Annotation[] annotations)
+    {
+        for (Annotation anno : annotations)
+        {
+            if (AnnotationUtil.isResourceAnnotation(anno.annotationType()))
+            {
+                return true;
+            }
+        }
+        
+        return false;
+    }
+
+    /**
      * Returns true if the annotation is a valid WebBeans Resource,
      * a resource defined in common annotations JSR-250, a remote EJB
      * or a web service.

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=742632&r1=742631&r2=742632&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
Mon Feb  9 17:12:17 2009
@@ -439,7 +439,7 @@
             {
                 if (!type.equals(EntityManagerFactory.class))
                 {
-                    throw new WebBeansConfigurationException("@PersistenceUnit must only
be injected into field with type EntityManagerFactory! class : "  
+                    throw new WebBeansConfigurationException("@PersistenceUnit must only
be injected into field/method with type EntityManagerFactory! class : "  
                                                              + clazz.getName() + " in field/method
: " + name);
                 }
             }
@@ -450,7 +450,7 @@
                 
                 if (!type.equals(EntityManager.class))
                 {
-                    throw new WebBeansConfigurationException("@PersistenceContext must only
be injected into field with type EntityManager! class : "  
+                    throw new WebBeansConfigurationException("@PersistenceContext must only
be injected into field/method with type EntityManager! class : "  
                                                              + clazz.getName() + " in field/method
: " + name);
                 }
              

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java?rev=742632&r1=742631&r2=742632&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java
Mon Feb  9 17:12:17 2009
@@ -34,21 +34,47 @@
     private EntityManager em;
 
     @PersistenceContext(unitName="openwebbeanstest", name="anotherEm")
-    private EntityManager em2;
+    private EntityManager emNamed;
 
-    public EntityManagerFactory getEntityManagerFactory()
+    private EntityManagerFactory emfMethodInjected;
+
+    private EntityManager emMethodInjected;
+
+    public EntityManagerFactory getFieldInjectedEntityManagerFactory()
     {
         return emf;
     }
 
-    public EntityManager getEntityManager()
+    public EntityManager getFieldInjectedEntityManager()
     {
         return em;
     }
     
-    public EntityManager getEntityManager2()
+    public EntityManager getFieldInjectedEntityManager2()
     {
-        return em2;
+        return emNamed;
     }
-    
+
+    public EntityManagerFactory getMethodInjectedEntityManagerFactory()
+    {
+        return emfMethodInjected;
+    }
+
+    @PersistenceUnit(unitName="openwebbeanstest")
+    public void setMethodInjectedEntityManagerFactory(EntityManagerFactory emf2)
+    {
+        this.emfMethodInjected = emf2;
+    }
+
+    public EntityManager getMethodInjectedEntityManager()
+    {
+        return emMethodInjected;
+    }
+
+    @PersistenceContext(unitName="openwebbeanstest")
+    public void setMethodInjectedEntityManager(EntityManager em3)
+    {
+        this.emMethodInjected = em3;
+    }
+
 }

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java?rev=742632&r1=742631&r2=742632&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java
(original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java
Mon Feb  9 17:12:17 2009
@@ -55,15 +55,23 @@
         TstResourcePersistenceBean persBean = (TstResourcePersistenceBean) getInstanceByName("tstResourcePersistenceBean");
         Assert.assertNotNull(persBean);
         
-        EntityManagerFactory emf = persBean.getEntityManagerFactory();
+        // test field injection
+        EntityManagerFactory emf = persBean.getFieldInjectedEntityManagerFactory();
         Assert.assertNotNull(emf);
 
-        EntityManager em = persBean.getEntityManager();
+        EntityManager em = persBean.getFieldInjectedEntityManager();
         Assert.assertNotNull(em);
     
-        EntityManager em2 = persBean.getEntityManager2();
+        EntityManager em2 = persBean.getFieldInjectedEntityManager2();
         Assert.assertNotNull(em2);
 
         Assert.assertTrue(em != em2);
+        
+        // test method injection
+        EntityManagerFactory emf2 = persBean.getMethodInjectedEntityManagerFactory();
+        Assert.assertNotNull(emf2);
+        
+        EntityManager em3 = persBean.getMethodInjectedEntityManager();
+        Assert.assertNotNull(em3);
     }
 }



Mime
View raw message