You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by jl...@apache.org on 2013/11/04 12:34:00 UTC

svn commit: r1538562 - /openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java

Author: jlmonteiro
Date: Mon Nov  4 11:33:59 2013
New Revision: 1538562

URL: http://svn.apache.org/r1538562
Log:
OWB-910 request context not initialized for HTTP lifecycle methods

Modified:
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java

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=1538562&r1=1538561&r2=1538562&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 Nov  4 11:33:59 2013
@@ -43,31 +43,33 @@ import java.util.logging.Logger;
 /**
  * Initializing the beans container for using in an web application
  * environment.
- *
+ * <p/>
  * This is the main entry point for starting the CDI container
  * for a servlet.
- *
- *
+ * <p/>
+ * <p/>
  * If you have a container with &lt; Servlet-2.5 then use
  * {@link WebBeansConfigurationFilter} and {@link WebBeansConfigurationHttpSessionListener}
  * instead.
  */
-public class WebBeansConfigurationListener implements ServletContextListener, ServletRequestListener, HttpSessionListener
-{
-    /**Logger instance*/
+public class WebBeansConfigurationListener implements ServletContextListener, ServletRequestListener, HttpSessionListener {
+    /**
+     * Logger instance
+     */
     private static final Logger logger = WebBeansLoggerFacade.getLogger(WebBeansConfigurationListener.class);
-    
-    /**Manages the container lifecycle*/
+
+    /**
+     * Manages the container lifecycle
+     */
     protected ContainerLifecycle lifeCycle = null;
-    
+
     protected FailOverService failoverService = null;
     private WebBeansContext webBeansContext;
 
     /**
      * Default constructor
      */
-    public WebBeansConfigurationListener()
-    {
+    public WebBeansConfigurationListener() {
         webBeansContext = WebBeansContext.getInstance();
         failoverService = webBeansContext.getService(FailOverService.class);
     }
@@ -76,31 +78,26 @@ public class WebBeansConfigurationListen
      * {@inheritDoc}
      */
     @Override
-    public void contextInitialized(ServletContextEvent event)
-    {
+    public void contextInitialized(ServletContextEvent event) {
         this.lifeCycle = webBeansContext.getService(ContainerLifecycle.class);
 
-        try
-        {
-                this.lifeCycle.startApplication(event);
-        }
-        catch (Exception e)
-        {
-             logger.log(Level.SEVERE,
-                     WebBeansLoggerFacade.constructMessage(
-                             OWBLogConst.ERROR_0018,
-                             ServletCompatibilityUtil.getServletInfo(event.getServletContext())));
-             WebBeansUtil.throwRuntimeExceptions(e);
+        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 event)
-    {
+    public void contextDestroyed(ServletContextEvent event) {
         this.lifeCycle.stopApplication(event);
         this.lifeCycle = null;
 
@@ -112,17 +109,14 @@ public class WebBeansConfigurationListen
      * {@inheritDoc}
      */
     @Override
-    public void requestDestroyed(ServletRequestEvent event)
-    {
-        if (logger.isLoggable(Level.FINE))
-        {
-            logger.log(Level.FINE, "Destroying a request : [{0}]", event.getServletRequest().getRemoteAddr());
+    public void requestDestroyed(ServletRequestEvent event) {
+        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)
-        {
+        if (elStore != null) {
             elStore.destroyELContextStore();
         }
 
@@ -135,8 +129,7 @@ public class WebBeansConfigurationListen
      * Ensures that all ThreadLocals, which could have been set in this
      * requests Thread, are removed in order to prevent memory leaks.
      */
-    private void cleanupRequestThreadLocals()
-    {
+    private void cleanupRequestThreadLocals() {
         WebContextsService.removeThreadLocals();
     }
 
@@ -144,24 +137,19 @@ public class WebBeansConfigurationListen
      * {@inheritDoc}
      */
     @Override
-    public void requestInitialized(ServletRequestEvent event)
-    {
-        try
-        {
-            if (logger.isLoggable(Level.FINE))
-            {
-                logger.log(Level.FINE, "Starting a new request : [{0}]", event.getServletRequest().getRemoteAddr());
+    public void requestInitialized(ServletRequestEvent event) {
+        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)
-        {
+        } catch (Exception e) {
             logger.log(Level.SEVERE,
-                    WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0019, event.getServletRequest()));
+                    WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0019, event == null ? "null" : event.getServletRequest()));
             WebBeansUtil.throwRuntimeExceptions(e);
         }
     }
@@ -170,18 +158,13 @@ public class WebBeansConfigurationListen
      * {@inheritDoc}
      */
     @Override
-    public void sessionCreated(HttpSessionEvent event)
-    {
-        try
-        {
-            if (logger.isLoggable(Level.FINE))
-            {
+    public void sessionCreated(HttpSessionEvent event) {
+        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)
-        {
+        } catch (Exception e) {
             logger.log(Level.SEVERE,
                     WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0020, event.getSession()));
             WebBeansUtil.throwRuntimeExceptions(e);
@@ -192,13 +175,26 @@ public class WebBeansConfigurationListen
      * {@inheritDoc}
      */
     @Override
-    public void sessionDestroyed(HttpSessionEvent event)
-    {
-        if (logger.isLoggable(Level.FINE))
-        {
+    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());
         this.lifeCycle.getContextService().endContext(ConversationScoped.class, event.getSession());
+
+        if (mustDestroy) {
+            requestDestroyed(null);
+        }
     }
+
+    private boolean ensureRequestScope() {
+        if (!this.lifeCycle.getContextService().getCurrentContext(RequestScoped.class).isActive()) {
+            requestInitialized(null);
+            return true;
+        }
+        return false;
+    }
+
 }