From 8256917551c259df2b7e69e32cd74497e5394786 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 23 Jan 2014 12:09:30 +0100 Subject: Refactored Resource into a new ResourceLoader interface, which allows CRISTAL processes in other enviroments with complex class loading (e.g. OSGi) to supply their own resource and class loader to the kernel and its modules. Fixes #149 --- src/main/java/com/c2kernel/process/Bootstrap.java | 9 +- .../java/com/c2kernel/process/ClientShell.java | 2 +- src/main/java/com/c2kernel/process/Gateway.java | 40 ++++- .../java/com/c2kernel/process/module/Module.java | 3 +- .../com/c2kernel/process/module/ModuleManager.java | 5 +- .../com/c2kernel/process/resource/Resource.java | 163 +++++++++++++++++++++ .../c2kernel/process/resource/ResourceLoader.java | 41 ++++++ 7 files changed, 248 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/c2kernel/process/resource/Resource.java create mode 100644 src/main/java/com/c2kernel/process/resource/ResourceLoader.java (limited to 'src/main/java/com/c2kernel/process') diff --git a/src/main/java/com/c2kernel/process/Bootstrap.java b/src/main/java/com/c2kernel/process/Bootstrap.java index 51b98b0..5f23261 100644 --- a/src/main/java/com/c2kernel/process/Bootstrap.java +++ b/src/main/java/com/c2kernel/process/Bootstrap.java @@ -34,7 +34,6 @@ import com.c2kernel.property.PropertyDescriptionList; import com.c2kernel.utils.FileStringUtility; import com.c2kernel.utils.LocalObjectLoader; import com.c2kernel.utils.Logger; -import com.c2kernel.utils.Resource; /** * @version $Revision: 1.25 $ $Date: 2006/01/10 09:48:32 $ @@ -86,7 +85,7 @@ public class Bootstrap public static void verifyBootDataItems() throws Exception { String bootItems; Logger.msg(1, "Verifying kernel boot items"); - bootItems = FileStringUtility.url2String(Resource.getKernelResourceURL("boot/allbootitems.txt")); + bootItems = FileStringUtility.url2String(Gateway.getResource().getKernelResourceURL("boot/allbootitems.txt")); verifyBootDataItems(bootItems, null, true); Logger.msg(1, "Boot data items complete"); } @@ -99,7 +98,7 @@ public class Bootstrap String itemType = thisItem.substring(0,delim); String itemName = thisItem.substring(delim+1); try { - String data = Resource.getTextResource(ns, "boot/"+thisItem+(itemType.equals("OD")?".xsd":".xml")); + String data = Gateway.getResource().getTextResource(ns, "boot/"+thisItem+(itemType.equals("OD")?".xsd":".xml")); if (data == null) Logger.die("No data found for "+getDataType(itemType)+" "+itemName); verifyResource(ns, itemName, 0, itemType, data, reset); @@ -169,7 +168,7 @@ public class Bootstrap */ private static ItemProxy createResourceItem(String itemType, String itemName, String ns) throws Exception { // create props - PropertyDescriptionList pdList = (PropertyDescriptionList)Gateway.getMarshaller().unmarshall(Resource.getTextResource(null, "boot/property/"+itemType+"Prop.xml")); + PropertyDescriptionList pdList = (PropertyDescriptionList)Gateway.getMarshaller().unmarshall(Gateway.getResource().getTextResource(null, "boot/property/"+itemType+"Prop.xml")); PropertyArrayList props = new PropertyArrayList(); for (int i = 0; i < pdList.list.size(); i++) { PropertyDescription pd = pdList.list.get(i); @@ -300,7 +299,7 @@ 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", Resource.getKernelVersion(), true), null); + Gateway.getStorage().put(serverEntity.getSysKey(), new Property("KernelVersion", Gateway.getKernelVersion(), true), null); if (Gateway.getProperty("ItemServer.Proxy.port") != null) Gateway.getStorage().put(serverEntity.getSysKey(), new Property("ProxyPort", Gateway.getProperty("ItemServer.Proxy.port"), true), null); diff --git a/src/main/java/com/c2kernel/process/ClientShell.java b/src/main/java/com/c2kernel/process/ClientShell.java index f109155..6ca0970 100644 --- a/src/main/java/com/c2kernel/process/ClientShell.java +++ b/src/main/java/com/c2kernel/process/ClientShell.java @@ -39,7 +39,7 @@ public class ClientShell extends StandardClient { public static void main(String[] args) throws Exception { Gateway.init(readC2KArgs(args)); String authClassName = Gateway.getProperty("cli.auth"); - Class authClass = Class.forName(authClassName); + Class authClass = Gateway.getResource().getClassForName(authClassName); Authenticator auth = (Authenticator)authClass.newInstance(); AgentProxy user = auth.authenticate(Gateway.getProperty("Name")); ClientShell shell = new ClientShell(user); diff --git a/src/main/java/com/c2kernel/process/Gateway.java b/src/main/java/com/c2kernel/process/Gateway.java index 53fa36f..b57e91b 100644 --- a/src/main/java/com/c2kernel/process/Gateway.java +++ b/src/main/java/com/c2kernel/process/Gateway.java @@ -20,11 +20,12 @@ import com.c2kernel.lookup.LDAPProperties; import com.c2kernel.persistency.ClusterStorageException; import com.c2kernel.persistency.TransactionManager; import com.c2kernel.process.module.ModuleManager; +import com.c2kernel.process.resource.Resource; +import com.c2kernel.process.resource.ResourceLoader; import com.c2kernel.utils.CastorXMLUtility; import com.c2kernel.utils.FileStringUtility; import com.c2kernel.utils.Language; import com.c2kernel.utils.Logger; -import com.c2kernel.utils.Resource; /************************************************************************** @@ -59,6 +60,7 @@ public class Gateway static private CorbaServer mCorbaServer; static private CastorXMLUtility mMarshaller; static private AgentProxy mCurrentUser = null; + static private ResourceLoader mResource; @@ -73,17 +75,32 @@ public class Gateway * @throws InvalidDataException - invalid properties caused a failure in initialisation */ static public void init(Properties props) throws InvalidDataException { + init(props, null); + } + + /** + * Initialises the Gateway and all of the client objects it holds, with + * the exception of the LDAPLookup, which is initialised during connect() + * + * @param props - java.util.Properties containing all application properties. + * If null, the java system properties are used + * @param res - ResourceLoader for the kernel to use to resolve all class resource requests + * such as for bootstrap descriptions and version information + * @throws InvalidDataException - invalid properties caused a failure in initialisation + */ + static public void init(Properties props, ResourceLoader res) throws InvalidDataException { // Init properties & resources mC2KProps = new Properties(); - Resource.initKernelBaseURL(); + mResource = res; + if (mResource == null) mResource = new Resource(); // report version info - Logger.msg("Kernel version: "+Resource.getKernelVersion()); + Logger.msg("Kernel version: "+getKernelVersion()); // load kernel mapfiles try { - mMarshaller = new CastorXMLUtility(Resource.getKernelResourceURL("mapFiles/")); + mMarshaller = new CastorXMLUtility(mResource.getKernelResourceURL("mapFiles/")); } catch (MalformedURLException e1) { throw new InvalidDataException("Invalid Resource Location", ""); } @@ -381,6 +398,11 @@ public class Gateway { return mMarshaller; } + + static public ResourceLoader getResource() + { + return mResource; + } static public EntityProxyManager getProxyManager() { @@ -417,5 +439,15 @@ public class Gateway Logger.msg(" "+name+": "+getProperty(name)); } } + + + static public String getKernelVersion() { + try { + return mResource.getTextResource(null, "textFiles/version.txt"); + } catch (Exception ex) { + return "No version info found"; + } + + } } diff --git a/src/main/java/com/c2kernel/process/module/Module.java b/src/main/java/com/c2kernel/process/module/Module.java index 58cf503..fda4b1b 100644 --- a/src/main/java/com/c2kernel/process/module/Module.java +++ b/src/main/java/com/c2kernel/process/module/Module.java @@ -17,7 +17,6 @@ import com.c2kernel.process.Gateway; import com.c2kernel.scripting.ErrorInfo; import com.c2kernel.scripting.ScriptingEngineException; import com.c2kernel.utils.Logger; -import com.c2kernel.utils.Resource; public class Module { @@ -79,7 +78,7 @@ public class Module { for (ModuleResource thisRes : imports.getResources()) { try { - Bootstrap.verifyResource(ns, thisRes.name, thisRes.version, thisRes.resourceType, Resource.getTextResource(ns, thisRes.resourceLocation), reset); + Bootstrap.verifyResource(ns, thisRes.name, thisRes.version, thisRes.resourceType, Gateway.getResource().getTextResource(ns, thisRes.resourceLocation), reset); } catch (Exception ex) { Logger.error(ex); } diff --git a/src/main/java/com/c2kernel/process/module/ModuleManager.java b/src/main/java/com/c2kernel/process/module/ModuleManager.java index f1d26fb..ca2e74b 100644 --- a/src/main/java/com/c2kernel/process/module/ModuleManager.java +++ b/src/main/java/com/c2kernel/process/module/ModuleManager.java @@ -17,7 +17,6 @@ import com.c2kernel.process.Gateway; import com.c2kernel.scripting.ScriptingEngineException; import com.c2kernel.utils.FileStringUtility; import com.c2kernel.utils.Logger; -import com.c2kernel.utils.Resource; public class ModuleManager { ArrayList modules = new ArrayList(); @@ -30,7 +29,7 @@ public class ModuleManager { this.isServer = isServer; try { Schema moduleSchema = new Schema("Module", 0, - FileStringUtility.url2String(Resource.getKernelResourceURL("boot/OD/Module.xsd"))); + FileStringUtility.url2String(Gateway.getResource().getKernelResourceURL("boot/OD/Module.xsd"))); moduleValidator = new OutcomeValidator(moduleSchema); } catch (InvalidDataException ex) { Logger.error(ex); @@ -48,7 +47,7 @@ public class ModuleManager { if (errors.length() > 0) throw new ModuleException("Module XML found at "+newModuleURL+" was not valid: "+errors); Module newModule = (Module)Gateway.getMarshaller().unmarshall(moduleXML); - if (newModule.resURL != null && newModule.resURL.length()>0) Resource.addModuleBaseURL(newModule.ns, newModule.resURL); + if (newModule.resURL != null && newModule.resURL.length()>0) Gateway.getResource().addModuleBaseURL(newModule.ns, newModule.resURL); modules.add(newModule); modulesXML.put(newModule.ns, moduleXML); if (loadedModules.contains(newModule.getName())) throw new ModuleException("Module name clash: "+newModule.getName()); diff --git a/src/main/java/com/c2kernel/process/resource/Resource.java b/src/main/java/com/c2kernel/process/resource/Resource.java new file mode 100644 index 0000000..4d07f35 --- /dev/null +++ b/src/main/java/com/c2kernel/process/resource/Resource.java @@ -0,0 +1,163 @@ +package com.c2kernel.process.resource; + +//Java +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.Hashtable; + +import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.utils.FileStringUtility; +import com.c2kernel.utils.Logger; + +/************************************************************************** + * + * @author $Author: abranson $ $Date: 2005/10/05 07:39:36 $ + * @version $Revision: 1.71 $ + **************************************************************************/ +public class Resource implements ResourceLoader { + private final Hashtable txtCache = new Hashtable(); + private final URL baseURL; + private final HashMap moduleBaseURLs = new HashMap(); + private final HashMap allBaseURLs = new HashMap(); + + public Resource() throws InvalidDataException { + baseURL = getURLorResURL("com/c2kernel/utils/resources/"); + allBaseURLs.put(null, baseURL); + } + + /* (non-Javadoc) + * @see com.c2kernel.utils.ResourceLoader#getKernelBaseURL() + */ + @Override + public URL getKernelBaseURL() { + return baseURL; + } + + @Override + public Class getClassForName(String name) throws ClassNotFoundException { + return Class.forName(name); + } + + /* (non-Javadoc) + * @see com.c2kernel.utils.ResourceLoader#getKernelResourceURL(java.lang.String) + */ + @Override + public URL getKernelResourceURL(String resName) throws MalformedURLException { + return new URL(baseURL, resName); + } + + /* (non-Javadoc) + * @see com.c2kernel.utils.ResourceLoader#addModuleBaseURL(java.lang.String, java.net.URL) + */ + @Override + public void addModuleBaseURL(String ns, URL newBaseURL) { + moduleBaseURLs.put(ns, newBaseURL); + allBaseURLs.put(ns, newBaseURL); + Logger.msg("Adding resource URL for "+ns+": "+newBaseURL.toString()); + } + + /* (non-Javadoc) + * @see com.c2kernel.utils.ResourceLoader#addModuleBaseURL(java.lang.String, java.lang.String) + */ + @Override + public void addModuleBaseURL(String ns, String newBaseURL) throws InvalidDataException { + addModuleBaseURL(ns, getURLorResURL(newBaseURL)); + } + + /* (non-Javadoc) + * @see com.c2kernel.utils.ResourceLoader#getModuleBaseURLs() + */ + @Override + public HashMap getModuleBaseURLs() { + return moduleBaseURLs; + } + + private HashMap getAllBaseURLs() { + return allBaseURLs; + } + + /* (non-Javadoc) + * @see com.c2kernel.utils.ResourceLoader#getModuleResourceURL(java.lang.String, java.lang.String) + */ + @Override + public URL getModuleResourceURL(String ns, String resName) throws MalformedURLException { + return new URL(moduleBaseURLs.get(ns), resName); + } + + static private URL getURLorResURL(String newURL) throws InvalidDataException { + URL result; + try { + result = new URL(newURL); + } catch (java.net.MalformedURLException ex) { + //it is assumed that a 'wrong' URL denotes a directory + //of files resolvable from the CLASSPATH + result = Resource.class.getClassLoader().getResource(newURL); + } + if (result == null) { + Logger.error("URL "+newURL+" could not be found"); + throw new InvalidDataException(); + } + return result; + } + /* (non-Javadoc) + * @see com.c2kernel.utils.ResourceLoader#findTextResource(java.lang.String) + */ + + @Override + public String findTextResource(String resName) { + for (String ns : getAllBaseURLs().keySet()) { + try { + return getTextResource(ns, resName); + } catch (ObjectNotFoundException ex) { } + } + Logger.warning("Text resource '"+resName+"' not found."); + return null; + } + + /* (non-Javadoc) + * @see com.c2kernel.utils.ResourceLoader#getAllTextResources(java.lang.String) + */ + @Override + public HashMap getAllTextResources(String resName) { + HashMap results = new HashMap(); + for (String ns : getAllBaseURLs().keySet()) { + try { + results.put(ns, getTextResource(ns, resName)); + } catch (ObjectNotFoundException ex) { } + } + return results; + } + + /* (non-Javadoc) + * @see com.c2kernel.utils.ResourceLoader#getTextResource(java.lang.String, java.lang.String) + */ + @Override + public String getTextResource(String ns, String resName) throws ObjectNotFoundException + // throws IOException + { + Logger.msg(8, "Resource::getTextResource() - Getting resource from "+ns+": " + resName); + + if (txtCache.containsKey(ns+'/'+resName)) { + return txtCache.get(ns+'/'+resName); + } + + try { + + String newRes = null; + URL loc; + + if (ns == null) // kernel + loc = getKernelResourceURL(resName); + else + loc = getModuleResourceURL(ns, resName); + Logger.msg(5, "Loading resource: "+loc.toString()); + newRes = FileStringUtility.url2String(loc); + txtCache.put(ns+'/'+resName, newRes); + return newRes; + } catch (Exception e) { + throw new ObjectNotFoundException(e.getMessage(),null); + } + } +} diff --git a/src/main/java/com/c2kernel/process/resource/ResourceLoader.java b/src/main/java/com/c2kernel/process/resource/ResourceLoader.java new file mode 100644 index 0000000..fdf2508 --- /dev/null +++ b/src/main/java/com/c2kernel/process/resource/ResourceLoader.java @@ -0,0 +1,41 @@ +package com.c2kernel.process.resource; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectNotFoundException; + +public interface ResourceLoader { + + public URL getKernelBaseURL(); + + public URL getKernelResourceURL(String resName) + throws MalformedURLException; + + public void addModuleBaseURL(String ns, URL newBaseURL); + + public void addModuleBaseURL(String ns, String newBaseURL) + throws InvalidDataException; + + public HashMap getModuleBaseURLs(); + + public URL getModuleResourceURL(String ns, String resName) + throws MalformedURLException; + + /************************************************************************** + * Gets any text resource files + **************************************************************************/ + + public String findTextResource(String resName); + + public HashMap getAllTextResources(String resName); + + public String getTextResource(String ns, String resName) + throws ObjectNotFoundException; + + public Class getClassForName(String name) + throws ClassNotFoundException; + +} \ No newline at end of file -- cgit v1.2.3