Author: markt Date: Mon Sep 7 15:54:08 2009 New Revision: 812210 URL: http://svn.apache.org/viewvc?rev=812210&view=rev Log: Add an method that enables JNDI and a test case to make sure it works
Modified: tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java Modified: tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java?rev=812210&r1=812209&r2=812210&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java (original) +++ tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java Mon Sep 7 15:54:08 2009 @@ -369,8 +369,8 @@ } /** - * Get the server object. You can add listeners and - * few more customizations. + * Get the server object. You can add listeners and few more + * customizations. JNDI is disabled by default. */ public StandardServer getServer() { @@ -528,6 +528,31 @@ } /** + * Enables JNDI naming which is disabled by default. + */ + public void enableNaming() { + // Make sure getServer() has been called as that is where naming is + // disabled + getServer(); + + System.setProperty("catalina.useNaming", "true"); + String value = "org.apache.naming"; + String oldValue = + System.getProperty(javax.naming.Context.URL_PKG_PREFIXES); + if (oldValue != null) { + value = value + ":" + oldValue; + } + System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value); + value = System.getProperty + (javax.naming.Context.INITIAL_CONTEXT_FACTORY); + if (value == null) { + System.setProperty + (javax.naming.Context.INITIAL_CONTEXT_FACTORY, + "org.apache.naming.java.javaURLContextFactory"); + } + } + + /** * Provide default configuration for a context. This is the programmatic * equivalent of the default web.xml. * Modified: tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java?rev=812210&r1=812209&r2=812210&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java (original) +++ tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java Mon Sep 7 15:54:08 2009 @@ -25,11 +25,15 @@ import java.util.List; import java.util.Map; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.catalina.core.StandardContext; +import org.apache.catalina.deploy.ContextEnvironment; import org.apache.tomcat.util.buf.ByteChunk; public class TestTomcat extends TestTomcatBase { @@ -42,8 +46,34 @@ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req, HttpServletResponse res) - throws IOException { - res.getWriter().write("Hello world"); + throws IOException { + res.getWriter().write("Hello world"); + } + } + + /** + * Simple servlet to test iJNDI + */ + public static class HelloWorldJndi extends HttpServlet { + + private static final long serialVersionUID = 1L; + + private static final String JNDI_ENV_NAME = "test"; + + public void doGet(HttpServletRequest req, HttpServletResponse res) + throws IOException { + + String name = null; + + try { + Context initCtx = new InitialContext(); + Context envCtx = (Context) initCtx.lookup("java:comp/env"); + name = (String) envCtx.lookup(JNDI_ENV_NAME); + } catch (NamingException e) { + throw new IOException(e); + } + + res.getWriter().write("Hello, " + name); } } @@ -96,7 +126,39 @@ System.err.println("Test time: " + (System.currentTimeMillis() - t0)); } + + /** + * Test for enabling JNDI. + */ + public void testEnableNaming() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // Must have a real docBase - just use temp + StandardContext ctx = + tomcat.addContext("/", System.getProperty("java.io.tmpdir")); + + // You can customise the context by calling its API + + // Enable JNDI - it is disabled by default + tomcat.enableNaming(); + + ContextEnvironment environment = new ContextEnvironment(); + environment.setType("java.lang.String"); + environment.setName(HelloWorldJndi.JNDI_ENV_NAME); + environment.setValue("Tomcat User"); + ctx.getNamingResources().addEnvironment(environment); + + Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi()); + ctx.addServletMapping("/", "jndiServlet"); + + tomcat.start(); + + ByteChunk res = getUrl("http://localhost:" + getPort() + "/"); + assertEquals(res.toString(), "Hello, Tomcat User"); + } + + /** * Wrapper for getting the response. */ --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org