openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1677289 - in /openwebbeans/trunk: webbeans-impl/src/main/java/org/apache/webbeans/context/ webbeans-impl/src/main/java/org/apache/webbeans/corespi/se/ webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/ webbeans-impl/src/test/java/o...
Date Sat, 02 May 2015 08:57:21 GMT
Author: struberg
Date: Sat May  2 08:57:20 2015
New Revision: 1677289

URL: http://svn.apache.org/r1677289
Log:
OWB-1064 split destroy of custom beans and system beans for ApplicationContext

This is needed as we still need the ExtensionBeans, BeanManagerBean, etc during
BeforeShutdown.

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ApplicationContext.java
    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/test/java/org/apache/webbeans/test/contexts/session/tests/SessionContextTest.java
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ApplicationContext.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ApplicationContext.java?rev=1677289&r1=1677288&r2=1677289&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ApplicationContext.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ApplicationContext.java
Sat May  2 08:57:20 2015
@@ -18,11 +18,14 @@
  */
 package org.apache.webbeans.context;
 
+import java.util.HashSet;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.context.spi.Contextual;
 
+import org.apache.webbeans.component.BuiltInOwbBean;
 import org.apache.webbeans.context.creational.BeanInstanceBag;
 
 /**
@@ -44,9 +47,36 @@ public class ApplicationContext extends
         componentInstanceMap = new ConcurrentHashMap<Contextual<?>, BeanInstanceBag<?>>();
     }
 
+
+    /**
+     * By default a Context destroys all it's Contextual Instances.
+     * But for the ApplicationContext we only need to destroy custom beans and _not_ Extensions
and internal beans
+     */
     @Override
-    public void destroy(Contextual<?> contextual)
+    public void destroy()
     {
-        super.destroy(contextual);
+        Set<Contextual<?>> keySet = new HashSet<Contextual<?>>(componentInstanceMap.keySet());
+        for (Contextual<?> contextual: keySet)
+        {
+            if (contextual instanceof BuiltInOwbBean)
+            {
+                // we do NOT yet dextroy our internal beans as we probably need them for
BeforeShutdown still.
+                continue;
+            }
+
+            destroy(contextual);
+        }
     }
+
+    /**
+     * This method should only get called at container shutdown.
+     * It will destroy the Extensions as well
+     */
+    public void destroySystemBeans()
+    {
+        super.destroy();
+        setActive(false);
+    }
+
+
 }

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=1677289&r1=1677288&r2=1677289&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
Sat May  2 08:57:20 2015
@@ -129,7 +129,7 @@ public class DefaultContextsService exte
         }
         else if(scopeType.equals(ApplicationScoped.class))
         {
-            return getCurrentApplicationContext();
+            return applicationContext;
         }
         else if(scopeType.equals(ConversationScoped.class) && supportsConversation)
         {
@@ -247,15 +247,16 @@ public class DefaultContextsService exte
 
         dependentContext.set(null);
         dependentContext.remove();
+
+        if (applicationContext != null)
+        {
+            applicationContext.destroy();
+            applicationContext.destroySystemBeans();
+        }
     }
 
 
-    private Context getCurrentApplicationContext()
-    {        
-        return applicationContext;
-    }
 
-    
     private Context getCurrentConversationContext()
     {
         ConversationContext conversationCtx = conversationContext.get();
@@ -378,7 +379,6 @@ public class DefaultContextsService exte
         if(applicationContext != null)
         {
             applicationContext.destroy();
-            applicationContext = null;
 
             // this is needed to get rid of ApplicationScoped beans which are cached inside
the proxies...
             WebBeansContext.currentInstance().getBeanManagerImpl().clearCacheProxies();

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=1677289&r1=1677288&r2=1677289&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
Sat May  2 08:57:20 2015
@@ -159,10 +159,9 @@ public abstract class AbstractLifeCycle
 
             //Fire shut down
             beanManager.fireLifecycleEvent(new BeforeShutdownImpl());
-            
-            //Destroys context
+
             contextsService.destroy(endObject);
-            
+
             //Unbind BeanManager
             jndiService.unbind(WebBeansConstants.WEB_BEANS_MANAGER_JNDI_NAME);
 

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=1677289&r1=1677288&r2=1677289&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
Sat May  2 08:57:20 2015
@@ -110,8 +110,6 @@ public class SessionContextTest extends
         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());
+        Assert.assertEquals(1, AppScopedBean.appContextDestroyedEvent.size());
     }
 }

