Author: struberg
Date: Mon Jun  1 14:03:31 2015
New Revision: 1682905

URL: http://svn.apache.org/r1682905
Log:
OWB-1055 fold back Begin and End listeners into one single Listener

It turned out not to be needed.

Removed:
    
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
    
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
Modified:
    
openwebbeans/trunk/webbeans-web/src/it/webcdiapp/src/main/webapp/WEB-INF/web.xml
    
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java

Modified: 
openwebbeans/trunk/webbeans-web/src/it/webcdiapp/src/main/webapp/WEB-INF/web.xml
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/it/webcdiapp/src/main/webapp/WEB-INF/web.xml?rev=1682905&r1=1682904&r2=1682905&view=diff
==============================================================================
Binary files - no diff available.

Modified: 
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java?rev=1682905&r1=1682904&r2=1682905&view=diff
==============================================================================
--- 
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
 (original)
+++ 
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
 Mon Jun  1 14:03:31 2015
@@ -18,14 +18,26 @@
  */
 package org.apache.webbeans.servlet;
 
+import org.apache.webbeans.config.OWBLogConst;
 import org.apache.webbeans.config.WebBeansContext;
-
+import org.apache.webbeans.el.ELContextStore;
+import org.apache.webbeans.logger.WebBeansLoggerFacade;
+import org.apache.webbeans.spi.ContainerLifecycle;
+import org.apache.webbeans.spi.ContextsService;
+import org.apache.webbeans.util.WebBeansUtil;
+import org.apache.webbeans.web.util.ServletCompatibilityUtil;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.context.spi.Context;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 import javax.servlet.ServletRequestEvent;
 import javax.servlet.ServletRequestListener;
 import javax.servlet.http.HttpSessionEvent;
 import javax.servlet.http.HttpSessionListener;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * Initializing the beans container for using in an web application
@@ -39,14 +51,17 @@ import javax.servlet.http.HttpSessionLis
  * {@link WebBeansConfigurationFilter} and {@link 
WebBeansConfigurationHttpSessionListener}
  * instead.
  *
- * @see BeginWebBeansConfigurationListener
- * @see EndWebBeansConfigurationListener
  */
 public class WebBeansConfigurationListener implements ServletContextListener, 
