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/process/resource/Resource.java | 163 +++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/main/java/com/c2kernel/process/resource/Resource.java (limited to 'src/main/java/com/c2kernel/process/resource/Resource.java') 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); + } + } +} -- cgit v1.2.3