diff options
Diffstat (limited to 'src/main/java/com/c2kernel/utils')
| -rw-r--r-- | src/main/java/com/c2kernel/utils/Logger.java | 10 | ||||
| -rw-r--r-- | src/main/java/com/c2kernel/utils/ObjectProperties.java | 156 |
2 files changed, 159 insertions, 7 deletions
diff --git a/src/main/java/com/c2kernel/utils/Logger.java b/src/main/java/com/c2kernel/utils/Logger.java index 72c6918..176f190 100644 --- a/src/main/java/com/c2kernel/utils/Logger.java +++ b/src/main/java/com/c2kernel/utils/Logger.java @@ -170,17 +170,13 @@ public class Logger static public void initConsole(String id)
{
- String portString = Gateway.getProperty(id+".Console.port");
- int port = 0;
- try {
- port = Integer.parseInt(portString);
- } catch (NumberFormatException ex) {
+ int port = Gateway.getProperties().getInt(id+".Console.port", 0);
+ if (port == 0)
Logger.msg("No port defined for "+id+" console. Using any port.");
- }
mConsole = new SimpleTCPIPServer(port, ScriptConsole.class, 5);
mConsole.startListening();
- Gateway.setProperty(id+".Console.port", String.valueOf(mConsole.getPort()));
+ Gateway.getProperties().setProperty(id+".Console.port", String.valueOf(mConsole.getPort()));
}
static public int getConsolePort() {
diff --git a/src/main/java/com/c2kernel/utils/ObjectProperties.java b/src/main/java/com/c2kernel/utils/ObjectProperties.java new file mode 100644 index 0000000..79aee78 --- /dev/null +++ b/src/main/java/com/c2kernel/utils/ObjectProperties.java @@ -0,0 +1,156 @@ +package com.c2kernel.utils;
+
+import java.util.Enumeration;
+import java.util.Properties;
+
+public class ObjectProperties extends Properties {
+
+ public ObjectProperties() {
+ // TODO Auto-generated constructor stub
+ }
+
+ public ObjectProperties(Properties defaults) {
+ super(defaults);
+ // TODO Auto-generated constructor stub
+ }
+
+ public String getString(String propName) {
+ return super.getProperty(propName);
+ }
+
+ public String getString(String propName, String defaultValue) {
+ return super.getProperty(propName, defaultValue);
+ }
+
+ /**
+ * ogattaz proposal
+ *
+ * @param propName
+ * the name of the property
+ * @return the object value of the property. Returns null if the property
+ * doesn't exist or if the properties of the gateway is null
+ */
+ public Object getObject(String propName) {
+ return getObject(propName, null);
+ }
+
+ /**
+ * ogattaz proposal
+ *
+ * @param aPropertyName
+ * the name of the property
+ * @param defaultValue
+ * the default value.
+ * @return the object value of the property. Returns the default value if the property
+ * doesn't exist or if the properties of the gateway is null.
+ * @return
+ */
+ public Object getObject(String propName,
+ Object defaultValue) {
+
+ Object wValue = get(propName);
+ if (wValue == null) {
+ return defaultValue;
+ }
+ return wValue;
+ }
+
+ /**
+ * ogattaz proposal
+ *
+ * @param propName
+ * the name of the paroperty
+ * @return the boolean value of the property. Returns false if the property
+ * doesn't exist or if the value is not a String or a Boolean
+ * instance
+ */
+ public boolean getBoolean(String aPropertyName) {
+ return getBoolean(aPropertyName, Boolean.FALSE);
+ }
+
+ /**
+ * ogattaz proposal
+ *
+ * @param propName
+ * the name of the parameter stored in the clc file
+ * @param defaultValue
+ * the default value
+ * @return the boolean value of the property. Returns the default value if
+ * the property doesn't exist or if the value is not a String or a
+ * Boolean instance
+ */
+ public boolean getBoolean(String aPropertyName,
+ boolean defaultValue) {
+
+ Object wValue = getObject(aPropertyName, new Boolean(defaultValue));
+ if (wValue instanceof Boolean) {
+ return ((Boolean) wValue).booleanValue();
+ }
+ if (wValue instanceof String) {
+ return Boolean.parseBoolean((String) wValue);
+ }
+ Logger.error("getBoolean(): unable to retrieve a int value for ["+aPropertyName+"]. Returning default value ["+defaultValue+"]. object found="+wValue);
+
+ return defaultValue;
+ }
+
+ /**
+ * ogattaz proposal
+ *
+ * @param propName
+ * the name of the property
+ * @return the int value of the property. Returns -1 if the property doesn't
+ * exist or if the value is not a String or an Integer instance
+ */
+ public int getInt(String aPropertyName) {
+ return getInt(aPropertyName, -1);
+ }
+
+ /**
+ * ogattaz proposal
+ *
+ * @param propName
+ * the name of the property
+ * @param defaultValue
+ * the default value
+ * @return the int value of the property. Returns the default vakue if the
+ * property doesn't exist or if the value is not a String or an
+ * Integer instance
+ */
+ public int getInt(String aPropertyName, int defaultValue) {
+
+ Object wValue = getObject(aPropertyName, new Integer(defaultValue));
+ if (wValue instanceof Integer) {
+ return ((Integer) wValue).intValue();
+ }
+ if (wValue instanceof String) {
+ try {
+ return Integer.parseInt((String) wValue);
+ } catch (NumberFormatException ex) { }
+ }
+ Logger.error("getInt(): unable to retrieve a int value for ["+aPropertyName+"]. Returning default value ["+defaultValue+"]. object found="+wValue);
+ return defaultValue;
+ }
+
+ /**
+ * Allow setting of properties as Objects
+ *
+ * @param aPropertyName
+ * the name of the property
+ * @param aPropertyValue
+ */
+ public void setProperty(String aPropertyName, Object aPropertyValue) {
+ put(aPropertyName, aPropertyValue);
+ }
+
+ public void dumpProps(int logLevel) {
+ if (!Logger.doLog(logLevel)) return;
+ Logger.msg(logLevel, "Properties:");
+ for (Enumeration<?> e = propertyNames(); e.hasMoreElements();) {
+ String name = (String) e.nextElement();
+ Logger.msg(" "+name+": "+getProperty(name));
+ }
+ }
+
+
+}
|
