KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > config > ResourceLoader


1 /*
2  * $Id: ResourceLoader.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.config;
26
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
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 JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36
37 /**
38  * Loads resources using dynamically specified resource loader classes
39  *
40  *@author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
41  *@version $Rev: 5462 $
42  *@since 2.0
43  */

44 public abstract class ResourceLoader {
45     
46     public static final String JavaDoc module = ResourceLoader.class.getName();
47     protected static UtilCache loaderCache = new UtilCache("resource.ResourceLoaders", 0, 0);
48
49     protected String JavaDoc name;
50     protected String JavaDoc prefix;
51     protected String JavaDoc envName;
52
53     public static InputStream JavaDoc loadResource(String JavaDoc xmlFilename, String JavaDoc location, String JavaDoc loaderName) throws GenericConfigException {
54         ResourceLoader loader = getLoader(xmlFilename, loaderName);
55         if (loader == null) {
56             throw new IllegalArgumentException JavaDoc("ResourceLoader not found with name [" + loaderName + "] in " + xmlFilename);
57         }
58         return loader.loadResource(location);
59     }
60
61     public static URL JavaDoc getURL(String JavaDoc xmlFilename, String JavaDoc location, String JavaDoc loaderName) throws GenericConfigException {
62         ResourceLoader loader = getLoader(xmlFilename, loaderName);
63         if (loader == null) {
64             throw new IllegalArgumentException JavaDoc("ResourceLoader not found with name [" + loaderName + "] in " + xmlFilename);
65         }
66         return loader.getURL(location);
67     }
68
69     public static ResourceLoader getLoader(String JavaDoc xmlFilename, String JavaDoc 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 JavaDoc rootElement = getXmlRootElement(xmlFilename);
77
78                     Element JavaDoc 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 JavaDoc getXmlRootElement(String JavaDoc xmlFilename) throws GenericConfigException {
93         Document JavaDoc 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 JavaDoc xmlFilename) throws GenericConfigException {
103         UtilCache.clearCachesThatStartWith(xmlFilename);
104     }
105
106     public static Document JavaDoc getXmlDocument(String JavaDoc xmlFilename) throws GenericConfigException {
107         Document JavaDoc document = (Document JavaDoc) loaderCache.get(xmlFilename);
108
109         if (document == null) {
110             synchronized (ResourceLoader.class) {
111                 document = (Document JavaDoc) loaderCache.get(xmlFilename);
112                 if (document == null) {
113                     URL JavaDoc 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 JavaDoc e) {
122                         throw new GenericConfigException("Error reading " + xmlFilename + "", e);
123                     } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
124                         throw new GenericConfigException("Error reading " + xmlFilename + "", e);
125                     } catch (java.io.IOException JavaDoc 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 JavaDoc loaderElement) throws GenericConfigException {
139         if (loaderElement == null)
140             return null;
141
142         String JavaDoc loaderName = loaderElement.getAttribute("name");
143         String JavaDoc className = loaderElement.getAttribute("class");
144         ResourceLoader loader = null;
145
146         try {
147             Class JavaDoc lClass = null;
148
149             if (className != null && className.length() > 0) {
150                 try {
151                     ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
152                     lClass = classLoader.loadClass(className);
153                 } catch (ClassNotFoundException JavaDoc 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 JavaDoc e) {
161                 throw new GenericConfigException("Error loading Resource Loader class \"" + className + "\"", e);
162             } catch (InstantiationException JavaDoc e) {
163                 throw new GenericConfigException("Error loading Resource Loader class \"" + className + "\"", e);
164             }
165         } catch (SecurityException JavaDoc 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 JavaDoc name, String JavaDoc prefix, String JavaDoc envName) {
179         this.name = name;
180         this.prefix = prefix;
181         this.envName = envName;
182     }
183
184     /** Just a utility method to be used in loadResource by the implementing class * @param location
185      * @param location
186      * @return
187      */

188     public String JavaDoc fullLocation(String JavaDoc location) {
189         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
190
191         if (envName != null && envName.length() > 0) {
192             String JavaDoc propValue = System.getProperty(envName);
193             if (propValue == null) {
194                 String JavaDoc 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 JavaDoc(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 JavaDoc loadResource(String JavaDoc location) throws GenericConfigException;
208     public abstract URL JavaDoc getURL(String JavaDoc location) throws GenericConfigException;
209 }
210
Popular Tags