ServletRequestListener, HttpSessionListener
 {
+    private static final Logger logger = 
WebBeansLoggerFacade.getLogger(WebBeansConfigurationListener.class);
+
+
+    /**Manages the container lifecycle*/
+    protected ContainerLifecycle lifeCycle = null;
+
     private WebBeansContext webBeansContext;
-    private BeginWebBeansConfigurationListener 
beginWebBeansConfigurationListener;
-    private EndWebBeansConfigurationListener endWebBeansConfigurationListener;
+    private ContextsService contextsService;
 
     /**
      * Default constructor
@@ -54,49 +69,161 @@ public class WebBeansConfigurationListen
     public WebBeansConfigurationListener()
     {
         webBeansContext = WebBeansContext.getInstance();
-        beginWebBeansConfigurationListener = new 
BeginWebBeansConfigurationListener(webBeansContext);
-        endWebBeansConfigurationListener = new 
EndWebBeansConfigurationListener(webBeansContext);
+        contextsService = webBeansContext.getContextsService();
     }
 
-
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    public void contextInitialized(ServletContextEvent sce)
+    public void contextInitialized(ServletContextEvent event)
     {
-        beginWebBeansConfigurationListener.contextInitialized(sce);
+        this.lifeCycle = webBeansContext.getService(ContainerLifecycle.class);
 
-        // for setting the lifecycle
-        endWebBeansConfigurationListener.contextInitialized(sce);
+        try
+        {
+            this.lifeCycle.startApplication(event);
+        }
+        catch (Exception e)
+        {
+            logger.log(Level.SEVERE,
+                WebBeansLoggerFacade.constructMessage(
+                    OWBLogConst.ERROR_0018,
+                    
ServletCompatibilityUtil.getServletInfo(event.getServletContext())));
+            WebBeansUtil.throwRuntimeExceptions(e);
+        }
     }
 
+
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    public void contextDestroyed(ServletContextEvent sce)
+    public void requestInitialized(ServletRequestEvent event)
     {
-        endWebBeansConfigurationListener.contextDestroyed(sce);
+        try
+        {
+            if (logger.isLoggable(Level.FINE))
+            {
+                logger.log(Level.FINE, "Starting a new request : [{0}]", event 
== null ? "null" : event.getServletRequest().getRemoteAddr());
+            }
+
+            
this.lifeCycle.getContextService().startContext(RequestScoped.class, event);
+
+            // we don't initialise the Session here but do it lazily if it 
gets requested
+            // the first time. See OWB-457
+        }
+        catch (Exception e)
+        {
+            logger.log(Level.SEVERE,
+                WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0019, 
event == null ? "null" : event.getServletRequest()));
+            WebBeansUtil.throwRuntimeExceptions(e);
+        }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    public void sessionCreated(HttpSessionEvent se)
+    public void sessionCreated(HttpSessionEvent event)
     {
-        beginWebBeansConfigurationListener.sessionCreated(se);
+        try
+        {
+            if (logger.isLoggable(Level.FINE))
+            {
+                logger.log(Level.FINE, "Starting a session with session id : 
[{0}]", event.getSession().getId());
+            }
+            
this.lifeCycle.getContextService().startContext(SessionScoped.class, 
event.getSession());
+        }
+        catch (Exception e)
+        {
+            logger.log(Level.SEVERE,
+                WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0020, 
event.getSession()));
+            WebBeansUtil.throwRuntimeExceptions(e);
+        }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    public void sessionDestroyed(HttpSessionEvent se)
+    public void contextDestroyed(ServletContextEvent event)
     {
-        endWebBeansConfigurationListener.sessionDestroyed(se);
-    }
+        if (lifeCycle != null)
+        {
+            lifeCycle.stopApplication(event);
+        }
 
+        // just to be sure that we didn't lazily create anything...
+        cleanupRequestThreadLocals();
+    }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    public void requestInitialized(ServletRequestEvent sre)
+    public void requestDestroyed(ServletRequestEvent event)
     {
-        beginWebBeansConfigurationListener.requestInitialized(sre);
+        if (logger.isLoggable(Level.FINE))
+        {
+            logger.log(Level.FINE, "Destroying a request : [{0}]", event == 
null ? "null" : event.getServletRequest().getRemoteAddr());
+        }
+
+        // clean up the EL caches after each request
+        ELContextStore elStore = ELContextStore.getInstance(false);
+        if (elStore != null)
+        {
+            elStore.destroyELContextStore();
+        }
+
+        this.lifeCycle.getContextService().endContext(RequestScoped.class, 
event);
+
+        this.cleanupRequestThreadLocals();
     }
 
+
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    public void requestDestroyed(ServletRequestEvent sre)
+    public void sessionDestroyed(HttpSessionEvent event)
+    {
+        if (logger.isLoggable(Level.FINE))
+        {
+            logger.log(Level.FINE, "Destroying a session with session id : 
[{0}]", event.getSession().getId());
+        }
+        boolean mustDestroy = ensureRequestScope();
+
+        this.lifeCycle.getContextService().endContext(SessionScoped.class, 
event.getSession());
+
+        if (mustDestroy)
+        {
+            requestDestroyed(null);
+        }
+    }
+
+    private boolean ensureRequestScope()
+    {
+        Context context = 
this.lifeCycle.getContextService().getCurrentContext(RequestScoped.class);
+
+        if (context == null || !context.isActive())
+        {
+            requestInitialized(null);
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Ensures that all ThreadLocals, which could have been set in this
+     * requests Thread, are removed in order to prevent memory leaks.
+     */
+    private void cleanupRequestThreadLocals()
     {
-        endWebBeansConfigurationListener.requestDestroyed(sre);
+        if (contextsService != null)
+        {
+            contextsService.removeThreadLocals();
+        }
     }
 
 }


Reply via email to