Return-Path: X-Original-To: apmail-openwebbeans-commits-archive@www.apache.org Delivered-To: apmail-openwebbeans-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2D63010B82 for ; Mon, 27 Apr 2015 12:59:03 +0000 (UTC) Received: (qmail 51634 invoked by uid 500); 27 Apr 2015 12:59:03 -0000 Delivered-To: apmail-openwebbeans-commits-archive@openwebbeans.apache.org Received: (qmail 51614 invoked by uid 500); 27 Apr 2015 12:59:03 -0000 Mailing-List: contact commits-help@openwebbeans.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openwebbeans.apache.org Delivered-To: mailing list commits@openwebbeans.apache.org Received: (qmail 51601 invoked by uid 99); 27 Apr 2015 12:59:03 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Apr 2015 12:59:03 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id CCE94AC0179 for ; Mon, 27 Apr 2015 12:59:02 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1676248 - in /openwebbeans/trunk/webbeans-web/src/main/java: META-INF/ org/apache/webbeans/servlet/ Date: Mon, 27 Apr 2015 12:59:02 -0000 To: commits@openwebbeans.apache.org From: struberg@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20150427125902.CCE94AC0179@hades.apache.org> 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); } }