git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1136028 13f79535-47bb-0310...
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 15 Jun 2011 13:21:16 +0000 (13:21 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 15 Jun 2011 13:21:16 +0000 (13:21 +0000)
java/org/apache/catalina/core/StandardWrapper.java
test/org/apache/catalina/core/TestStandardContext.java
webapps/docs/changelog.xml

index 7b06fbc..07b6c60 100644 (file)
@@ -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()));
index 488fbfb..eb46037 100644 (file)
@@ -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<Class<?>> 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.
index 0d61feb..04a0f05 100644 (file)
 <section name="Tomcat 7.0.17 (markt)">
   <subsection name="Catalina">
     <changelog>
+      <fix>
+        <bug>51376</bug>: 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)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">