Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49952
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 6 Oct 2010 21:46:43 +0000 (21:46 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 6 Oct 2010 21:46:43 +0000 (21:46 +0000)
Allow ServletContainerInitializers to add listeners to a web application.
Patch provided by David Jencks.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1005265 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/core/LocalStrings.properties
java/org/apache/catalina/core/StandardContext.java
test/org/apache/catalina/startup/TestListener.java [new file with mode: 0644]
webapps/docs/changelog.xml

index 45b59ce..dd447e0 100644 (file)
@@ -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
index b4db3be..46248b2 100644 (file)
@@ -4289,7 +4289,14 @@ public class StandardContext extends ContainerBase
             }
         }
 
+        //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
diff --git a/test/org/apache/catalina/startup/TestListener.java b/test/org/apache/catalina/startup/TestListener.java
new file mode 100644 (file)
index 0000000..bf44cd1
--- /dev/null
@@ -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
+        }
+    }
+}
index ce0d50b..8dccec6 100644 (file)
         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">