You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openwebbeans.apache.org by Romain Manni-Bucau <rm...@gmail.com> on 2015/04/27 15:04:25 UTC

Fwd: svn commit: r1676248 - in /openwebbeans/trunk/webbeans-web/src/main/java: META-INF/ org/apache/webbeans/servlet/

Hi Mark

Did you test on tomcat 8? Blind guess - ie not tested - is it doesnt work
cause init/destroy order is logical
---------- Message transféré ----------
De : <st...@apache.org>
Date : 27 avr. 2015 14:59
Objet : svn commit: r1676248 - in
/openwebbeans/trunk/webbeans-web/src/main/java: META-INF/
org/apache/webbeans/servlet/
À : <co...@openwebbeans.apache.org>
Cc :

Author: struberg
Date: Mon Apr 27 12:59:02 2015
New Revision: 1676248

URL: http://svn.apache.org/r1676248
Log:
OWB-1055 Split WebBeansConfigurationListener in Begin and End listeners

We need this as the order is the same for all init and destroy methods.
But CDI should get init as first listener, but destroyed as last.

Added:

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
Removed:
    openwebbeans/trunk/webbeans-web/src/main/java/META-INF/
Modified:

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

Added:
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java?rev=1676248&view=auto
==============================================================================
---
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
(added)
+++
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
Mon Apr 27 12:59:02 2015
@@ -0,0 +1,175 @@
+/*
+ * 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.servlet;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
+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;
+
+import org.apache.webbeans.config.OWBLogConst;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.logger.WebBeansLoggerFacade;
+import org.apache.webbeans.spi.ContainerLifecycle;
+import org.apache.webbeans.util.WebBeansUtil;
+import org.apache.webbeans.web.util.ServletCompatibilityUtil;
+
+/**
+ * As the ordering of servlet listener invocations is the same for all
+ * *Initialized events (e.g. contextInitialized, requestInitialized)
+ * and for all *Destroyed events (e.g. contextDestroyed, requestDestroyed)
+ * we need a different listener for start and end events.
+ *
+ * The {@link BeginWebBeansConfigurationListener} needs to be invoked as
+ * very first listener in the chain whereas the
+ * {@link EndWebBeansConfigurationListener} needs to be invoked as last
+ * in the chain.
+ *
+ * The {@link WebBeansConfigurationListener} exists for backward
compatibility
+ * reasons and simply delegates through to the 2 other listeners.
+ *
+ * Note: You only need the separate Begin and End listeners if your
environment
+ * supports injection into ServletListeners and Filters or if you use a
manual
+ * BeanManager lookup in any of these.
+ *
+ * @see EndWebBeansConfigurationListener
+ * @see WebBeansConfigurationListener
+ */
+public class BeginWebBeansConfigurationListener implements
ServletContextListener, ServletRequestListener, HttpSessionListener
+{
+
+    /**Logger instance*/
+    private static final Logger logger =
WebBeansLoggerFacade.getLogger(WebBeansConfigurationListener.class);
+
+    /**Manages the container lifecycle*/
+    protected ContainerLifecycle lifeCycle = null;
+
+    private WebBeansContext webBeansContext;
+
+    /**
+     * Default constructor
+     */
+    public BeginWebBeansConfigurationListener()
+    {
+        webBeansContext = WebBeansContext.getInstance();
+    }
+
+    /**
+     * Constructor for manual creation
+     */
+    public BeginWebBeansConfigurationListener(WebBeansContext
webBeansContext)
+    {
+        this.webBeansContext = webBeansContext;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    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);
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    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)
+        {
+            logger.log(Level.SEVERE,
+
WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0019, event == null
? "null" : event.getServletRequest()));
+            WebBeansUtil.throwRuntimeExceptions(e);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    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)
+        {
+            logger.log(Level.SEVERE,
+
WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0020,
event.getSession()));
+            WebBeansUtil.throwRuntimeExceptions(e);
+        }
+    }
+
+    @Override
+    public void contextDestroyed(ServletContextEvent sce)
+    {
+        // nothing to do, cleanup is done in
EndWebBeansConfigurationListener
+    }
+
+    @Override
+    public void sessionDestroyed(HttpSessionEvent se)
+    {
+        // nothing to do, cleanup is done in
EndWebBeansConfigurationListener
+    }
+
+    @Override
+    public void requestDestroyed(ServletRequestEvent sre)
+    {
+        // nothing to do, cleanup is done in
EndWebBeansConfigurationListener
+    }
+}

