epugh 2003/03/21 21:19:51
Modified: configuration/src/java/org/apache/commons/configuration
CompositeConfiguration.java
ConfigurationFactory.java DOM4JConfiguration.java
JNDIConfiguration.java PropertiesConfiguration.java
Log:
Finally got these to work with ConfigurationFactory and basePath's
Revision Changes Path
1.8 +11 -3
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.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- CompositeConfiguration.java 20 Mar 2003 08:32:52 -0000 1.7
+++ CompositeConfiguration.java 22 Mar 2003 05:19:50 -0000 1.8
@@ -697,7 +697,8 @@
*/
public String getString(String key)
{
- return getFirstMatchingConfig(key).getString(key);
+
+ return getString(key,null);
}
/**
@@ -755,7 +756,14 @@
*/
public Vector getVector(String key)
{
- return getFirstMatchingConfig(key).getVector(key);
+ try {
+
+ return getFirstMatchingConfig(key).getVector(key);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return new Vector();
+ }
}
/**
1.2 +43 -35
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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ConfigurationFactory.java 18 Mar 2003 21:43:17 -0000 1.1
+++ ConfigurationFactory.java 22 Mar 2003 05:19:50 -0000 1.2
@@ -60,6 +60,7 @@
import java.net.URL;
import org.apache.commons.digester.Digester;
+import org.apache.commons.digester.ObjectParamRule;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -80,25 +81,26 @@
{
/** The XML file with the details about the configuration to load */
private String configurationFileName;
-
+
+ /** The basePath to prefix file paths for file based property files. */
+ private String basePath;
+
/** static logger */
private static Log log = LogFactory.getLog(ConfigurationFactory.class);
-
+
/** URL for xml digester rules file */
private URL digesterRules;
-
+
/** The digester namespace to parse */
private String digesterRuleNamespaceURI;
public ConfigurationFactory()
- {}
+ {}
public Configuration getConfiguration() throws Exception
{
- InputStream input =
- new FileInputStream(new File(getConfigurationFileName()));
- CompositeConfiguration compositeConfiguration =
- new CompositeConfiguration();
+ InputStream input = new FileInputStream(new
File(getConfigurationFileName()));
+ CompositeConfiguration compositeConfiguration = new
CompositeConfiguration();
Digester digester = null;
@@ -169,34 +171,25 @@
{
// Initialize a new Digester instance
- digester.addObjectCreate(
- "configuration/properties",
- "org.apache.commons.configuration.PropertiesConfiguration");
+ digester.addObjectCreate("configuration/properties",
"org.apache.commons.configuration.PropertiesConfiguration");
digester.addSetProperties("configuration/properties");
digester.addCallMethod("configuration/properties", "load");
- digester.addSetNext(
- "configuration/properties",
- "addConfiguration",
- "org.apache.commons.configuration.Configuration");
-
- digester.addObjectCreate(
- "configuration/dom4j",
- "org.apache.commons.configuration.DOM4JConfiguration");
+ digester.addCallMethod("configuration/properties", "setBasePath", 1, new
String[] { "java.lang.String" });
+ ObjectParamRule opr = new ObjectParamRule(0, getBasePath());
+ digester.addRule("configuration/properties", opr);
+ digester.addSetNext("configuration/properties", "addConfiguration",
"org.apache.commons.configuration.Configuration");
+
+ digester.addObjectCreate("configuration/dom4j",
"org.apache.commons.configuration.DOM4JConfiguration");
digester.addSetProperties("configuration/dom4j");
digester.addCallMethod("configuration/dom4j", "load");
- digester.addSetNext(
- "configuration/dom4j",
- "addConfiguration",
- "org.apache.commons.configuration.Configuration");
-
- digester.addObjectCreate(
- "configuration/jndi",
- "org.apache.commons.configuration.JNDIConfiguration");
+ digester.addCallMethod("configuration/dom4j", "setBasePath", 1, new
String[] { "java.lang.String" });
+ ObjectParamRule opr2 = new ObjectParamRule(0, getBasePath());
+ digester.addRule("configuration/dom4j", opr2);
+ digester.addSetNext("configuration/dom4j", "addConfiguration",
"org.apache.commons.configuration.Configuration");
+
+ digester.addObjectCreate("configuration/jndi",
"org.apache.commons.configuration.JNDIConfiguration");
digester.addSetProperties("configuration/jndi");
- digester.addSetNext(
- "configuration/jndi",
- "addConfiguration",
- "org.apache.commons.configuration.Configuration");
+ digester.addSetNext("configuration/jndi", "addConfiguration",
"org.apache.commons.configuration.Configuration");
return digester;
}
@@ -219,9 +212,7 @@
this.digesterRuleNamespaceURI = digesterRuleNamespaceURI;
}
- private Digester configureDigester(
- CompositeConfiguration compositeConfiguration,
- Digester digester)
+ private Digester configureDigester(CompositeConfiguration
compositeConfiguration, Digester digester)
{
digester.push(compositeConfiguration);
if (getDigesterRuleNamespaceURI() != null)
@@ -237,6 +228,23 @@
digester.setValidating(false);
return digester;
+ }
+
+ /**
+ * @return String
+ */
+ public String getBasePath()
+ {
+ return basePath;
+ }
+
+ /**
+ * Sets the basePath.
+ * @param basePath The basePath to set
+ */
+ public void setBasePath(String basePath)
+ {
+ this.basePath = basePath;
}
}
1.3 +77 -20
jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/DOM4JConfiguration.java
Index: DOM4JConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/DOM4JConfiguration.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DOM4JConfiguration.java 18 Mar 2003 21:41:56 -0000 1.2
+++ DOM4JConfiguration.java 22 Mar 2003 05:19:50 -0000 1.3
@@ -93,16 +93,22 @@
private static final char ATTRIB_MARKER = '@';
private static final String ATTRIB_START_MARKER = "[" + ATTRIB_MARKER;
+ /** File separator. */
+ protected String fileSeparator = System.getProperty("file.separator");
+
/**
* For consistency with properties files. Access nodes via an
* "A.B.C" notation.
*/
private static final String NODE_DELIMITER = ".";
+ /** The base path to our data source */
+ private String basePath;
+
/**
* A handle to our data source.
*/
- private File file;
+ private String fileName;
/**
* The XML document from our data source.
@@ -159,8 +165,7 @@
private static File resourceURLToFile(String resource)
{
- URL confURL =
- DOM4JConfiguration.class.getClassLoader().getResource(resource);
+ URL confURL =
DOM4JConfiguration.class.getClassLoader().getResource(resource);
if (confURL == null)
{
confURL = ClassLoader.getSystemResource(resource);
@@ -194,12 +199,7 @@
for (int j = 0, k = attributes.size(); j < k; j++)
{
Attribute a = (Attribute) attributes.get(j);
- String attName =
- subhierarchy.toString()
- + '['
- + ATTRIB_MARKER
- + a.getName()
- + ']';
+ String attName = subhierarchy.toString() + '[' + ATTRIB_MARKER +
a.getName() + ']';
String attValue = a.getValue();
super.addProperty(attName, attValue);
}
@@ -254,10 +254,7 @@
int index = eName.indexOf(ATTRIB_START_MARKER);
if (index > -1)
{
- attName =
- eName.substring(
- index + ATTRIB_START_MARKER.length(),
- eName.length() - 1);
+ attName = eName.substring(index + ATTRIB_START_MARKER.length(),
eName.length() - 1);
eName = eName.substring(0, index);
}
// If we don't find this part of the property in the XML heirarchy
@@ -303,10 +300,7 @@
int index = eName.indexOf(ATTRIB_START_MARKER);
if (index > -1)
{
- attName =
- eName.substring(
- index + ATTRIB_START_MARKER.length(),
- eName.length() - 1);
+ attName = eName.substring(index + ATTRIB_START_MARKER.length(),
eName.length() - 1);
eName = eName.substring(0, index);
}
element = element.element(eName);
@@ -360,7 +354,7 @@
try
{
OutputFormat outputter = OutputFormat.createPrettyPrint();
- out = new BufferedOutputStream(new FileOutputStream(file));
+ out = new BufferedOutputStream(new FileOutputStream(getFile()));
writer = new XMLWriter(out, outputter);
writer.write(document);
}
@@ -383,6 +377,41 @@
*/
public File getFile()
{
+
+ File file = null;
+ if (StringUtils.isEmpty(basePath))
+ {
+ // Good luck... This will fail 99 out of 100 times.
+ file = new File(getFileName());
+ }
+ else
+ {
+ StringBuffer fileName = new StringBuffer();
+ fileName.append(basePath);
+
+ // My best friend. Paranoia.
+ if (!basePath.endsWith(fileSeparator))
+ {
+ fileName.append(fileSeparator);
+ }
+
+ //
+ // We have a relative path, and we have
+ // two possible forms here. If we have the
+ // "./" form then just strip that off first
+ // before continuing.
+ //
+ if (getFileName().startsWith("." + fileSeparator))
+ {
+ fileName.append(getFileName().substring(2));
+ }
+ else
+ {
+ fileName.append(getFileName());
+ }
+
+ file = new File(fileName.toString());
+ }
return file;
}
@@ -392,12 +421,40 @@
*/
public void setFile(File file)
{
- this.file = file;
+ this.fileName = file.getAbsolutePath();
}
public void setFileName(String fileName)
{
- setFile(new File(fileName));
+
+ this.fileName = fileName;
+
+ }
+
+ /**
+ * Returns the basePath.
+ * @return String
+ */
+ public String getBasePath()
+ {
+ return basePath;
+ }
+
+ /**
+ * Returns the fileName.
+ * @return String
+ */
+ public String getFileName()
+ {
+ return fileName;
+ }
+ /**
+ * Sets the basePath.
+ * @param basePath The basePath to set
+ */
+ public void setBasePath(String basePath)
+ {
+ this.basePath = basePath;
}
}
1.4 +9 -3
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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- JNDIConfiguration.java 19 Mar 2003 10:32:23 -0000 1.3
+++ JNDIConfiguration.java 22 Mar 2003 05:19:50 -0000 1.4
@@ -574,7 +574,8 @@
*/
public String getString(String key)
{
- return getValueFromJNDI(key);
+
+ return getString(key,null);
}
/**
@@ -588,7 +589,12 @@
*/
public String getString(String key, String defaultValue)
{
- throw new Error("This operation is not supported");
+ try {
+ return getValueFromJNDI(key);
+ }
+ catch (NoSuchElementException nsee){
+ return defaultValue;
+ }
}
/**
1.8 +53 -36
jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java
Index: PropertiesConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/PropertiesConfiguration.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- PropertiesConfiguration.java 18 Mar 2003 21:41:56 -0000 1.7
+++ PropertiesConfiguration.java 22 Mar 2003 05:19:51 -0000 1.8
@@ -60,6 +60,9 @@
import java.io.IOException;
import java.io.InputStream;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
import org.apache.commons.lang.StringUtils;
/**
@@ -87,12 +90,14 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Martin Poeschl</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh</a>
* @version $Id$
*/
-public class PropertiesConfiguration
- extends BasePropertiesConfiguration
- implements Configuration
+public class PropertiesConfiguration extends BasePropertiesConfiguration implements
Configuration
{
+ /** Static logger */
+ Log log = LogFactory.getLog(PropertiesConfiguration.class);
+
/** File separator. */
protected String fileSeparator = System.getProperty("file.separator");
@@ -144,6 +149,7 @@
*/
public PropertiesConfiguration(String fileName) throws IOException
{
+
load(fileName);
}
@@ -165,26 +171,7 @@
*/
public void load(String fileName) throws IOException
{
- if (fileName == null)
- {
- throw new FileNotFoundException("fileName was not set.");
- }
- File file = new File(fileName);
- File baseFile = file.getParentFile();
-
- if (baseFile != null)
- {
- basePath = baseFile.getAbsolutePath();
- setIncludesAllowed(true);
- }
- else
- {
- // For some unknown reason we have no path to load includes
- // from. So, don't allow includes.
- setIncludesAllowed(false);
- }
-
- load(getPropertyStream(file.getName()));
+ load(getPropertyStream(fileName));
}
/**
@@ -194,8 +181,7 @@
* @param defaults Configuration defaults to use if key not in file
* @throws IOException Error while loading the properties file
*/
- public PropertiesConfiguration(String file, Configuration defaults)
- throws IOException
+ public PropertiesConfiguration(String file, Configuration defaults) throws
IOException
{
this(file);
this.defaults = defaults;
@@ -209,8 +195,7 @@
* should be used if a key is not in the file.
* @throws IOException Error while loading the properties file
*/
- public PropertiesConfiguration(String file, String defaultFile)
- throws IOException
+ public PropertiesConfiguration(String file, String defaultFile) throws
IOException
{
this(file);
if (StringUtils.isNotEmpty(defaultFile))
@@ -228,12 +213,11 @@
* @return An Input Stream
* @throws IOException Error while loading the properties file
*/
- public InputStream getPropertyStream(String resourceName)
- throws IOException
+ public InputStream getPropertyStream(String resourceName) throws IOException
{
InputStream resource = null;
File file = null;
-
+
if (resourceName.startsWith(fileSeparator))
{
/*
@@ -244,6 +228,7 @@
}
else
{
+
if (StringUtils.isEmpty(basePath))
{
// Good luck... This will fail 99 out of 100 times.
@@ -274,15 +259,14 @@
{
fileName.append(resourceName);
}
-
+
file = new File(fileName.toString());
}
}
if (file == null || !file.exists())
{
- throw new FileNotFoundException(
- "Could not open File " + resourceName);
+ throw new FileNotFoundException("Could not open file " + file);
}
else
{
@@ -292,10 +276,24 @@
}
else
{
- throw new IOException(
- "File " + resourceName + " exists but could not be read.");
+ throw new IOException("File " + resourceName + " exists but could
not be read.");
}
}
+
+ File baseFile = file.getParentFile();
+
+ if (baseFile != null)
+ {
+ basePath = baseFile.getAbsolutePath();
+ setIncludesAllowed(true);
+ }
+ else
+ {
+ // For some unknown reason we have no path to load includes
+ // from. So, don't allow includes.
+ setIncludesAllowed(false);
+ }
+
return resource;
}
/**
@@ -314,6 +312,25 @@
public void setFileName(String fileName)
{
this.fileName = fileName;
+
+ }
+
+ /**
+ * Returns the basePath.
+ * @return String
+ */
+ public String getBasePath()
+ {
+ return basePath;
+ }
+
+ /**
+ * Sets the basePath.
+ * @param basePath The basePath to set
+ */
+ public void setBasePath(String basePath)
+ {
+ this.basePath = basePath;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]