From 3bb0aefa38c27221114a2db749d2eaa1a9df0336 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Mon, 15 Sep 2014 11:41:39 +0200 Subject: 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. --- .../java/com/c2kernel/utils/ObjectProperties.java | 40 +++++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'src/main/java/com/c2kernel/utils/ObjectProperties.java') 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 retArr = new ArrayList(); + StringTokenizer tok = new StringTokenizer((String)val, ","); + while (tok.hasMoreTokens()) + retArr.add(getInstance(tok.nextToken())); + return retArr; + } + else { + ArrayList retArr = new ArrayList(); + retArr.add(val); + return retArr; + } + } + + public ArrayList getInstances(String propName) throws InstantiationException, IllegalAccessException, ClassNotFoundException { + return getInstances(propName, null); + } } -- cgit v1.2.3