Author: oheger
Date: Wed Aug 23 13:14:03 2006
New Revision: 434154
URL: http://svn.apache.org/viewvc?rev=434154&view=rev
Log:
Applied patch from Gabriele Garuglieri related to CONFIGURATION-216; fixes
problems with resolving relative paths when configuration files are loaded from
classpath
Added:
jakarta/commons/proper/configuration/trunk/conf/config/deep/testEqualDeep.properties
(with props)
jakarta/commons/proper/configuration/trunk/conf/config/deep/testFileFromClasspath.xml
(with props)
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
Added:
jakarta/commons/proper/configuration/trunk/conf/config/deep/testEqualDeep.properties
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/conf/config/deep/testEqualDeep.properties?rev=434154&view=auto
==============================================================================
---
jakarta/commons/proper/configuration/trunk/conf/config/deep/testEqualDeep.properties
(added)
+++
jakarta/commons/proper/configuration/trunk/conf/config/deep/testEqualDeep.properties
Wed Aug 23 13:14:03 2006
@@ -0,0 +1,12 @@
+property.a = a
+property.b = b
+property.c = 100
+
+#
+# Value set twice
+property.a = aa
+
+clear.property = delete me
+
+existing.property = i exist
+
Propchange:
jakarta/commons/proper/configuration/trunk/conf/config/deep/testEqualDeep.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/configuration/trunk/conf/config/deep/testEqualDeep.properties
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange:
jakarta/commons/proper/configuration/trunk/conf/config/deep/testEqualDeep.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jakarta/commons/proper/configuration/trunk/conf/config/deep/testFileFromClasspath.xml
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/conf/config/deep/testFileFromClasspath.xml?rev=434154&view=auto
==============================================================================
---
jakarta/commons/proper/configuration/trunk/conf/config/deep/testFileFromClasspath.xml
(added)
+++
jakarta/commons/proper/configuration/trunk/conf/config/deep/testFileFromClasspath.xml
Wed Aug 23 13:14:03 2006
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+
+<configuration>
+ <properties config-name="propConf" fileName="testEqual.properties"/>
+ <properties config-name="propConfDeep" fileName="testEqualDeep.properties"/>
+</configuration>
+
Propchange:
jakarta/commons/proper/configuration/trunk/conf/config/deep/testFileFromClasspath.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/configuration/trunk/conf/config/deep/testFileFromClasspath.xml
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange:
jakarta/commons/proper/configuration/trunk/conf/config/deep/testFileFromClasspath.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java?rev=434154&r1=434153&r2=434154&view=diff
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
Wed Aug 23 13:14:03 2006
@@ -26,6 +26,7 @@
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
+import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
@@ -523,8 +524,10 @@
}
/**
- * Set the name of the file. The passed in file name should not contain a
- * path. Use <code>[EMAIL PROTECTED]
AbstractFileConfiguration#setPath(String)
+ * Set the name of the file. The passed in file name can contain a
+ * relative path.
+ * It must be used when referring files with relative paths from classpath.
+ * Use <code>[EMAIL PROTECTED] AbstractFileConfiguration#setPath(String)
* setPath()}</code> to set a full qualified file name.
*
* @param fileName the name of the file
@@ -559,7 +562,8 @@
/**
* Return the file where the configuration is stored. If the base path is a
- * URL with a protocol different than "file", the return value
+ * URL with a protocol different than "file", or the
configuration
+ * file is within a compressed archive, the return value
* will not point to a valid file object.
*
* @return the file where the configuration is stored; this can be
<b>null</b>
@@ -600,19 +604,51 @@
/**
* Returns the full path to the file this configuration is based on. The
- * return value is valid only if this configuration is based on a file on
- * the local disk.
+ * return value is a valid File path only if this configuration is based
on
+ * a file on the local disk.
+ * If the configuration was loaded from a packed archive the returned value
+ * is the string form of the URL from which the configuration was loaded.
*
* @return the full path to the configuration file
*/
public String getPath()
{
- return getFile().getAbsolutePath();
+ String path = null;
+ File file = getFile();
+ // if resource was loaded from jar file may be null
+ if (file != null)
+ {
+ path = file.getAbsolutePath();
+ }
+
+ // try to see if file was loaded from a jar
+ if (path == null)
+ {
+ if (sourceURL != null)
+ {
+ path = sourceURL.getPath();
+ }
+ else
+ {
+ try {
+ path = ConfigurationUtils.getURL(getBasePath(),
+ getFileName()).getPath();
+ } catch (MalformedURLException e) {
+ // simply ignore it and return null
+ }
+ }
+ }
+
+ return path;
}
/**
- * Sets the location of this configuration as a full path name. The passed
- * in path should represent a valid file name.
+ * Sets the location of this configuration as a full or relative path
name.
+ * The passed in path should represent a valid file name on the file
system.
+ * It must not be used to specify relative paths for files that exist
+ * in classpath, either plain file system or compressed archive,
+ * because this method expands any relative path to an absolute one which
+ * may end in an invalid absolute path for classpath references.
*
* @param path the full path name of the configuration file
*/
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java?rev=434154&r1=434153&r2=434154&view=diff
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
Wed Aug 23 13:14:03 2006
@@ -65,7 +65,7 @@
{
this();
// store the file name
- delegate.setPath(fileName);
+ delegate.setFileName(fileName);
// load the file
load();
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=434154&r1=434153&r2=434154&view=diff
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
Wed Aug 23 13:14:03 2006
@@ -486,31 +486,32 @@
}
}
- String resourceName = null;
+ // attempt to load from classpath
if (url == null)
{
- if (base != null)
- {
- resourceName = base + RESOURCE_PATH_SEPARATOR + name;
- }
- else
- {
- resourceName = name;
- }
+ url = locateFromClasspath(name);
}
+ return url;
+ }
+ /**
+ * Tries to find a resource with the given name in the classpath.
+ * @param resourceName the name of the resource
+ * @return the URL to the found resource or <b>null</b> if the resource
+ * cannot be found
+ */
+ static URL locateFromClasspath(String resourceName)
+ {
+ URL url = null;
// attempt to load from the context classpath
- if (url == null)
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (loader != null)
{
- ClassLoader loader =
Thread.currentThread().getContextClassLoader();
- if (loader != null)
- {
- url = loader.getResource(resourceName);
+ url = loader.getResource(resourceName);
- if (url != null)
- {
- log.debug("Configuration loaded from the context classpath
(" + resourceName + ")");
- }
+ if (url != null)
+ {
+ log.debug("Configuration loaded from the context classpath ("
+ resourceName + ")");
}
}
@@ -524,7 +525,6 @@
log.debug("Configuration loaded from the system classpath (" +
resourceName + ")");
}
}
-
return url;
}
Modified:
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java?rev=434154&r1=434153&r2=434154&view=diff
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
Wed Aug 23 13:14:03 2006
@@ -492,6 +492,20 @@
}
/**
+ * Tests the loading of configuration file in a Combined configuration
+ * when the configuration source is in the classpath.
+ */
+ public void testLoadFromClassPath() throws ConfigurationException
+ {
+ DefaultConfigurationBuilder cf =
+ new
DefaultConfigurationBuilder("conf/config/deep/testFileFromClasspath.xml");
+ CombinedConfiguration config = cf.getConfiguration(true);
+ Configuration config1 = config.getConfiguration("propConf");
+ Configuration config2 = config.getConfiguration("propConfDeep");
+ compare(config1, config2);
+ }
+
+ /**
* Tests cloning a file based configuration.
*/
public void testClone() throws ConfigurationException
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=434154&r1=434153&r2=434154&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Wed Aug 23
13:14:03 2006
@@ -23,6 +23,11 @@
<body>
<release version="1.3-rc2" date="in SVN">
+ <action dev="oheger" type="update" issue="CONFIGURATION-216"
due-to="Gabriele Garuglieri">
+ There were still some problems with resolving relative paths when
+ configuration files are loaded from classpath. This fix addresses these
+ issues.
+ </action>
<action dev="oheger" type="update" issue="CONFIGURATION-220">
DataConfiguration.getDateArray() used to ignore the format argument.
This was fixed.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]