Added:
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java?rev=1676248&view=auto
==============================================================================
---
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
(added)
+++
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
Mon Apr 27 12:59:02 2015
@@ -0,0 +1,167 @@
+/*
+ * 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.servlet;
+
+import javax.enterprise.context.ConversationScoped;
+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;
+
+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.web.context.WebContextsService;
+
+/**
+ * This listener should be the last in the invocation chain.
+ *
+ * @see BeginWebBeansConfigurationListener
+ * @see WebBeansConfigurationListener
+ */
+public class EndWebBeansConfigurationListener implements
ServletContextListener, ServletRequestListener, HttpSessionListener
+{
+    /**Logger instance*/
+    private static final Logger logger =
WebBeansLoggerFacade.getLogger(WebBeansConfigurationListener.class);
+
+    private WebBeansContext webBeansContext;
+    private ContainerLifecycle lifeCycle;
+
+    /**
+     * Default constructor
+     */
+    public EndWebBeansConfigurationListener()
+    {
+        webBeansContext = WebBeansContext.getInstance();
+    }
+
+    /**
+     * Constructor for manual creation
+     */
+    public EndWebBeansConfigurationListener(WebBeansContext
webBeansContext)
+    {
+        this.webBeansContext = webBeansContext;
+    }
+
+    @Override
+    public void contextInitialized(ServletContextEvent sce)
+    {
+        // this must return the booted OWB container as the
BeginWebBeansConfigurationListener did already run
+        this.lifeCycle =
webBeansContext.getService(ContainerLifecycle.class);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void contextDestroyed(ServletContextEvent event)
+    {
+        lifeCycle.stopApplication(event);
+
+        // just to be sure that we didn't lazily create anything...
+        cleanupRequestThreadLocals();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    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)
+        {
+            elStore.destroyELContextStore();
+        }
+
+        this.lifeCycle.getContextService().endContext(RequestScoped.class,
event);
+
+        this.cleanupRequestThreadLocals();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    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);
+        }
+    }
+
+    @Override
+    public void sessionCreated(HttpSessionEvent se)
+    {
+        // nothing to do, init is done in
BeginWebBeansConfigurationListener
+    }
+
+    @Override
+    public void requestInitialized(ServletRequestEvent sre)
+    {
+        // nothing to do, init is done in
BeginWebBeansConfigurationListener
+    }
+
+    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()
+    {
+        WebContextsService.removeThreadLocals();
+    }
+
+}

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=1676248&r1=1676247&r2=1676248&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 Apr 27 12:59:02 2015
@@ -18,27 +18,14 @@
  */
 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.util.WebBeansUtil;
-import org.apache.webbeans.web.context.WebContextsService;
-import org.apache.webbeans.web.util.ServletCompatibilityUtil;
-
-import javax.enterprise.context.ConversationScoped;
-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
@@ -54,13 +41,9 @@ import java.util.logging.Logger;
  */
 public class WebBeansConfigurationListener implements
ServletContextListener, ServletRequestListener, HttpSessionListener
 {
-    /**Logger instance*/
-    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;

     /**
      * Default constructor
@@ -68,155 +51,49 @@ public class WebBeansConfigurationListen
     public WebBeansConfigurationListener()
     {
         webBeansContext = WebBeansContext.getInstance();
+        beginWebBeansConfigurationListener = new
BeginWebBeansConfigurationListener(webBeansContext);
+        endWebBeansConfigurationListener = new
EndWebBeansConfigurationListener(webBeansContext);
     }

-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    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);
-        }
-    }
-
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
-    public void contextDestroyed(ServletContextEvent event)
+    public void contextInitialized(ServletContextEvent sce)
     {
-        this.lifeCycle.stopApplication(event);
-        this.lifeCycle = null;
+        beginWebBeansConfigurationListener.contextInitialized(sce);

-        // just to be sure that we didn't lazily create anything...
-        cleanupRequestThreadLocals();
+        // for setting the lifecycle
+        endWebBeansConfigurationListener.contextInitialized(sce);
     }

-    /**
-     * {@inheritDoc}
-     */
     @Override
-    public void requestDestroyed(ServletRequestEvent event)
+    public void contextDestroyed(ServletContextEvent sce)
     {
-        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();
+        endWebBeansConfigurationListener.contextDestroyed(sce);
     }

-    /**
-     * Ensures that all ThreadLocals, which could have been set in this
-     * requests Thread, are removed in order to prevent memory leaks.
-     */
-    private void cleanupRequestThreadLocals()
+    @Override
+    public void sessionCreated(HttpSessionEvent se)
     {
-        WebContextsService.removeThreadLocals();
+        beginWebBeansConfigurationListener.sessionCreated(se);
     }

-    /**
-     * {@inheritDoc}
-     */
     @Override
-    public void requestInitialized(ServletRequestEvent event)
+    public void sessionDestroyed(HttpSessionEvent se)
     {
-        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);
-        }
+        endWebBeansConfigurationListener.sessionDestroyed(se);
     }

