commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r529919 - in /jakarta/commons/proper/configuration/trunk: src/java/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/ xdocs/
Date Wed, 18 Apr 2007 08:45:49 GMT
Author: ebourg
Date: Wed Apr 18 01:45:48 2007
New Revision: 529919

URL: http://svn.apache.org/viewvc?view=rev&rev=529919
Log:
DataConfiguration now supports the InetAddress type

Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDataConfiguration.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java?view=diff&rev=529919&r1=529918&r2=529919
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java
Wed Apr 18 01:45:48 2007
@@ -37,8 +37,8 @@
 
 /**
  * Decorator providing additional getters for any Configuration. This extended
- * Configuration supports more types: URL, Locale, Date, Calendar, Color, as
- * well as lists and arrays for all types.
+ * Configuration supports more types: URL, Locale, Date, Calendar, Color,
+ * InetAddress, as well as lists and arrays for all types.
  *
  * <h4>Example</h4>
  *
@@ -46,6 +46,7 @@
  * <pre>
  * title.color = #0000FF
  * default.locales = fr,en,de
+ * remote.host = 192.168.0.53
  * </pre>
  *
  * Usage:
@@ -58,6 +59,7 @@
  *
  * // retrieve a property using a generic getter
  * Locale[] locales = (Locale[]) config.getArray(Locale.class, "default.locales");
+ * InetAddress host = (InetAddress) config.get(InetAddress.class, "remote.host");
  * </pre>
  *
  * <h4>Dates</h4>

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java?view=diff&rev=529919&r1=529918&r2=529919
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
Wed Apr 18 01:45:48 2007
@@ -20,10 +20,13 @@
 import java.awt.Color;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -39,6 +42,7 @@
 import org.apache.commons.collections.iterators.SingletonIterator;
 import org.apache.commons.lang.BooleanUtils;
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.SystemUtils;
 
 /**
  * A utility class to convert the configuration properties into any type.
@@ -145,6 +149,10 @@
         {
             return PropertyConverter.toColor(value);
         }
+        else if (InetAddress.class.isAssignableFrom(cls))
+        {
+            return PropertyConverter.toInetAddress(value);
+        }
 
         throw new ConversionException("The value '" + value + "' (" + value.getClass() +
") can't be converted to a " + cls.getName() + " object");
     }
@@ -608,6 +616,38 @@
         else
         {
             throw new ConversionException("The value " + value + " can't be converted to
a Color");
+        }
+    }
+
+    /**
+     * Convert the specified value into an internet address.
+     *
+     * @param value the value to convert
+     * @return the converted value
+     * @throws ConversionException thrown if the value cannot be converted to a InetAddress
+     *
+     * @since 1.5
+     */
+    static InetAddress toInetAddress(Object value) throws ConversionException
+    {
+        if (value instanceof InetAddress)
+        {
+            return (InetAddress) value;
+        }
+        else if (value instanceof String)
+        {
+            try
+            {
+                return InetAddress.getByName((String) value);
+            }
+            catch (UnknownHostException e)
+            {
+                throw new ConversionException("The value " + value + " can't be converted
to a InetAddress", e);
+            }
+        }
+        else
+        {
+            throw new ConversionException("The value " + value + " can't be converted to
a InetAddress");
         }
     }
 

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDataConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDataConfiguration.java?view=diff&rev=529919&r1=529918&r2=529919
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDataConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDataConfiguration.java
Wed Apr 18 01:45:48 2007
@@ -21,6 +21,7 @@
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.net.URL;
+import java.net.InetAddress;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -289,6 +290,10 @@
         conf.addProperty("calendar.list6", calendars);
         conf.addProperty("calendar.list.interpolated", "${calendar.string},2004-12-31");
         conf.addPropertyDirect("calendar.list7", new String[] { "2004-01-01", "2004-12-31"
});
+
+        conf.addProperty("ip.string", "127.0.0.1");
+        conf.addProperty("ip.string.interpolated", "${ip.string}");
+        conf.addProperty("ip.object", InetAddress.getByName("127.0.0.1"));
     }
 
     public void testGetConfiguration()
@@ -1653,6 +1658,31 @@
         ListAssert.assertEquals(new ArrayList(), conf.getCalendarList("empty"));
     }
 
+    public void testGetInetAddress() throws Exception
+    {
+        InetAddress expected = InetAddress.getByName("127.0.0.1");
+
+        // address as string
+        assertEquals(expected, conf.get(InetAddress.class, "ip.string"));
+
+        // address object
+        assertEquals(expected, conf.get(InetAddress.class, "ip.object"));
+
+        // interpolated value
+        assertEquals(expected, conf.get(InetAddress.class, "ip.string.interpolated"));
+
+        conf.setProperty("ip.unknownhost", "123");
+        try
+        {
+            conf.get(InetAddress.class, "ip.unknownhost");
+            fail("ConversionException should be thrown for unknown hosts");
+        }
+        catch (ConversionException e)
+        {
+            // expected
+        }
+    }
+
     public void testConversionException()
     {
         conf.addProperty("key1", new Object());
@@ -2252,6 +2282,16 @@
         {
             conf.getCalendarList("key2");
             fail("getCalendarList didn't throw a ConversionException");
+        }
+        catch (ConversionException e)
+        {
+            // expected
+        }
+
+        try
+        {
+            conf.get(InetAddress.class, "key1");
+            fail("getInetAddress didn't throw a ConversionException");
         }
         catch (ConversionException e)
         {

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=529919&r1=529918&r2=529919
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Wed Apr 18 01:45:48 2007
@@ -23,6 +23,10 @@
 
   <body>
     <release version="1.5-SNAPSHOT" date="in SVN" description="">
+      <action dev="ebourg" type="add">
+        DataConfiguration now supports the InetAddress type, properties are
+        converted to this type using the new generic getters.
+      </action>
       <action dev="ebourg" type="fix">
         The object getters in DataConfiguration with no default value
         (i.e getURL(key)) now throw a NoSuchElementException if the flag



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message