Add an method that enables JNDI and a test case to make sure it works
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 7 Sep 2009 15:54:08 +0000 (15:54 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 7 Sep 2009 15:54:08 +0000 (15:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@812210 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/startup/Tomcat.java
test/org/apache/catalina/startup/TestTomcat.java

index d8f1552..4279d5f 100644 (file)
@@ -369,8 +369,8 @@ public class Tomcat {
     }
 
     /**
-     * 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 @@ public class Tomcat {
     }
     
     /**
+     * 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. 
      * 
index db9251d..441bda2 100644 (file)
@@ -25,11 +25,15 @@ import java.net.URL;
 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 @@ public class TestTomcat extends TestTomcatBase {
         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 @@ public class TestTomcat extends TestTomcatBase {
         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.
      */