summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/utils/Resource.java
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2014-01-23 12:09:30 +0100
committerAndrew Branson <andrew.branson@cern.ch>2014-01-23 12:09:30 +0100
commit8256917551c259df2b7e69e32cd74497e5394786 (patch)
tree068ec6e5acf41aabeb1538c6731c8e8f70d1ddb1 /src/main/java/com/c2kernel/utils/Resource.java
parent428d828ca640d1348979f9982d1c0bc0a489a3b4 (diff)
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
Diffstat (limited to 'src/main/java/com/c2kernel/utils/Resource.java')
-rw-r--r--src/main/java/com/c2kernel/utils/Resource.java136
1 files changed, 0 insertions, 136 deletions
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<String, String> txtCache = new Hashtable<String, String>();
- static private URL baseURL;
- static private HashMap<String, URL> moduleBaseURLs = new HashMap<String, URL>();
- static private HashMap<String, URL> allBaseURLs = new HashMap<String, URL>();
-
- 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<String, URL> getModuleBaseURLs() {
- return moduleBaseURLs;
- }
-
- public static HashMap<String, URL> 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<String, String> getAllTextResources(String resName) {
- HashMap<String, String> results = new HashMap<String, String>();
- 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";
- }
-
- }
-}