You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2015/04/09 23:50:31 UTC

svn commit: r1672501 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/corespi/se/ main/java/org/apache/webbeans/lifecycle/ test/java/org/apache/webbeans/test/contexts/session/common/ test/java/org/apache/webbeans/test/contexts/...

Author: struberg
Date: Thu Apr  9 21:50:31 2015
New Revision: 1672501

URL: http://svn.apache.org/r1672501
Log:
OWB-1025 add @Initialized and @Destroyed handling to Standalone Lifecycle

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/se/DefaultContextsService.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/AbstractLifeCycle.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/StandaloneLifeCycle.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/common/AppScopedBean.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/tests/SessionContextTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/se/DefaultContextsService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/se/DefaultContextsService.java?rev=1672501&r1=1672500&r2=1672501&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/se/DefaultContextsService.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/se/DefaultContextsService.java Thu Apr  9 21:50:31 2015
@@ -29,6 +29,8 @@ import javax.enterprise.context.SessionS
 import javax.enterprise.context.spi.Context;
 import javax.inject.Singleton;
 
+import org.apache.webbeans.annotation.DestroyedLiteral;
+import org.apache.webbeans.annotation.InitializedLiteral;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.context.AbstractContextsService;
 import org.apache.webbeans.context.ApplicationContext;
@@ -61,8 +63,14 @@ public class DefaultContextsService exte
         dependentContext = new ThreadLocal<DependentContext>();
         singletonContext = new ThreadLocal<SingletonContext>();
     }
-    
-    
+
+    private final WebBeansContext webBeansContext;
+
+    public DefaultContextsService(WebBeansContext webBeansContext)
+    {
+        this.webBeansContext = webBeansContext;
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -262,6 +270,11 @@ public class DefaultContextsService exte
         ctx.setActive(true);
         
         applicationContext = ctx;
+
+        // We do ALSO send the @Initialized(ApplicationScoped.class) at this location but this is WAY to early for userland apps
+        // This also gets sent in the application startup code after AfterDeploymentValidation got fired.
+        // see AbstractLifecycle#afterStartApplication
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), InitializedLiteral.INSTANCE_APPLICATION_SCOPED);
     }
 
     
@@ -271,7 +284,7 @@ public class DefaultContextsService exte
         ctx.setActive(true);
         
         conversationContext.set(ctx);
-        
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), InitializedLiteral.INSTANCE_CONVERSATION_SCOPED);
     }
 
     
@@ -282,6 +295,7 @@ public class DefaultContextsService exte
         ctx.setActive(true);
         
         requestContext.set(ctx);
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), InitializedLiteral.INSTANCE_REQUEST_SCOPED);
     }
 
     
@@ -291,6 +305,7 @@ public class DefaultContextsService exte
         ctx.setActive(true);
         
         sessionContext.set(ctx);
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), InitializedLiteral.INSTANCE_SESSION_SCOPED);
     }
 
     
@@ -301,6 +316,7 @@ public class DefaultContextsService exte
         ctx.setActive(true);
         
         singletonContext.set(ctx);
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), InitializedLiteral.INSTANCE_SINGLETON_SCOPED);
     }
 
     
@@ -313,6 +329,7 @@ public class DefaultContextsService exte
 
             // this is needed to get rid of ApplicationScoped beans which are cached inside the proxies...
             WebBeansContext.currentInstance().getBeanManagerImpl().clearCacheProxies();
+            webBeansContext.getBeanManagerImpl().fireEvent(new Object(), DestroyedLiteral.INSTANCE_APPLICATION_SCOPED);
         }
     }
 
@@ -326,6 +343,7 @@ public class DefaultContextsService exte
 
         conversationContext.set(null);
         conversationContext.remove();
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), DestroyedLiteral.INSTANCE_CONVERSATION_SCOPED);
     }
 
     
@@ -338,6 +356,7 @@ public class DefaultContextsService exte
 
         requestContext.set(null);
         requestContext.remove();
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), DestroyedLiteral.INSTANCE_REQUEST_SCOPED);
     }
 
     
