You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ep...@apache.org on 2003/06/20 03:12:50 UTC
cvs commit: jakarta-commons-sandbox/configuration/src/test-cactus/org/apache/commons/configuration TestConfigurationFactoryWithJNDI.java
epugh 2003/06/19 18:12:49
Modified: configuration/conf digesterRules.xml
configuration build.xml project.xml
configuration/src/test-cactus/testapp/WEB-INF web.xml
configuration/src/java/org/apache/commons/configuration
JNDIConfiguration.java CompositeConfiguration.java
ConfigurationFactory.java
configuration/xdocs tasks.xml
configuration/src/test-cactus/org/apache/commons/configuration
TestConfigurationFactoryWithJNDI.java
Log:
Fixs for:
1) JNDIConfiguration.getKeys()
2) build.xml
3) Style cleanups
Revision Changes Path
1.2 +1 -1 jakarta-commons-sandbox/configuration/conf/digesterRules.xml
Index: digesterRules.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/configuration/conf/digesterRules.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- digesterRules.xml 18 Mar 2003 21:45:51 -0000 1.1
+++ digesterRules.xml 20 Jun 2003 01:12:48 -0000 1.2
@@ -14,7 +14,7 @@
<call-method-rule methodname="load"/>
</pattern>
<pattern value="configuration/jndi">
- <object-create-rule classname="org.apache.commons.configuration.JNIDConfiguration"/>
+ <object-create-rule classname="org.apache.commons.configuration.JNDIConfiguration"/>
<set-properties-rule/>
<set-next-rule methodname="addConfiguration" paramtype="org.apache.commons.configuration.Configuration"/>
1.11 +137 -88 jakarta-commons-sandbox/configuration/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/configuration/build.xml,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- build.xml 14 Jan 2003 03:53:12 -0000 1.10
+++ build.xml 20 Jun 2003 01:12:49 -0000 1.11
@@ -1,138 +1,187 @@
<?xml version="1.0" encoding="UTF-8"?>
-<!-- ===================================================================== -->
-<!-- -->
-<!-- $Id$ -->
-<!-- -->
-<!-- ===================================================================== -->
+<!--
+ build.xml generated by maven from project.xml version 1.0-dev-3.20030607.194155
+ on date June 19 2003, time 1735
+-->
<project default="jar" name="commons-configuration" basedir=".">
-
- <property name="defaulttargetdir" value="target"/>
- <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="target/docs/apidocs"/>
- <property name="final.name" value="commons-configuration-0.8.1"/>
-
+ <property name="defaulttargetdir" value="target">
+ </property>
+ <property name="libdir" value="target/lib">
+ </property>
+ <property name="classesdir" value="target/classes">
+ </property>
+ <property name="testclassesdir" value="target/test-classes">
+ </property>
+ <property name="testreportdir" value="target/test-reports">
+ </property>
+ <property name="distdir" value="dist">
+ </property>
+ <property name="javadocdir" value="dist/docs/api">
+ </property>
+ <property name="final.name" value="commons-configuration-1.0-dev-3.20030607.194155">
+ </property>
<target name="init" description="o Initializes some properties">
-
+ <mkdir dir="${libdir}">
+ </mkdir>
<condition property="noget">
- <equals arg2="only" arg1="${build.sysclasspath}"></equals>
+ <equals arg2="only" arg1="${build.sysclasspath}">
+ </equals>
</condition>
</target>
-
- <target name="jar" description="o Create the jar" depends="get-deps">
-
- <mkdir dir="${classesdir}"></mkdir>
-
+ <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>
+ <pathelement location="src/java">
+ </pathelement>
</src>
<classpath>
- <fileset dir="lib">
- <include name="*.jar"></include>
+ <fileset dir="${libdir}">
+ <include name="*.jar">
+ </include>
</fileset>
</classpath>
</javac>
-
-
- <jar jarfile="target/${final.name}.jar" excludes="**/package.html" basedir="${classesdir}"></jar>
-
+ <copy todir="${classesdir}">
+ <fileset dir="conf">
+ <include name="*">
+ </include>
+ </fileset>
+ </copy>
+ <copy todir="${testclassesdir}">
+ <fileset dir="conf">
+ <include name="*.properties">
+ </include>
+ </fileset>
+ </copy>
+ </target>
+ <target name="jar" description="o Create the jar" depends="compile,test">
+ <jar jarfile="target/${final.name}.jar" excludes="**/package.html" basedir="${classesdir}">
+ </jar>
</target>
-
<target name="clean" description="o Clean up the generated directories">
- <delete dir="${defaulttargetdir}"></delete>
- <delete dir="${distdir}"></delete>
+ <delete dir="${defaulttargetdir}">
+ </delete>
+ <delete dir="${distdir}">
+ </delete>
</target>
-
<target name="dist" description="o Create a distribution" depends="jar, javadoc">
- <mkdir dir="dist"></mkdir>
+ <mkdir dir="dist">
+ </mkdir>
<copy todir="dist">
- <fileset dir="${defaulttargetdir}"></fileset>
+ <fileset dir="${defaulttargetdir}" includes="*.jar">
+ </fileset>
+ <fileset dir="${basedir}" includes="LICENSE*, README*">
+ </fileset>
</copy>
</target>
-
- <target name="test" description="o Run the test cases" depends="compile-tests">
- <mkdir dir="${testreportdir}"></mkdir>
- <junit dir="./" printSummary="yes" fork="true" haltonerror="true">
- <sysproperty key="basedir" value="src/test"></sysproperty>
- <formatter type="xml"></formatter>
- <formatter usefile="true" type="plain"></formatter>
+ <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="lib">
- <include name="*.jar"></include>
+ <fileset dir="${libdir}">
+ <include name="*.jar">
+ </include>
</fileset>
- <pathelement location="target/${final.name}.jar"></pathelement>
- <pathelement path="${testclassesdir}"></pathelement>
+ <pathelement path="${testclassesdir}">
+ </pathelement>
+ <pathelement path="${classesdir}">
+ </pathelement>
</classpath>
<batchtest todir="${testreportdir}">
- <fileset dir="src/test"
- excludes="**/TestBasePropertiesConfiguration.java">
+ <fileset dir="src/test">
+ <include name="**/*Test*.java">
+ </include>
+ <exclude name="**/TestBasePropertiesConfiguration.java">
+ </exclude>
+ <exclude name="**/TestClassPropertiesConfiguration.java">
+ </exclude>
</fileset>
</batchtest>
</junit>
</target>
-
- <target name="compile-tests" depends="jar">
- <mkdir dir="${testclassesdir}"></mkdir>
+ <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>
+ <pathelement location="src/test">
+ </pathelement>
</src>
<classpath>
- <fileset dir="lib">
- <include name="*.jar"></include>
+ <fileset dir="${libdir}">
+ <include name="*.jar">
+ </include>
</fileset>
- <pathelement location="target/${final.name}.jar"></pathelement>
+ <pathelement path="${classesdir}">
+ </pathelement>
</classpath>
</javac>
-
- <copy todir="${testclassesdir}/org/apache/commons/configuration">
- <fileset dir="conf" includes="*.properties"/>
- </copy>
-
</target>
-
<target name="javadoc" description="o Generate javadoc" depends="jar">
-
-
- <mkdir dir="${javadocdir}"></mkdir>
-
-
+ <mkdir dir="${javadocdir}">
+ </mkdir>
<tstamp>
- <format pattern="2001-yyyy" property="year"></format>
+ <format pattern="2001-yyyy" property="year">
+ </format>
</tstamp>
-
- <property name="copyright" value="Copyright &copy; Apache Software Foundation. All Rights Reserved."/>
-
- <property name="title" value="${final.name} API"/>
-
+ <property name="copyright" value="Copyright &copy; Apache Software Foundation. All Rights Reserved.">
+ </property>
+ <property name="title" value="commons-configuration 1.0-dev-3.20030607.194155 API">
+ </property>
<javadoc use="true" private="true" destdir="${javadocdir}" author="true" version="true" sourcepath="src/java" packagenames="org.apache.commons.configuration.*">
<classpath>
- <fileset dir="lib">
- <include name="*.jar"></include>
+ <fileset dir="${libdir}">
+ <include name="*.jar">
+ </include>
</fileset>
- <pathelement location="target/${final.name}.jar"></pathelement>
+ <pathelement location="target/${final.name}.jar">
+ </pathelement>
</classpath>
</javadoc>
-
</target>
-
<target name="get-deps" unless="noget" depends="init">
-
- <mkdir dir="lib"></mkdir>
-
- <get dest="lib/commons-collections-2.0.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-collections/jars/commons-collections-2.0.jar"></get>
- <get dest="lib/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>
- <get dest="lib/dom4j-1.3.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/dom4j/jars/dom4j-1.3.jar"></get>
- <get dest="lib/junit-3.8.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/junit/jars/junit-3.8.1.jar"></get>
- <get dest="lib/xerces-2.2.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/xerces/jars/xerces-2.2.1.jar"></get>
- <get dest="lib/xml-apis-2.2.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/xml-apis/jars/xml-apis-2.2.1.jar"></get>
-
+ <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>
+ <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>
+ <get dest="${libdir}/commons-logging-1.0.2.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-logging/jars/commons-logging-1.0.2.jar">
+ </get>
+ <get dest="${libdir}/dom4j-1.3.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/dom4j/jars/dom4j-1.3.jar">
+ </get>
+ <get dest="${libdir}/commons-digester-1.5.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-digester/jars/commons-digester-1.5.jar">
+ </get>
+ <get dest="${libdir}/commons-beanutils-1.6.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-beanutils/jars/commons-beanutils-1.6.jar">
+ </get>
+ <get dest="${libdir}/xerces-2.2.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/xerces/jars/xerces-2.2.1.jar">
+ </get>
+ <get dest="${libdir}/xml-apis-2.0.2.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/xml-apis/jars/xml-apis-2.0.2.jar">
+ </get>
+ <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>
+ <get dest="${libdir}/ant-1.5.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/ant/jars/ant-1.5.jar">
+ </get>
+ <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">
+ </get>
+ </target>
+ <target name="install-maven">
+ <get dest="${user.home}/maven-install-latest.jar" usetimestamp="true" src="${repo}/maven/maven-install-latest.jar">
+ </get>
+ <unjar dest="${maven.home}" src="${user.home}/maven-install-latest.jar">
+ </unjar>
</target>
-
</project>
-
1.27 +10 -4 jakarta-commons-sandbox/configuration/project.xml
Index: project.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/configuration/project.xml,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- project.xml 7 Jun 2003 17:57:28 -0000 1.26
+++ project.xml 20 Jun 2003 01:12:49 -0000 1.27
@@ -101,7 +101,7 @@
<dependencies>
<dependency>
<id>commons-collections</id>
- <version>2.1-dev</version>
+ <version>2.1</version>
<properties>
<war.bundle.jar>true</war.bundle.jar>
</properties>
@@ -132,7 +132,7 @@
</dependency>
<dependency>
<id>commons-digester</id>
- <version>1.4.1</version>
+ <version>1.5</version>
<properties>
<war.bundle.jar>true</war.bundle.jar>
</properties>
@@ -155,12 +155,18 @@
<id>xerces</id>
<version>2.2.1</version>
<url>http://xml.apache.org/xerces2-j/</url>
+ <properties>
+ <war.bundle.jar>true</war.bundle.jar>
+ </properties>
</dependency>
<dependency>
<id>xml-apis</id>
- <version>1.0.b2</version>
+ <version>2.0.2</version>
<url>http://xml.apache.org/commons/</url>
+ <properties>
+ <war.bundle.jar>true</war.bundle.jar>
+ </properties>
</dependency>
</dependencies>
1.2 +16 -0 jakarta-commons-sandbox/configuration/src/test-cactus/testapp/WEB-INF/web.xml
Index: web.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/configuration/src/test-cactus/testapp/WEB-INF/web.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- web.xml 28 Feb 2003 20:31:48 -0000 1.1
+++ web.xml 20 Jun 2003 01:12:49 -0000 1.2
@@ -89,4 +89,20 @@
<env-entry-value>jndivalue2</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
+ <env-entry>
+ <env-entry-name>test/jndi</env-entry-name>
+ <env-entry-value>only_jndi</env-entry-value>
+ <env-entry-type>java.lang.String</env-entry-type>
+ </env-entry>
+ <env-entry>
+ <env-entry-name>test/deep/somekey</env-entry-name>
+ <env-entry-value>somevalue</env-entry-value>
+ <env-entry-type>java.lang.String</env-entry-type>
+ </env-entry>
+ <env-entry>
+ <env-entry-name>test/deep/verydeep/averydeepname</env-entry-name>
+ <env-entry-value>averydeepvalue</env-entry-value>
+ <env-entry-type>java.lang.String</env-entry-type>
+ </env-entry>
+
</web-app>
1.7 +164 -30 jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/JNDIConfiguration.java
Index: JNDIConfiguration.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/JNDIConfiguration.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- JNDIConfiguration.java 7 Jun 2003 19:30:43 -0000 1.6
+++ JNDIConfiguration.java 20 Jun 2003 01:12:49 -0000 1.7
@@ -54,17 +54,22 @@
* <http://www.apache.org/>.
*/
+import java.util.ArrayList;
import java.util.Iterator;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.List;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Vector;
+import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
/**
* This Configuration class allows you to interface with a JNDI datasource.
@@ -74,9 +79,10 @@
*/
public class JNDIConfiguration implements Configuration
{
+ private static Log log = LogFactory.getLog(JNDIConfiguration.class);
private String prefix;
private Context envCtx;
- private Map clearedProperties = new HashMap();
+ private List clearedProperties = new ArrayList();
/**
* Creates an empty JNDIConfiguration object which can then
@@ -98,6 +104,53 @@
}
/**
+ * This method recursive traverse the JNDI tree, looking for Context objects.
+ * When it finds them, it traverses them as well. Otherwise it just adds the
+ * values to the list of keys found.
+ * @param keys All the keys that have been found.
+ * @param enum An enumeration of all the elements found at a specific context
+ * @param key What key we are building on.
+ * @throws NamingException If JNDI has an issue.
+ */
+ private void recursiveGetKeys(
+ List keys,
+ NamingEnumeration enum,
+ String key)
+ throws NamingException
+ {
+ while (enum.hasMoreElements())
+ {
+
+ Binding binding = (Binding) enum.next();
+
+ StringBuffer newKey = new StringBuffer();
+ newKey.append(key);
+ if (newKey.length() > 0)
+ {
+ newKey.append(".");
+ }
+ newKey.append(binding.getName());
+
+ if (binding.getObject() instanceof Context)
+ {
+ Context c = (Context) binding.getObject();
+ NamingEnumeration enum2 = c.listBindings("");
+
+ recursiveGetKeys(keys, enum2, newKey.toString());
+ } else
+ {
+ if (!keys.contains(newKey.toString()))
+ {
+ keys.add(newKey.toString());
+ }
+
+ }
+
+ }
+
+ }
+
+ /**
* Get the list of the keys contained in the configuration
* repository.
*
@@ -105,20 +158,99 @@
*/
public Iterator getKeys()
{
- throw new Error("This operation is not supported");
+ return getKeys("");
+
}
/**
* Get the list of the keys contained in the configuration
- * repository.
+ * repository that match a passed in beginning pattern.
*
+ * @param key the key pattern to match on.
* @return An Iterator.
*/
public Iterator getKeys(String key)
{
+ List keys = new ArrayList();
- throw new Error("This operation is not supported");
+ try
+ {
+ String[] splitKeys = StringUtils.split(key, ".");
+ for (int i = 0; i < splitKeys.length; i++)
+ {
+ keys.add(splitKeys[i]);
+ }
+
+ Context context = null;
+ if (keys.size() == 0)
+ {
+ context = getContext();
+ } else
+ {
+ context =
+ getStartingContextPoint(
+ keys,
+ getContext().listBindings(""));
+ }
+
+ if (context != null)
+ {
+ NamingEnumeration enum = context.listBindings("");
+ recursiveGetKeys(keys, enum, key);
+ }
+
+ } catch (NamingException ne)
+ {
+ log.warn(ne);
+ }
+ return keys.iterator();
}
+
+ /**
+ * Because JNDI is based on a tree configuration, we need to filter down the
+ * tree, till we find the Context specified by the key to start from.
+ * Otherwise return null.
+ *
+ * @param The key (or name) of the Context we are looking to start from.
+ * @return The context at that key's location in the JNDI tree, or null if not found
+ * @throws NamingException if JNDI has an issue
+ */
+ private Context getStartingContextPoint(List keys, NamingEnumeration enum)
+ throws NamingException
+ {
+
+ String keyToSearchFor = (String) keys.get(0);
+ log.debug("Key to search for is " + keyToSearchFor);
+ while (enum.hasMoreElements())
+ {
+ Binding binding = (Binding) enum.next();
+ log.debug(
+ "Binding for name: "
+ + binding.getName()
+ + ", object:"
+ + binding.getObject()
+ + ", class:"
+ + binding.getClassName());
+ if (binding.getObject() instanceof Context
+ && binding.getName().equals(keyToSearchFor))
+ {
+ keys.remove(0);
+ Context c = (Context) binding.getObject();
+ if (keys.size() > 0)
+ {
+ return getStartingContextPoint(keys, c.listBindings(""));
+ } else
+ {
+ return c;
+ }
+
+ }
+
+ }
+ return null;
+
+ }
+
/**
* Get a list of properties associated with the given
* configuration key.
@@ -176,7 +308,10 @@
*/
public void clearProperty(String key)
{
- clearedProperties.put(key, key);
+ if (!clearedProperties.contains(key))
+ {
+ clearedProperties.add(key);
+ }
}
/**
@@ -185,23 +320,18 @@
*/
public boolean containsKey(String key)
{
- if (clearedProperties.containsKey(key))
+ if (clearedProperties.contains(key))
{
return false;
}
-
+
key = StringUtils.replace(key, ".", "/");
try
{
- if (envCtx == null)
- {
- Context initCtx = new InitialContext();
- envCtx = (Context) initCtx.lookup(getPrefix());
- }
- String value = (String) envCtx.lookup(key);
+
+ String value = (String) getContext().lookup(key);
return true;
- }
- catch (javax.naming.NamingException ne)
+ } catch (javax.naming.NamingException ne)
{
return false;
}
@@ -589,10 +719,10 @@
*/
public String getString(String key, String defaultValue)
{
- try {
+ try
+ {
return getValueFromJNDI(key);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
}
@@ -658,28 +788,32 @@
private String getValueFromJNDI(String key)
{
- if (clearedProperties.containsKey(key))
+ if (clearedProperties.contains(key))
{
return null;
}
try
{
key = StringUtils.replace(key, ".", "/");
- if (envCtx == null)
- {
- Context initCtx = new InitialContext();
- envCtx = (Context) initCtx.lookup(getPrefix());
- }
- String value = (String) envCtx.lookup(key);
+
+ String value = (String) getContext().lookup(key);
return value;
- }
- catch (Exception e)
+ } catch (Exception e)
{
throw new NoSuchElementException(
'\'' + key + "' doesn't map to an existing object");
+ }
+ }
+ private Context getContext() throws NamingException
+ {
+ if (envCtx == null)
+ {
+ Context initCtx = new InitialContext();
+ envCtx = (Context) initCtx.lookup(getPrefix());
}
+ return envCtx;
}
}
1.12 +19 -29 jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/CompositeConfiguration.java
Index: CompositeConfiguration.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/CompositeConfiguration.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- CompositeConfiguration.java 18 Jun 2003 17:45:50 -0000 1.11
+++ CompositeConfiguration.java 20 Jun 2003 01:12:49 -0000 1.12
@@ -55,7 +55,6 @@
*/
import java.util.ArrayList;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
@@ -146,7 +145,8 @@
for (Iterator j = config.getKeys(); j.hasNext();)
{
String key = (String) j.next();
- if (!keys.contains(key)){
+ if (!keys.contains(key))
+ {
keys.add(key);
}
}
@@ -168,9 +168,10 @@
for (Iterator j = config.getKeys(key); j.hasNext();)
{
String newKey = (String) j.next();
- if (!keys.contains(newKey)){
- keys.add(newKey);
- }
+ if (!keys.contains(newKey))
+ {
+ keys.add(newKey);
+ }
}
}
return keys.iterator();
@@ -353,8 +354,7 @@
{
return getFirstMatchingConfig(key).getBoolean(key, defaultValue);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
}
@@ -411,8 +411,7 @@
{
return getFirstMatchingConfig(key).getByte(key, defaultValue);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
}
@@ -469,8 +468,7 @@
{
return getFirstMatchingConfig(key).getDouble(key, defaultValue);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
}
@@ -510,8 +508,7 @@
{
return getFirstMatchingConfig(key).getFloat(key, defaultValue);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
}
@@ -568,8 +565,7 @@
{
return getFirstMatchingConfig(key).getInteger(key, defaultValue);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
}
@@ -626,8 +622,7 @@
{
return getFirstMatchingConfig(key).getLong(key, defaultValue);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
}
@@ -684,8 +679,7 @@
try
{
return getFirstMatchingConfig(key).getShort(key, defaultValue);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
}
@@ -701,7 +695,7 @@
*/
public String getString(String key)
{
-
+
return getString(key, null);
}
@@ -720,8 +714,7 @@
{
return getFirstMatchingConfig(key).getString(key, defaultValue);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
@@ -743,8 +736,7 @@
{
return getFirstMatchingConfig(key).getStringArray(key);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return new String[0];
}
@@ -763,8 +755,7 @@
try
{
return getFirstMatchingConfig(key).getVector(key);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return new Vector();
}
@@ -785,8 +776,7 @@
{
return getFirstMatchingConfig(key).getVector(key, defaultValue);
- }
- catch (NoSuchElementException nsee)
+ } catch (NoSuchElementException nsee)
{
return defaultValue;
}
1.7 +83 -26 jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/ConfigurationFactory.java
Index: ConfigurationFactory.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/ConfigurationFactory.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ConfigurationFactory.java 7 Jun 2003 19:30:43 -0000 1.6
+++ ConfigurationFactory.java 20 Jun 2003 01:12:49 -0000 1.7
@@ -131,15 +131,15 @@
{
Digester digester;
CompositeConfiguration configuration = new CompositeConfiguration();
- InputStream input = new FileInputStream(new File(getConfigurationFileName()));
+ InputStream input =
+ new FileInputStream(new File(getConfigurationFileName()));
if (getDigesterRules() == null)
{
digester = new Digester();
configureNamespace(digester);
initDefaultDigesterRules(digester);
- }
- else
+ } else
{
digester = DigesterLoader.createDigester(getDigesterRules());
@@ -156,8 +156,7 @@
{
digester.parse(input);
input.close();
- }
- catch (SAXException e)
+ } catch (SAXException e)
{
log.error("SAX Exception caught", e);
throw e;
@@ -212,11 +211,20 @@
protected void initDefaultDigesterRules(Digester digester)
{
// Properties and DOM4J need a base path. Supply it with inner classes
- setupDigesterInstance(digester, "configuration/properties", new BasePathConfigurationFactory(PropertiesConfiguration.class));
- setupDigesterInstance(digester, "configuration/dom4j", new BasePathConfigurationFactory(DOM4JConfiguration.class));
-
+ setupDigesterInstance(
+ digester,
+ "configuration/properties",
+ new BasePathConfigurationFactory(PropertiesConfiguration.class));
+ setupDigesterInstance(
+ digester,
+ "configuration/dom4j",
+ new BasePathConfigurationFactory(DOM4JConfiguration.class));
+
// JNDI can be instantiated directly.
- setupDigesterInstance(digester, "configuration/jndi", JNDIConfiguration.class);
+ setupDigesterInstance(
+ digester,
+ "configuration/jndi",
+ new JNDIConfigurationFactory(JNDIConfiguration.class));
}
/**
@@ -228,28 +236,40 @@
* @param factory An ObjectCreationFactory Instance to use for creating new
* objects
*/
- protected void setupDigesterInstance(Digester digester, String matchString, ObjectCreationFactory factory)
+ protected void setupDigesterInstance(
+ Digester digester,
+ String matchString,
+ ObjectCreationFactory factory)
{
digester.addFactoryCreate(matchString, factory);
digester.addSetProperties(matchString);
digester.addCallMethod(matchString, "load");
- digester.addSetNext(matchString, "addConfiguration", Configuration.class.getName());
+ digester.addSetNext(
+ matchString,
+ "addConfiguration",
+ Configuration.class.getName());
}
/**
- * Sets up a configuration instance that can directly construct a new
+ * Sets up a configuration instance that needs an JNDIConfiguration Factory to
* object.
*
* @param digester The current digester
* @param matchString The pattern to match with this Rule
- * @param clazz A Class object that describes the wanted class.
+ * @param factory An JNDIConfigurationFactory Instance to use for creating new
+ * JNDIConfiguration objects
*/
- protected void setupDigesterInstance(Digester digester,
- String matchString, Class clazz)
+ protected void setupDigesterInstance(
+ Digester digester,
+ String matchString,
+ JNDIConfigurationFactory factory)
{
- digester.addFactoryCreate(matchString, clazz);
+ digester.addFactoryCreate(matchString, factory);
digester.addSetProperties(matchString);
- digester.addSetNext(matchString, "addConfiguration", Configuration.class.getName());
+ digester.addSetNext(
+ matchString,
+ "addConfiguration",
+ Configuration.class.getName());
}
/**
@@ -286,8 +306,7 @@
{
digester.setNamespaceAware(true);
digester.setRuleNamespaceURI(getDigesterRuleNamespaceURI());
- }
- else
+ } else
{
digester.setNamespaceAware(false);
}
@@ -324,8 +343,8 @@
*
*/
public class BasePathConfigurationFactory
- extends AbstractObjectCreationFactory
- implements ObjectCreationFactory
+ extends AbstractObjectCreationFactory
+ implements ObjectCreationFactory
{
/** Actual class to use. */
private Class clazz;
@@ -339,7 +358,7 @@
{
this.clazz = clazz;
}
-
+
/**
* Gets called by the digester. We ignore the attributes
*
@@ -347,12 +366,50 @@
*
* @throws Exception Couldn't instantiate the requested object.
*/
- public Object createObject(Attributes attributes)
- throws Exception
+ public Object createObject(Attributes attributes) throws Exception
{
- BasePathConfiguration bpc = (BasePathConfiguration) clazz.newInstance();
+ BasePathConfiguration bpc =
+ (BasePathConfiguration) clazz.newInstance();
bpc.setBasePath(getBasePath());
return bpc;
}
}
+
+ /**
+ * A tiny inner class that allows the Configuration Factory to
+ * let the digester construct JNDIPathConfiguration objects.
+ *
+ */
+ public class JNDIConfigurationFactory
+ extends AbstractObjectCreationFactory
+ implements ObjectCreationFactory
+ {
+ /** Actual class to use. */
+ private Class clazz;
+
+ /**
+ * C'tor
+ *
+ * @param clazz The class which we should instantiate.
+ */
+ public JNDIConfigurationFactory(Class clazz)
+ {
+ this.clazz = clazz;
+ }
+
+ /**
+ * Gets called by the digester. We ignore the attributes
+ *
+ * @param attributes ignored
+ *
+ * @throws Exception Couldn't instantiate the requested object.
+ */
+ public Object createObject(Attributes attributes) throws Exception
+ {
+ JNDIConfiguration jndi = (JNDIConfiguration) clazz.newInstance();
+
+ return jndi;
+ }
+ }
+
}
1.5 +6 -0 jakarta-commons-sandbox/configuration/xdocs/tasks.xml
Index: tasks.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/configuration/xdocs/tasks.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- tasks.xml 18 Jun 2003 17:45:50 -0000 1.4
+++ tasks.xml 20 Jun 2003 01:12:49 -0000 1.5
@@ -57,6 +57,12 @@
expect the order that keys are returned to be the order they
are added into the properties file.
</li>
+ <li>
+ <strong>JNDIConfiguration.getKeys() Addition</strong>
+ The JNDIConfiguration.getKeys() method was returning an unsupported
+ operation error. However, this is an important method to have
+ supported.
+ </li>
</ul>
</subsection>
</section>
1.3 +66 -12 jakarta-commons-sandbox/configuration/src/test-cactus/org/apache/commons/configuration/TestConfigurationFactoryWithJNDI.java
Index: TestConfigurationFactoryWithJNDI.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/configuration/src/test-cactus/org/apache/commons/configuration/TestConfigurationFactoryWithJNDI.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestConfigurationFactoryWithJNDI.java 19 Mar 2003 10:33:00 -0000 1.2
+++ TestConfigurationFactoryWithJNDI.java 20 Jun 2003 01:12:49 -0000 1.3
@@ -57,20 +57,16 @@
*/
import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
-import java.util.*;
+import java.util.Iterator;
+import java.util.List;
-import junit.framework.TestCase;
-
-import org.apache.commons.digester.Digester;
+import org.apache.cactus.ServletTestCase;
+import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.xml.sax.SAXException;
-
-import org.apache.cactus.ServletTestCase;
-public class TestConfigurationFactoryWithJNDI extends ServletTestCase {
+public class TestConfigurationFactoryWithJNDI extends ServletTestCase
+{
private File testDigesterFile = new File("conf/testDigesterConfigurationWJNDI.xml");
private static Log log = LogFactory.getLog(TestConfigurationFactoryWithJNDI.class);
@@ -99,7 +95,65 @@
assertEquals("Make sure the JNDI config overwrites everything else!","80", compositeConfiguration.getString("test.short"));
}
-
-
+
+ /**
+ * Verify the getKeys() method works.
+ * @throws Exception
+ */
+ public void testGetKeys() throws Exception
+ {
+ ConfigurationFactory cf = new ConfigurationFactory();
+ cf.setConfigurationFileName(testDigesterFile.toString());
+
+ Configuration c = cf.getConfiguration();
+
+ List iteratedList = IteratorUtils.toList(c.getKeys());
+ assertTrue(iteratedList.contains("test.jndi"));
+ }
+
+ /**
+ * Test that a simple key works with JNDI
+ * @throws Exception
+ */
+ public void testGetKeysWithString() throws Exception
+ {
+ String KEY = "test";
+ ConfigurationFactory cf = new ConfigurationFactory();
+ cf.setConfigurationFileName(testDigesterFile.toString());
+
+ Configuration c = cf.getConfiguration();
+
+ List iteratedList = IteratorUtils.toList(c.getKeys(KEY));
+
+ assertTrue("Size:" + iteratedList.size(),iteratedList.size()>0);
+ assertTrue(iteratedList.contains("test.jndi"));
+ for (Iterator i = iteratedList.iterator();i.hasNext();){
+ String foundKey = (String)i.next();
+ assertTrue(foundKey.startsWith(KEY));
+ }
+ }
+
+ /**
+ * Verify that if a key is made of multiple parts, we still find
+ * the correct JNDI Context.
+ * @throws Exception
+ */
+ public void testGetKeysWithString2() throws Exception
+ {
+ String KEY = "test.deep";
+ ConfigurationFactory cf = new ConfigurationFactory();
+ cf.setConfigurationFileName(testDigesterFile.toString());
+
+ Configuration c = cf.getConfiguration();
+
+ List iteratedList = IteratorUtils.toList(c.getKeys(KEY));
+
+ assertTrue("Size:" + iteratedList.size(),iteratedList.size()==2);
+ assertTrue(iteratedList.contains("test.deep.somekey"));
+ for (Iterator i = iteratedList.iterator();i.hasNext();){
+ String foundKey = (String)i.next();
+ assertTrue(foundKey.startsWith(KEY));
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org