1 25 package org.ofbiz.base.config; 26 27 import java.io.InputStream ; 28 import java.net.URL ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.base.util.UtilURL; 32 import org.ofbiz.base.util.UtilXml; 33 import org.ofbiz.base.util.cache.UtilCache; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 37 44 public abstract class ResourceLoader { 45 46 public static final String module = ResourceLoader.class.getName(); 47 protected static UtilCache loaderCache = new UtilCache("resource.ResourceLoaders", 0, 0); 48 49 protected String name; 50 protected String prefix; 51 protected String envName; 52 53 public static InputStream loadResource(String xmlFilename, String location, String loaderName) throws GenericConfigException { 54 ResourceLoader loader = getLoader(xmlFilename, loaderName); 55 if (loader == null) { 56 throw new IllegalArgumentException ("ResourceLoader not found with name [" + loaderName + "] in " + xmlFilename); 57 } 58 return loader.loadResource(location); 59 } 60 61 public static URL getURL(String xmlFilename, String location, String loaderName) throws GenericConfigException { 62 ResourceLoader loader = getLoader(xmlFilename, loaderName); 63 if (loader == null) { 64 throw new IllegalArgumentException ("ResourceLoader not found with name [" + loaderName + "] in " + xmlFilename); 65 } 66 return loader.getURL(location); 67 } 68 69 public static ResourceLoader getLoader(String xmlFilename, String loaderName) throws GenericConfigException { 70 ResourceLoader loader = (ResourceLoader) loaderCache.get(xmlFilename + "::" + loaderName); 71 72 if (loader == null) { 73 synchronized (ResourceLoader.class) { 74 loader = (ResourceLoader) loaderCache.get(xmlFilename + "::" + loaderName); 75 if (loader == null) { 76 Element rootElement = getXmlRootElement(xmlFilename); 77 78 Element loaderElement = UtilXml.firstChildElement(rootElement, "resource-loader", "name", loaderName); 79 80 loader = makeLoader(loaderElement); 81 82 if (loader != null) { 83 loaderCache.put(xmlFilename + "::" + loaderName, loader); 84 } 85 } 86 } 87 } 88 89 return loader; 90 } 91 92 public static Element getXmlRootElement(String xmlFilename) throws GenericConfigException { 93 Document document = ResourceLoader.getXmlDocument(xmlFilename); 94 95 if (document != null) { 96 return document.getDocumentElement(); 97 } else { 98 return null; 99 } 100 } 101 102 public static void invalidateDocument(String xmlFilename) throws GenericConfigException { 103 UtilCache.clearCachesThatStartWith(xmlFilename); 104 } 105 106 public static Document getXmlDocument(String xmlFilename) throws GenericConfigException { 107 Document document = (Document ) loaderCache.get(xmlFilename); 108 109 if (document == null) { 110 synchronized (ResourceLoader.class) { 111 document = (Document ) loaderCache.get(xmlFilename); 112 if (document == null) { 113 URL confUrl = UtilURL.fromResource(xmlFilename); 114 115 if (confUrl == null) { 116 throw new GenericConfigException("ERROR: could not find the [" + xmlFilename + "] XML file on the classpath"); 117 } 118 119 try { 120 document = UtilXml.readXmlDocument(confUrl); 121 } catch (org.xml.sax.SAXException e) { 122 throw new GenericConfigException("Error reading " + xmlFilename + "", e); 123 } catch (javax.xml.parsers.ParserConfigurationException e) { 124 throw new GenericConfigException("Error reading " + xmlFilename + "", e); 125 } catch (java.io.IOException e) { 126 throw new GenericConfigException("Error reading " + xmlFilename + "", e); 127 } 128 129 if (document != null) { 130 loaderCache.put(xmlFilename, document); 131 } 132 } 133 } 134 } 135 return document; 136 } 137 138 public static ResourceLoader makeLoader(Element loaderElement) throws GenericConfigException { 139 if (loaderElement == null) 140 return null; 141 142 String loaderName = loaderElement.getAttribute("name"); 143 String className = loaderElement.getAttribute("class"); 144 ResourceLoader loader = null; 145 146 try { 147 Class lClass = null; 148 149 if (className != null && className.length() > 0) { 150 try { 151 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 152 lClass = classLoader.loadClass(className); 153 } catch (ClassNotFoundException e) { 154 throw new GenericConfigException("Error loading Resource Loader class \"" + className + "\"", e); 155 } 156 } 157 158 try { 159 loader = (ResourceLoader) lClass.newInstance(); 160 } catch (IllegalAccessException e) { 161 throw new GenericConfigException("Error loading Resource Loader class \"" + className + "\"", e); 162 } catch (InstantiationException e) { 163 throw new GenericConfigException("Error loading Resource Loader class \"" + className + "\"", e); 164 } 165 } catch (SecurityException e) { 166 throw new GenericConfigException("Error loading Resource Loader class \"" + className + "\"", e); 167 } 168 169 if (loader != null) { 170 loader.init(loaderName, loaderElement.getAttribute("prefix"), loaderElement.getAttribute("prepend-env")); 171 } 172 173 return loader; 174 } 175 176 protected ResourceLoader() {} 177 178 public void init(String name, String prefix, String envName) { 179 this.name = name; 180 this.prefix = prefix; 181 this.envName = envName; 182 } 183 184 188 public String fullLocation(String location) { 189 StringBuffer buf = new StringBuffer (); 190 191 if (envName != null && envName.length() > 0) { 192 String propValue = System.getProperty(envName); 193 if (propValue == null) { 194 String errMsg = "The Java environment (-Dxxx=yyy) variable with name " + envName + " is not set, cannot load resource."; 195 Debug.logError(errMsg, module); 196 throw new IllegalArgumentException (errMsg); 197 } 198 buf.append(propValue); 199 } 200 if (prefix != null && prefix.length() > 0) { 201 buf.append(prefix); 202 } 203 buf.append(location); 204 return buf.toString(); 205 } 206 207 public abstract InputStream loadResource(String location) throws GenericConfigException; 208 public abstract URL getURL(String location) throws GenericConfigException; 209 } 210 | Popular Tags |