Author: markt Date: Mon Jan 19 09:14:29 2015 New Revision: 1652940 URL: http://svn.apache.org/r1652940 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/tc8.0.x/trunk/ (props changed) tomcat/tc8.0.x/trunk/java/org/apache/catalina/core/StandardContext.java tomcat/tc8.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java tomcat/tc8.0.x/trunk/test/org/apache/catalina/core/TesterTldListener.java tomcat/tc8.0.x/trunk/webapps/docs/changelog.xml Propchange: tomcat/tc8.0.x/trunk/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon Jan 19 09:14:29 2015 @@ -1 +1 @@ -/tomcat/trunk:1636524,1637156,1637176,1637188,1637331,1637684,1637695,1638720-1638725,1639653,1640010,1640083-1640084,1640088,1640275,1640322,1640347,1640361,1640365,1640403,1640410,1640652,1640655-1640658,1640688,1640700-1640883,1640903,1640976,1640978,1641000,1641026,1641038-1641039,1641051-1641052,1641058,1641064,1641300,1641369,1641374,1641380,1641486,1641634,1641656-1641692,1641704,1641707-1641718,1641720-1641722,1641735,1641981,1642233,1642280,1642554,1642564,1642595,1642606,1642668,1642679,1642697,1642699,1642766,1643002,1643045,1643054-1643055,1643066,1643121,1643128,1643206,1643209-1643210,1643216,1643249,1643270,1643283,1643309-1643310,1643323,1643365-1643366,1643370-1643371,1643465,1643474,1643536,1643570,1643634,1643649,1643651,1643654,1643675,1643731,1643733-1643734,1643761,1643766,1643814,1643937,1643963,1644017,1644169,1644201-1644203,1644321,1644323,1644516,1644523,1644529,1644535,1644730,1644768,1644784-1644785,1644790,1644793,1644815,1644884,1644886,1644890,1644892 ,1644910,1644924,1644929-1644930,1644935,1644989,1645011,1645247,1645355,1645357-1645358,1645455,1645465,1645469,1645471,1645473,1645475,1645486-1645488,1645626,1645641,1645685,1645743,1645763,1645951-1645953,1645955,1645993,1646098-1646106,1646178,1646220,1646302,1646304,1646420,1646470-1646471,1646476,1646559,1646717-1646723,1646773,1647026,1647042,1647530,1647655,1648304,1648815,1648907,1650081,1650365,1651116,1651120,1651280,1651470 +/tomcat/trunk:1636524,1637156,1637176,1637188,1637331,1637684,1637695,1638720-1638725,1639653,1640010,1640083-1640084,1640088,1640275,1640322,1640347,1640361,1640365,1640403,1640410,1640652,1640655-1640658,1640688,1640700-1640883,1640903,1640976,1640978,1641000,1641026,1641038-1641039,1641051-1641052,1641058,1641064,1641300,1641369,1641374,1641380,1641486,1641634,1641656-1641692,1641704,1641707-1641718,1641720-1641722,1641735,1641981,1642233,1642280,1642554,1642564,1642595,1642606,1642668,1642679,1642697,1642699,1642766,1643002,1643045,1643054-1643055,1643066,1643121,1643128,1643206,1643209-1643210,1643216,1643249,1643270,1643283,1643309-1643310,1643323,1643365-1643366,1643370-1643371,1643465,1643474,1643536,1643570,1643634,1643649,1643651,1643654,1643675,1643731,1643733-1643734,1643761,1643766,1643814,1643937,1643963,1644017,1644169,1644201-1644203,1644321,1644323,1644516,1644523,1644529,1644535,1644730,1644768,1644784-1644785,1644790,1644793,1644815,1644884,1644886,1644890,1644892 ,1644910,1644924,1644929-1644930,1644935,1644989,1645011,1645247,1645355,1645357-1645358,1645455,1645465,1645469,1645471,1645473,1645475,1645486-1645488,1645626,1645641,1645685,1645743,1645763,1645951-1645953,1645955,1645993,1646098-1646106,1646178,1646220,1646302,1646304,1646420,1646470-1646471,1646476,1646559,1646717-1646723,1646773,1647026,1647042,1647530,1647655,1648304,1648815,1648907,1650081,1650365,1651116,1651120,1651280,1651470,1652938 Modified: tomcat/tc8.0.x/trunk/java/org/apache/catalina/core/StandardContext.java URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1652940&r1=1652939&r2=1652940&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/java/org/apache/catalina/core/StandardContext.java (original) +++ tomcat/tc8.0.x/trunk/java/org/apache/catalina/core/StandardContext.java Mon Jan 19 09:14:29 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? @@ -4621,7 +4634,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 '" + @@ -4690,12 +4702,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)) @@ -4737,8 +4748,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) @@ -4748,7 +4762,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); @@ -4795,8 +4813,10 @@ public class StandardContext extends Con setApplicationEventListeners(null); setApplicationLifecycleListeners(null); - return (ok); + noPluggabilityServletContext = null; + noPluggabilityListeners.clear(); + return ok; } Modified: tomcat/tc8.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java?rev=1652940&r1=1652939&r2=1652940&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java (original) +++ tomcat/tc8.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java Mon Jan 19 09:14:29 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/tc8.0.x/trunk/test/org/apache/catalina/core/TesterTldListener.java URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/test/org/apache/catalina/core/TesterTldListener.java?rev=1652940&r1=1652939&r2=1652940&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/test/org/apache/catalina/core/TesterTldListener.java (original) +++ tomcat/tc8.0.x/trunk/test/org/apache/catalina/core/TesterTldListener.java Mon Jan 19 09:14:29 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"); + } } } Modified: tomcat/tc8.0.x/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/webapps/docs/changelog.xml?rev=1652940&r1=1652939&r2=1652940&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/webapps/docs/changelog.xml (original) +++ tomcat/tc8.0.x/trunk/webapps/docs/changelog.xml Mon Jan 19 09:14:29 2015 @@ -55,6 +55,13 @@ <bug>57431</bug>: Enable usage of custom class for context creation when using embedded tomcat. (fschumacher) </add> + <fix> + <bug>57446</bug>: Ensure that <code>ServletContextListener</code>s that + have limited access to <code>ServletContext</code> methods are called + with the same <code>ServletContext</code> instance for both + <code>contextInitialized()</code> and <code>contextDestroyed()</code>. + (markt) + </fix> </changelog> </subsection> <subsection name="Cluster"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org