@@ -350,6 +369,7 @@ public class DefaultContextsService exte
 
         sessionContext.set(null);
         sessionContext.remove();
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), DestroyedLiteral.INSTANCE_SESSION_SCOPED);
     }
 
     
@@ -362,6 +382,7 @@ public class DefaultContextsService exte
 
         singletonContext.set(null);
         singletonContext.remove();
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), DestroyedLiteral.INSTANCE_SINGLETON_SCOPED);
     }
 
 }

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=1672501&r1=1672500&r2=1672501&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 Apr  9 21:50:31 2015
@@ -229,7 +229,7 @@ public abstract class AbstractLifeCycle
     {
         //Do nothing as default
     }
-        
+
     protected void afterStartApplication(Object startupObject)
     {
         //Do nothing as default

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/StandaloneLifeCycle.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/StandaloneLifeCycle.java?rev=1672501&r1=1672500&r2=1672501&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/StandaloneLifeCycle.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/StandaloneLifeCycle.java Thu Apr  9 21:50:31 2015
@@ -26,6 +26,7 @@ import javax.inject.Singleton;
 import java.util.Properties;
 import java.util.logging.Logger;
 
+import org.apache.webbeans.annotation.InitializedLiteral;
 import org.apache.webbeans.config.WebBeansFinder;
 import org.apache.webbeans.el.ELContextStore;
 import org.apache.webbeans.logger.WebBeansLoggerFacade;
@@ -50,19 +51,27 @@ public class StandaloneLifeCycle extends
     @Override
     public void beforeStartApplication(Object object)
     {
+        webBeansContext.getContextsService().startContext(Singleton.class, null);
+        webBeansContext.getContextsService().startContext(ApplicationScoped.class, null);
+    }
+
+    @Override
+    protected void afterStartApplication(Object startupObject)
+    {
+        // the ApplicationContext is already started, but we fire the event again as the userland beans are only available now
+        webBeansContext.getBeanManagerImpl().fireEvent(new Object(), InitializedLiteral.INSTANCE_APPLICATION_SCOPED);
+
         webBeansContext.getContextsService().startContext(RequestScoped.class, null);
         webBeansContext.getContextsService().startContext(SessionScoped.class, null);
         webBeansContext.getContextsService().startContext(ConversationScoped.class, null);
-        webBeansContext.getContextsService().startContext(ApplicationScoped.class, null);
-        webBeansContext.getContextsService().startContext(Singleton.class, null);
     }
 
     @Override
     public void beforeStopApplication(Object endObject)
     {
         webBeansContext.getContextsService().endContext(RequestScoped.class, null);
-        webBeansContext.getContextsService().endContext(SessionScoped.class, null);
         webBeansContext.getContextsService().endContext(ConversationScoped.class, null);
+        webBeansContext.getContextsService().endContext(SessionScoped.class, null);
         webBeansContext.getContextsService().endContext(ApplicationScoped.class, null);
         webBeansContext.getContextsService().endContext(Singleton.class, null);
 

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/common/AppScopedBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/common/AppScopedBean.java?rev=1672501&r1=1672500&r2=1672501&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/common/AppScopedBean.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/common/AppScopedBean.java Thu Apr  9 21:50:31 2015
@@ -19,14 +19,62 @@
 package org.apache.webbeans.test.contexts.session.common;
 
 import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Destroyed;
+import javax.enterprise.context.Initialized;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Observes;
 import javax.inject.Inject;
+import java.util.ArrayList;
+import java.util.List;
 
 @ApplicationScoped
 public class AppScopedBean {
     
     private @Inject PersonalDataBean pdb;
 
+    public static List<Object> appContextInitializedEvent = new ArrayList<Object>();
+    public static List<Object> appContextDestroyedEvent = new ArrayList<Object>();
+
+    public static List<Object> sessionContextInitializedEvent = new ArrayList<Object>();
+    public static List<Object> sessionContextDestroyedEvent = new ArrayList<Object>();
+
+    public static List<Object> requestContextInitializedEvent = new ArrayList<Object>();
+    public static List<Object> requestContextDestroyedEvent = new ArrayList<Object>();
+
     public PersonalDataBean getPdb() {
         return pdb;
     }
+
+    public void appContextInitialized(@Observes @Initialized(ApplicationScoped.class) Object payload)
+    {
+        appContextInitializedEvent.add(payload);
+    }
+
+    public void appContextDestroyed(@Observes @Destroyed(ApplicationScoped.class) Object payload)
+    {
+        appContextDestroyedEvent.add(payload);
+    }
+
+    public void sessionContextInitialized(@Observes @Initialized(SessionScoped.class) Object payload)
+    {
+        sessionContextInitializedEvent.add(payload);
+    }
+
+    public void sessionContextDestroyed(@Observes @Destroyed(SessionScoped.class) Object payload)
+    {
+        sessionContextDestroyedEvent.add(payload);
+    }
+
+    public void requestContextInitialized(@Observes @Initialized(RequestScoped.class) Object payload)
+    {
+        requestContextInitializedEvent.add(payload);
+    }
+
+    public void requestContextDestroyed(@Observes @Destroyed(RequestScoped.class) Object payload)
+    {
+        requestContextDestroyedEvent.add(payload);
+    }
+
+    
 }

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/tests/SessionContextTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/tests/SessionContextTest.java?rev=1672501&r1=1672500&r2=1672501&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/tests/SessionContextTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/session/tests/SessionContextTest.java Thu Apr  9 21:50:31 2015
@@ -21,6 +21,7 @@ package org.apache.webbeans.test.context
 import java.util.ArrayList;
 import java.util.Collection;
 
+import javax.enterprise.context.RequestScoped;
 import javax.enterprise.context.SessionScoped;
 
 import junit.framework.Assert;
@@ -73,6 +74,13 @@ public class SessionContextTest extends
         classes.add(CircularDependentScopedBean.class);
         classes.add(CircularApplicationScopedBean.class);
 
+        AppScopedBean.appContextInitializedEvent.clear();
+        AppScopedBean.appContextDestroyedEvent.clear();
+        AppScopedBean.sessionContextInitializedEvent.clear();
+        AppScopedBean.sessionContextDestroyedEvent.clear();
+        AppScopedBean.requestContextInitializedEvent.clear();
+        AppScopedBean.requestContextDestroyedEvent.clear();
+
         startContainer(classes);
         
         AppScopedBean appBeanInstance = getInstance(AppScopedBean.class);
@@ -82,12 +90,28 @@ public class SessionContextTest extends
         
         // now we reset the session Context so we should get a new contextual instance.
         getWebBeansContext().getContextsService().endContext(SessionScoped.class, null);
+        getWebBeansContext().getContextsService().endContext(RequestScoped.class, null);
+        getWebBeansContext().getContextsService().startContext(RequestScoped.class, null);
         getWebBeansContext().getContextsService().startContext(SessionScoped.class, null);
-        
+
         PersonalDataBean pdb2 = appBeanInstance.getPdb().getInstance();
         Assert.assertNotNull(pdb2);
         
         // pdb1 and pdb2 are in different sessions, so they must not be the same instance!
         Assert.assertTrue(pdb1 != pdb2);
+
+        Assert.assertEquals(1, AppScopedBean.appContextInitializedEvent.size());
+        Assert.assertEquals(2, AppScopedBean.sessionContextInitializedEvent.size());
+        Assert.assertEquals(1, AppScopedBean.sessionContextDestroyedEvent.size());
+        Assert.assertEquals(2, AppScopedBean.requestContextInitializedEvent.size());
+        Assert.assertEquals(1, AppScopedBean.requestContextDestroyedEvent.size());
+
+        shutDownContainer();
+        Assert.assertEquals(2, AppScopedBean.requestContextDestroyedEvent.size());
+        Assert.assertEquals(2, AppScopedBean.sessionContextDestroyedEvent.size());
+
+        // looks weird at a first glance but thats how it is defined in the spec.
+        // the @Destroyed(ApplicationScoped.class) gets only fired AFTER the ApplicationContext gets destroyed...
+        Assert.assertEquals(0, AppScopedBean.appContextDestroyedEvent.size());
     }
 }