-    /**
-     * {@inheritDoc}
-     */
+
     @Override
-    public void sessionCreated(HttpSessionEvent event)
+    public void requestInitialized(ServletRequestEvent sre)
     {
-        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);
-        }
+        beginWebBeansConfigurationListener.requestInitialized(sre);
     }

-    /**
-     * {@inheritDoc}
-     */
     @Override
-    public void sessionDestroyed(HttpSessionEvent event)
+    public void requestDestroyed(ServletRequestEvent sre)
     {
-        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()
-    {
-        Context context =
this.lifeCycle.getContextService().getCurrentContext(RequestScoped.class);
-
-        if (context == null || !context.isActive())
-        {
-            requestInitialized(null);
-            return true;
-        }
-        return false;
+        endWebBeansConfigurationListener.requestDestroyed(sre);
     }

 }

Re: svn commit: r1676248 - in /openwebbeans/trunk/webbeans-web/src/main/java: META-INF/ org/apache/webbeans/servlet/

Posted by Romain Manni-Bucau <rm...@gmail.com>.
this means this will not work used in tomcat 8 as a normal servlet
container (and potentially jetty, didnt check if spec changed). Said
otherwise - and IIRC - in tomcat 8 begin listener in init phase will be end
listener in destroy phase.
Check org.apache.catalina.core.StandardContext#listenerStop:

