From 755bb76c94953b62a08e9fecf523bc5c42ec79ac Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 26 Feb 2014 10:29:36 +0100 Subject: Properties extended as ObjectProperties, which can return any Object, and wrap int and boolean parsing. Gateway.getProperties() returns the new ObjectProperty instance. Old Gateway property methods still present but deprecated. Refs #149 --- src/main/java/com/c2kernel/entity/agent/Job.java | 4 - .../c2kernel/entity/proxy/EntityProxyManager.java | 9 +- src/main/java/com/c2kernel/lookup/LDAPLookup.java | 2 +- .../java/com/c2kernel/lookup/LDAPProperties.java | 15 +- .../persistency/ClusterStorageManager.java | 6 +- .../c2kernel/persistency/XMLClusterStorage.java | 2 +- src/main/java/com/c2kernel/process/Bootstrap.java | 12 +- .../java/com/c2kernel/process/ClientShell.java | 4 +- src/main/java/com/c2kernel/process/Gateway.java | 13 +- .../com/c2kernel/process/module/ModuleManager.java | 6 +- .../java/com/c2kernel/scripting/ScriptConsole.java | 4 +- src/main/java/com/c2kernel/utils/Logger.java | 10 +- .../java/com/c2kernel/utils/ObjectProperties.java | 156 +++++++++++++++++++++ 13 files changed, 198 insertions(+), 45 deletions(-) create mode 100644 src/main/java/com/c2kernel/utils/ObjectProperties.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/c2kernel/entity/agent/Job.java b/src/main/java/com/c2kernel/entity/agent/Job.java index 7717fdb..02dd541 100644 --- a/src/main/java/com/c2kernel/entity/agent/Job.java +++ b/src/main/java/com/c2kernel/entity/agent/Job.java @@ -3,7 +3,6 @@ package com.c2kernel.entity.agent; import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.entity.C2KLocalObject; -import com.c2kernel.entity.proxy.AgentProxy; import com.c2kernel.entity.proxy.ItemProxy; import com.c2kernel.lifecycle.instance.Activity; import com.c2kernel.lifecycle.instance.stateMachine.Transition; @@ -63,8 +62,6 @@ public class Job implements C2KLocalObject private ItemProxy item = null; - private AgentProxy agent = null; - private boolean outcomeSet; /*************************************************************************** @@ -165,7 +162,6 @@ public class Job implements C2KLocalObject public void setAgentId(int id) { agentId = id; - agent = null; } public String getAgentName() diff --git a/src/main/java/com/c2kernel/entity/proxy/EntityProxyManager.java b/src/main/java/com/c2kernel/entity/proxy/EntityProxyManager.java index 9f134f0..c49e7f5 100644 --- a/src/main/java/com/c2kernel/entity/proxy/EntityProxyManager.java +++ b/src/main/java/com/c2kernel/entity/proxy/EntityProxyManager.java @@ -281,18 +281,17 @@ public class EntityProxyManager public static void initServer() { Logger.msg(5, "EntityProxyFactory::initServer - Starting....."); - String port = Gateway.getProperty("ItemServer.Proxy.port"); - serverName = Gateway.getProperty("ItemServer.name"); - if (port == null) { + int port = Gateway.getProperties().getInt("ItemServer.Proxy.port", 0); + serverName = Gateway.getProperties().getProperty("ItemServer.name"); + if (port == 0) { Logger.error("ItemServer.Proxy.port not defined in connect file. Remote proxies will not be informed of entity changes."); return; } // set up the proxy server try { - int portNo = Integer.parseInt(port); Logger.msg(5, "EntityProxyFactory::initServer - Initialising proxy informer on port "+port); - proxyServer = new SimpleTCPIPServer(portNo, ProxyClientConnection.class, 200); + proxyServer = new SimpleTCPIPServer(port, ProxyClientConnection.class, 200); proxyServer.startListening(); } catch (Exception ex) { Logger.error("Error setting up Proxy Server. Remote proxies will not be informed of entity changes."); diff --git a/src/main/java/com/c2kernel/lookup/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/LDAPLookup.java index c6f86d0..08a80b6 100644 --- a/src/main/java/com/c2kernel/lookup/LDAPLookup.java +++ b/src/main/java/com/c2kernel/lookup/LDAPLookup.java @@ -71,7 +71,7 @@ public class LDAPLookup DomainPath.mTypeRoot = "cn=domain,"+props.mLocalPath; mNextKeyManager = new NextKeyManager(this, "cn=last,"+EntityPath.mTypeRoot); - Logger.msg(7, "LDAP.useOldProps="+Gateway.getProperty("LDAP.useOldProps", "false")); + Logger.msg(7, "LDAP.useOldProps="+Gateway.getProperties().getBoolean("LDAP.useOldProps", false)); mPropManager = new LDAPPropertyManager(this); mRoleManager = new LDAPRoleManager(this, "cn=agent,"+DomainPath.mTypeRoot, EntityPath.mTypeRoot); diff --git a/src/main/java/com/c2kernel/lookup/LDAPProperties.java b/src/main/java/com/c2kernel/lookup/LDAPProperties.java index a9ae699..df0b85d 100644 --- a/src/main/java/com/c2kernel/lookup/LDAPProperties.java +++ b/src/main/java/com/c2kernel/lookup/LDAPProperties.java @@ -27,14 +27,13 @@ public class LDAPProperties public LDAPProperties() { - mGlobalPath = Gateway.getProperty( "LDAP.GlobalPath" ); - mRootPath = Gateway.getProperty( "LDAP.RootPath" ); - mLocalPath = Gateway.getProperty( "LDAP.LocalPath" ); - mPort = Integer.valueOf(Gateway.getProperty( "LDAP.port", "389" )); - mHost = Gateway.getProperty( "LDAP.host" ); - mUser = Gateway.getProperty( "LDAP.user" ); - mPassword = Gateway.getProperty( "LDAP.password" ); - mDbPath = Gateway.getProperty( "LDAP.dbPath" ); + mGlobalPath = Gateway.getProperties().getProperty( "LDAP.GlobalPath" ); + mRootPath = Gateway.getProperties().getProperty( "LDAP.RootPath" ); + mLocalPath = Gateway.getProperties().getProperty( "LDAP.LocalPath" ); + mPort = Gateway.getProperties().getInt( "LDAP.port", 389 ); + mHost = Gateway.getProperties().getProperty( "LDAP.host" ); + mUser = Gateway.getProperties().getProperty( "LDAP.user" ); + mPassword = Gateway.getProperties().getProperty( "LDAP.password" ); mRootPath += "," + mGlobalPath; mLocalPath += "," + mRootPath; diff --git a/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java b/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java index 6ca5502..402c466 100644 --- a/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java +++ b/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java @@ -40,7 +40,7 @@ public class ClusterStorageManager { * This property is usually process specific, and so should be in the server/client.conf and not the connect file. */ public ClusterStorageManager() throws ClusterStorageException { - Object clusterStorageProp = Gateway.getProperty("ClusterStorage"); + Object clusterStorageProp = Gateway.getProperties().getObject("ClusterStorage"); if (clusterStorageProp == null || clusterStorageProp.equals("")) { throw new ClusterStorageException("ClusterStorageManager.init() - no ClusterStorages defined. No persistency!"); } @@ -236,7 +236,7 @@ public class ClusterStorageManager { if (result != null) { // got it! // store it in the cache if (sysKeyMemCache == null) { // create cache if needed - boolean useWeak = Gateway.getProperty("Storage.useWeakCache","false").equals("true"); + boolean useWeak = Gateway.getProperties().getBoolean("Storage.useWeakCache", false); Logger.msg(7,"ClusterStorageManager.put() - Creating "+(useWeak?"Weak":"Strong")+" cache for entity "+sysKeyIntObj); sysKeyMemCache = useWeak?new WeakCache():new SoftCache(0); synchronized (memoryCache) { @@ -276,7 +276,7 @@ public class ClusterStorageManager { if (memoryCache.containsKey(sysKeyIntObj)) sysKeyMemCache = memoryCache.get(sysKeyIntObj); else { - boolean useWeak = Gateway.getProperty("Storage.useWeakCache","false").equals("true"); + boolean useWeak = Gateway.getProperties().getBoolean("Storage.useWeakCache", false); Logger.msg(7,"ClusterStorageManager.put() - Creating "+(useWeak?"Weak":"Strong")+" cache for entity "+sysKeyIntObj); sysKeyMemCache = useWeak?new WeakCache():new SoftCache(0); synchronized (memoryCache) { diff --git a/src/main/java/com/c2kernel/persistency/XMLClusterStorage.java b/src/main/java/com/c2kernel/persistency/XMLClusterStorage.java index 056fe28..f63dac6 100644 --- a/src/main/java/com/c2kernel/persistency/XMLClusterStorage.java +++ b/src/main/java/com/c2kernel/persistency/XMLClusterStorage.java @@ -18,7 +18,7 @@ public class XMLClusterStorage extends ClusterStorage { @Override public void open() throws ClusterStorageException { - String rootProp = Gateway.getProperty("XMLStorage.root"); + String rootProp = Gateway.getProperties().getProperty("XMLStorage.root"); if (rootProp == null) throw new ClusterStorageException("XMLClusterStorage.open() - Root path not given in config file."); diff --git a/src/main/java/com/c2kernel/process/Bootstrap.java b/src/main/java/com/c2kernel/process/Bootstrap.java index 5f23261..4f64adf 100644 --- a/src/main/java/com/c2kernel/process/Bootstrap.java +++ b/src/main/java/com/c2kernel/process/Bootstrap.java @@ -178,7 +178,7 @@ public class Bootstrap } CompositeActivity ca = new CompositeActivity(); - if (ns!=null && Gateway.getProperty("Module.debug", "false").equals("true")) { + if (ns!=null && Gateway.getProperties().getBoolean("Module.debug", false)) { String wf; if (itemType.equals("CA")) wf = "ManageCompositeActDef"; else if (itemType.equals("EA")) wf = "ManageElementaryActDef"; @@ -272,7 +272,7 @@ public class Bootstrap */ public static void checkAdminAgents() throws Exception { // check for administrative user - String adminPassword = Gateway.getProperty("AdminPassword", "admin12345"); + String adminPassword = Gateway.getProperties().getProperty("AdminPassword", "admin12345"); checkAgent("admin", adminPassword, "Admin", false); @@ -284,7 +284,7 @@ public class Bootstrap } public static void createServerItem() throws Exception { - String serverName = Gateway.getProperty("ItemServer.name"); + String serverName = Gateway.getProperties().getProperty("ItemServer.name"); thisServerPath = new DomainPath("/servers/"+serverName); EntityPath serverEntity; try { @@ -300,12 +300,12 @@ public class Bootstrap Gateway.getStorage().put(serverEntity.getSysKey(), new Property("Name", serverName, false), null); Gateway.getStorage().put(serverEntity.getSysKey(), new Property("Type", "Server", false), null); Gateway.getStorage().put(serverEntity.getSysKey(), new Property("KernelVersion", Gateway.getKernelVersion(), true), null); - if (Gateway.getProperty("ItemServer.Proxy.port") != null) + if (Gateway.getProperties().getProperty("ItemServer.Proxy.port") != null) Gateway.getStorage().put(serverEntity.getSysKey(), - new Property("ProxyPort", Gateway.getProperty("ItemServer.Proxy.port"), true), null); + new Property("ProxyPort", Gateway.getProperties().getProperty("ItemServer.Proxy.port"), false), null); Gateway.getStorage().put(serverEntity.getSysKey(), new Property("ConsolePort", String.valueOf(Logger.getConsolePort()), true), null); - Gateway.getProxyManager().connectToProxyServer(Gateway.getProperty("ItemServer.name"), Integer.parseInt(Gateway.getProperty("ItemServer.Proxy.port"))); + Gateway.getProxyManager().connectToProxyServer(Gateway.getProperties().getProperty("ItemServer.name"), Gateway.getProperties().getInt("ItemServer.Proxy.port")); } diff --git a/src/main/java/com/c2kernel/process/ClientShell.java b/src/main/java/com/c2kernel/process/ClientShell.java index 6ca0970..6a620d8 100644 --- a/src/main/java/com/c2kernel/process/ClientShell.java +++ b/src/main/java/com/c2kernel/process/ClientShell.java @@ -38,10 +38,10 @@ public class ClientShell extends StandardClient { public static void main(String[] args) throws Exception { Gateway.init(readC2KArgs(args)); - String authClassName = Gateway.getProperty("cli.auth"); + String authClassName = Gateway.getProperties().getProperty("cli.auth"); Class authClass = Gateway.getResource().getClassForName(authClassName); Authenticator auth = (Authenticator)authClass.newInstance(); - AgentProxy user = auth.authenticate(Gateway.getProperty("Name")); + AgentProxy user = auth.authenticate(Gateway.getProperties().getProperty("Name")); ClientShell shell = new ClientShell(user); shell.run(); } diff --git a/src/main/java/com/c2kernel/process/Gateway.java b/src/main/java/com/c2kernel/process/Gateway.java index b57e91b..0d9dbe6 100644 --- a/src/main/java/com/c2kernel/process/Gateway.java +++ b/src/main/java/com/c2kernel/process/Gateway.java @@ -26,6 +26,7 @@ import com.c2kernel.utils.CastorXMLUtility; import com.c2kernel.utils.FileStringUtility; import com.c2kernel.utils.Language; import com.c2kernel.utils.Logger; +import com.c2kernel.utils.ObjectProperties; /************************************************************************** @@ -50,7 +51,7 @@ import com.c2kernel.utils.Logger; public class Gateway { - static private Properties mC2KProps; + static private ObjectProperties mC2KProps; static private ModuleManager mModules; static private org.omg.CORBA.ORB mORB; static private boolean orbDestroyed = false; @@ -60,7 +61,7 @@ public class Gateway static private CorbaServer mCorbaServer; static private CastorXMLUtility mMarshaller; static private AgentProxy mCurrentUser = null; - static private ResourceLoader mResource; + static private ResourceLoader mResource; @@ -91,7 +92,7 @@ public class Gateway static public void init(Properties props, ResourceLoader res) throws InvalidDataException { // Init properties & resources - mC2KProps = new Properties(); + mC2KProps = new ObjectProperties(); mResource = res; if (mResource == null) mResource = new Resource(); @@ -413,15 +414,18 @@ public class Gateway return getProperty("LocalCentre"); } + @Deprecated static public String getProperty(String propName) { return getProperty(propName, null); } + @Deprecated static public String getProperty(String propName, String defaultValue) { if (mC2KProps == null) return defaultValue; return mC2KProps.getProperty(propName, defaultValue); } + @Deprecated static public void setProperty(String propName, String propValue) { if (mC2KProps == null) return; mC2KProps.put(propName, propValue); @@ -440,6 +444,9 @@ public class Gateway } } + static public ObjectProperties getProperties() { + return mC2KProps; + } static public String getKernelVersion() { try { diff --git a/src/main/java/com/c2kernel/process/module/ModuleManager.java b/src/main/java/com/c2kernel/process/module/ModuleManager.java index ca2e74b..3cb4904 100644 --- a/src/main/java/com/c2kernel/process/module/ModuleManager.java +++ b/src/main/java/com/c2kernel/process/module/ModuleManager.java @@ -137,19 +137,19 @@ public class ModuleManager { public void registerModules() throws ModuleException { ItemProxy serverEntity; try { - serverEntity = (ItemProxy)Gateway.getProxyManager().getProxy(new DomainPath("/servers/"+Gateway.getProperty("ItemServer.name"))); + serverEntity = (ItemProxy)Gateway.getProxyManager().getProxy(new DomainPath("/servers/"+Gateway.getProperties().getProperty("ItemServer.name"))); } catch (ObjectNotFoundException e) { throw new ModuleException("Cannot find local server name."); } Logger.debug(3, "Registering modules"); - boolean reset = Gateway.getProperty("Module.reset", "false").equals("true"); + boolean reset = Gateway.getProperties().getBoolean("Module.reset", false); for (Module thisMod : modules) { Logger.msg("Registering module "+thisMod.getName()); try { - String nsReset = Gateway.getProperty("Module."+thisMod.ns+".reset"); + String nsReset = Gateway.getProperties().getProperty("Module."+thisMod.ns+".reset"); boolean thisReset = nsReset == null?reset:nsReset.equals("true"); thisMod.importAll(serverEntity, modulesXML.get(thisMod.ns), thisReset); } catch (Exception e) { diff --git a/src/main/java/com/c2kernel/scripting/ScriptConsole.java b/src/main/java/com/c2kernel/scripting/ScriptConsole.java index 9b70641..83e210b 100644 --- a/src/main/java/com/c2kernel/scripting/ScriptConsole.java +++ b/src/main/java/com/c2kernel/scripting/ScriptConsole.java @@ -46,10 +46,10 @@ public class ScriptConsole implements SocketHandler { static { securityMode = ALLOW; - String hosts = Gateway.getProperty("ItemServer.Console.allow"); + String hosts = Gateway.getProperties().getProperty("ItemServer.Console.allow"); if (hosts == null || hosts.equals("")) { securityMode = DENY; - hosts = Gateway.getProperty("ItemServer.Console.deny"); + hosts = Gateway.getProperties().getProperty("ItemServer.Console.deny"); } if (hosts == null || hosts.equals("")) { // by default only allow localhost securityMode = ALLOW; 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)); + } + } + + +} -- cgit v1.2.3