Author: bergmark
Date: Thu Nov 11 18:31:20 2010
New Revision: 1034025

URL: http://svn.apache.org/viewvc?rev=1034025&view=rev
Log:
[OWB-489] Added support for AnnotatedTypes added by Extensions in 
BeforeBeanDiscovery.addAnnotatedType

Added:
    
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/BeforeBeanDiscoveryTest.java
   (with props)
    
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/addannotated/
    
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/addannotated/extension/
    
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/addannotated/extension/AddAdditionalAnnotatedTypeExtension.java
   (with props)
Modified:
    
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
    
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java

Modified: 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java?rev=1034025&r1=1034024&r2=1034025&view=diff
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
 (original)
+++ 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
 Thu Nov 11 18:31:20 2010
@@ -22,10 +22,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
-import java.util.ArrayList;
 import java.util.Stack;
 
 import javax.enterprise.inject.Model;
@@ -169,6 +170,9 @@ public class BeansDeployer
                 //Discover classpath classes
                 deployFromClassPath(scanner);
                 
+                //Deploy additional Annotated Types
+                deployAdditionalAnnotatedTypes();
+                
                 //Check Specialization
                 checkSpecializations(scanner);
                 
@@ -192,6 +196,8 @@ public class BeansDeployer
         }
     }
     
+    
+
     /**
      * Configure Default Beans.
      */
