tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r1021903 - /openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java
Date Tue, 12 Oct 2010 20:00:51 GMT
Author: dblevins
Date: Tue Oct 12 20:00:51 2010
New Revision: 1021903

URL: http://svn.apache.org/viewvc?rev=1021903&view=rev
Log:
Can now use Options to get any object that has a constructor that takes a string

Modified:
    openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java

Modified: openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java
URL: http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java?rev=1021903&r1=1021902&r2=1021903&view=diff
==============================================================================
--- openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java
(original)
+++ openejb/branches/openejb-3.1.x/container/openejb-loader/src/main/java/org/apache/openejb/loader/Options.java
Tue Oct 12 20:00:51 2010
@@ -20,11 +20,11 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.EnumSet;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.Collections;
+import java.lang.reflect.Constructor;
 
 /**
  * The purpose of this class is to provide a more strongly typed version of a
@@ -65,7 +65,7 @@ import java.util.Collections;
  * Additionally TRUE is an alias for ALL and FALSE an alias for NONE.  This allows options
  * that used to support only true/false values to be further defined in the future without
  * breaking compatibility.
- * 
+ *
  * @version $Rev$ $Date$
  */
 public class Options {
@@ -82,6 +82,10 @@ public class Options {
         this.properties = properties;
     }
 
+    public Properties getProperties() {
+        return properties;
+    }
+
     public void setLogger(Log logger) {
         parent.setLogger(logger);
     }
@@ -100,10 +104,29 @@ public class Options {
         return value != null ? log(property, value) : parent.get(property, defaultValue);
     }
 
+    public <T> T get(String property, T defaultValue) {
+        if (defaultValue == null) throw new NullPointerException("defaultValue");
+
+        String value = properties.getProperty(property);
+
+        if (value == null || value.equals("")) return parent.get(property, defaultValue);
+
+        try {
+            Class<?> type = defaultValue.getClass();
+            Constructor<?> constructor = type.getConstructor(String.class);
+            T t = (T) constructor.newInstance(value);
+            return log(property, t);
+        } catch (Exception e) {
+            e.printStackTrace();
+            warn(property, value, e);
+            return parent.get(property, defaultValue);
+        }
+    }
+
     public int get(String property, int defaultValue) {
         String value = properties.getProperty(property);
 
-        if (value == null) return parent.get(property, defaultValue);
+        if (value == null || value.equals("")) return parent.get(property, defaultValue);
 
         try {
             return log(property, Integer.parseInt(value));
@@ -116,7 +139,7 @@ public class Options {
     public long get(String property, long defaultValue) {
         String value = properties.getProperty(property);
 
-        if (value == null) return parent.get(property, defaultValue);
+        if (value == null || value.equals("")) return parent.get(property, defaultValue);
 
         try {
             return log(property, Long.parseLong(value));
@@ -129,7 +152,7 @@ public class Options {
     public boolean get(String property, boolean defaultValue) {
         String value = properties.getProperty(property);
 
-        if (value == null) return parent.get(property, defaultValue);
+        if (value == null || value.equals("")) return parent.get(property, defaultValue);
 
         try {
             return log(property, Boolean.parseBoolean(value));
@@ -156,7 +179,7 @@ public class Options {
     public <T extends Enum<T>> T get(String property, T defaultValue) {
         String value = properties.getProperty(property);
 
-        if (value == null) return parent.get(property, defaultValue);
+        if (value == null || value.equals("")) return parent.get(property, defaultValue);
 
         if (defaultValue == null) throw new IllegalArgumentException("Must supply a default
for property " + property);
 
@@ -194,7 +217,7 @@ public class Options {
     protected <T extends Enum<T>> Set<T> getAll(String property, Set<T>
defaultValue, Class<T> enumType) {
         String value = properties.getProperty(property);
 
-        if (value == null) return parent.getAll(property, defaultValue, enumType);
+        if (value == null || value.equals("")) return parent.getAll(property, defaultValue,
enumType);
 
         // Shorthand for specifying ALL or NONE for any option
         // that allows for multiple values of the enum
@@ -239,7 +262,7 @@ public class Options {
         T value = map.get(name.toUpperCase());
 
         // Call Enum.valueOf for the clean exception
-        if (value == null) Enum.valueOf(enumType, name);
+        if (value == null || value.equals("")) Enum.valueOf(enumType, name);
 
         return value;
     }
@@ -334,6 +357,11 @@ public class Options {
         }
 
         @Override
+        public <T> T get(String property, T defaultValue) {
+            return log(property, defaultValue);
+        }
+
+        @Override
         public int get(String property, int defaultValue) {
             return log(property, defaultValue);
         }



Mime
View raw message