diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2014-09-15 11:41:39 +0200 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2014-09-15 11:41:39 +0200 |
| commit | 3bb0aefa38c27221114a2db749d2eaa1a9df0336 (patch) | |
| tree | 022a172278d54a35ae464ecd3fc8a19ab6231bf9 /src/main/java/com/c2kernel/utils | |
| parent | 95b8f12336bbc46f8d3e7cd407b89911aba8d2c5 (diff) | |
Add trim calls to ObjectProperties.getString() to discard any extra
whitespace around the values. Fixes #165
Remove old Gateway.getProperty methods - there should be no deprecated
methods in the 3.0 release
Move all getProperty() calls to getString or other so they will be
trimmed.
Introduce ObjectProperties.getInstances to create ArrayLists of objects
from comma-separated class name lists.
Diffstat (limited to 'src/main/java/com/c2kernel/utils')
| -rw-r--r-- | src/main/java/com/c2kernel/utils/ObjectProperties.java | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/src/main/java/com/c2kernel/utils/ObjectProperties.java b/src/main/java/com/c2kernel/utils/ObjectProperties.java index 8dacd2c..d9bf0f6 100644 --- a/src/main/java/com/c2kernel/utils/ObjectProperties.java +++ b/src/main/java/com/c2kernel/utils/ObjectProperties.java @@ -1,7 +1,9 @@ package com.c2kernel.utils;
+import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
+import java.util.StringTokenizer;
public class ObjectProperties extends Properties {
@@ -13,11 +15,13 @@ public class ObjectProperties extends Properties { }
public String getString(String propName) {
- return super.getProperty(propName);
+ return getString(propName, null);
}
public String getString(String propName, String defaultValue) {
- return super.getProperty(propName, defaultValue);
+ String value = super.getProperty(propName, defaultValue);
+ if (value!=null) value = value.trim();
+ return value;
}
/**
@@ -145,7 +149,11 @@ public class ObjectProperties extends Properties { Logger.msg(logLevel, "Properties:");
for (Enumeration<?> e = propertyNames(); e.hasMoreElements();) {
String name = (String) e.nextElement();
- Logger.msg(" "+name+" ("+getObject(name).getClass().getSimpleName()+"): "+getObject(name).toString());
+ Object value = getObject(name);
+ if (value == null)
+ Logger.msg(" "+name+": null");
+ else
+ Logger.msg(" "+name+" ("+getObject(name).getClass().getSimpleName()+"): '"+getObject(name).toString()+"'");
}
}
@@ -154,12 +162,34 @@ public class ObjectProperties extends Properties { if (prop == null || prop.equals(""))
throw new InstantiationException("Property '"+propName+"' was not defined. Cannot instantiate.");
if (prop instanceof String)
- return Class.forName((String)prop).newInstance();
+ return Class.forName(((String)prop).trim()).newInstance();
return prop;
}
public Object getInstance(String propName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return getInstance(propName, null);
}
-
+
+ public ArrayList<?> getInstances(String propName, Object defaultVal) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
+ Object val = getObject(propName, defaultVal);
+ if (val == null) return null;
+ if (val instanceof ArrayList)
+ return (ArrayList<?>)val;
+ else if (val instanceof String) {
+ ArrayList<Object> retArr = new ArrayList<Object>();
+ StringTokenizer tok = new StringTokenizer((String)val, ",");
+ while (tok.hasMoreTokens())
+ retArr.add(getInstance(tok.nextToken()));
+ return retArr;
+ }
+ else {
+ ArrayList<Object> retArr = new ArrayList<Object>();
+ retArr.add(val);
+ return retArr;
+ }
+ }
+
+ public ArrayList<?> getInstances(String propName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
+ return getInstances(propName, null);
+ }
}
|