Modified: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java?rev=1677289&r1=1677288&r2=1677289&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
(original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
Sat May  2 08:57:20 2015
@@ -142,29 +142,41 @@ public class WebContextsService extends
     @Override
     public void destroy(Object destroyObject)
     {
-        //Destroy application context
-        endContext(ApplicationScoped.class, destroyObject);
+        RequestContext requestCtx = requestContexts.get();
+        if (requestCtx != null)
+        {
+            requestCtx.destroy();
+            requestContexts.set(null);
+            requestContexts.remove();
+        }
+
+        SessionContext sessionCtx = sessionContexts.get();
+        if (sessionCtx != null)
+        {
+            sessionCtx.destroy();
+            sessionContexts.set(null);
+            sessionContexts.remove();
+        }
+
+        ConversationContext conversationCtx = conversationContexts.get();
+        if (conversationCtx != null)
+        {
+            conversationCtx.destroy();
+            conversationContexts.set(null);
+            conversationContexts.remove();
+        }
 
-        // we also need to destroy the shared ApplicationContext
-        applicationContext.destroy();
-        
-        //Destroy singleton context
-        endContext(Singleton.class, destroyObject);
         if (singletonContext != null)
         {
             singletonContext.destroy();
+            singletonContext = null;
         }
 
-        //Thread local values to null
-        requestContexts.set(null);
-        sessionContexts.set(null);
-        conversationContexts.set(null);
-
-        //Remove thread locals
-        //for preventing memory leaks
-        requestContexts.remove();
-        sessionContexts.remove();
-        conversationContexts.remove();
+        if (applicationContext != null)
+        {
+            applicationContext.destroy();
+            applicationContext.destroySystemBeans();
+        }
     }
     
     
@@ -215,11 +227,11 @@ public class WebContextsService extends
     {
         if(scopeType.equals(RequestScoped.class))
         {
-            return getRequestContext();
+            return getRequestContext(true);
         }
         else if(scopeType.equals(SessionScoped.class))
         {
-            return getSessionContext();
+            return getSessionContext(true);
         }
         else if(scopeType.equals(ApplicationScoped.class))
         {
@@ -227,7 +239,7 @@ public class WebContextsService extends
         }
         else if(supportsConversation && scopeType.equals(ConversationScoped.class))
         {
-            return getConversationContext();
+            return getConversationContext(true);
         }
         else if(scopeType.equals(Dependent.class))
         {
@@ -338,7 +350,7 @@ public class WebContextsService extends
         }
 
         //Get context
-        RequestContext context = getRequestContext();
+        RequestContext context = getRequestContext(false);
 
         //Destroy context
         if (context != null)
@@ -474,7 +486,8 @@ public class WebContextsService extends
         sessionContexts.set(null);
         sessionContexts.remove();
 
-
+        // note that this does _not_ set it to inactive and _not_ destroy Extensions and
internal Beans
+        applicationContext.destroy();
     }
 
     /**
@@ -496,8 +509,6 @@ public class WebContextsService extends
         if (applicationContext == null)
         {
             applicationContext = newApplicationContext;
-            Object payLoad = startupObject != null && startupObject instanceof ServletContext
? (ServletContext) startupObject : new Object();
-            webBeansContext.getBeanManagerImpl().fireEvent(payLoad, InitializedLiteral.INSTANCE_APPLICATION_SCOPED);
         }
     }
 
@@ -508,22 +519,16 @@ public class WebContextsService extends
      */
     protected void destroyApplicationContext(Object endObject)
     {
-        //look for thread local
-        //this can be set by initRequestContext
-        ApplicationContext context = applicationContext;
-        singletonContext = null;
-
         //Destroy context
-        if(context != null)
+        if(applicationContext != null)
         {
-            context.destroy();
-        }
+            applicationContext.destroy();
+            // this is needed to get rid of ApplicationScoped beans which are cached inside
the proxies...
+            webBeansContext.getBeanManagerImpl().clearCacheProxies();
 
-        // this is needed to get rid of ApplicationScoped beans which are cached inside the
proxies...
-        webBeansContext.getBeanManagerImpl().clearCacheProxies();
-
-        Object payload = endObject != null && endObject instanceof ServletContext
? endObject : new Object();
-        webBeansContext.getBeanManagerImpl().fireEvent(payload, DestroyedLiteral.INSTANCE_APPLICATION_SCOPED);
+            Object payload = endObject != null && endObject instanceof ServletContext
? endObject : new Object();
+            webBeansContext.getBeanManagerImpl().fireEvent(payload, DestroyedLiteral.INSTANCE_APPLICATION_SCOPED);
+        }
     }
     
     /**
@@ -537,16 +542,16 @@ public class WebContextsService extends
             return;
         }
 
-        SingletonContext newSingletonContext = new SingletonContext();
-        newSingletonContext.setActive(true);
-
-        if (singletonContext == null)
+        synchronized (this)
         {
-            singletonContext = newSingletonContext;
+            if (singletonContext == null)
+            {
+                singletonContext = new SingletonContext();
+                singletonContext.setActive(true);
+                Object payLoad = startupObject != null && startupObject instanceof
ServletContext ? (ServletContext) startupObject : new Object();
+                webBeansContext.getBeanManagerImpl().fireEvent(payLoad, InitializedLiteral.INSTANCE_SINGLETON_SCOPED);
+            }
         }
-
-        Object payLoad = startupObject != null && startupObject instanceof ServletContext
? (ServletContext) startupObject : new Object();
-        webBeansContext.getBeanManagerImpl().fireEvent(payLoad, InitializedLiteral.INSTANCE_SINGLETON_SCOPED);
     }
     
     /**
@@ -555,15 +560,13 @@ public class WebContextsService extends
      */
     protected void destroySingletonContext(Object endObject)
     {
-        SingletonContext context = singletonContext;
-
-        if (context != null)
+        if (singletonContext != null)
         {
-            context.destroy();
+            singletonContext.destroy();
+            singletonContext = null;
+            Object payload = endObject != null ? endObject : new Object();
+            webBeansContext.getBeanManagerImpl().fireEvent(payload, DestroyedLiteral.INSTANCE_SINGLETON_SCOPED);
         }
-
-        Object payload = endObject != null && endObject instanceof ServletContext
? endObject : new Object();
-        webBeansContext.getBeanManagerImpl().fireEvent(payload, DestroyedLiteral.INSTANCE_SINGLETON_SCOPED);
     }
 
     /**
@@ -596,7 +599,7 @@ public class WebContextsService extends
             return;
         }
 
-        ConversationContext context = getConversationContext();
+        ConversationContext context = getConversationContext(false);
 
         if (context != null)
         {
@@ -613,19 +616,24 @@ public class WebContextsService extends
      * Get current request ctx.
      * @return request context
      */
