Author: oheger
Date: Tue Sep 10 19:32:13 2013
New Revision: 1521604
URL: http://svn.apache.org/r1521604
Log:
Removed further methods from ConfigurationUtils.
Callers now use the corresponding methods in FileLocatorUtils.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/resolver/CatalogResolver.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLPropertiesConfiguration.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=1521604&r1=1521603&r2=1521604&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
Tue Sep 10 19:32:13 2013
@@ -36,7 +36,6 @@ import org.apache.commons.configuration.
import org.apache.commons.configuration.sync.NoOpSynchronizer;
import org.apache.commons.configuration.sync.Synchronizer;
import org.apache.commons.configuration.tree.ExpressionEngine;
-import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -57,9 +56,6 @@ public final class ConfigurationUtils
/** Constant for the resource path separator.*/
static final String RESOURCE_PATH_SEPARATOR = "/";
- /** Constant for the file URL protocol */
- private static final String FILE_SCHEME = "file:";
-
/** Constant for the name of the clone() method.*/
private static final String METHOD_CLONE = "clone";
@@ -450,278 +446,6 @@ public final class ConfigurationUtils
}
/**
- * Helper method for constructing a file object from a base path and a
- * file name. This method is called if the base path passed to
- * {@code getURL()} does not seem to be a valid URL.
- *
- * @param basePath the base path
- * @param fileName the file name
- * @return the resulting file
- */
- static File constructFile(String basePath, String fileName)
- {
- File file;
-
- File absolute = null;
- if (fileName != null)
- {
- absolute = new File(fileName);
- }
-
- if (StringUtils.isEmpty(basePath) || (absolute != null &&
absolute.isAbsolute()))
- {
- file = new File(fileName);
- }
- else
- {
- StringBuilder fName = new StringBuilder();
- fName.append(basePath);
-
- // My best friend. Paranoia.
- if (!basePath.endsWith(File.separator))
- {
- fName.append(File.separator);
- }
-
- //
- // 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 (fileName.startsWith("." + File.separator))
- {
- fName.append(fileName.substring(2));
- }
- else
- {
- fName.append(fileName);
- }
-
- file = new File(fName.toString());
- }
-
- return file;
- }
-
- /**
- * Return the location of the specified resource by searching the user home
- * directory, the current classpath and the system classpath.
- *
- * @param name the name of the resource
- *
- * @return the location of the resource
- */
- public static URL locate(String name)
- {
- return locate(null, name);
- }
-
- /**
- * Return the location of the specified resource by searching the user home
- * directory, the current classpath and the system classpath.
- *
- * @param base the base path of the resource
- * @param name the name of the resource
- *
- * @return the location of the resource
- */
- public static URL locate(String base, String name)
- {
- return locate(FileSystem.getDefaultFileSystem(), base, name);
- }
-
- /**
- * Return the location of the specified resource by searching the user home
- * directory, the current classpath and the system classpath.
- *
- * @param fileSystem the FileSystem to use.
- * @param base the base path of the resource
- * @param name the name of the resource
- *
- * @return the location of the resource
- */
- public static URL locate(FileSystem fileSystem, String base, String name)
- {
- if (LOG.isDebugEnabled())
- {
- StringBuilder buf = new StringBuilder();
- buf.append("ConfigurationUtils.locate(): base is ").append(base);
- buf.append(", name is ").append(name);
- LOG.debug(buf.toString());
- }
-
- if (name == null)
- {
- // undefined, always return null
- return null;
- }
-
- // attempt to create an URL directly
-
- URL url = fileSystem.locateFromURL(base, name);
-
- // attempt to load from an absolute path
- if (url == null)
- {
- File file = new File(name);
- if (file.isAbsolute() && file.exists()) // already absolute?
- {
- try
- {
- url = toURL(file);
- LOG.debug("Loading configuration from the absolute path "
+ name);
- }
- catch (MalformedURLException e)
- {
- LOG.warn("Could not obtain URL from file", e);
- }
- }
- }
-
- // attempt to load from the base directory
- if (url == null)
- {
- try
- {
- File file = constructFile(base, name);
- if (file != null && file.exists())
- {
- url = toURL(file);
- }
-
- if (url != null)
- {
- LOG.debug("Loading configuration from the path " + file);
- }
- }
- catch (MalformedURLException e)
- {
- LOG.warn("Could not obtain URL from file", e);
- }
- }
-
- // attempt to load from the user home directory
- if (url == null)
- {
- try
- {
- File file = constructFile(System.getProperty("user.home"),
name);
- if (file != null && file.exists())
- {
- url = toURL(file);
- }
-
- if (url != null)
- {
- LOG.debug("Loading configuration from the home path " +
file);
- }
-
- }
- catch (MalformedURLException e)
- {
- LOG.warn("Could not obtain URL from file", e);
- }
- }
-
- // attempt to load from classpath
- if (url == null)
- {
- 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
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if (loader != null)
- {
- url = loader.getResource(resourceName);
-
- if (url != null)
- {
- LOG.debug("Loading configuration from the context classpath ("
+ resourceName + ")");
- }
- }
-
- // attempt to load from the system classpath
- if (url == null)
- {
- url = ClassLoader.getSystemResource(resourceName);
-
- if (url != null)
- {
- LOG.debug("Loading configuration from the system classpath ("
+ resourceName + ")");
- }
- }
- return url;
- }
-
- /**
- * Return the path without the file name, for example
http://xyz.net/foo/bar.xml
- * results in http://xyz.net/foo/
- *
- * @param url the URL from which to extract the path
- * @return the path component of the passed in URL
- */
- public static String getBasePath(URL url)
- {
- if (url == null)
- {
- return null;
- }
-
- String s = url.toString();
- if (s.startsWith(FILE_SCHEME) && !s.startsWith("file://"))
- {
- s = "file://" + s.substring(FILE_SCHEME.length());
- }
-
- if (s.endsWith("/") || StringUtils.isEmpty(url.getPath()))
- {
- return s;
- }
- else
- {
- return s.substring(0, s.lastIndexOf("/") + 1);
- }
- }
-
- /**
- * Extract the file name from the specified URL.
- *
- * @param url the URL from which to extract the file name
- * @return the extracted file name
- */
- public static String getFileName(URL url)
- {
- if (url == null)
- {
- return null;
- }
-
- String path = url.getPath();
-
- if (path.endsWith("/") || StringUtils.isEmpty(path))
- {
- return null;
- }
- else
- {
- return path.substring(path.lastIndexOf("/") + 1);
- }
- }
-
- /**
* Convert the specified file into an URL. This method is equivalent
* to file.toURI().toURL(). It was used to work around a bug in the JDK
* preventing the transformation of a file into an URL if the file name
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java?rev=1521604&r1=1521603&r2=1521604&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
Tue Sep 10 19:32:13 2013
@@ -34,6 +34,7 @@ import org.apache.commons.configuration.
import org.apache.commons.configuration.io.FileHandler;
import org.apache.commons.configuration.io.FileLocator;
import org.apache.commons.configuration.io.FileLocatorAware;
+import org.apache.commons.configuration.io.FileLocatorUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
@@ -1388,7 +1389,7 @@ public class PropertiesConfiguration ext
{
assert locator != null : "Locator has not been set!";
URL url =
- ConfigurationUtils.locate(locator.getFileSystem(),
+ FileLocatorUtils.locate(locator.getFileSystem(),
locator.getBasePath(), fileName);
if (url == null)
{
@@ -1396,7 +1397,7 @@ public class PropertiesConfiguration ext
if (baseURL != null)
{
url =
- ConfigurationUtils.locate(locator.getFileSystem(),
+ FileLocatorUtils.locate(locator.getFileSystem(),
baseURL.toString(), fileName);
}
}
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java?rev=1521604&r1=1521603&r2=1521604&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
Tue Sep 10 19:32:13 2013
@@ -68,6 +68,107 @@ public final class FileLocatorUtils
}
/**
+ * Return the location of the specified resource by searching the user home
+ * directory, the current classpath and the system classpath.
+ *
+ * @param fileSystem the FileSystem to use.
+ * @param base the base path of the resource
+ * @param name the name of the resource
+ *
+ * @return the location of the resource
+ */
+ public static URL locate(FileSystem fileSystem, String base, String name)
+ {
+ if (LOG.isDebugEnabled())
+ {
+ StringBuilder buf = new StringBuilder();
+ buf.append("ConfigurationUtils.locate(): base is ").append(base);
+ buf.append(", name is ").append(name);
+ LOG.debug(buf.toString());
+ }
+
+ if (name == null)
+ {
+ // undefined, always return null
+ return null;
+ }
+
+ // attempt to create an URL directly
+
+ URL url = fileSystem.locateFromURL(base, name);
+
+ // attempt to load from an absolute path
+ if (url == null)
+ {
+ File file = new File(name);
+ if (file.isAbsolute() && file.exists()) // already absolute?
+ {
+ try
+ {
+ url = toURL(file);
+ LOG.debug("Loading configuration from the absolute path "
+ name);
+ }
+ catch (MalformedURLException e)
+ {
+ LOG.warn("Could not obtain URL from file", e);
+ }
+ }
+ }
+
+ // attempt to load from the base directory
+ if (url == null)
+ {
+ try
+ {
+ File file = constructFile(base, name);
+ if (file != null && file.exists())
+ {
+ url = toURL(file);
+ }
+
+ if (url != null)
+ {
+ LOG.debug("Loading configuration from the path " + file);
+ }
+ }
+ catch (MalformedURLException e)
+ {
+ LOG.warn("Could not obtain URL from file", e);
+ }
+ }
+
+ // attempt to load from the user home directory
+ if (url == null)
+ {
+ try
+ {
+ File file = constructFile(System.getProperty("user.home"),
name);
+ if (file != null && file.exists())
+ {
+ url = toURL(file);
+ }
+
+ if (url != null)
+ {
+ LOG.debug("Loading configuration from the home path " +
file);
+ }
+
+ }
+ catch (MalformedURLException e)
+ {
+ LOG.warn("Could not obtain URL from file", e);
+ }
+ }
+
+ // attempt to load from classpath
+ if (url == null)
+ {
+ url = locateFromClasspath(name);
+ }
+ return url;
+ }
+
+ /**
* Return the path without the file name, for example
http://xyz.net/foo/bar.xml
* results in http://xyz.net/foo/
*
@@ -147,7 +248,7 @@ public final class FileLocatorUtils
* @param fileName the file name
* @return the file object (<b>null</b> if no file can be obtained)
*/
- public static File getFile(String basePath, String fileName)
+ static File getFile(String basePath, String fileName)
{
// Check if the file name is absolute
File f = new File(fileName);
@@ -198,107 +299,6 @@ public final class FileLocatorUtils
}
/**
- * Return the location of the specified resource by searching the user home
- * directory, the current classpath and the system classpath.
- *
- * @param fileSystem the FileSystem to use.
- * @param base the base path of the resource
- * @param name the name of the resource
- *
- * @return the location of the resource
- */
- static URL locate(FileSystem fileSystem, String base, String name)
- {
- if (LOG.isDebugEnabled())
- {
- StringBuilder buf = new StringBuilder();
- buf.append("ConfigurationUtils.locate(): base is ").append(base);
- buf.append(", name is ").append(name);
- LOG.debug(buf.toString());
- }
-
- if (name == null)
- {
- // undefined, always return null
- return null;
- }
-
- // attempt to create an URL directly
-
- URL url = fileSystem.locateFromURL(base, name);
-
- // attempt to load from an absolute path
- if (url == null)
- {
- File file = new File(name);
- if (file.isAbsolute() && file.exists()) // already absolute?
- {
- try
- {
- url = toURL(file);
- LOG.debug("Loading configuration from the absolute path "
+ name);
- }
- catch (MalformedURLException e)
- {
- LOG.warn("Could not obtain URL from file", e);
- }
- }
- }
-
- // attempt to load from the base directory
- if (url == null)
- {
- try
- {
- File file = constructFile(base, name);
- if (file != null && file.exists())
- {
- url = toURL(file);
- }
-
- if (url != null)
- {
- LOG.debug("Loading configuration from the path " + file);
- }
- }
- catch (MalformedURLException e)
- {
- LOG.warn("Could not obtain URL from file", e);
- }
- }
-
- // attempt to load from the user home directory
- if (url == null)
- {
- try
- {
- File file = constructFile(System.getProperty("user.home"),
name);
- if (file != null && file.exists())
- {
- url = toURL(file);
- }
-
- if (url != null)
- {
- LOG.debug("Loading configuration from the home path " +
file);
- }
-
- }
- catch (MalformedURLException e)
- {
- LOG.warn("Could not obtain URL from file", e);
- }
- }
-
- // attempt to load from classpath
- if (url == null)
- {
- url = locateFromClasspath(name);
- }
- return url;
- }
-
- /**
* Tries to find a resource with the given name in the classpath.
*
* @param resourceName the name of the resource
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/resolver/CatalogResolver.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/resolver/CatalogResolver.java?rev=1521604&r1=1521603&r2=1521604&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/resolver/CatalogResolver.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/resolver/CatalogResolver.java
Tue Sep 10 19:32:13 2013
@@ -24,8 +24,8 @@ import java.net.URLConnection;
import java.util.Vector;
import org.apache.commons.configuration.ConfigurationException;
-import org.apache.commons.configuration.ConfigurationUtils;
import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
+import org.apache.commons.configuration.io.FileLocatorUtils;
import org.apache.commons.configuration.io.FileSystem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -392,7 +392,7 @@ public class CatalogResolver implements
try
{
- url = ConfigurationUtils.locate(fs, base, fileName);
+ url = FileLocatorUtils.locate(fs, base, fileName);
if (url != null)
{
is = fs.getInputStream(url);
@@ -442,7 +442,7 @@ public class CatalogResolver implements
*/
public void parseCatalog(String baseDir, String fileName) throws
IOException
{
- base = ConfigurationUtils.locate(fs, baseDir, fileName);
+ base = FileLocatorUtils.locate(fs, baseDir, fileName);
catalogCwd = base;
default_override = catalogManager.getPreferPublic();
catalogManager.debug.message(DEBUG_NORMAL, "Parse catalog: " +
fileName);
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java?rev=1521604&r1=1521603&r2=1521604&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
Tue Sep 10 19:32:13 2013
@@ -142,37 +142,6 @@ public class TestConfigurationUtils
}
@Test
- public void testGetBasePath() throws Exception
- {
- URL url = new URL("http://xyz.net/foo/bar.xml");
- assertEquals("base path of " + url, "http://xyz.net/foo/",
ConfigurationUtils.getBasePath(url));
-
- url = new URL("http://xyz.net/foo/");
- assertEquals("base path of " + url, "http://xyz.net/foo/",
ConfigurationUtils.getBasePath(url));
-
- url = new URL("http://xyz.net/foo");
- assertEquals("base path of " + url, "http://xyz.net/",
ConfigurationUtils.getBasePath(url));
-
- url = new URL("http://xyz.net/");
- assertEquals("base path of " + url, "http://xyz.net/",
ConfigurationUtils.getBasePath(url));
-
- url = new URL("http://xyz.net");
- assertEquals("base path of " + url, "http://xyz.net",
ConfigurationUtils.getBasePath(url));
- }
-
- @Test
- public void testGetFileName() throws Exception
- {
- assertEquals("file name for a null URL", null,
ConfigurationUtils.getFileName(null));
-
- URL url = new URL("http://xyz.net/foo/");
- assertEquals("file for a directory URL " + url, null,
ConfigurationUtils.getFileName(url));
-
- url = new URL("http://xyz.net/foo/bar.xml");
- assertEquals("file name for a valid URL " + url, "bar.xml",
ConfigurationUtils.getFileName(url));
- }
-
- @Test
public void testCopy()
{
// create the source configuration
@@ -219,23 +188,6 @@ public class TestConfigurationUtils
ListAssert.assertEquals("'key2' property", expected,
conf2.getList("key2"));
}
- @Test
- public void testLocateWithNullTCCL() throws Exception
- {
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- try
- {
- Thread.currentThread().setContextClassLoader(null);
- assertNull(ConfigurationUtils.locate("abase", "aname"));
- // This assert fails when maven 2 is used, so commented out
- //assertNotNull(ConfigurationUtils.locate("test.xml"));
- }
- finally
- {
- Thread.currentThread().setContextClassLoader(cl);
- }
- }
-
/**
* Tests converting a configuration into a hierarchical one.
*/
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLPropertiesConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLPropertiesConfiguration.java?rev=1521604&r1=1521603&r2=1521604&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLPropertiesConfiguration.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLPropertiesConfiguration.java
Tue Sep 10 19:32:13 2013
@@ -32,6 +32,7 @@ import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.configuration.io.FileHandler;
+import org.apache.commons.configuration.io.FileLocatorUtils;
import org.apache.commons.configuration.io.FileSystem;
import org.junit.Rule;
import org.junit.Test;
@@ -84,7 +85,7 @@ public class TestXMLPropertiesConfigurat
@Test
public void testDOMLoad() throws Exception
{
- URL location =
ConfigurationUtils.locate(FileSystem.getDefaultFileSystem(), null,
TEST_PROPERTIES_FILE);
+ URL location =
FileLocatorUtils.locate(FileSystem.getDefaultFileSystem(), null,
TEST_PROPERTIES_FILE);
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
File file = new File(location.toURI());