From: markt Date: Wed, 31 Aug 2011 14:17:41 +0000 (+0000) Subject: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51744 X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=50b21aca621b4ce43b566e91631dfe26adcec72b;p=tomcat7.0 Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51744 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 --- diff --git a/java/org/apache/naming/NamingContext.java b/java/org/apache/naming/NamingContext.java index 0a27a0c39..8d0c0772d 100644 --- a/java/org/apache/naming/NamingContext.java +++ b/java/org/apache/naming/NamingContext.java @@ -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(); } diff --git a/test/org/apache/naming/resources/TestNamingContext.java b/test/org/apache/naming/resources/TestNamingContext.java index 64720ec25..8777ded35 100644 --- a/test/org/apache/naming/resources/TestNamingContext.java +++ b/test/org/apache/naming/resources/TestNamingContext.java @@ -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); + } + } + } }