openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1524636 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/config/ main/java/org/apache/webbeans/container/ main/java/org/apache/webbeans/event/ main/java/org/apache/webbeans/lifecycle/ test/java/org/apache/webbeans/...
Date Thu, 19 Sep 2013 07:11:18 GMT
Author: struberg
Date: Thu Sep 19 07:11:18 2013
New Revision: 1524636

URL: http://svn.apache.org/r1524636
Log:
OWB-872 only deliver CDI Lifecycle events to Extensions

'Normal' CDI beans will get skipped in the delivery

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/scopeextension/broken/
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/scopeextension/broken/CdiBeanWithLifecycleObserver.java
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
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/AbstractLifeCycle.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.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=1524636&r1=1524635&r2=1524636&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 Sep 19 07:11:18 2013
@@ -80,7 +80,6 @@ import javax.enterprise.inject.spi.Annot
 import javax.enterprise.inject.spi.AnnotatedMethod;
 import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.Decorator;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.ObserverMethod;
@@ -348,8 +347,8 @@ public class BeansDeployer
      */
     private void fireBeforeBeanDiscoveryEvent()
     {
-        BeanManager manager = webBeansContext.getBeanManagerImpl();
-        manager.fireEvent(new BeforeBeanDiscoveryImpl(webBeansContext));
+        BeanManagerImpl manager = webBeansContext.getBeanManagerImpl();
+        manager.fireLifecycleEvent(new BeforeBeanDiscoveryImpl(webBeansContext));
     }
     
     /**
@@ -358,7 +357,7 @@ public class BeansDeployer
     private void fireAfterBeanDiscoveryEvent()
     {
         BeanManagerImpl manager = webBeansContext.getBeanManagerImpl();
-        manager.fireEvent(new AfterBeanDiscoveryImpl(webBeansContext));
+        manager.fireLifecycleEvent(new AfterBeanDiscoveryImpl(webBeansContext));
 
         webBeansContext.getWebBeansUtil().inspectErrorStack(
             "There are errors that are added by AfterBeanDiscovery event observers. Look
at logs for further details");
@@ -370,7 +369,7 @@ public class BeansDeployer
     private void fireAfterDeploymentValidationEvent()
     {
         BeanManagerImpl manager = webBeansContext.getBeanManagerImpl();
-        manager.fireEvent(new AfterDeploymentValidationImpl(manager));
+        manager.fireLifecycleEvent(new AfterDeploymentValidationImpl(manager));
 
         webBeansContext.getWebBeansUtil().inspectErrorStack(
             "There are errors that are added by AfterDeploymentValidation event observers.
Look at logs for further details");

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=1524636&r1=1524635&r2=1524636&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 Sep 19 07:11:18 2013
@@ -423,17 +423,27 @@ public class BeanManagerImpl extends Abs
     @Override
     public void fireEvent(Object event, Annotation... bindings)
     {       
-        fireEvent(event, new EventMetadataImpl(event.getClass(), null, bindings));
+        fireEvent(event, new EventMetadataImpl(event.getClass(), null, bindings), false);
     }
 
-    public void fireEvent(Object event, EventMetadata metadata)
-    {                
+    /**
+     * Like {@link #fireEvent(Object, java.lang.annotation.Annotation...)} but intended for
+     * internal CDI Container lifecycle events. The difference is that those
+     * events must only be delivered to CDI Extensions and not to normal beans.
+     */
+    public void fireLifecycleEvent(Object event, Annotation... bindings)
+    {
+        fireEvent(event, new EventMetadataImpl(event.getClass(), null, bindings), true);
+    }
+
+    public void fireEvent(Object event, EventMetadata metadata, boolean isLifecycleEvent)
+    {
         if (ClassUtil.isDefinitionContainsTypeVariables(event.getClass()))
         {
             throw new IllegalArgumentException("Event class : " + event.getClass().getName()
+ " can not be defined as generic type");
         }
 
-        notificationManager.fireEvent(event, metadata);
+        notificationManager.fireEvent(event, metadata, isLifecycleEvent);
     }
 
     public Set<Bean<?>> getComponents()

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java?rev=1524636&r1=1524635&r2=1524636&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
Thu Sep 19 07:11:18 2013
@@ -79,7 +79,7 @@ public class EventImpl<T> implements Eve
     @Override
     public void fire(T event)
     {
-        webBeansContext.getBeanManagerImpl().fireEvent(event, new EventMetadataImpl(eventType,
injectionPoint, injectedBindings));
+        webBeansContext.getBeanManagerImpl().fireEvent(event, new EventMetadataImpl(eventType,
injectionPoint, injectedBindings), false);
     }
 
     /**

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java?rev=1524636&r1=1524635&r2=1524636&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
Thu Sep 19 07:11:18 2013
@@ -35,6 +35,7 @@ import javax.enterprise.event.Observes;
 import javax.enterprise.event.Reception;
 import javax.enterprise.event.TransactionPhase;
 import javax.enterprise.inject.spi.AnnotatedMethod;
+import javax.enterprise.inject.spi.Extension;
 import javax.enterprise.inject.spi.ObserverMethod;
 import javax.enterprise.inject.spi.ProcessProducer;
 import javax.enterprise.util.TypeLiteral;
@@ -438,7 +439,7 @@ public final class NotificationManager
         return matching;
     }
 
-    public void fireEvent(Object event, EventMetadata metadata)
+    public void fireEvent(Object event, EventMetadata metadata, boolean isLifecycleEvent)
     {
         Set<ObserverMethod<? super Object>> observerMethods = resolveObservers(event,
metadata);
 
@@ -446,6 +447,12 @@ public final class NotificationManager
         {
             try
             {
+                if (isLifecycleEvent && !Extension.class.isAssignableFrom(observer.getBeanClass()))
+                {
+                    // we must not fire Extension Lifecycle events to beans which are no
Extensions
+                    continue;
+                }
+
                 TransactionPhase phase = observer.getTransactionPhase();
                 
                 if(phase != null && !phase.equals(TransactionPhase.IN_PROGRESS))

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java?rev=1524636&r1=1524635&r2=1524636&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java
Thu Sep 19 07:11:18 2013
@@ -46,7 +46,6 @@ import javax.enterprise.inject.spi.Injec
 
 import org.apache.webbeans.annotation.AnnotationManager;
 import org.apache.webbeans.component.AbstractOwbBean;
-import org.apache.webbeans.component.InjectionTargetBean;
 import org.apache.webbeans.component.WebBeansType;
 import org.apache.webbeans.config.OWBLogConst;
 import org.apache.webbeans.config.WebBeansContext;
@@ -383,8 +382,7 @@ public class ObserverMethodImpl<T> imple
     @SuppressWarnings("unchecked")
     public Class<?> getBeanClass()
     {
-        InjectionTargetBean<T> abs = (InjectionTargetBean<T>) bean;
-        return abs.getBeanClass();
+        return bean.getBeanClass();
     }
 
     /** 

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/AbstractLifeCycle.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/AbstractLifeCycle.java?rev=1524636&r1=1524635&r2=1524636&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/AbstractLifeCycle.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/AbstractLifeCycle.java
Thu Sep 19 07:11:18 2013
@@ -150,7 +150,7 @@ public abstract class AbstractLifeCycle 
             contextsService.startContext(ApplicationScoped.class, endObject);
             
             //Fire shut down
-            beanManager.fireEvent(new BeforeShutdownImpl());
+            beanManager.fireLifecycleEvent(new BeforeShutdownImpl());
             
             //Destroys context
             contextsService.destroy(endObject);

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.java?rev=1524636&r1=1524635&r2=1524636&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/ExtensionTest.java
Thu Sep 19 07:11:18 2013
@@ -38,6 +38,7 @@ import org.apache.webbeans.newtests.port
 import org.apache.webbeans.newtests.portable.scopeextension.ExternalTestScoped;
 import org.apache.webbeans.newtests.portable.scopeextension.ExternalTestScopedBean;
 import org.apache.webbeans.newtests.portable.scopeextension.ExternalUnserializableTestScopedBean;
+import org.apache.webbeans.newtests.portable.scopeextension.broken.CdiBeanWithLifecycleObserver;
 import org.apache.webbeans.portable.events.discovery.BeforeShutdownImpl;
 import org.junit.Test;
 
@@ -58,10 +59,8 @@ public class ExtensionTest extends Abstr
     @Test
     public void testScopeExtension()
     {
-        Collection<Class<?>> classes = new ArrayList<Class<?>>();
-        classes.add(ExternalTestScopedBean.class);
         addExtension(new ExternalTestScopeExtension());
-        startContainer(classes);
+        startContainer(ExternalTestScopedBean.class);
 
         WebBeansContext webBeansContext = WebBeansContext.getInstance();
         webBeansContext.getContextFactory().initApplicationContext(null);
@@ -83,7 +82,21 @@ public class ExtensionTest extends Abstr
         webBeansContext.getContextFactory().destroyApplicationContext(null);
 
         shutDownContainer();
-        
+    }
+
+    @Test
+    public void testLifecycleObservingInStandardCdiBeans() throws Exception
+    {
+        CdiBeanWithLifecycleObserver.beforeBeanDiscoveryCalled = false;
+        CdiBeanWithLifecycleObserver.afterBeanDiscoveryCalled = false;
+
+        startContainer(CdiBeanWithLifecycleObserver.class);
+
+        CdiBeanWithLifecycleObserver instance = getInstance(CdiBeanWithLifecycleObserver.class);
+        Assert.assertNotNull(instance);
+
+        Assert.assertFalse(CdiBeanWithLifecycleObserver.beforeBeanDiscoveryCalled);
+        Assert.assertFalse(CdiBeanWithLifecycleObserver.afterBeanDiscoveryCalled);
     }
     
     /**

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/scopeextension/broken/CdiBeanWithLifecycleObserver.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/scopeextension/broken/CdiBeanWithLifecycleObserver.java?rev=1524636&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/scopeextension/broken/CdiBeanWithLifecycleObserver.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/portable/scopeextension/broken/CdiBeanWithLifecycleObserver.java
Thu Sep 19 07:11:18 2013
@@ -0,0 +1,66 @@
+/*
+ * 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.scopeextension.broken;
+
+import javax.enterprise.context.Dependent;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+
+/**
+ * This is in fact a broken CDI bean as it implements
+ * a CDI Container Lifecycle observer.
+ * We must ensure that this bean doesn't get called during bootstrap.
+ * The CDI spec says nothing about whether we must throw a DeploymentException
+ * or not, so we leave this out.
+ */
+@Dependent
+public class CdiBeanWithLifecycleObserver
+{
+    public static boolean beforeBeanDiscoveryCalled = false;
+    public static boolean afterBeanDiscoveryCalled = false;
+
+    private int meaningOfLife = 42;
+
+    public int getMeaningOfLife()
+    {
+        return meaningOfLife;
+    }
+
+    public void setMeaningOfLife(int meaningOfLife)
+    {
+        this.meaningOfLife = meaningOfLife;
+    }
+
+    /**
+     * This method must not be called by the container during bootstrap
+     */
+    public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd)
+    {
+        beforeBeanDiscoveryCalled = true;
+    }
+
+    /**
+     * This method must not be called by the container during bootstrap
+     */
+    public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd)
+    {
+        afterBeanDiscoveryCalled = true;
+    }
+}



Mime
View raw message