for (int i = 0; i < listeners.length; i++) {
    int j = (listeners.length - 1) - i



Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2015-04-27 15:12 GMT+02:00 Mark Struberg <st...@yahoo.de>:

> This commit does not yet integrate the listeners to our tomcat plugins.
> It just allows the users to configure it separately (Begin+End) or as a
> single one as always (WebBeansConfigurationListener)
>
> But thanks for the tip!
>
> That means we might need to have an own tc8 module?
>
> LieGrue,
> strub
>
> > Am 27.04.2015 um 15:04 schrieb Romain Manni-Bucau <rmannibucau@gmail.com
> >:
> >
> > Hi Mark
> >
> > Did you test on tomcat 8? Blind guess - ie not tested - is it doesnt work
> > cause init/destroy order is logical
> > ---------- Message transféré ----------
> > De : <st...@apache.org>
> > Date : 27 avr. 2015 14:59
> > Objet : svn commit: r1676248 - in
> > /openwebbeans/trunk/webbeans-web/src/main/java: META-INF/
> > org/apache/webbeans/servlet/
> > À : <co...@openwebbeans.apache.org>
> > Cc :
> >
> > Author: struberg
> > Date: Mon Apr 27 12:59:02 2015
> > New Revision: 1676248
> >
> > URL: http://svn.apache.org/r1676248
> > Log:
> > OWB-1055 Split WebBeansConfigurationListener in Begin and End listeners
> >
> > We need this as the order is the same for all init and destroy methods.
> > But CDI should get init as first listener, but destroyed as last.
> >
> > Added:
> >
> >
> 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
> > Removed:
> >    openwebbeans/trunk/webbeans-web/src/main/java/META-INF/
> > Modified:
> >
> >
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
> >
> > Added:
> >
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
> > URL:
> >
> http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java?rev=1676248&view=auto
> >
> ==============================================================================
> > ---
> >
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
> > (added)
> > +++
> >
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
> > Mon Apr 27 12:59:02 2015
> > @@ -0,0 +1,175 @@
> > +/*
> > + * 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.servlet;
> > +
> > +import javax.enterprise.context.RequestScoped;
> > +import javax.enterprise.context.SessionScoped;
> > +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;
> > +
> > +import org.apache.webbeans.config.OWBLogConst;
> > +import org.apache.webbeans.config.WebBeansContext;
> > +import org.apache.webbeans.logger.WebBeansLoggerFacade;
> > +import org.apache.webbeans.spi.ContainerLifecycle;
> > +import org.apache.webbeans.util.WebBeansUtil;
> > +import org.apache.webbeans.web.util.ServletCompatibilityUtil;
> > +
> > +/**
> > + * As the ordering of servlet listener invocations is the same for all
> > + * *Initialized events (e.g. contextInitialized, requestInitialized)
> > + * and for all *Destroyed events (e.g. contextDestroyed,
> requestDestroyed)
> > + * we need a different listener for start and end events.
> > + *
> > + * The {@link BeginWebBeansConfigurationListener} needs to be invoked as
> > + * very first listener in the chain whereas the
> > + * {@link EndWebBeansConfigurationListener} needs to be invoked as last
> > + * in the chain.
> > + *
> > + * The {@link WebBeansConfigurationListener} exists for backward
> > compatibility
> > + * reasons and simply delegates through to the 2 other listeners.
> > + *
> > + * Note: You only need the separate Begin and End listeners if your
> > environment
> > + * supports injection into ServletListeners and Filters or if you use a
> > manual
> > + * BeanManager lookup in any of these.
> > + *
> > + * @see EndWebBeansConfigurationListener
> > + * @see WebBeansConfigurationListener
> > + */
> > +public class BeginWebBeansConfigurationListener implements
> > ServletContextListener, ServletRequestListener, HttpSessionListener
> > +{
> > +
> > +    /**Logger instance*/
> > +    private static final Logger logger =
> > WebBeansLoggerFacade.getLogger(WebBeansConfigurationListener.class);
> > +
> > +    /**Manages the container lifecycle*/
> > +    protected ContainerLifecycle lifeCycle = null;
> > +
> > +    private WebBeansContext webBeansContext;
> > +
> > +    /**
> > +     * Default constructor
> > +     */
> > +    public BeginWebBeansConfigurationListener()
> > +    {
> > +        webBeansContext = WebBeansContext.getInstance();
> > +    }
> > +
> > +    /**
> > +     * Constructor for manual creation
> > +     */
> > +    public BeginWebBeansConfigurationListener(WebBeansContext
> > webBeansContext)
> > +    {
> > +        this.webBeansContext = webBeansContext;
> > +    }
> > +
> > +    /**
> > +     * {@inheritDoc}
> > +     */
> > +    @Override
> > +    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);
> > +        }
> > +    }
> > +
> > +
> > +    /**
> > +     * {@inheritDoc}
> > +     */
> > +    @Override
> > +    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)
> > +        {
> > +            logger.log(Level.SEVERE,
> > +
> > WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0019, event ==
> null
> > ? "null" : event.getServletRequest()));
> > +            WebBeansUtil.throwRuntimeExceptions(e);
> > +        }
> > +    }
> > +
> > +    /**
> > +     * {@inheritDoc}
> > +     */
> > +    @Override
> > +    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)
> > +        {
> > +            logger.log(Level.SEVERE,
> > +
> > WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0020,
> > event.getSession()));
> > +            WebBeansUtil.throwRuntimeExceptions(e);
> > +        }
> > +    }
> > +
> > +    @Override
> > +    public void contextDestroyed(ServletContextEvent sce)
> > +    {
> > +        // nothing to do, cleanup is done in
> > EndWebBeansConfigurationListener
> > +    }
> > +
> > +    @Override
> > +    public void sessionDestroyed(HttpSessionEvent se)
> > +    {
> > +        // nothing to do, cleanup is done in
> > EndWebBeansConfigurationListener
> > +    }
> > +
> > +    @Override
> > +    public void requestDestroyed(ServletRequestEvent sre)
> > +    {
> > +        // nothing to do, cleanup is done in
> > EndWebBeansConfigurationListener
> > +    }
> > +}
> >
> > Added:
> >
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
> > URL:
> >
> http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java?rev=1676248&view=auto
> >
> ==============================================================================
> > ---
> >
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
> > (added)
> > +++
> >
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
> > Mon Apr 27 12:59:02 2015
> > @@ -0,0 +1,167 @@
> > +/*
> > + * 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.servlet;
> > +
> > +import javax.enterprise.context.ConversationScoped;
> > +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;
> > +
> > +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.web.context.WebContextsService;
> > +
> > +/**
> > + * This listener should be the last in the invocation chain.
> > + *
> > + * @see BeginWebBeansConfigurationListener
> > + * @see WebBeansConfigurationListener
> > + */
> > +public class EndWebBeansConfigurationListener implements
> > ServletContextListener, ServletRequestListener, HttpSessionListener
> > +{
> > +    /**Logger instance*/
> > +    private static final Logger logger =
> > WebBeansLoggerFacade.getLogger(WebBeansConfigurationListener.class);
> > +
> > +    private WebBeansContext webBeansContext;
> > +    private ContainerLifecycle lifeCycle;
> > +
> > +    /**
> > +     * Default constructor
> > +     */
> > +    public EndWebBeansConfigurationListener()
> > +    {
> > +        webBeansContext = WebBeansContext.getInstance();
> > +    }
> > +
> > +    /**
> > +     * Constructor for manual creation
> > +     */
> > +    public EndWebBeansConfigurationListener(WebBeansContext
> > webBeansContext)
> > +    {
> > +        this.webBeansContext = webBeansContext;
> > +    }
> > +
> > +    @Override
> > +    public void contextInitialized(ServletContextEvent sce)
> > +    {
> > +        // this must return the booted OWB container as the
> > BeginWebBeansConfigurationListener did already run
> > +        this.lifeCycle =
> > webBeansContext.getService(ContainerLifecycle.class);
> > +    }
> > +
> > +    /**
> > +     * {@inheritDoc}
> > +     */
> > +    @Override
> > +    public void contextDestroyed(ServletContextEvent event)
> > +    {
> > +        lifeCycle.stopApplication(event);
> > +
> > +        // just to be sure that we didn't lazily create anything...
> > +        cleanupRequestThreadLocals();
> > +    }
> > +
> > +    /**
> > +     * {@inheritDoc}
> > +     */
> > +    @Override
> > +    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)
> > +        {
> > +            elStore.destroyELContextStore();
> > +        }
> > +
> > +
> this.lifeCycle.getContextService().endContext(RequestScoped.class,
> > event);
> > +
> > +        this.cleanupRequestThreadLocals();
> > +    }
> > +
> > +
> > +    /**
> > +     * {@inheritDoc}
> > +     */
> > +    @Override
> > +    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);
> > +        }
> > +    }
> > +
> > +    @Override
> > +    public void sessionCreated(HttpSessionEvent se)
> > +    {
> > +        // nothing to do, init is done in
> > BeginWebBeansConfigurationListener
> > +    }
> > +
> > +    @Override
> > +    public void requestInitialized(ServletRequestEvent sre)
> > +    {
> > +        // nothing to do, init is done in
> > BeginWebBeansConfigurationListener
> > +    }
> > +
> > +    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()
> > +    {
> > +        WebContextsService.removeThreadLocals();
> > +    }
> > +
> > +}
> >
> > 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=1676248&r1=1676247&r2=1676248&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 Apr 27 12:59:02 2015
> > @@ -18,27 +18,14 @@
> >  */
> > 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.util.WebBeansUtil;
> > -import org.apache.webbeans.web.context.WebContextsService;
> > -import org.apache.webbeans.web.util.ServletCompatibilityUtil;
> > -
> > -import javax.enterprise.context.ConversationScoped;
> > -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
> > @@ -54,13 +41,9 @@ import java.util.logging.Logger;
> >  */
> > public class WebBeansConfigurationListener implements
> > ServletContextListener, ServletRequestListener, HttpSessionListener
> > {
> > -    /**Logger instance*/
> > -    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;
> >
> >     /**
> >      * Default constructor
> > @@ -68,155 +51,49 @@ public class WebBeansConfigurationListen
> >     public WebBeansConfigurationListener()
> >     {
> >         webBeansContext = WebBeansContext.getInstance();
> > +        beginWebBeansConfigurationListener = new
> > BeginWebBeansConfigurationListener(webBeansContext);
> > +        endWebBeansConfigurationListener = new
> > EndWebBeansConfigurationListener(webBeansContext);
> >     }
> >
> > -    /**
> > -     * {@inheritDoc}
> > -     */
> > -    @Override
> > -    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);
> > -        }
> > -    }
> > -
> > -
> > -    /**
> > -     * {@inheritDoc}
> > -     */
> >     @Override
> > -    public void contextDestroyed(ServletContextEvent event)
> > +    public void contextInitialized(ServletContextEvent sce)
> >     {
> > -        this.lifeCycle.stopApplication(event);
> > -        this.lifeCycle = null;
> > +        beginWebBeansConfigurationListener.contextInitialized(sce);
> >
> > -        // just to be sure that we didn't lazily create anything...
> > -        cleanupRequestThreadLocals();
> > +        // for setting the lifecycle
> > +        endWebBeansConfigurationListener.contextInitialized(sce);
> >     }
> >
> > -    /**
> > -     * {@inheritDoc}
> > -     */
> >     @Override
> > -    public void requestDestroyed(ServletRequestEvent event)
> > +    public void contextDestroyed(ServletContextEvent sce)
> >     {
> > -        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();
> > +        endWebBeansConfigurationListener.contextDestroyed(sce);
> >     }
> >
> > -    /**
> > -     * Ensures that all ThreadLocals, which could have been set in this
> > -     * requests Thread, are removed in order to prevent memory leaks.
> > -     */
> > -    private void cleanupRequestThreadLocals()
> > +    @Override
> > +    public void sessionCreated(HttpSessionEvent se)
> >     {
> > -        WebContextsService.removeThreadLocals();
> > +        beginWebBeansConfigurationListener.sessionCreated(se);
> >     }
> >
> > -    /**
> > -     * {@inheritDoc}
> > -     */
> >     @Override
> > -    public void requestInitialized(ServletRequestEvent event)
> > +    public void sessionDestroyed(HttpSessionEvent se)
> >     {
> > -        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);
> > -        }
> > +        endWebBeansConfigurationListener.sessionDestroyed(se);
> >     }
> >
> > -    /**
> > -     * {@inheritDoc}
> > -     */
> > +
> >     @Override
> > -    public void sessionCreated(HttpSessionEvent event)
> > +    public void requestInitialized(ServletRequestEvent sre)
> >     {
> > -        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);
> > -        }
> > +        beginWebBeansConfigurationListener.requestInitialized(sre);
> >     }
> >
> > -    /**
> > -     * {@inheritDoc}
> > -     */
> >     @Override
> > -    public void sessionDestroyed(HttpSessionEvent event)
> > +    public void requestDestroyed(ServletRequestEvent sre)
> >     {
> > -        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()
> > -    {
> > -        Context context =
> >
> this.lifeCycle.getContextService().getCurrentContext(RequestScoped.class);
> > -
> > -        if (context == null || !context.isActive())
> > -        {
> > -            requestInitialized(null);
> > -            return true;
> > -        }
> > -        return false;
> > +        endWebBeansConfigurationListener.requestDestroyed(sre);
> >     }
> >
> > }
>
>

