Author: bayard Date: Tue Dec 21 08:34:08 2004 New Revision: 122972 URL: http://svn.apache.org/viewcvs?view=rev&rev=122972 Log: extracted config from factory Added: incubator/directory/naming/trunk/config/ incubator/directory/naming/trunk/config/build.xml incubator/directory/naming/trunk/config/project.properties incubator/directory/naming/trunk/config/project.xml incubator/directory/naming/trunk/config/src/ incubator/directory/naming/trunk/config/src/conf/ incubator/directory/naming/trunk/config/src/conf/naming.dtd incubator/directory/naming/trunk/config/src/java/ incubator/directory/naming/trunk/config/src/java/org/ incubator/directory/naming/trunk/config/src/java/org/apache/ incubator/directory/naming/trunk/config/src/java/org/apache/naming/ incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/ incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/Config.java incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/ParseException.java incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/XmlConfigurator.java incubator/directory/naming/trunk/config/src/test/ incubator/directory/naming/trunk/config/src/test/org/ incubator/directory/naming/trunk/config/src/test/org/apache/ incubator/directory/naming/trunk/config/src/test/org/apache/naming/ incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/ incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/XmlConfiguratorTest.java incubator/directory/naming/trunk/config/src/test/test-jndi.xml Removed: incubator/directory/naming/trunk/factory/src/conf/ incubator/directory/naming/trunk/factory/src/test/org/apache/naming/config/ incubator/directory/naming/trunk/factory/src/test/test-jndi.xml
Added: incubator/directory/naming/trunk/config/build.xml Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/config/build.xml?view=auto&rev=122972 ============================================================================== --- (empty file) +++ incubator/directory/naming/trunk/config/build.xml Tue Dec 21 08:34:08 2004 @@ -0,0 +1,178 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + build.xml for directory-naming-config to support gump integration and ant + command line builds. This build script gets dependent jars from ibibilio, with the + exception of javamail and jta jars, which it tries to load from the local maven + repository, located at ${localrepo}. If build.sysclasspath is set to "only", dependent + jars are expected to be available via the system classpath. + No dist target is provided. +--> + +<project default="jar" name="directory-naming-config" basedir="."> + <property name="defaulttargetdir" value="target"/> + <property name="libdir" value="target/lib"/> + <property name="classesdir" value="target/classes"/> + <property name="testclassesdir" value="target/test-classes"/> + <property name="testreportdir" value="target/test-reports"/> + <property name="distdir" value="dist"/> + <property name="javadocdir" value="dist/docs/api"/> + <property name="coredir" value="../core"/> + <property name="coreclassesdir" value="../core/target/classes"/> + <property name="localrepo" value="${user.home}/.maven/repository"/> + <property name="final.name" value="directory-naming-config-${DATE_STAMP}"/> + + <target name="init" description="Initializes some properties"> + <mkdir dir="${libdir}"/> + <condition property="noget"> + <equals arg2="only" arg1="${build.sysclasspath}"> + </equals> + </condition> + </target> + + <target name="compile" description="o Compile the code" depends="get-deps"> + <mkdir dir="${classesdir}"> + </mkdir> + <javac destdir="${classesdir}" deprecation="true" debug="true" optimize="false" excludes="**/package.html"> + <src> + <pathelement location="src/java"> + </pathelement> + </src> + <classpath> + <fileset dir="${libdir}"> + <include name="*.jar"> + </include> + </fileset> + <pathelement path="${coreclassesdir}"> + </pathelement> + </classpath> + </javac> + <copy todir="${classesdir}"> + <fileset dir="src/java"> + <include name="**/*.properties"> + </include> + </fileset> + </copy> + <copy todir="${classesdir}"> + <fileset dir="src/conf"> + <include name="**/*.dtd"> + </include> + </fileset> + </copy> + <copy todir="${testclassesdir}"> + <fileset dir="src/test"> + <include name="**/*.xml"> + </include> + <include name="**/*.properties"> + </include> + </fileset> + </copy> + </target> + + <target name="jar" description="o Create the jar" depends="compile,test"> + <mkdir dir="${distdir}"/> + <jar jarfile="${distdir}/${final.name}.jar" excludes="**/package.html" basedir="${classesdir}"/> + </target> + + <target name="clean" description="o Clean up the generated directories"> + <delete dir="${defaulttargetdir}"> + </delete> + <delete dir="${distdir}"> + </delete> + </target> + + <target name="test" description="o Run the test cases" if="test.failure" depends="internal-test"> + <fail message="There were test failures."> + </fail> + </target> + <target name="internal-test" depends="compile-tests"> + <mkdir dir="${testreportdir}"> + </mkdir> + <junit dir="./" failureproperty="test.failure" printSummary="yes" fork="true" haltonerror="true"> + <sysproperty key="basedir" value="."> + </sysproperty> + <formatter type="xml"> + </formatter> + <formatter usefile="false" type="plain"> + </formatter> + <classpath> + <fileset dir="${libdir}"> + <include name="*.jar"> + </include> + </fileset> + <pathelement path="${testclassesdir}"> + </pathelement> + <pathelement path="${classesdir}"> + </pathelement> + <pathelement path="${coreclassesdir}"> + </pathelement> + </classpath> + <batchtest todir="${testreportdir}"> + <fileset dir="src/test"> + <include name="**/*Test*"> + </include> + </fileset> + </batchtest> + </junit> + </target> + + <target name="compile-tests" depends="compile"> + <mkdir dir="${testclassesdir}"> + </mkdir> + <javac destdir="${testclassesdir}" deprecation="true" debug="true" optimize="false" excludes="**/package.html"> + <src> + <pathelement location="src/test"> + </pathelement> + </src> + <classpath> + <fileset dir="${libdir}"> + <include name="*.jar"> + </include> + </fileset> + <pathelement path="${classesdir}"> + </pathelement> + <pathelement path="${coreclassesdir}"> + </pathelement> + </classpath> + </javac> + </target> + + <target name="javadoc" description="o Generate javadoc" depends="jar"> + <mkdir dir="${javadocdir}"> + </mkdir> + <tstamp> + <format pattern="2003-yyyy" property="year"> + </format> + </tstamp> + <property name="copyright" value="Copyright &copy; Apache Software Foundation. All Rights Reserved."> + </property> + <property name="title" value="Naming Factories SNAPSHOT API"> + </property> + <javadoc use="true" private="true" destdir="${javadocdir}" author="true" version="true" sourcepath="src/java" packagenames="org.apache.naming.*"> + <classpath> + <fileset dir="${libdir}"> + <include name="*.jar"> + </include> + </fileset> + <pathelement location="target/${final.name}.jar"> + </pathelement> + </classpath> + </javadoc> + </target> + + <target name="get-deps" unless="noget" depends="init"> + + <get dest="${libdir}/commons-logging-1.0.3.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-logging/jars/commons-logging-1.0.3.jar"/> + <get dest="${libdir}/commons-lang-1.0.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-lang/jars/commons-lang-1.0.1.jar"/> + <get dest="${libdir}/commons-digester-1.4.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-digester/jars/commons-digester-1.4.1.jar"/> + <get dest="${libdir}/commons-beanutils-1.6.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-beanutils/jars/commons-beanutils-1.6.1.jar"/> + <get dest="${libdir}/commons-dbcp-1.0.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-dbcp/jars/commons-dbcp-1.0.jar"/> + <get dest="${libdir}/commons-pool-1.0.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-pool/jars/commons-pool-1.0.1.jar"/> + <get dest="${libdir}/hsqldb-1.7.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/hsqldb/jars/hsqldb-1.7.1.jar"/> + <get dest="${libdir}/commons-collections-2.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-collections/jars/commons-collections-2.1.jar"/> + <get dest="${libdir}/mx4j-jmx-1.1.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/mx4j/jars/mx4j-jmx-1.1.1.jar"/> + <get dest="${libdir}/junit-3.8.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/junit/jars/junit-3.8.1.jar"/> + <get dest="${libdir}/ant-1.5.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/ant/jars/ant-1.5.jar"/> + <get dest="${libdir}/ant-optional-1.5.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/ant/jars/ant-optional-1.5.jar"/> + </target> +</project> Added: incubator/directory/naming/trunk/config/project.properties Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/config/project.properties?view=auto&rev=122972 ============================================================================== --- (empty file) +++ incubator/directory/naming/trunk/config/project.properties Tue Dec 21 08:34:08 2004 @@ -0,0 +1,5 @@ +maven.checkstyle.properties=${basedir}/../checkstyle.xml +maven.checkstyle.header.file=${basedir}/../LICENSE.txt +maven.multiproject.type=jar + +module.path=config Added: incubator/directory/naming/trunk/config/project.xml Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/config/project.xml?view=auto&rev=122972 ============================================================================== --- (empty file) +++ incubator/directory/naming/trunk/config/project.xml Tue Dec 21 08:34:08 2004 @@ -0,0 +1,67 @@ +<?xml version="1.0"?> +<project> + <extend>${basedir}/../project.xml</extend> + <id>directory-naming-config</id> + <name>Naming Config</name> + + <dependencies> + + <!-- Compile time dependencies --> + <dependency> + <groupId>${pom.groupId}</groupId> + <artifactId>directory-naming-core</artifactId> + <version>${pom.currentVersion}</version> + </dependency> + <dependency> + <groupId>${pom.groupId}</groupId> + <artifactId>directory-naming-factory</artifactId> + <version>${pom.currentVersion}</version> + </dependency> + + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + <version>1.0.3</version> + </dependency> + + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>1.0.1</version> + </dependency> + + <dependency> + <groupId>commons-digester</groupId> + <artifactId>commons-digester</artifactId> + <version>1.4.1</version> + </dependency> + + <!-- Runtime dependencies - Digester --> + <dependency> + <groupId>commons-beanutils</groupId> + <artifactId>commons-beanutils</artifactId> + <version>1.6.1</version> + </dependency> + + <!-- Runtime dependencies - DBCP factories --> + <dependency> + <groupId>commons-dbcp</groupId> + <artifactId>commons-dbcp</artifactId> + <version>1.0</version> + </dependency> + + <dependency> + <groupId>commons-pool</groupId> + <artifactId>commons-pool</artifactId> + <version>1.0.1</version> + </dependency> + + <!-- Test dependencies - used to test database connection factories --> + <dependency> + <groupId>hsqldb</groupId> + <artifactId>hsqldb</artifactId> + <version>1.7.1</version> + </dependency> + </dependencies> +</project> + Added: incubator/directory/naming/trunk/config/src/conf/naming.dtd Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/config/src/conf/naming.dtd?view=auto&rev=122972 ============================================================================== --- (empty file) +++ incubator/directory/naming/trunk/config/src/conf/naming.dtd Tue Dec 21 08:34:08 2004 @@ -0,0 +1,22 @@ +<!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)> Added: 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=auto&rev=122972 ============================================================================== --- (empty file) +++ incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/Config.java Tue Dec 21 08:34:08 2004 @@ -0,0 +1,304 @@ +/* + * Copyright 1999,2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.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.naming.ResourceRef; + +/** + * Configuration classes. + * + * @author <a href="[EMAIL PROTECTED]">Brett Porter</a> + * @version $Id: Config.java,v 1.2 2003/12/01 02:02:45 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()); + addSubContextNames(name, sortedSubcontextNameSet); + } + for (Iterator i = resourceList.iterator(); i.hasNext();) + { + Resource r = (Resource) i.next(); + CompositeName name = new CompositeName(r.getName()); + addSubContextNames(name, sortedSubcontextNameSet); + } + } + + private void addSubContextNames(CompositeName name, Set sortedSubcontextNameSet) { + for (int j = 1; j <= name.size() - 1; j++) { + sortedSubcontextNameSet.add(name.getPrefix(j).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(Boolean.class.getName())) + { + return Boolean.valueOf(value); + } + else if (type.equals(Integer.class.getName())) + { + return Integer.valueOf(value); + } + else if (type.equals(Short.class.getName())) + { + return Short.valueOf(value); + } + else if (type.equals(Character.class.getName())) + { + return new Character(value.charAt(0)); + } + else if (type.equals(Double.class.getName())) + { + return Double.valueOf(value); + } + else if (type.equals(Float.class.getName())) + { + return Float.valueOf(value); + } + else if (type.equals(Byte.class.getName())) + { + return Byte.valueOf(value); + } + else if (type.equals(Long.class.getName())) + { + return Long.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(); + } + } +} + Added: incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/ParseException.java Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/ParseException.java?view=auto&rev=122972 ============================================================================== --- (empty file) +++ incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/ParseException.java Tue Dec 21 08:34:08 2004 @@ -0,0 +1,35 @@ +/* + * Copyright 1999,2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.naming.config; + +/** + * Exception during reading of an XML configuration file. + * + * @author <a href="[EMAIL PROTECTED]">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); + } +} Added: 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=auto&rev=122972 ============================================================================== --- (empty file) +++ incubator/directory/naming/trunk/config/src/java/org/apache/naming/config/XmlConfigurator.java Tue Dec 21 08:34:08 2004 @@ -0,0 +1,149 @@ +/* + * Copyright 1999,2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.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.naming.ContextAccessController; +import org.xml.sax.SAXException; + +/** + * Configure an in memory JNDI implementation using an XML configuration file. + * + * @author <a href="[EMAIL PROTECTED]">Brett Porter</a> + * @version $Id: XmlConfigurator.java,v 1.2 2003/12/01 02:02:45 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.naming.java.javaURLContextFactory"); + System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.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); + } + } + + 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); + } + } + +} Added: 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=auto&rev=122972 ============================================================================== --- (empty file) +++ incubator/directory/naming/trunk/config/src/test/org/apache/naming/config/XmlConfiguratorTest.java Tue Dec 21 08:34:08 2004 @@ -0,0 +1,129 @@ +/* + * Copyright 1999,2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.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="[EMAIL PROTECTED]">Brett Porter</a> + * @version $Id: XmlConfiguratorTest.java,v 1.2 2003/12/01 02:02:45 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); + + 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(); + } + } + } +} + Added: incubator/directory/naming/trunk/config/src/test/test-jndi.xml Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/config/src/test/test-jndi.xml?view=auto&rev=122972 ============================================================================== --- (empty file) +++ incubator/directory/naming/trunk/config/src/test/test-jndi.xml Tue Dec 21 08:34:08 2004 @@ -0,0 +1,35 @@ +<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/mytruebool" value="true" type="java.lang.Boolean" /> + <environment name="config/myfalsebool" value="false" type="java.lang.Boolean" /> + <environment name="config/port" value="80" type="java.lang.Integer" /> + + <environment name="jdbc/config/pool/user" value="dbuser" type="java.lang.String" /> + + <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> Deleted: /incubator/directory/naming/trunk/factory/src/test/test-jndi.xml Url: http://svn.apache.org/viewcvs/incubator/directory/naming/trunk/factory/src/test/test-jndi.xml?view=auto&rev=122971 ==============================================================================
