Author: markt Date: Wed Oct 6 21:46:43 2010 New Revision: 1005265 URL: http://svn.apache.org/viewvc?rev=1005265&view=rev Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49952 Allow ServletContainerInitializers to add listeners to a web application. Patch provided by David Jencks.
Added: tomcat/trunk/test/org/apache/catalina/startup/TestListener.java (with props) Modified: tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/core/StandardContext.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties?rev=1005265&r1=1005264&r2=1005265&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties Wed Oct 6 21:46:43 2010 @@ -16,6 +16,7 @@ applicationContext.addFilter.ise=Filters can not be added to context {0} as the context has been initialised applicationContext.addListener.iae.cnfe=Unable to create an instance of type [{0}] applicationContext.addListener.iae.wrongType=The type specified [{0}] is not one of the expected listener types +applicationContext.addListener.iae.sclNotAllowed=Once the first ServletContextListener has been called, no more ServletContextListeners may be added. applicationContext.addListener.ise=Listeners can not be added to context {0} as the context has been initialised applicationContext.addRole.ise=Roles can not be added to context {0} as the context has been initialised applicationContext.addServlet.ise=Servlets can not be added to context {0} as the context has been initialised Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1005265&r1=1005264&r2=1005265&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Wed Oct 6 21:46:43 2010 @@ -4289,7 +4289,14 @@ public class StandardContext extends Con } } + //Listeners may have been added by ServletContextInitializers. Put them after the ones we know about. + for (Object eventListener: getApplicationEventListeners()) { + eventListeners.add(eventListener); + } setApplicationEventListeners(eventListeners.toArray()); + for (Object lifecycleListener: getApplicationLifecycleListeners()) { + lifecycleListeners.add(lifecycleListener); + } setApplicationLifecycleListeners(lifecycleListeners.toArray()); // Send application start events Added: tomcat/trunk/test/org/apache/catalina/startup/TestListener.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TestListener.java?rev=1005265&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/TestListener.java (added) +++ tomcat/trunk/test/org/apache/catalina/startup/TestListener.java Wed Oct 6 21:46:43 2010 @@ -0,0 +1,123 @@ +/* + * 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.catalina.startup; + +import java.util.Set; + +import javax.servlet.ServletContainerInitializer; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.ServletException; + +import org.apache.catalina.Context; + +public class TestListener extends TomcatBaseTest { + + /** + * Check that a ServletContainerInitializer can install a + * {...@link ServletContextListener} and that it gets initialized. + * @throws Exception + */ + public void testServletContainerInitializer() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + Context context = tomcat.addContext("/", + System.getProperty("java.io.tmpdir")); + + context.addServletContainerInitializer(new SCI(), null); + tomcat.start(); + assertTrue(SCL.initialized); + } + + /** + * Check that a {...@link ServletContextListener} cannot install a + * {...@link ServletContextInitializer}. + * @throws Exception + */ + public void testServletContextListener() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + Context context = tomcat.addContext("/", + System.getProperty("java.io.tmpdir")); + + // SCL2 pretends to be in web.xml, and tries to install a + // ServletContextInitializer. + context.addApplicationListener(SCL2.class.getName()); + tomcat.start(); + + //check that the ServletContextInitializer wasn't initialized. + assertFalse(SCL3.initialized); + } + + public static class SCI implements ServletContainerInitializer { + + @Override + public void onStartup(Set<Class<?>> c, ServletContext ctx) + throws ServletException { + ctx.addListener(new SCL()); + } + } + + public static class SCL implements ServletContextListener { + + static boolean initialized = false; + + @Override + public void contextInitialized(ServletContextEvent sce) { + initialized = true; + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // NOOP + } + } + + public static class SCL2 implements ServletContextListener { + + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext sc = sce.getServletContext(); + sc.addListener(SCL3.class.getName()); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // NOOP + } + } + + public static class SCL3 implements ServletContextListener { + + static boolean initialized = false; + + @Override + public void contextInitialized(ServletContextEvent sce) { + initialized = true; + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // NOOP + } + } +} Propchange: tomcat/trunk/test/org/apache/catalina/startup/TestListener.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1005265&r1=1005264&r2=1005265&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Wed Oct 6 21:46:43 2010 @@ -62,6 +62,10 @@ attributes that were showing as Unavailable in JConsole. Patch provided by Chamith Buddhika. (markt) </fix> + <fix> + <bug>49952</bug>: Allow ServletContainerInitializers to add listeners to + a web application. Patch provided by David Jencks. (markt) + </fix> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org