Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51744
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 31 Aug 2011 14:17:41 +0000 (14:17 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 31 Aug 2011 14:17:41 +0000 (14:17 +0000)
Don't allow user code to close the JNDI context while a web app is running

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

java/org/apache/naming/NamingContext.java
test/org/apache/naming/resources/TestNamingContext.java

index 0a27a0c..8d0c077 100644 (file)
@@ -743,8 +743,8 @@ public class NamingContext implements Context {
      * @exception NamingException if a naming exception is encountered
      */
     @Override
-    public void close()
-        throws NamingException {
+    public void close() throws NamingException {
+        checkWritable();
         env.clear();
     }
 
index 64720ec..8777ded 100644 (file)
@@ -214,5 +214,61 @@ public class TestNamingContext extends TomcatBaseTest {
         }
     }
     
+    @Test
+    public void testBug51744() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+        tomcat.enableNaming();
+        
+        // Must have a real docBase - just use temp
+        StandardContext ctx = (StandardContext)
+            tomcat.addContext("", System.getProperty("java.io.tmpdir"));
+        
+        // Map the test Servlet
+        Bug51744Servlet bug51744Servlet = new Bug51744Servlet();
+        Tomcat.addServlet(ctx, "bug51744Servlet", bug51744Servlet);
+        ctx.addServletMapping("/", "bug51744Servlet");
+
+        tomcat.start();
+
+        ByteChunk bc = new ByteChunk();
+        int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
+        assertEquals(200, rc);
+        assertEquals(Bug51744Servlet.EXPECTED, bc.toString());
+    }
+
+    public static final class Bug51744Servlet extends HttpServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        public static final String EXPECTED = "TestValue";
 
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws ServletException, IOException {
+
+            resp.setContentType("text/plain;UTF-8");
+            PrintWriter out = resp.getWriter();
+
+            try {
+                Context ctx1 = new InitialContext();
+                Context env1 = (Context) ctx1.lookup("java:comp/env");
+                env1.addToEnvironment("TestName", EXPECTED);
+
+                boolean error = false;
+                try {
+                    env1.close();
+                } catch (NamingException ne) {
+                    error = true;
+                }
+                if (!error) {
+                    throw new ServletException(
+                            "No error when one was expected");
+                }
+                
+                out.print(env1.getEnvironment().get("TestName"));
+            } catch (NamingException ne) {
+                ne.printStackTrace(out);
+            }
+        }
+    }
 }