@@ -473,27 +479,7 @@ public class BeansDeployer
                 
                 if (null != annotatedType)
                 {
-                    //Fires ProcessAnnotatedType
-                    ProcessAnnotatedTypeImpl<?> processAnnotatedEvent = 
WebBeansUtil.fireProcessAnnotatedTypeEvent(annotatedType);     
-                    
-                    //if veto() is called
-                    if(processAnnotatedEvent.isVeto())
-                    {
-                        continue;
-                    }
-                    
-                    //Try class is Managed Bean
-                    boolean isDefined = 
defineManagedBean((Class<Object>)implClass, 
(ProcessAnnotatedTypeImpl<Object>)processAnnotatedEvent);
-                    
-                    //Try class is EJB bean
-                    if(!isDefined && this.discoverEjb)
-                    {                    
-                        if(EJBWebBeansConfigurator.isSessionBean(implClass))
-                        {
-                            logger.debug("Found Enterprise Bean with class 
name : [{0}]", implClass.getName());
-                            defineEnterpriseWebBean((Class<Object>)implClass, 
(ProcessAnnotatedTypeImpl<Object>)processAnnotatedEvent);                       
 
-                        }
-                    }                                     
+                    deploySingleAnnotatedType(implClass, annotatedType);
                 } 
                 else
                 {
@@ -512,6 +498,55 @@ public class BeansDeployer
     }
     
     /**
+     * Deploys any AnnotatedTypes added to the BeanManager during 
beforeBeanDiscovery.
+     */
+    private void deployAdditionalAnnotatedTypes()
+    {
+        BeanManagerImpl beanManager = BeanManagerImpl.getManager();
+        
+        List<AnnotatedType<?>> annotatedTypes = 
beanManager.getAnnotatedTypes();
+        
+        for(AnnotatedType<?> type : annotatedTypes)
+        {
+            Class implClass = type.getJavaClass();
+            
+            deploySingleAnnotatedType(implClass, type);                        
   
+        }
+    }
+    
+    /**
+     * Common helper method used to deploy annotated types discovered through
+     * scanning or during beforeBeanDiscovery.
+     * 
+     * @Param Class implClass the class of the bean to be deployed
+     * @Param AnnotatedType the AnnotatedType representing the bean to be 
deployed
+     */
+    private void deploySingleAnnotatedType(Class implClass, AnnotatedType 
annotatedType)
+    {
+        // Fires ProcessAnnotatedType
+        ProcessAnnotatedTypeImpl<?> processAnnotatedEvent = 
WebBeansUtil.fireProcessAnnotatedTypeEvent(annotatedType);
+
+        // if veto() is called
+        if (processAnnotatedEvent.isVeto())
+        {
+            return;
+        }
+
+        // Try class is Managed Bean
+        boolean isDefined = defineManagedBean((Class<Object>) implClass, 
(ProcessAnnotatedTypeImpl<Object>) processAnnotatedEvent);
+
+        // Try class is EJB bean
+        if (!isDefined && this.discoverEjb)
+        {
+            if (EJBWebBeansConfigurator.isSessionBean(implClass))
+            {
+                logger.debug("Found Enterprise Bean with class name : [{0}]", 
implClass.getName());
+                defineEnterpriseWebBean((Class<Object>) implClass, 
(ProcessAnnotatedTypeImpl<Object>) processAnnotatedEvent);
+            }
+        }
+    }
+    
+    /**
      * Discovers and deploys classes from XML.
      * 
      * NOTE : Currently XML file is just used for configuring.

Modified: 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java?rev=1034025&r1=1034024&r2=1034025&view=diff
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
 (original)
+++ 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
 Thu Nov 11 18:31:20 2010
@@ -1105,6 +1105,11 @@ public class BeanManagerImpl implements 
         return additionalScopes;
     }
     
+    public List<AnnotatedType<?>> getAnnotatedTypes()
+    {
+        return this.additionalAnnotatedTypes;
+    }
+    
     public void clear()
     {
         this.additionalAnnotatedTypes.clear();

Added: 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/BeforeBeanDiscoveryTest.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/BeforeBeanDiscoveryTest.java?rev=1034025&view=auto
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/BeforeBeanDiscoveryTest.java
 (added)
+++ 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/BeforeBeanDiscoveryTest.java
 Thu Nov 11 18:31:20 2010
@@ -0,0 +1,61 @@
+package org.apache.webbeans.newtests.portable;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.enterprise.inject.Default;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.util.AnnotationLiteral;
+
+import junit.framework.Assert;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import 
org.apache.webbeans.newtests.portable.addannotated.extension.AddAdditionalAnnotatedTypeExtension;
+import 
org.apache.webbeans.newtests.portable.events.extensions.AddBeanExtension.MyBean;
+import org.junit.Test;
+
+public class BeforeBeanDiscoveryTest extends AbstractUnitTest
+{
+
+    @SuppressWarnings("serial")
+    @Test
+    public void testAddAdditionalAnnotatedType()
+    {
+        Collection<URL> beanXmls = new ArrayList<URL>();
+
+        Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
+
+        addExtension(new AddAdditionalAnnotatedTypeExtension());
+
+        startContainer(beanClasses, beanXmls);
+
+        Bean<?> bean = getBeanManager().getBeans(MyBean.class, new 
AnnotationLiteral<Default>()
+        {
+        }).iterator().next();
+
+        // Bean should not be null, as we added it as an additional annotated
+        // type during before bean discovery in the extension
+        Assert.assertNotNull(bean);
+
+        shutDownContainer();
+    }
+}

Propchange: 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/BeforeBeanDiscoveryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/addannotated/extension/AddAdditionalAnnotatedTypeExtension.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/addannotated/extension/AddAdditionalAnnotatedTypeExtension.java?rev=1034025&view=auto
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/addannotated/extension/AddAdditionalAnnotatedTypeExtension.java
 (added)
+++ 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/addannotated/extension/AddAdditionalAnnotatedTypeExtension.java
 Thu Nov 11 18:31:20 2010
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.newtests.portable.addannotated.extension;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+import javax.enterprise.inject.spi.Extension;
+
+import 
org.apache.webbeans.newtests.portable.events.extensions.AddBeanExtension.MyBean;
+
+public class AddAdditionalAnnotatedTypeExtension implements Extension
+{
+
+    public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd, 
BeanManager bm)
+    {
+        bbd.addAnnotatedType(bm.createAnnotatedType(MyBean.class));
+    }
+
+}

Propchange: 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/addannotated/extension/AddAdditionalAnnotatedTypeExtension.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to