You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/09/28 21:33:24 UTC

svn commit: r1527231 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/io/FileHandler.java test/java/org/apache/commons/configuration/io/TestFileHandler.java

Author: oheger
Date: Sat Sep 28 19:33:24 2013
New Revision: 1527231

URL: http://svn.apache.org/r1527231
Log:
FileHandler now uses the locator-based locate() method when loading its file.

This means that the customizable FileLocationStrategy is applied; applications
can change the algorithm for searching for configuration files.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java?rev=1527231&r1=1527230&r2=1527231&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java Sat Sep 28 19:33:24 2013
@@ -59,10 +59,10 @@ import org.apache.commons.configuration.
  * former case the file is resolved based on the current directory.</li>
  * <li>As file paths in string form: With the {@code setPath()} method a full
  * path to a configuration file can be provided as a string.</li>
- * <li>Separated as base path and file name: This is the native form in which
- * the location is stored. The base path is a string defining either a local
- * directory or a URL. It can be set using the {@code setBasePath()} method. The
- * file name, non surprisingly, defines the name of the configuration file.</li>
+ * <li>Separated as base path and file name: The base path is a string defining
+ * either a local directory or a URL. It can be set using the
+ * {@code setBasePath()} method. The file name, non surprisingly, defines the
+ * name of the configuration file.</li>
  * </ul>
  * </p>
  * <p>
@@ -74,8 +74,27 @@ import org.apache.commons.configuration.
  * location is not changed.
  * </p>
  * <p>
- * When loading or saving a {@code FileBased} object some additional functionality
- * is performed if the object implements one of the following interfaces:
+ * The actual position of the file to be loaded is determined by a
+ * {@link FileLocationStrategy} based on the location information that has been
+ * provided. By providing a custom location strategy the algorithm for searching
+ * files can be adapted. Save operations require more explicit information. They
+ * cannot rely on a location strategy because the file to be written may not yet
+ * exist. So there may be some differences in the way location information is
+ * interpreted by load and save operations. In order to avoid this, the
+ * following approach is recommended:
+ * <ul>
+ * <li>Use the desired {@code setXXX()} methods to define the location of the
+ * file to be loaded.</li>
+ * <li>Call the {@code locate()} method. This method resolves the referenced
+ * file (if possible) and fills out all supported location information.</li>
+ * <li>Later on, {@code save()} can be called. This method now has sufficient
+ * information to store the file at the correct location.</li>
+ * </ul>
+ * </p>
+ * <p>
+ * When loading or saving a {@code FileBased} object some additional
+ * functionality is performed if the object implements one of the following
+ * interfaces:
  * <ul>
  * <li>{@code FileLocatorAware}: In this case an object with the current file
  * location is injected before the load or save operation is executed. This is
@@ -94,6 +113,7 @@ import org.apache.commons.configuration.
  * </p>
  *
  * @version $Id$
+ * @since 2.0
  */
 public class FileHandler
 {
@@ -391,7 +411,9 @@ public class FileHandler
     }
 
     /**
-     * Returns the location of the associated file as a URL.
+     * Returns the location of the associated file as a URL. If a URL is set,
+     * it is directly returned. Otherwise, an attempt to locate the referenced
+     * file is made.
      *
      * @return a URL to the associated file; can be <b>null</b> if the location
      *         is unspecified
@@ -400,8 +422,7 @@ public class FileHandler
     {
         FileLocator locator = getFileLocator();
         return (locator.getSourceURL() != null) ? locator.getSourceURL()
-                : FileLocatorUtils.locate(FileLocatorUtils.obtainFileSystem(locator),
-                        locator.getBasePath(), locator.getFileName());
+                : FileLocatorUtils.locate(locator);
     }
 
     /**
@@ -914,14 +935,8 @@ public class FileHandler
      */
     private void load(FileLocator locator) throws ConfigurationException
     {
-        if (locator.getSourceURL() != null)
-        {
-            load(locator.getSourceURL(), locator);
-        }
-        else
-        {
-            load(locator.getFileName(), locator);
-        }
+        URL url = FileLocatorUtils.locateOrThrow(locator);
+        load(url, locator);
     }
 
     /**
@@ -965,15 +980,8 @@ public class FileHandler
     private void load(String fileName, FileLocator locator)
             throws ConfigurationException
     {
-        URL url =
-                FileLocatorUtils.locate(FileLocatorUtils.obtainFileSystem(locator),
-                        locator.getBasePath(), fileName);
-
-        if (url == null)
-        {
-            throw new ConfigurationException(
-                    "Cannot locate configuration source " + fileName);
-        }
+        FileLocator locFileName = createLocatorWithFileName(fileName, locator);
+        URL url = FileLocatorUtils.locateOrThrow(locFileName);
         load(url, locator);
     }
 
@@ -1300,6 +1308,21 @@ public class FileHandler
     }
 
     /**
+     * Creates a {@code FileLocator} which is a copy of the passed in one, but
+     * has the given file name set to reference the target file.
+     *
+     * @param fileName the file name
+     * @param locator the {@code FileLocator} to copy
+     * @return the manipulated {@code FileLocator} with the file name
+     */
+    private FileLocator createLocatorWithFileName(String fileName,
+            FileLocator locator)
+    {
+        return FileLocatorUtils.fileLocator(locator).sourceURL(null)
+                .fileName(fileName).create();
+    }
+
+    /**
      * Checks whether a content object is available. If not, an exception is
      * thrown. This method is called whenever the content object is accessed.
      *

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java?rev=1527231&r1=1527230&r2=1527231&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java Sat Sep 28 19:33:24 2013
@@ -581,6 +581,22 @@ public class TestFileHandler
     }
 
     /**
+     * Tests that a load() operation with a file path overrides a URL which
+     * might have been set.
+     */
+    @Test
+    public void testLoadFromFilePathWithURLDefined()
+            throws ConfigurationException
+    {
+        File file = createTestFile();
+        FileBasedTestImpl content = new FileBasedTestImpl();
+        FileHandler handler = new FileHandler(content);
+        handler.setURL(ConfigurationAssert.getTestURL("test.xml"));
+        handler.load(file.getAbsolutePath());
+        assertEquals("Wrong content", CONTENT, content.getContent());
+    }
+
+    /**
      * Tests whether data from an input stream can be read.
      */
     @Test