Re: svn commit: r1676248 - in /openwebbeans/trunk/webbeans-web/src/main/java: META-INF/ org/apache/webbeans/servlet/

Posted by Mark Struberg <st...@yahoo.de>.
This commit does not yet integrate the listeners to our tomcat plugins.
It just allows the users to configure it separately (Begin+End) or as a single one as always (WebBeansConfigurationListener)

But thanks for the tip!

That means we might need to have an own tc8 module?

LieGrue,
strub

> Am 27.04.2015 um 15:04 schrieb Romain Manni-Bucau <rm...@gmail.com>:
> 
> Hi Mark
> 
> Did you test on tomcat 8? Blind guess - ie not tested - is it doesnt work
> cause init/destroy order is logical
> ---------- Message transféré ----------
> De : <st...@apache.org>
> Date : 27 avr. 2015 14:59
> Objet : svn commit: r1676248 - in
> /openwebbeans/trunk/webbeans-web/src/main/java: META-INF/
> org/apache/webbeans/servlet/
> À : <co...@openwebbeans.apache.org>
> Cc :
> 
> Author: struberg
> Date: Mon Apr 27 12:59:02 2015
> New Revision: 1676248
> 
> URL: http://svn.apache.org/r1676248
> Log:
> OWB-1055 Split WebBeansConfigurationListener in Begin and End listeners
> 
> We need this as the order is the same for all init and destroy methods.
> But CDI should get init as first listener, but destroyed as last.
> 
> Added:
> 
> 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
> Removed:
>    openwebbeans/trunk/webbeans-web/src/main/java/META-INF/
> Modified:
> 
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
> 
> Added:
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
> URL:
> http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java?rev=1676248&view=auto
> ==============================================================================
> ---
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
> (added)
> +++
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/BeginWebBeansConfigurationListener.java
> Mon Apr 27 12:59:02 2015
> @@ -0,0 +1,175 @@
> +/*
> + * 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.servlet;
> +
> +import javax.enterprise.context.RequestScoped;
> +import javax.enterprise.context.SessionScoped;
> +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;
> +
> +import org.apache.webbeans.config.OWBLogConst;
> +import org.apache.webbeans.config.WebBeansContext;
> +import org.apache.webbeans.logger.WebBeansLoggerFacade;
> +import org.apache.webbeans.spi.ContainerLifecycle;
> +import org.apache.webbeans.util.WebBeansUtil;
> +import org.apache.webbeans.web.util.ServletCompatibilityUtil;
> +
> +/**
> + * As the ordering of servlet listener invocations is the same for all
> + * *Initialized events (e.g. contextInitialized, requestInitialized)
> + * and for all *Destroyed events (e.g. contextDestroyed, requestDestroyed)
> + * we need a different listener for start and end events.
> + *
> + * The {@link BeginWebBeansConfigurationListener} needs to be invoked as
> + * very first listener in the chain whereas the
> + * {@link EndWebBeansConfigurationListener} needs to be invoked as last
> + * in the chain.
> + *
> + * The {@link WebBeansConfigurationListener} exists for backward
> compatibility
> + * reasons and simply delegates through to the 2 other listeners.
> + *
> + * Note: You only need the separate Begin and End listeners if your
> environment
> + * supports injection into ServletListeners and Filters or if you use a
> manual
> + * BeanManager lookup in any of these.
> + *
> + * @see EndWebBeansConfigurationListener
> + * @see WebBeansConfigurationListener
> + */
> +public class BeginWebBeansConfigurationListener implements
> ServletContextListener, ServletRequestListener, HttpSessionListener
> +{
> +
> +    /**Logger instance*/
> +    private static final Logger logger =
> WebBeansLoggerFacade.getLogger(WebBeansConfigurationListener.class);
> +
> +    /**Manages the container lifecycle*/
> +    protected ContainerLifecycle lifeCycle = null;
> +
> +    private WebBeansContext webBeansContext;
> +
> +    /**
> +     * Default constructor
> +     */
> +    public BeginWebBeansConfigurationListener()
> +    {
> +        webBeansContext = WebBeansContext.getInstance();
> +    }
> +
> +    /**
> +     * Constructor for manual creation
> +     */
> +    public BeginWebBeansConfigurationListener(WebBeansContext
> webBeansContext)
> +    {
> +        this.webBeansContext = webBeansContext;
> +    }
> +
> +    /**
> +     * {@inheritDoc}
> +     */
> +    @Override
> +    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);
> +        }
> +    }
> +
> +
> +    /**
> +     * {@inheritDoc}
> +     */
> +    @Override
> +    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)
> +        {
> +            logger.log(Level.SEVERE,
> +
> WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0019, event == null
> ? "null" : event.getServletRequest()));
> +            WebBeansUtil.throwRuntimeExceptions(e);
> +        }
> +    }
> +
> +    /**
> +     * {@inheritDoc}
> +     */
> +    @Override
> +    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)
> +        {
> +            logger.log(Level.SEVERE,
> +
> WebBeansLoggerFacade.constructMessage(OWBLogConst.ERROR_0020,
> event.getSession()));
> +            WebBeansUtil.throwRuntimeExceptions(e);
> +        }
> +    }
> +
> +    @Override
> +    public void contextDestroyed(ServletContextEvent sce)
> +    {
> +        // nothing to do, cleanup is done in
> EndWebBeansConfigurationListener
> +    }
> +
> +    @Override
> +    public void sessionDestroyed(HttpSessionEvent se)
> +    {
> +        // nothing to do, cleanup is done in
> EndWebBeansConfigurationListener
> +    }
> +
> +    @Override
> +    public void requestDestroyed(ServletRequestEvent sre)
> +    {
> +        // nothing to do, cleanup is done in
> EndWebBeansConfigurationListener
> +    }
> +}
> 
> Added:
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
> URL:
> http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java?rev=1676248&view=auto
> ==============================================================================
> ---
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
> (added)
> +++
> openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/EndWebBeansConfigurationListener.java
> Mon Apr 27 12:59:02 2015
> @@ -0,0 +1,167 @@
> +/*
> + * 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.servlet;
> +
> +import javax.enterprise.context.ConversationScoped;
> +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;
> +
> +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.web.context.WebContextsService;
> +
> +/**
> + * This listener should be the last in the invocation chain.
> + *
> + * @see BeginWebBeansConfigurationListener
> + * @see WebBeansConfigurationListener
> + */
> +public class EndWebBeansConfigurationListener implements
> ServletContextListener, ServletRequestListener, HttpSessionListener
> +{
> +    /**Logger instance*/
> +    private static final Logger logger =
> WebBeansLoggerFacade.getLogger(WebBeansConfigurationListener.class);
> +
> +    private WebBeansContext webBeansContext;
> +    private ContainerLifecycle lifeCycle;
> +
> +    /**
> +     * Default constructor
> +     */
> +    public EndWebBeansConfigurationListener()
> +    {
> +        webBeansContext = WebBeansContext.getInstance();
> +    }
> +
> +    /**
> +     * Constructor for manual creation
> +     */
> +    public EndWebBeansConfigurationListener(WebBeansContext
> webBeansContext)
> +    {
> +        this.webBeansContext = webBeansContext;
> +    }
> +
> +    @Override
> +    public void contextInitialized(ServletContextEvent sce)
> +    {
> +        // this must return the booted OWB container as the
> BeginWebBeansConfigurationListener did already run
> +        this.lifeCycle =
> webBeansContext.getService(ContainerLifecycle.class);
> +    }
> +
> +    /**
> +     * {@inheritDoc}
> +     */
> +    @Override
> +    public void contextDestroyed(ServletContextEvent event)
> +    {
> +        lifeCycle.stopApplication(event);
> +
> +        // just to be sure that we didn't lazily create anything...
> +        cleanupRequestThreadLocals();
> +    }
> +
> +    /**
> +     * {@inheritDoc}
> +     */
> +    @Override
> +    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)
> +        {
> +            elStore.destroyELContextStore();
> +        }
> +
> +        this.lifeCycle.getContextService().endContext(RequestScoped.class,
> event);
> +
> +        this.cleanupRequestThreadLocals();
> +    }
> +
> +
> +    /**
> +     * {@inheritDoc}
> +     */
> +    @Override
> +    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);
> +        }
> +    }
> +
> +    @Override
> +    public void sessionCreated(HttpSessionEvent se)
> +    {
> +        // nothing to do, init is done in
> BeginWebBeansConfigurationListener
> +    }
> +
> +    @Override
> +    public void requestInitialized(ServletRequestEvent sre)
> +    {
> +        // nothing to do, init is done in
> BeginWebBeansConfigurationListener
> +    }
> +
> +    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()
> +    {
> +        WebContextsService.removeThreadLocals();
> +    }
> +
> +}
> 
> 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=1676248&r1=1676247&r2=1676248&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 Apr 27 12:59:02 2015
> @@ -18,27 +18,14 @@
>  */
> 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.util.WebBeansUtil;
> -import org.apache.webbeans.web.context.WebContextsService;
> -import org.apache.webbeans.web.util.ServletCompatibilityUtil;
> -
> -import javax.enterprise.context.ConversationScoped;
> -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
> @@ -54,13 +41,9 @@ import java.util.logging.Logger;
>  */
> public class WebBeansConfigurationListener implements
> ServletContextListener, ServletRequestListener, HttpSessionListener
> {
> -    /**Logger instance*/
> -    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;
> 
>     /**
>      * Default constructor
> @@ -68,155 +51,49 @@ public class WebBeansConfigurationListen
>     public WebBeansConfigurationListener()
>     {
>         webBeansContext = WebBeansContext.getInstance();
> +        beginWebBeansConfigurationListener = new
> BeginWebBeansConfigurationListener(webBeansContext);
> +        endWebBeansConfigurationListener = new
> EndWebBeansConfigurationListener(webBeansContext);
>     }
> 
> -    /**
> -     * {@inheritDoc}
> -     */
> -    @Override
> -    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);
> -        }
> -    }
> -
> -
> -    /**
> -     * {@inheritDoc}
> -     */
>     @Override
> -    public void contextDestroyed(ServletContextEvent event)
> +    public void contextInitialized(ServletContextEvent sce)
>     {
> -        this.lifeCycle.stopApplication(event);
> -        this.lifeCycle = null;
> +        beginWebBeansConfigurationListener.contextInitialized(sce);
> 
> -        // just to be sure that we didn't lazily create anything...
> -        cleanupRequestThreadLocals();
> +        // for setting the lifecycle
> +        endWebBeansConfigurationListener.contextInitialized(sce);
>     }
> 
> -    /**
> -     * {@inheritDoc}
> -     */
>     @Override
> -    public void requestDestroyed(ServletRequestEvent event)
> +    public void contextDestroyed(ServletContextEvent sce)
>     {
> -        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();
> +        endWebBeansConfigurationListener.contextDestroyed(sce);
>     }
> 
> -    /**
> -     * Ensures that all ThreadLocals, which could have been set in this
> -     * requests Thread, are removed in order to prevent memory leaks.
> -     */
> -    private void cleanupRequestThreadLocals()
> +    @Override
> +    public void sessionCreated(HttpSessionEvent se)
>     {
> -        WebContextsService.removeThreadLocals();
> +        beginWebBeansConfigurationListener.sessionCreated(se);
>     }
> 
> -    /**
> -     * {@inheritDoc}
> -     */
>     @Override
> -    public void requestInitialized(ServletRequestEvent event)
> +    public void sessionDestroyed(HttpSessionEvent se)
>     {
> -        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);
> -        }
> +        endWebBeansConfigurationListener.sessionDestroyed(se);
>     }
> 
> -    /**
> -     * {@inheritDoc}
> -     */
> +
>     @Override
> -    public void sessionCreated(HttpSessionEvent event)
> +    public void requestInitialized(ServletRequestEvent sre)
>     {
> -        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);
> -        }
> +        beginWebBeansConfigurationListener.requestInitialized(sre);
>     }
> 
> -    /**
> -     * {@inheritDoc}
> -     */
>     @Override
> -    public void sessionDestroyed(HttpSessionEvent event)
> +    public void requestDestroyed(ServletRequestEvent sre)
>     {
> -        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()
> -    {
> -        Context context =
> this.lifeCycle.getContextService().getCurrentContext(RequestScoped.class);
> -
> -        if (context == null || !context.isActive())
> -        {
> -            requestInitialized(null);
> -            return true;
> -        }
> -        return false;
> +        endWebBeansConfigurationListener.requestDestroyed(sre);
>     }
> 
> }