From 567eafe259bc525f0a717a3ddc888f9fc3bc5a3f Mon Sep 17 00:00:00 2001 From: markt Date: Wed, 15 Jun 2011 13:21:16 +0000 Subject: [PATCH] git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1136028 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/catalina/core/StandardWrapper.java | 4 ++ .../apache/catalina/core/TestStandardContext.java | 84 ++++++++++++++++++++++ webapps/docs/changelog.xml | 6 ++ 3 files changed, 94 insertions(+) diff --git a/java/org/apache/catalina/core/StandardWrapper.java b/java/org/apache/catalina/core/StandardWrapper.java index 7b06fbc07..07b6c6078 100644 --- a/java/org/apache/catalina/core/StandardWrapper.java +++ b/java/org/apache/catalina/core/StandardWrapper.java @@ -1020,6 +1020,10 @@ public class StandardWrapper extends ContainerBase public synchronized void load() throws ServletException { instance = loadServlet(); + if (!instanceInitialized) { + initServlet(instance); + } + if (isJspServlet) { StringBuilder oname = new StringBuilder(MBeanUtils.getDomain(getParent())); diff --git a/test/org/apache/catalina/core/TestStandardContext.java b/test/org/apache/catalina/core/TestStandardContext.java index 488fbfb3c..eb46037d8 100644 --- a/test/org/apache/catalina/core/TestStandardContext.java +++ b/test/org/apache/catalina/core/TestStandardContext.java @@ -321,6 +321,90 @@ public class TestStandardContext extends TomcatBaseTest { } + public void testBug51376() throws Exception { + // Set up a container + Tomcat tomcat = getTomcatInstance(); + + // Must have a real docBase - just use temp + File docBase = new File(System.getProperty("java.io.tmpdir")); + Context ctx = tomcat.addContext("", docBase.getAbsolutePath()); + + // Add ServletContainerInitializer + Bug51376SCI sci = new Bug51376SCI(); + ctx.addServletContainerInitializer(sci, null); + + // Start the context + tomcat.start(); + + // Stop the context + ctx.stop(); + + // Make sure that init() and destroy() were called correctly + assertTrue(sci.getServlet().isOk()); + } + + public static final class Bug51376SCI + implements ServletContainerInitializer { + + private Bug51376Servlet s = null; + + private Bug51376Servlet getServlet() { + return s; + } + + @Override + public void onStartup(Set> c, ServletContext ctx) + throws ServletException { + // Register and map servlet + s = new Bug51376Servlet(); + ServletRegistration.Dynamic sr = ctx.addServlet("bug51376", s); + sr.addMapping("/bug51376"); + sr.setLoadOnStartup(1); + } + } + + public static final class Bug51376Servlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + private Boolean initOk = null; + private Boolean destoryOk = null; + + @Override + public void init() { + if (initOk == null && destoryOk == null) { + initOk = Boolean.TRUE; + } else { + initOk = Boolean.FALSE; + } + } + + @Override + public void destroy() { + if (initOk.booleanValue() && destoryOk == null) { + destoryOk = Boolean.TRUE; + } else { + destoryOk = Boolean.FALSE; + } + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + resp.setContentType("text/plain"); + resp.getWriter().write("OK"); + } + + protected boolean isOk() { + if (initOk != null && initOk.booleanValue() && destoryOk != null && + destoryOk.booleanValue()) { + return true; + } else { + return false; + } + } + } + /** * Test case for bug 49711: HttpServletRequest.getParts does not work * in a filter. diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 0d61feb4b..04a0f05fa 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -45,6 +45,12 @@
+ + 51376: When adding a Servlet via + ServletContext#addServlet(String, Servlet), the Servlet was not + initialized when the web application started and a load on startup value + was set. (markt) + -- 2.11.0