commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r529948 - in /jakarta/commons/proper/configuration/trunk: ./ src/java/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/ xdocs/
Date Wed, 18 Apr 2007 10:09:37 GMT
Author: ebourg
Date: Wed Apr 18 03:09:35 2007
New Revision: 529948

URL: http://svn.apache.org/viewvc?view=rev&rev=529948
Log:
DataConfiguration now supports the javax.mail.internet.InternetAddress type for email adresses

Modified:
    jakarta/commons/proper/configuration/trunk/project.xml
    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/project.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/project.xml?view=diff&rev=529948&r1=529947&r2=529948
==============================================================================
--- jakarta/commons/proper/configuration/trunk/project.xml (original)
+++ jakarta/commons/proper/configuration/trunk/project.xml Wed Apr 18 03:09:35 2007
@@ -333,6 +333,16 @@
       </properties>
     </dependency>
 
+    <dependency>
+      <groupId>javax.mail</groupId>
+      <artifactId>mail</artifactId>
+      <version>1.4</version>
+      <url>http://java.sun.com/products/javamail/</url>
+      <properties>
+        <optional>true</optional>
+      </properties>
+    </dependency>
+
     <!-- Needed for testing -->
 
     <dependency>

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=529948&r1=529947&r2=529948
==============================================================================
--- 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 03:09:35 2007
@@ -37,16 +37,27 @@
 
 /**
  * Decorator providing additional getters for any Configuration. This extended
- * Configuration supports more types: URL, Locale, Date, Calendar, Color,
- * InetAddress, as well as lists and arrays for all types.
+ * Configuration supports more types:
+ * <ul>
+ *   <li>{@link java.net.URL}</li>
+ *   <li>{@link java.util.Locale}</li>
+ *   <li>{@link java.util.Date}</li>
+ *   <li>{@link java.util.Calendar}</li>
+ *   <li>{@link java.awt.Color}</li>
+ *   <li>{@link java.net.InetAddress}</li>
+ *   <li>{@link javax.mail.internet.InternetAddress} (requires Javamail in the classpath)</li>
+ * </ul>
+ *
+ * Lists and arrays are available for all types.
  *
  * <h4>Example</h4>
  *
  * Configuration file <tt>config.properties</tt>:
  * <pre>
  * title.color = #0000FF
- * default.locales = fr,en,de
  * remote.host = 192.168.0.53
+ * default.locales = fr,en,de
+ * email.contact = ebourg@apache.org, oheger@apache.org
  * </pre>
  *
  * Usage:
@@ -58,8 +69,9 @@
  * Color color = config.getColor("title.color");
  *
  * // 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");
+ * Locale[] locales = (Locale[]) config.getArray(Locale.class, "default.locales");
+ * List contacts = config.getList(InternetAddress.class, "email.contact");
  * </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=529948&r1=529947&r2=529948
==============================================================================
--- 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 03:09:35 2007
@@ -20,12 +20,11 @@
 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.InetAddress;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -42,7 +41,6 @@
 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.
@@ -149,6 +147,10 @@
         {
             return PropertyConverter.toColor(value);
         }
+        else if (cls.getName().equals("javax.mail.internet.InternetAddress"))
+        {
+            return PropertyConverter.toInternetAddress(value);
+        }
         else if (InetAddress.class.isAssignableFrom(cls))
         {
             return PropertyConverter.toInetAddress(value);
@@ -648,6 +650,39 @@
         else
         {
             throw new ConversionException("The value " + value + " can't be converted to
a InetAddress");
+        }
+    }
+
+    /**
+     * Convert the specified value into an email address.
+     *
+     * @param value the value to convert
+     * @return the converted value
+     * @throws ConversionException thrown if the value cannot be converted to an email address
+     *
+     * @since 1.5
+     */
+    static Object toInternetAddress(Object value) throws ConversionException
+    {
+        if (value.getClass().getName().equals("javax.mail.internet.InternetAddress"))
+        {
+            return value;
+        }
+        else if (value instanceof String)
+        {
+            try
+            {
+                Constructor ctor = Class.forName("javax.mail.internet.InternetAddress").getConstructor(new
Class[] { String.class });
+                return ctor.newInstance(new Object[] { value });
+            }
+            catch (Exception e)
+            {
+                throw new ConversionException("The value " + value + " can't be converted
to a InternetAddress", e);
+            }
+        }
+        else
+        {
+            throw new ConversionException("The value " + value + " can't be converted to
a InternetAddress");
         }
     }
 

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=529948&r1=529947&r2=529948
==============================================================================
--- 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 03:09:35 2007
@@ -20,8 +20,8 @@
 import java.awt.Color;
 import java.math.BigDecimal;
 import java.math.BigInteger;
-import java.net.URL;
 import java.net.InetAddress;
+import java.net.URL;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -31,6 +31,7 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.NoSuchElementException;
+import javax.mail.internet.InternetAddress;
 
 import junit.framework.TestCase;
 import junitx.framework.ArrayAssert;
@@ -291,9 +292,15 @@
         conf.addProperty("calendar.list.interpolated", "${calendar.string},2004-12-31");
         conf.addPropertyDirect("calendar.list7", new String[] { "2004-01-01", "2004-12-31"
});
 
+        // host address
         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"));
+
+        // email address
+        conf.addProperty("email.string", "ebourg@apache.org");
+        conf.addProperty("email.string.interpolated", "${email.string}");
+        conf.addProperty("email.object", new InternetAddress("ebourg@apache.org"));
     }
 
     public void testGetConfiguration()
@@ -1683,6 +1690,31 @@
         }
     }
 
+    public void testGetInternetAddress() throws Exception
+    {
+        InternetAddress expected = new InternetAddress("ebourg@apache.org");
+
+        // address as string
+        assertEquals(expected, conf.get(InternetAddress.class, "email.string"));
+
+        // address object
+        assertEquals(expected, conf.get(InternetAddress.class, "email.object"));
+
+        // interpolated value
+        assertEquals(expected, conf.get(InternetAddress.class, "email.string.interpolated"));
+
+        conf.setProperty("email.invalid", "ebourg@apache@org");
+        try
+        {
+            conf.get(InternetAddress.class, "email.invalid");
+            fail("ConversionException should be thrown for invalid emails");
+        }
+        catch (ConversionException e)
+        {
+            // expected
+        }
+    }
+
     public void testConversionException()
     {
         conf.addProperty("key1", new Object());
@@ -2292,6 +2324,16 @@
         {
             conf.get(InetAddress.class, "key1");
             fail("getInetAddress didn't throw a ConversionException");
+        }
+        catch (ConversionException e)
+        {
+            // expected
+        }
+
+        try
+        {
+            conf.get(InternetAddress.class, "key1");
+            fail("getInternetAddress 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=529948&r1=529947&r2=529948
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Wed Apr 18 03:09:35 2007
@@ -24,8 +24,9 @@
   <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.
+        DataConfiguration now supports the java.net.InetAddress and
+        javax.mail.internet.InternetAddress types. Properties are
+        converted to these types using the new generic getters.
       </action>
       <action dev="ebourg" type="fix">
         The object getters in DataConfiguration with no default value



---------------------------------------------------------------------
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