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 2014/06/29 21:45:15 UTC

svn commit: r1606588 - 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: Sun Jun 29 19:45:14 2014
New Revision: 1606588

URL: http://svn.apache.org/r1606588
Log:
FileHandler can now be initialized from a map.

Again, the goal is to simplify the creation of parameters objects for
file-based configurations from within IoC frameworks.

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=1606588&r1=1606587&r2=1606588&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 Sun Jun 29 19:45:14 2014
@@ -29,6 +29,7 @@ import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.atomic.AtomicReference;
 
@@ -183,8 +184,7 @@ public class FileHandler
      */
     public FileHandler(FileBased obj)
     {
-        content = obj;
-        fileLocator = initFileLocator();
+        this(obj, emptyFileLocator());
     }
 
     /**
@@ -202,14 +202,34 @@ public class FileHandler
      */
     public FileHandler(FileBased obj, FileHandler c)
     {
-        if (c == null)
-        {
-            throw new IllegalArgumentException(
-                    "FileHandler to assign must not be null!");
-        }
+        this(obj, checkSourceHandler(c).getFileLocator());
+    }
 
+    /**
+     * Creates a new instance of {@code FileHandler} based on the given
+     * {@code FileBased} and {@code FileLocator} objects.
+     *
+     * @param obj the {@code FileBased} object to manage
+     * @param locator the {@code FileLocator}
+     */
+    private FileHandler(FileBased obj, FileLocator locator)
+    {
         content = obj;
-        fileLocator = new AtomicReference<FileLocator>(c.getFileLocator());
+        fileLocator = new AtomicReference<FileLocator>(locator);
+    }
+
+    /**
+     * Creates a new {@code FileHandler} instance from properties stored in a
+     * map. This method tries to extract a {@link FileLocator} from the map. A
+     * new {@code FileHandler} is created based on this {@code FileLocator}.
+     *
+     * @param map the map (may be <b>null</b>)
+     * @return the newly created {@code FileHandler}
+     * @see FileLocatorUtils#fromMap(Map)
+     */
+    public static FileHandler fromMap(Map<String, Object> map)
+    {
+        return new FileHandler(null, FileLocatorUtils.fromMap(map));
     }
 
     /**
@@ -636,7 +656,7 @@ public class FileHandler
      */
     public boolean locate()
     {
-        boolean result = false;
+        boolean result;
         boolean done;
 
         do
@@ -1477,15 +1497,30 @@ public class FileHandler
     }
 
     /**
-     * Initializes the reference to the {@code FileLocator}. Sets an undefined
-     * locator object.
+     * Creates an uninitialized file locator.
      *
-     * @return the reference to the file locator
+     * @return the locator
      */
-    private static AtomicReference<FileLocator> initFileLocator()
+    private static FileLocator emptyFileLocator()
     {
-        return new AtomicReference<FileLocator>(FileLocatorUtils.fileLocator()
-                .create());
+        return FileLocatorUtils.fileLocator().create();
+    }
+
+    /**
+     * Helper method for checking a file handler which is to be copied. Throws
+     * an exception if the handler is <b>null</b>.
+     *
+     * @param c the {@code FileHandler} from which to copy the location
+     * @return the same {@code FileHandler}
+     */
+    private static FileHandler checkSourceHandler(FileHandler c)
+    {
+        if (c == null)
+        {
+            throw new IllegalArgumentException(
+                    "FileHandler to assign must not be null!");
+        }
+        return c;
     }
 
     /**

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=1606588&r1=1606587&r2=1606588&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 Sun Jun 29 19:45:14 2014
@@ -40,7 +40,9 @@ import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.commons.configuration.ConfigurationAssert;
 import org.apache.commons.configuration.PropertiesConfiguration;
@@ -1514,6 +1516,23 @@ public class TestFileHandler
     }
 
     /**
+     * Tests whether an instance can be created from a map with the properties
+     * of a FileLocator.
+     */
+    @Test
+    public void testInitFromMap()
+    {
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().fileName(TEST_FILENAME)
+                        .basePath("someBasePath").encoding("someEncoding")
+                        .create();
+        Map<String, Object> map = new HashMap<String, Object>();
+        FileLocatorUtils.put(locator, map);
+        FileHandler handler = FileHandler.fromMap(map);
+        assertEquals("Wrong locator", locator, handler.getFileLocator());
+    }
+
+    /**
      * An implementation of the FileBased interface used for test purposes.
      */
     private static class FileBasedTestImpl implements FileBased