You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2008/02/18 00:00:14 UTC

svn commit: r628573 - in /commons/proper/configuration/trunk: src/java/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/ xdocs/

Author: ebourg
Date: Sun Feb 17 15:00:12 2008
New Revision: 628573

URL: http://svn.apache.org/viewvc?rev=628573&view=rev
Log:
CONFIGURATION-300: Fixed the creation of a file based configuration if the filename contains a '#' (Java 1.4+ only)

Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java
    commons/proper/configuration/trunk/xdocs/changes.xml

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java?rev=628573&r1=628572&r2=628573&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java Sun Feb 17 15:00:12 2008
@@ -238,7 +238,7 @@
     {
         try
         {
-            load(file.toURL());
+            load(ConfigurationUtils.toURL(file));
         }
         catch (ConfigurationException e)
         {

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=628573&r1=628572&r2=628573&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java Sun Feb 17 15:00:12 2008
@@ -34,6 +34,7 @@
 import org.apache.commons.configuration.event.ConfigurationErrorListener;
 import org.apache.commons.configuration.event.EventSource;
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.SystemUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -294,7 +295,7 @@
         File f = new File(file);
         if (f.isAbsolute()) // already absolute?
         {
-            return f.toURL();
+            return toURL(f);
         }
 
         try
@@ -311,7 +312,7 @@
         }
         catch (MalformedURLException uex)
         {
-            return constructFile(basePath, file).toURL();
+            return toURL(constructFile(basePath, file));
         }
     }
 
@@ -452,7 +453,7 @@
             {
                 try
                 {
-                    url = file.toURL();
+                    url = toURL(file);
                     log.debug("Loading configuration from the absolute path " + name);
                 }
                 catch (MalformedURLException e)
@@ -470,7 +471,7 @@
                 File file = constructFile(base, name);
                 if (file != null && file.exists())
                 {
-                    url = file.toURL();
+                    url = toURL(file);
                 }
 
                 if (url != null)
@@ -492,7 +493,7 @@
                 File file = constructFile(System.getProperty("user.home"), name);
                 if (file != null && file.exists())
                 {
-                    url = file.toURL();
+                    url = toURL(file);
                 }
 
                 if (url != null)
@@ -676,6 +677,40 @@
         else
         {
             return null;
+        }
+    }
+
+    /**
+     * Convert the specified file into an URL. This method is equivalent
+     * to file.toURI().toURL() on Java 1.4 and above, and equivalent to
+     * file.toURL() on Java 1.3. This is to work around a bug in the JDK
+     * preventing the transformation of a file into an URL if the file name
+     * contains a '#' character. See the issue CONFIGURATION-300 for
+     * more details.
+     *
+     * @param file the file to be converted into an URL
+     */
+    static URL toURL(File file) throws MalformedURLException
+    {
+        if (SystemUtils.isJavaVersionAtLeast(1.4f))
+        {
+            try
+            {
+                Method toURI = file.getClass().getMethod("toURI", (Class[]) null);
+                Object uri = toURI.invoke(file, (Class[]) null);
+                Method toURL = uri.getClass().getMethod("toURL", (Class[]) null);
+                URL url = (URL) toURL.invoke(uri, (Class[]) null);
+
+                return url;
+            }
+            catch (Exception e)
+            {
+                throw new MalformedURLException(e.getMessage());
+            }
+        }
+        else
+        {
+            return file.toURL();
         }
     }
 

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java?rev=628573&r1=628572&r2=628573&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java Sun Feb 17 15:00:12 2008
@@ -35,6 +35,7 @@
 import java.util.List;
 
 import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
+import org.apache.commons.lang.SystemUtils;
 
 import junit.framework.TestCase;
 
@@ -736,6 +737,24 @@
         {
             assertTrue("Wrong root cause: " + cex,
                     cex.getCause() instanceof IOException);
+        }
+    }
+
+    /**
+     * Test the creation of a file containing a '#' in its name. This test is
+     * skipped on Java 1.3 as it always fails.
+     */
+    public void testFileWithSharpSymbol() throws Exception
+    {
+        if (SystemUtils.isJavaVersionAtLeast(1.4f))
+        {
+            File file = new File("target/sharp#1.properties");
+            file.createNewFile();
+
+            PropertiesConfiguration conf = new PropertiesConfiguration(file);
+            conf.save();
+
+            assertTrue("Missing file " + file, file.exists());
         }
     }
 

Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=628573&r1=628572&r2=628573&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Sun Feb 17 15:00:12 2008
@@ -40,6 +40,10 @@
         Instantiating an XMLPropertyListConfiguration no longer fails
         if the DTD is missing from the classpath.
       </action>
+      <action dev="ebourg" type="fix" issue="CONFIGURATION-300">
+        It's now possible to read a configuration file containing
+        a '#' in its name (requires Java 1.4 or above).
+      </action>
     </release>
 
     <release version="1.5" date="2007-11-24" description="Many smaller bugfixes">