You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by br...@apache.org on 2003/11/14 21:43:26 UTC

cvs commit: jakarta-commons-sandbox/naming/factory/src/test/org/apache/commons/naming/config XmlConfiguratorTest.java

brett       2003/11/14 12:43:26

  Modified:    naming   STATUS.html
  Added:       naming/factory/src/conf naming.dtd
               naming/factory/src/java/org/apache/commons/naming/config
                        Config.java ParseException.java
                        XmlConfigurator.java
               naming/factory/src/test test-jndi.xml
               naming/factory/src/test/org/apache/commons/naming/config
                        XmlConfiguratorTest.java
  Log:
  Add basic construction from an XML configuration, update status file
  
  Revision  Changes    Path
  1.4       +11 -4     jakarta-commons-sandbox/naming/STATUS.html
  
  Index: STATUS.html
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/naming/STATUS.html,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- STATUS.html	24 Aug 2003 16:08:19 -0000	1.3
  +++ STATUS.html	14 Nov 2003 20:43:26 -0000	1.4
  @@ -36,12 +36,18 @@
   <li><a href="http://www.junit.org">JUnit Testing Framework</a>
       (Version 3.7 or later) - for unit tests only, not required
       for deployment</li>
  -<li>Commons-Collections 2.1</li>
   <li>JTA 1.0.1</li>
   <li>JavaMail 1.2</li>
   <li>Tyrex 1.0</li>
   <li>JDBC 2.0</li>
   <li>MX4J's mx4j-jmx.jar from http://mx4j.sourceforge.net</li>
  +<li>Commons-Collections 2.1</li>
  +<li>Commons-Logging 1.0.3</li>
  +<li>Commons-Lang 1.0.1</li>
  +<li>Commons-Digester 1.4.1</li>
  +<li>Commons-Beanutils 1.6.1</li>
  +<li>Commons-DBCP 1.0</li>
  +<li>Commons-Pool 1.0.1</li>
   </ul>
   
   
  @@ -70,9 +76,9 @@
   <p>Want to help?  Here's some "to do" items the team has identified as possibly being in scope for Naming.
   Note that all are still under discussion, so please mail the list before actioning.</p>
   <ul>
  -<li>Separate Naming into two jars, a core and an optional. The optional would contain all of the above dependencies. </li>
   <li>Unit tests!</li>
  -<li>Add support for loading naming data from XML configuration files</li>
  +<li>Improved external configuration</li>
  +<li>Clarify the structure of core and optional/factory</li>
   </ul>
   
   
  @@ -86,6 +92,7 @@
   <ul>
   <li><a href="mailto:bayard@apache.org">Henri Yandell</a></li>
   <li><a href="mailto:psteitz@apache.org">Phil Steitz</a></li>
  +<li><a href="mailto:brett@apache.org">Brett Porter</a></li>
   <!-- New committers, add your name here -->
   </ul>
   
  
  
  
  1.1                  jakarta-commons-sandbox/naming/factory/src/conf/naming.dtd
  
  Index: naming.dtd
  ===================================================================
  <!ELEMENT naming (context*)>
  
  <!ELEMENT context ((context|environment|resource)*)>
  <!ATTLIST context
  	name  CDATA #IMPLIED>
  
  <!ELEMENT environment (EMPTY)>
  <!ATTLIST environment 
  	name  CDATA #REQUIRED
          type  CDATA #REQUIRED
          value CDATA #REQUIRED
  >
  
  <!ELEMENT resource (parameter*)>
  <!ATTLIST resource
  	name  CDATA #REQUIRED
          type  CDATA #REQUIRED
  >
  
  <!ELEMENT parameter (name,value)>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT value (#PCDATA)>
  
  
  
  1.1                  jakarta-commons-sandbox/naming/factory/src/java/org/apache/commons/naming/config/Config.java
  
  Index: Config.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/naming/factory/src/java/org/apache/commons/naming/config/Config.java,v 1.1 2003/11/14 20:43:26 brett Exp $
   * $Revision: 1.1 $
   * $Date: 2003/11/14 20:43:26 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  package org.apache.commons.naming.config;
  
  import java.util.Collection;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.Map;
  import java.util.Set;
  import java.util.TreeSet;
  
  import javax.naming.CompositeName;
  import javax.naming.InvalidNameException;
  import javax.naming.StringRefAddr;
  
  import org.apache.commons.lang.builder.ToStringBuilder;
  import org.apache.commons.lang.builder.ToStringStyle;
  import org.apache.commons.naming.ResourceRef;
  
  /**
   * Configuration classes. 
   * 
   * @author <a href="brett@apache.org">Brett Porter</a>
   * @version $Id: Config.java,v 1.1 2003/11/14 20:43:26 brett Exp $
   */
  public final class Config
  {
      public static final class Naming
      {
          private final Collection contextList = new LinkedList();
  
          public void addContext(Context context)
          {
              contextList.add(context);
          }
          /**
           * @return
           */
          public Collection getContextList()
          {
              return Collections.unmodifiableCollection(contextList);
          }
  
          public Set generateSortedSubcontextNameSet() throws InvalidNameException
          {
              Set sortedSubcontextNameSet = new TreeSet();
              for (Iterator i = contextList.iterator(); i.hasNext();)
              {
                  Context context = (Context) i.next();
                  context.addSubContextNames(sortedSubcontextNameSet);
              }
              return Collections.unmodifiableSet(sortedSubcontextNameSet);
          }
  
          public String toString()
          {
              return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                  .append("contextList", contextList)
                  .toString();
          }
      }
  
      public static final class Context
      {
          private String name;
          private final Collection environmentList = new LinkedList();
          private final Collection resourceList = new LinkedList();
  
          public void addEnvironment(Environment environment)
          {
              environmentList.add(environment);
          }
  
          /**
           * @param sortedSubcontextNameSet
           */
          public void addSubContextNames(Set sortedSubcontextNameSet) throws InvalidNameException
          {
              if (name != null)
              {
                  sortedSubcontextNameSet.add(name);
              }
              for (Iterator i = environmentList.iterator(); i.hasNext();)
              {
                  Environment e = (Environment) i.next();
                  CompositeName name = new CompositeName(e.getName());
                  sortedSubcontextNameSet.add(name.getPrefix(name.size() - 1).toString());
              }
              for (Iterator i = resourceList.iterator(); i.hasNext();)
              {
                  Resource r = (Resource) i.next();
                  CompositeName name = new CompositeName(r.getName());
                  sortedSubcontextNameSet.add(name.getPrefix(name.size() - 1).toString());
              }
          }
  
          public void addResource(Resource resource)
          {
              resourceList.add(resource);
          }
          public Collection getEnvironmentList()
          {
              return Collections.unmodifiableCollection(environmentList);
          }
  
          public String getName()
          {
              return name;
          }
  
          public void setName(String name)
          {
              this.name = name;
          }
  
          public Collection getResourceList()
          {
              return Collections.unmodifiableCollection(resourceList);
          }
  
          public String toString()
          {
              return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                  .append("name", name)
                  .append("environmentList", environmentList)
                  .append("resourceList", resourceList)
                  .toString();
          }
      }
  
      public static final class Environment
      {
          private String name;
          private String value;
          private String type;
          public String getName()
          {
              return name;
          }
  
          public void setName(String name)
          {
              this.name = name;
          }
  
          public String getType()
          {
              return type;
          }
  
          public void setType(String type)
          {
              this.type = type;
          }
  
          public String getValue()
          {
              return value;
          }
  
          public void setValue(String value)
          {
              this.value = value;
          }
  
          public String toString()
          {
              return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                  .append("name", name)
                  .append("type", type)
                  .append("value", value)
                  .toString();
          }
  
          /**
           * @todo finish types, throw exceptions
           * @return
           */
          public Object createValue()
          {
              if (type.equals(String.class.getName()))
              {
                  return value;
              }
              else if (type.equals(Integer.class.getName()))
              {
                  return Integer.valueOf(value);
              }
              return null;
          }
      }
  
      public static final class Resource
      {
          private String name;
          private String type;
          private final Map parameters = new HashMap();
  
          public void addParameter(String name, String value)
          {
              parameters.put(name, value);
          }
          public String getName()
          {
              return name;
          }
  
          public void setName(String name)
          {
              this.name = name;
          }
  
          public Map getParameters()
          {
              return parameters;
          }
  
          public String getType()
          {
              return type;
          }
  
          public void setType(String type)
          {
              this.type = type;
          }
  
          /**
           * @todo finish types, throw exceptions
           * @return
           */
          public Object createValue()
          {
              ResourceRef ref = new ResourceRef(type, null, null, null);
              for (Iterator i = parameters.keySet().iterator(); i.hasNext();)
              {
                  String name = (String) i.next();
                  String value = (String) parameters.get(name);
                  ref.add(new StringRefAddr(name, value));
              }
              return ref;
          }
  
          public String toString()
          {
              return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                  .append("name", name)
                  .append("type", type)
                  .append("parameters", parameters)
                  .toString();
          }
      }
  }
  
  
  
  
  1.1                  jakarta-commons-sandbox/naming/factory/src/java/org/apache/commons/naming/config/ParseException.java
  
  Index: ParseException.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/naming/factory/src/java/org/apache/commons/naming/config/ParseException.java,v 1.1 2003/11/14 20:43:26 brett Exp $
   * $Revision: 1.1 $
   * $Date: 2003/11/14 20:43:26 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.commons.naming.config;
  
  /**
   * Exception during reading of an XML configuration file.
   * 
   * @author <a href="brett@apache.org">Brett Porter</a>
   * @version $Id: ParseException.java,v 1.1 2003/11/14 20:43:26 brett Exp $
   */
  public class ParseException extends Exception
  {
      ParseException(String msg) {
          super(msg);
      }
  
      ParseException(String msg, Throwable cause) {
          super(msg, cause);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/naming/factory/src/java/org/apache/commons/naming/config/XmlConfigurator.java
  
  Index: XmlConfigurator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/naming/factory/src/java/org/apache/commons/naming/config/XmlConfigurator.java,v 1.1 2003/11/14 20:43:26 brett Exp $
   * $Revision: 1.1 $
   * $Date: 2003/11/14 20:43:26 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.commons.naming.config;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.Iterator;
  import java.util.Set;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import org.apache.commons.digester.Digester;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.commons.naming.ContextAccessController;
  import org.xml.sax.SAXException;
  
  /**
   * Configure an in memory JNDI implementation using an XML configuration file.
   * 
   * @author <a href="brett@apache.org">Brett Porter</a>
   * @version $Id: XmlConfigurator.java,v 1.1 2003/11/14 20:43:26 brett Exp $
   */
  public class XmlConfigurator
  {
      private static final String COMP_CONTEXT_NAME = "java:comp";
      private static final String ENV_CONTEXT_NAME = "env";
      private static final String ROOT_ELEMENT = "naming";
      private static final String CONTEXT_ELEMENT = ROOT_ELEMENT + "/context";
      private static final String ENV_ELEMENT = CONTEXT_ELEMENT + "/environment";
      private static final String RES_ELEMENT = CONTEXT_ELEMENT + "/resource";
      private static final String RES_PARAM_ELEMENT = RES_ELEMENT + "/parameter";
      private static Context envContext = null;
  
      private static final Log LOG = LogFactory.getLog(XmlConfigurator.class);
  
      public static synchronized void setupInitialContext() throws NamingException {
          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.commons.naming.java.javaURLContextFactory");
          System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.commons.naming");
  
          Context initialContext = new InitialContext();
          envContext = initialContext.createSubcontext(COMP_CONTEXT_NAME).createSubcontext(ENV_CONTEXT_NAME);
      }
  
      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);
          initialContext = null;
      }
  
      public static synchronized void loadConfiguration(InputStream inputFile) throws NamingException, ParseException {
          if (envContext == null)
          {
              setupInitialContext();
          }
          ContextAccessController.setWritable("root", "x");
  
          Digester digester = new Digester();
  // TODO: string constants
          digester.addObjectCreate(ROOT_ELEMENT, Config.Naming.class);
          digester.addObjectCreate(CONTEXT_ELEMENT, Config.Context.class);
          digester.addSetProperties(CONTEXT_ELEMENT);
          digester.addSetNext(CONTEXT_ELEMENT, "addContext");
          // TODO: handle context inside context?
          digester.addObjectCreate(ENV_ELEMENT, Config.Environment.class);
          digester.addSetProperties(ENV_ELEMENT);
          digester.addSetNext(ENV_ELEMENT, "addEnvironment");
          digester.addObjectCreate(RES_ELEMENT, Config.Resource.class);
          digester.addSetProperties(RES_ELEMENT);
          digester.addSetNext(RES_ELEMENT, "addResource");
          digester.addCallMethod(RES_PARAM_ELEMENT + "", "addParameter", 2);
          digester.addCallParam(RES_PARAM_ELEMENT + "/name", 0);
          digester.addCallParam(RES_PARAM_ELEMENT + "/value", 1);
  
          try
          {
              Config.Naming naming = (Config.Naming) digester.parse(inputFile);
              if (naming == null) {
                  throw new ParseException("Unable to find root element '" + ROOT_ELEMENT + "'");
              }
              if (LOG.isDebugEnabled()) {
                  LOG.debug("XML configuration loaded: " + naming.toString());
              }
  
              precreateSubcontextTree(envContext, naming.generateSortedSubcontextNameSet());
  
              for (Iterator i = naming.getContextList().iterator(); i.hasNext();)
              {
                  Config.Context ctx = (Config.Context) i.next();
                  Context jndiCtx = envContext;
                  if (ctx.getName() != null)
                  {
                      jndiCtx = (Context) jndiCtx.lookup(ctx.getName());
                  }
  
                  for (Iterator j = ctx.getEnvironmentList().iterator(); j.hasNext();)
                  {
                      Config.Environment e = (Config.Environment) j.next();
                      jndiCtx.rebind(e.getName(), e.createValue());
                  }
  
                  for (Iterator j = ctx.getResourceList().iterator(); j.hasNext();)
                  {
                      Config.Resource r = (Config.Resource) j.next();
                      jndiCtx.bind(r.getName(), r.createValue());
                  }
              }
          }
          catch (IOException e)
          {
              throw new ParseException("Error reading configuration file", e);
          }
          catch (SAXException e)
          {
              throw new ParseException("Error reading configuration file", e);
          }
      }
  
      /**
       * @param set
       */
      private static void precreateSubcontextTree(Context ctx, Set sortedSubcontextNameSet) throws NamingException
      {
          // TODO: don't recreate
          for (Iterator i = sortedSubcontextNameSet.iterator(); i.hasNext();)
          {
              String name = (String) i.next();
              ctx.createSubcontext(name);
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/naming/factory/src/test/test-jndi.xml
  
  Index: test-jndi.xml
  ===================================================================
  <naming>
    <context>
      <context name="sub/context">
      
        <environment name="config/host" value="jakarta.apache.org" type="java.lang.String" />
        <environment name="config/port" value="8000" type="java.lang.Integer" />
      </context>
  
      <environment name="config/host" value="www.apache.org" type="java.lang.String" />
      <environment name="config/port" value="80" type="java.lang.Integer" />
  
      <resource name="jdbc/pool" type="javax.sql.DataSource">
        <parameter>
          <name>driverClassName</name>
          <value>org.hsqldb.jdbcDriver</value>
        </parameter>
        <parameter>
          <name>url</name>
          <value>jdbc:hsqldb:target/hsqldb</value>
        </parameter>
        <parameter>
          <name>username</name>
          <value>sa</value>
        </parameter>
        <parameter>
          <name>password</name>
          <value></value>
        </parameter>
      </resource>
    </context>
  </naming>
  
  
  
  1.1                  jakarta-commons-sandbox/naming/factory/src/test/org/apache/commons/naming/config/XmlConfiguratorTest.java
  
  Index: XmlConfiguratorTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/naming/factory/src/test/org/apache/commons/naming/config/XmlConfiguratorTest.java,v 1.1 2003/11/14 20:43:26 brett Exp $
   * $Revision: 1.1 $
   * $Date: 2003/11/14 20:43:26 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.commons.naming.config;
  
  import java.sql.Connection;
  import java.sql.ResultSet;
  import java.sql.Statement;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.sql.DataSource;
  
  import junit.framework.TestCase;
  
  /**
   * Test case for the XML configuration methods, testing environment entries
   * and database connection resource factories.
   * 
   * @author <a href="brett@apache.org">Brett Porter</a>
   * @version $Id: XmlConfiguratorTest.java,v 1.1 2003/11/14 20:43:26 brett Exp $
   */
  public class XmlConfiguratorTest extends TestCase
  {
      public XmlConfiguratorTest(String name) {
          super(name);
      }
  
      /*
       * @see TestCase#setUp()
       */
      protected void setUp() throws Exception {
          super.setUp();
          XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml"));
      }
  
      /*
       * @see TestCase#tearDown()
       */
      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);
      }
  
      /**
       * 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");
          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(); 
              }
          }
      }
  }
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org