Author: markt Date: Mon Jan 19 09:08:08 2015 New Revision: 1652938 URL: http://svn.apache.org/r1652938 Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=57446 Ensure that the ServletContext presented to a listener for contextInitialized() is the same as the one presented when contextDestroyed() is called for the same listener.
Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java tomcat/trunk/test/org/apache/catalina/core/TesterTldListener.java 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=1652938&r1=1652937&r2=1652938&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Mon Jan 19 09:08:08 2015 @@ -210,6 +210,12 @@ public class StandardContext extends Con private final Object applicationListenersLock = new Object(); + /** + * The set of application listeners that are required to have limited access + * to ServletContext methods. See Servlet 3.1 section 4.4. + */ + + private final Set<Object> noPluggabilityListeners = new HashSet<>(); /** * The set of instantiated application event listener objects. Note that @@ -289,6 +295,13 @@ public class StandardContext extends Con */ protected ApplicationContext context = null; + /** + * The wrapped version of the associated ServletContext that is presented + * to listeners that are required to have limited access to ServletContext + * methods. See Servlet 3.1 section 4.4. + */ + private NoPluggabilityServletContext noPluggabilityServletContext = null; + /** * Should we attempt to use cookies for session id communication? @@ -4618,7 +4631,6 @@ public class StandardContext extends Con String listeners[] = findApplicationListeners(); Object results[] = new Object[listeners.length]; boolean ok = true; - Set<Object> noPluggabilityListeners = new HashSet<>(); for (int i = 0; i < results.length; i++) { if (getLogger().isDebugEnabled()) getLogger().debug(" Configuring event listener class '" + @@ -4687,12 +4699,11 @@ public class StandardContext extends Con return ok; } - ServletContextEvent event = - new ServletContextEvent(getServletContext()); + ServletContextEvent event = new ServletContextEvent(getServletContext()); ServletContextEvent tldEvent = null; if (noPluggabilityListeners.size() > 0) { - tldEvent = new ServletContextEvent(new NoPluggabilityServletContext( - getServletContext())); + noPluggabilityServletContext = new NoPluggabilityServletContext(getServletContext()); + tldEvent = new ServletContextEvent(noPluggabilityServletContext); } for (int i = 0; i < instances.length; i++) { if (!(instances[i] instanceof ServletContextListener)) @@ -4734,8 +4745,11 @@ public class StandardContext extends Con boolean ok = true; Object listeners[] = getApplicationLifecycleListeners(); if (listeners != null && listeners.length > 0) { - ServletContextEvent event = - new ServletContextEvent(getServletContext()); + ServletContextEvent event = new ServletContextEvent(getServletContext()); + ServletContextEvent tldEvent = null; + if (noPluggabilityServletContext != null) { + tldEvent = new ServletContextEvent(noPluggabilityServletContext); + } for (int i = 0; i < listeners.length; i++) { int j = (listeners.length - 1) - i; if (listeners[j] == null) @@ -4745,7 +4759,11 @@ public class StandardContext extends Con (ServletContextListener) listeners[j]; try { fireContainerEvent("beforeContextDestroyed", listener); - listener.contextDestroyed(event); + if (noPluggabilityListeners.contains(listener)) { + listener.contextDestroyed(tldEvent); + } else { + listener.contextDestroyed(event); + } fireContainerEvent("afterContextDestroyed", listener); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); @@ -4792,8 +4810,10 @@ public class StandardContext extends Con setApplicationEventListeners(null); setApplicationLifecycleListeners(null); - return (ok); + noPluggabilityServletContext = null; + noPluggabilityListeners.clear(); + return ok; } Modified: tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java?rev=1652938&r1=1652937&r2=1652938&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java (original) +++ tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java Mon Jan 19 09:08:08 2015 @@ -871,7 +871,8 @@ public class TestStandardContext extends ctx.stop(); String log = TesterTldListener.getLog(); - Assert.assertTrue(log, log.contains("PASS")); + Assert.assertTrue(log, log.contains("PASS-01")); + Assert.assertTrue(log, log.contains("PASS-02")); Assert.assertFalse(log, log.contains("FAIL")); } Modified: tomcat/trunk/test/org/apache/catalina/core/TesterTldListener.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TesterTldListener.java?rev=1652938&r1=1652937&r2=1652938&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/core/TesterTldListener.java (original) +++ tomcat/trunk/test/org/apache/catalina/core/TesterTldListener.java Mon Jan 19 09:08:08 2015 @@ -28,6 +28,8 @@ public class TesterTldListener implement return log.toString(); } + private ServletContext servletContext; + @Override public void contextInitialized(ServletContextEvent sce) { @@ -35,17 +37,23 @@ public class TesterTldListener implement // Try and use one of the Servlet 3.0 methods that should be blocked try { + servletContext = sce.getServletContext(); sc.getEffectiveMajorVersion(); - log.append("FAIL"); + log.append("FAIL-01"); } catch (UnsupportedOperationException uoe) { - log.append("PASS"); + log.append("PASS-01"); } catch (Exception e) { - log.append("FAIL"); + log.append("FAIL-02"); } } @Override public void contextDestroyed(ServletContextEvent sce) { - // NO-OP + // Bug 57446. Same ServletContext should be presented as at init + if (servletContext == sce.getServletContext()) { + log.append("PASS-02"); + } else { + //log.append("FAIL-03"); + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org