Fix remainder of https://issues.apache.org/bugzilla/show_bug.cgi?id=49181
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 12 May 2010 10:24:00 +0000 (10:24 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 12 May 2010 10:24:00 +0000 (10:24 +0000)
Add additional check to prevent adding Servlet context listeners once the calls to the Servlet Context listeners start

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

java/org/apache/catalina/core/ApplicationContext.java
java/org/apache/catalina/core/StandardContext.java

index 47b18b1..d901b1a 100644 (file)
@@ -184,6 +184,13 @@ public class ApplicationContext
     private Set<SessionTrackingMode> defaultSessionTrackingModes = null;
     private Set<SessionTrackingMode> supportedSessionTrackingModes = null;
 
+    /**
+     * Flag that indicates if a new {@link ServletContextListener} may be added
+     * to the application. Once the first {@link ServletContextListener} is
+     * called, not more may be added.
+     */
+    private boolean newServletContextListenerAllowed = true;
+
     // --------------------------------------------------------- Public Methods
 
 
@@ -1275,8 +1282,8 @@ public class ApplicationContext
         }
         
         if (t instanceof HttpSessionListener
-                || t instanceof ServletContextListener) {
-            // TODO SERVLET3 - if ServletContextListener then also need to check caller? spec isn't clear
+                || (t instanceof ServletContextListener &&
+                        newServletContextListenerAllowed)) {
             context.addApplicationLifecycleListener(t);
             match = true;
         }
@@ -1471,6 +1478,10 @@ public class ApplicationContext
     }
 
 
+    protected void setNewServletContextListenerAllowed(boolean allowed) {
+        this.newServletContextListenerAllowed = allowed;
+    }
+    
     // -------------------------------------------------------- Private Methods
 
 
index 9a0a6e2..3869e05 100644 (file)
@@ -4266,6 +4266,10 @@ public class StandardContext extends ContainerBase
         if (getLogger().isDebugEnabled())
             getLogger().debug("Sending application start events");
 
+        // Ensure context is not null
+        getServletContext();
+        context.setNewServletContextListenerAllowed(false);
+        
         Object instances[] = getApplicationLifecycleListeners();
         if (instances == null)
             return (ok);