Author: psteitz Date: Sun Dec 26 22:01:14 2004 New Revision: 123389 URL: http://svn.apache.org/viewcvs?view=rev&rev=123389 Log: servlet. Modified: incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/Config.java incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/XmlConfigurator.java incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/XmlConfiguratorTest.java incubator/directory/naming/trunk/factory/project.xml incubator/directory/naming/trunk/project.xml incubator/directory/naming/trunk/xdocs/using.xml
Modified: incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/Config.java Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/Config.java?view=diff&rev=123389&p1=incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/Config.java&r1=123388&p2=incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/Config.java&r2=123389 ============================================================================== --- incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/Config.java (original) +++ incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/Config.java Sun Dec 26 22:01:14 2004 @@ -92,7 +92,7 @@ */ public void addSubContextNames(Set sortedSubcontextNameSet) throws InvalidNameException { - if (name != null) + if ((name != null) && !environmentList.iterator().hasNext() && !resourceList.iterator().hasNext()) { sortedSubcontextNameSet.add(name); } Modified: incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/XmlConfigurator.java Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/XmlConfigurator.java?view=diff&rev=123389&p1=incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/XmlConfigurator.java&r1=123388&p2=incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/XmlConfigurator.java&r2=123389 ============================================================================== --- incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/XmlConfigurator.java (original) +++ incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/XmlConfigurator.java Sun Dec 26 22:01:14 2004 @@ -24,6 +24,9 @@ import javax.naming.Context; import javax.naming.InitialContext; +import javax.naming.Name; +import javax.naming.NameClassPair; +import javax.naming.NamingEnumeration; import javax.naming.NamingException; import org.apache.commons.digester.Digester; @@ -61,10 +64,11 @@ public static synchronized void destroyInitialContext() throws NamingException { Context initialContext = new InitialContext(); - Context ctx = (Context) initialContext.lookup(COMP_CONTEXT_NAME); - ctx.destroySubcontext(ENV_CONTEXT_NAME); - envContext = null; - initialContext.destroySubcontext(COMP_CONTEXT_NAME); + NamingEnumeration contexts = initialContext.list(""); + while (contexts.hasMore()) { + initialContext.destroySubcontext(((NameClassPair) contexts.next()).getName()); + } + envContext = null; initialContext = null; } @@ -102,7 +106,6 @@ LOG.debug("XML configuration loaded: " + naming.toString()); } - precreateSubcontextTree(envContext, naming.generateSortedSubcontextNameSet()); for (Iterator i = naming.getContextList().iterator(); i.hasNext();) { @@ -110,8 +113,16 @@ Context jndiCtx = envContext; if (ctx.getName() != null) { - jndiCtx = (Context) jndiCtx.lookup(ctx.getName()); + destroyInitialContext(); + Context initialContext = new InitialContext(); + Name nm = initialContext.getNameParser("").parse(ctx.getName()); + envContext = initialContext.createSubcontext(nm.get(0)); + jndiCtx = envContext; + for (int k = 1; k < nm.size(); k++) { + jndiCtx = jndiCtx.createSubcontext(nm.get(k)); + } } + precreateSubcontextTree(jndiCtx, naming.generateSortedSubcontextNameSet()); for (Iterator j = ctx.getEnvironmentList().iterator(); j.hasNext();) { Modified: incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/XmlConfiguratorTest.java Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/XmlConfiguratorTest.java?view=diff&rev=123389&p1=incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/XmlConfiguratorTest.java&r1=123388&p2=incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/XmlConfiguratorTest.java&r2=123389 ============================================================================== --- incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/XmlConfiguratorTest.java (original) +++ incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/XmlConfiguratorTest.java Sun Dec 26 22:01:14 2004 @@ -45,7 +45,6 @@ */ protected void setUp() throws Exception { super.setUp(); - XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml")); } /* @@ -54,76 +53,113 @@ protected void tearDown() throws Exception { super.tearDown(); XmlConfigurator.destroyInitialContext(); - } - - /** - * Test for correctly configured environment entries. - * @throws Exception as tests do - */ - public void testEnvironment() throws Exception { - Context ctx = new InitialContext(); - Context env = (Context) ctx.lookup("java:comp/env"); - String host = (String) env.lookup("config/host"); - Integer port = (Integer) env.lookup("config/port"); - - assertEquals("Check host", "www.apache.org", host); - assertEquals("Check port", new Integer(80), port); - - Boolean trueBool = (Boolean) env.lookup("config/mytruebool"); - Boolean falseBool = (Boolean) env.lookup("config/myfalsebool"); - - assertTrue("Check true boolean value", trueBool.booleanValue()); - assertTrue("Check false boolean value", !falseBool.booleanValue()); - } - - /** - * Test config as a subcontext of a different root. - * @throws Exception if it fails - */ - public void testDuplicateSubcontextName() throws Exception { - Context ctx = new InitialContext(); - Context env = (Context) ctx.lookup("java:comp/env"); - String user = (String) env.lookup("jdbc/config/pool/user"); - - assertEquals("Check user", "dbuser", user); - } - - /** - * Test for correctly configured and operational database connection - * resource factories. - * @throws Exception as tests do - */ - public void testJdbc() throws Exception { - Context ctx = new InitialContext(); - Context env = (Context) ctx.lookup("java:comp/env"); - System.err.println("foo: "+env.lookup("jdbc/pool")); - System.err.println("foc: "+env.lookup("jdbc/pool").getClass()); - DataSource ds = (DataSource) env.lookup("jdbc/pool"); - Connection con = null; - Statement stat = null; - ResultSet rs = null; - try { - con = ds.getConnection(); - stat = con.createStatement(); - stat.executeUpdate("DROP TABLE DUAL IF EXISTS"); - stat.executeUpdate("CREATE TABLE DUAL(value char(50))"); - stat.executeUpdate("INSERT INTO DUAL VALUES(1)"); - rs = stat.executeQuery("SELECT * FROM DUAL"); - while (rs.next()) { - assertEquals("Check you get back what you put into the DB", 1, rs.getInt(1)); - } - } - finally { - if (rs != null) { - rs.close(); - } - if (stat != null) { - stat.close(); - } - if (con != null) { - con.close(); - } - } + } + + /** + * Default root context name -- what XmlConfigurator assumes if root context + * element in xml config file does not have a name attribute, as in + * test-jndi.xml + */ + protected static String DEFAULT_ROOT="java:comp/env"; + + /** + * Alternate root context name specified in test-jndi2.xml. Must match + * name attribute of top-level context element in test-jndi2.xml + */ + protected static String ALT_ROOT="alt/root/context"; + + /** + * Test for correctly configured environment entries. + * @throws Exception as tests do + */ + public void testEnvironment() throws Exception { + XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml")); + checkEnvironment(DEFAULT_ROOT); + XmlConfigurator.destroyInitialContext(); + XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi2.xml")); + checkEnvironment(ALT_ROOT); + } + + protected void checkEnvironment(String root) throws Exception { + Context ctx = new InitialContext(); + Context env = (Context) ctx.lookup(root); + String host = (String) env.lookup("config/host"); + Integer port = (Integer) env.lookup("config/port"); + + assertEquals("Check host", "www.apache.org", host); + assertEquals("Check port", new Integer(80), port); + + Boolean trueBool = (Boolean) env.lookup("config/mytruebool"); + Boolean falseBool = (Boolean) env.lookup("config/myfalsebool"); + + assertTrue("Check true boolean value", trueBool.booleanValue()); + assertTrue("Check false boolean value", !falseBool.booleanValue()); + + trueBool = (Boolean) ctx.lookup(root + "/config/mytruebool"); + assertTrue("Check true boolean value -- root lookup", trueBool.booleanValue()); + + } + + /** + * Test config as a subcontext of a different root. + * @throws Exception if it fails + */ + public void testDuplicateSubcontextName() throws Exception{ + XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml")); + checkDuplicateSubcontextName(DEFAULT_ROOT); + } + + protected void checkDuplicateSubcontextName(String root) throws Exception { + Context ctx = new InitialContext(); + Context env = (Context) ctx.lookup(root); + String user = (String) env.lookup("jdbc/config/pool/user"); + assertEquals("Check user", "dbuser", user); + user = (String) ctx.lookup(root + "/jdbc/config/pool/user"); + assertEquals("Check user -- root lookup", "dbuser", user); + } + + /** + * Test for correctly configured and operational database connection + * resource factories. + * @throws Exception as tests do + */ + public void testJdbc() throws Exception { + XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml")); + checkJdbc(DEFAULT_ROOT); + XmlConfigurator.destroyInitialContext(); + XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi2.xml")); + checkJdbc(ALT_ROOT); + } + + protected void checkJdbc(String root) throws Exception { + Context ctx = new InitialContext(); + Context env = (Context) ctx.lookup(root); + DataSource ds = (DataSource) env.lookup("jdbc/pool"); + Connection con = null; + Statement stat = null; + ResultSet rs = null; + try { + con = ds.getConnection(); + stat = con.createStatement(); + stat.executeUpdate("DROP TABLE DUAL IF EXISTS"); + stat.executeUpdate("CREATE TABLE DUAL(value char(50))"); + stat.executeUpdate("INSERT INTO DUAL VALUES(1)"); + rs = stat.executeQuery("SELECT * FROM DUAL"); + while (rs.next()) { + assertEquals("Check you get back what you put into the DB", 1, rs.getInt(1)); + } + } + finally { + if (rs != null) { + rs.close(); + } + if (stat != null) { + stat.close(); + } + if (con != null) { + con.close(); + } + } } } Modified: incubator/directory/naming/trunk/factory/project.xml Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/factory/project.xml?view=diff&rev=123389&p1=incubator/directory/naming/trunk/factory/project.xml&r1=123388&p2=incubator/directory/naming/trunk/factory/project.xml&r2=123389 ============================================================================== --- incubator/directory/naming/trunk/factory/project.xml (original) +++ incubator/directory/naming/trunk/factory/project.xml Sun Dec 26 22:01:14 2004 @@ -19,10 +19,24 @@ <version>1.0.1B-rc1</version> </dependency> + <!-- <dependency> <groupId>geronimo-spec</groupId> <artifactId>geronimo-spec-javamail</artifactId> <version>1.3.1-rc1</version> + </dependency> + --> + + <dependency> + <groupId>javamail</groupId> + <artifactId>javamail</artifactId> + <version>1.2</version> + </dependency> + + <dependency> + <groupId>javamail</groupId> + <artifactId>activation</artifactId> + <version>1.2</version> </dependency> <dependency> Modified: incubator/directory/naming/trunk/project.xml Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/project.xml?view=diff&rev=123389&p1=incubator/directory/naming/trunk/project.xml&r1=123388&p2=incubator/directory/naming/trunk/project.xml&r2=123389 ============================================================================== --- incubator/directory/naming/trunk/project.xml (original) +++ incubator/directory/naming/trunk/project.xml Sun Dec 26 22:01:14 2004 @@ -150,8 +150,8 @@ <reports> <!-- <report>maven-changelog-plugin</report> --> <!-- <report>maven-changes-plugin</report> --> - <!-- breaks for me <report>maven-checkstyle-plugin</report> --> - <!-- <report>maven-clover-plugin</report> --> + <report>maven-checkstyle-plugin</report> + <report>maven-clover-plugin</report> <!-- <report>maven-developer-activity-plugin</report> --> <!-- <report>maven-file-activity-plugin</report> --> <report>maven-javadoc-plugin</report> @@ -159,7 +159,7 @@ <report>maven-junit-report-plugin</report> <report>maven-jxr-plugin</report> <!-- <report>maven-license-plugin</report> --> - <!-- <report>maven-linkcheck-plugin</report> --> + <report>maven-linkcheck-plugin</report> <!-- <report>maven-statcvs-plugin</report> --> <report>maven-tasklist-plugin</report> </reports> Modified: incubator/directory/naming/trunk/xdocs/using.xml Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/xdocs/using.xml?view=diff&rev=123389&p1=incubator/directory/naming/trunk/xdocs/using.xml&r1=123388&p2=incubator/directory/naming/trunk/xdocs/using.xml&r2=123389 ============================================================================== --- incubator/directory/naming/trunk/xdocs/using.xml (original) +++ incubator/directory/naming/trunk/xdocs/using.xml Sun Dec 26 22:01:14 2004 @@ -19,12 +19,42 @@ <section name="Examples"> <subsection name="Configuring JNDI resources using XMLConfigurator"> <p> - The <code>XMLConfigurator</code> allows you to configure and load JNDI - resources using xml configuration files using a syntax very similar to that - used by Jakarta Tomcat's server.xml file. [FIXME -- add schema/dtd reference.] - For a full introduction to JNDI resources and resource factories, see + The <code>XMLConfigurator</code> allows you set JNDI environment entries + and to configure and load JNDI resources in the "java:comp/env" namespace + using xml configuration files. The xml syntax is similar to that used by + Jakarta Tomcat's <code>server.xml</code> and the J2EE <code>web.xml</code> + configuration files. For a full introduction to JNDI resources and resource factories, see <a href="http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-resources-howto.html"> Tomcat JNDI Resources HOW-TO</a> + </p> + <p> + The structure of the configuation file is + <source><![CDATA[ +<naming> + <context> + <environment name="..." value="..." type="..." /> + ... + <resource name="..." type="..."> + <parameter> + <name>...</name> + <value>...</value> + </parameter> + ... + </resource> + </context> +</naming> + ]]> + </source> + </p> + <p> + The <code>environment</code> entries achieve the same effect as + <code>env-entry</code> elements in <code>web.xml</code>. The + <code>resource</code> elements behave like <code>Resource</code> elements + in <code>server.xml</code>. Note that here "resource" starts with a + lower case "r" and the <code>parameter</code> elements are its content + (In Tomcat's <code>server.xml</code>, <code>Resource</code> is an empty + tag and the parameters are included in a <code>ResourceParams</code> + element.) </p> <p> Here is an example, showing how to configure a database connection pool and
