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 --- .../com/c2kernel/lifecycle/instance/Activity.java | 3 +- .../predefined/entitycreation/Outcome.java | 4 +- src/main/java/com/c2kernel/lookup/LDAPLookup.java | 3 +- .../persistency/ClusterStorageManager.java | 4 +- 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 ++++++ src/main/java/com/c2kernel/scripting/Script.java | 7 +- src/main/java/com/c2kernel/utils/Resource.java | 136 ----------------- 13 files changed, 257 insertions(+), 163 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 delete mode 100644 src/main/java/com/c2kernel/utils/Resource.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/c2kernel/lifecycle/instance/Activity.java b/src/main/java/com/c2kernel/lifecycle/instance/Activity.java index 889006c..064bd4d 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/Activity.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/Activity.java @@ -29,7 +29,6 @@ import com.c2kernel.process.Gateway; import com.c2kernel.utils.DateUtility; import com.c2kernel.utils.LocalObjectLoader; import com.c2kernel.utils.Logger; -import com.c2kernel.utils.Resource; /** * @version $Revision: 1.222 $ $Date: 2005/10/05 07:39:37 $ * @author $Author: abranson $ @@ -87,7 +86,7 @@ public class Activity extends WfVertex } catch (ObjectNotFoundException ex) { if (name.equals(getDefaultSMName()) && version == 0) { // default state machine not imported yet. Fake it. try { - String marshalledSM = Resource.getTextResource(null, "boot/SM/"+getDefaultSMName()+".xml"); + String marshalledSM = Gateway.getResource().getTextResource(null, "boot/SM/"+getDefaultSMName()+".xml"); StateMachine bootstrap = (StateMachine)Gateway.getMarshaller().unmarshall(marshalledSM); bootstrap.validate(); machine = bootstrap; diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/Outcome.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/Outcome.java index 9ab5793..1a966b5 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/Outcome.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/Outcome.java @@ -1,7 +1,7 @@ package com.c2kernel.lifecycle.instance.predefined.entitycreation; import com.c2kernel.common.ObjectNotFoundException; -import com.c2kernel.utils.Resource; +import com.c2kernel.process.Gateway; public class Outcome { public String schema, viewname, path, data; @@ -20,7 +20,7 @@ public class Outcome { public String getData(String ns) throws ObjectNotFoundException { if (data == null) - data = Resource.getTextResource(ns, path); + data = Gateway.getResource().getTextResource(ns, path); return data; } diff --git a/src/main/java/com/c2kernel/lookup/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/LDAPLookup.java index 23d30d4..c6f86d0 100644 --- a/src/main/java/com/c2kernel/lookup/LDAPLookup.java +++ b/src/main/java/com/c2kernel/lookup/LDAPLookup.java @@ -18,7 +18,6 @@ import com.c2kernel.process.Gateway; import com.c2kernel.property.PropertyDescription; import com.c2kernel.property.PropertyDescriptionList; import com.c2kernel.utils.Logger; -import com.c2kernel.utils.Resource; import com.novell.ldap.LDAPAttributeSet; import com.novell.ldap.LDAPConnection; import com.novell.ldap.LDAPDN; @@ -300,7 +299,7 @@ public class LDAPLookup public void install() throws ObjectNotFoundException { createBootTree(); - initTree( Resource.getTextResource(null, "boot/LDAPboot.txt")); + initTree( Gateway.getResource().getTextResource(null, "boot/LDAPboot.txt")); } public void initTree(String bootFile) diff --git a/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java b/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java index 16e942d..f7304b1 100644 --- a/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java +++ b/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java @@ -53,9 +53,9 @@ public class ClusterStorageManager { String newStorageClass = tok.nextToken(); try { try { - newStorage = (ClusterStorage)(Class.forName(newStorageClass).newInstance()); + newStorage = (ClusterStorage)(Gateway.getResource().getClassForName(newStorageClass).newInstance()); } catch (ClassNotFoundException ex2) { - newStorage = (ClusterStorage)(Class.forName("com.c2kernel.persistency."+newStorageClass).newInstance()); + newStorage = (ClusterStorage)(Gateway.getResource().getClassForName("com.c2kernel.persistency."+newStorageClass).newInstance()); } newStorage.open(); Logger.msg(5, "ClusterStorageManager.init() - Cluster storage " + newStorageClass + 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 diff --git a/src/main/java/com/c2kernel/scripting/Script.java b/src/main/java/com/c2kernel/scripting/Script.java index 9166e2e..e3b4478 100644 --- a/src/main/java/com/c2kernel/scripting/Script.java +++ b/src/main/java/com/c2kernel/scripting/Script.java @@ -29,7 +29,6 @@ import com.c2kernel.entity.proxy.ItemProxy; import com.c2kernel.process.Gateway; import com.c2kernel.utils.LocalObjectLoader; import com.c2kernel.utils.Logger; -import com.c2kernel.utils.Resource; /************************************************************************** * @@ -159,7 +158,7 @@ public class Script PrintWriter output = new PrintWriter(out); context.setWriter(output); context.setErrorWriter(output); - HashMap consoleScripts = Resource.getAllTextResources("textFiles/consoleScript."+lang+".txt"); + HashMap consoleScripts = Gateway.getResource().getAllTextResources("textFiles/consoleScript."+lang+".txt"); for (String ns : consoleScripts.keySet()) { try { engine.put(ScriptEngine.FILENAME, ns+" init script"); @@ -305,7 +304,7 @@ public class Script { try { - addInputParam(name, Class.forName(type)); + addInputParam(name, Gateway.getResource().getClassForName(type)); } catch (ClassNotFoundException ex) { @@ -351,7 +350,7 @@ public class Script { try { - addOutput(name, Class.forName(type)); + addOutput(name, Gateway.getResource().getClassForName(type)); } catch (ClassNotFoundException ex) { throw new ParameterException("Output parameter " + name + " specifies class " + type + " which was not found."); diff --git a/src/main/java/com/c2kernel/utils/Resource.java b/src/main/java/com/c2kernel/utils/Resource.java deleted file mode 100644 index d24b8f8..0000000 --- a/src/main/java/com/c2kernel/utils/Resource.java +++ /dev/null @@ -1,136 +0,0 @@ -package com.c2kernel.utils; - -//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; - -/************************************************************************** - * - * @author $Author: abranson $ $Date: 2005/10/05 07:39:36 $ - * @version $Revision: 1.71 $ - **************************************************************************/ -public class Resource { - static private Hashtable txtCache = new Hashtable(); - static private URL baseURL; - static private HashMap moduleBaseURLs = new HashMap(); - static private HashMap allBaseURLs = new HashMap(); - - public static void initKernelBaseURL() throws InvalidDataException { - baseURL = getURLorResURL("com/c2kernel/utils/resources/"); - allBaseURLs.put(null, baseURL); - } - - public static URL getKernelBaseURL() { - return baseURL; - } - - public static URL getKernelResourceURL(String resName) throws MalformedURLException { - return new URL(baseURL, resName); - } - - public static void addModuleBaseURL(String ns, URL newBaseURL) { - moduleBaseURLs.put(ns, newBaseURL); - allBaseURLs.put(ns, newBaseURL); - Logger.msg("Adding resource URL for "+ns+": "+newBaseURL.toString()); - } - - public static void addModuleBaseURL(String ns, String newBaseURL) throws InvalidDataException { - addModuleBaseURL(ns, getURLorResURL(newBaseURL)); - } - - public static URL getModuleBaseURL(String ns) { - return moduleBaseURLs.get(ns); - } - - public static HashMap getModuleBaseURLs() { - return moduleBaseURLs; - } - - public static HashMap getAllBaseURLs() { - return allBaseURLs; - } - - static public URL getModuleResourceURL(String ns, String resName) throws MalformedURLException { - return new URL(moduleBaseURLs.get(ns), resName); - } - - private static 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; - } - /************************************************************************** - * Gets any text resource files - **************************************************************************/ - - static 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; - } - - static 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; - } - - static 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); - } - } - - static public String getKernelVersion() { - try { - return FileStringUtility.url2String(getKernelResourceURL("textFiles/version.txt")); - } catch (Exception ex) { - return "No version info found"; - } - - } -} -- cgit v1.2.3