-    private  RequestContext getRequestContext()
+    public RequestContext getRequestContext(boolean create)
     {
-        return requestContexts.get();
+        RequestContext requestContext = requestContexts.get();
+        if (requestContext == null && create)
+        {
+            initRequestContext(null);
+        }
+        return requestContext;
     }
 
     /**
      * Get current session ctx.
      * @return session context
      */
-    private  SessionContext getSessionContext()
+    public SessionContext getSessionContext(boolean create)
     {
         SessionContext context = sessionContexts.get();
-        if (null == context)
+        if (null == context && create)
         {
             lazyStartSessionContext();
             context = sessionContexts.get();
@@ -638,10 +646,10 @@ public class WebContextsService extends
      * Get current conversation ctx.
      * @return conversation context
      */
-    private  ConversationContext getConversationContext()
+    public  ConversationContext getConversationContext(boolean create)
     {
         ConversationContext conversationContext = conversationContexts.get();
-        if (conversationContext == null)
+        if (conversationContext == null && create)
         {
             conversationContext = conversationManager.getConversationContext();
             conversationContexts.set(conversationContext);
@@ -676,7 +684,7 @@ public class WebContextsService extends
             logger.log(Level.FINE, ">lazyStartSessionContext");
         }
 
-        RequestContext context = getRequestContext();
+        RequestContext context = getRequestContext(true);
         if (context == null)
         {
             logger.log(Level.WARNING, "Could NOT lazily initialize session context because
NO active request context");
@@ -724,7 +732,7 @@ public class WebContextsService extends
         {
             // getSessionContext() implicitely creates and binds the SessionContext
             // to the current Thread if it doesn't yet exist.
-            getSessionContext().setActive(true);
+            getSessionContext(true).setActive(true);
         }
         else
         {

Modified: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java?rev=1677289&r1=1677288&r2=1677289&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
(original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
Sat May  2 08:57:20 2015
@@ -21,6 +21,7 @@ package org.apache.webbeans.web.lifecycl
 import org.apache.webbeans.annotation.InitializedLiteral;
 import org.apache.webbeans.config.OWBLogConst;
 import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.el.ELContextStore;
 import org.apache.webbeans.exception.WebBeansException;
 import org.apache.webbeans.lifecycle.AbstractLifeCycle;
 import org.apache.webbeans.logger.WebBeansLoggerFacade;
@@ -29,13 +30,16 @@ import org.apache.webbeans.spi.adaptor.E
 import org.apache.webbeans.web.util.ServletCompatibilityUtil;
 
 import javax.el.ELResolver;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.ConversationScoped;
 import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
 import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Singleton;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.jsp.JspApplicationContext;
 import javax.servlet.jsp.JspFactory;
-import java.util.concurrent.ScheduledExecutorService;
 import java.util.logging.Level;
 
 /**
@@ -51,9 +55,6 @@ import java.util.logging.Level;
  */
 public final class WebContainerLifecycle extends AbstractLifeCycle
 {
-    /**Manages unused conversations*/
-    private ScheduledExecutorService service = null;
-
 
     /**
      * Creates a new lifecycle instance and initializes
@@ -140,10 +141,19 @@ public final class WebContainerLifecycle
     @Override
     protected void beforeStopApplication(Object stopObject)
     {
-        if(service != null)
+        webBeansContext.getContextsService().endContext(RequestScoped.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);
+
+        // clean up the EL caches after each request
+        ELContextStore elStore = ELContextStore.getInstance(false);
+        if (elStore != null)
         {
-            service.shutdownNow();
+            elStore.destroyELContextStore();
         }
+
     }
 
     /**



Mime
View raw message