KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > CachedClassLoader


1 /*
2  * $Id: CachedClassLoader.java 5993 2005-10-21 06:32:11Z jonesde $
3  *
4  * Copyright (c) 2003 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 package org.ofbiz.base.util;
25
26 import java.net.URL JavaDoc;
27 import java.net.URLClassLoader JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Caching Class Loader
34  *
35  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
36  * @version $Rev: 5993 $
37  * @since 2.1
38  */

39 public class CachedClassLoader extends URLClassLoader JavaDoc {
40     
41     public static final String JavaDoc module = CachedClassLoader.class.getName();
42     
43     private String JavaDoc contextName;
44
45     public static Map JavaDoc globalClassNameClassMap = new HashMap JavaDoc();
46     public static HashSet JavaDoc globalBadClassNameSet = new HashSet JavaDoc();
47
48     public Map JavaDoc localClassNameClassMap = new HashMap JavaDoc();
49     public HashSet JavaDoc localBadClassNameSet = new HashSet JavaDoc();
50
51     public static Map JavaDoc globalResourceMap = new HashMap JavaDoc();
52     public static HashSet JavaDoc globalBadResourceNameSet = new HashSet JavaDoc();
53
54     public Map JavaDoc localResourceMap = new HashMap JavaDoc();
55     public HashSet JavaDoc localBadResourceNameSet = new HashSet JavaDoc();
56
57     static {
58         // setup some commonly used classes...
59
globalClassNameClassMap.put("Object", java.lang.Object JavaDoc.class);
60         globalClassNameClassMap.put("java.lang.Object", java.lang.Object JavaDoc.class);
61
62         globalClassNameClassMap.put("String", java.lang.String JavaDoc.class);
63         globalClassNameClassMap.put("java.lang.String", java.lang.String JavaDoc.class);
64         
65         globalClassNameClassMap.put("Boolean", java.lang.Boolean JavaDoc.class);
66         globalClassNameClassMap.put("java.lang.Boolean", java.lang.Boolean JavaDoc.class);
67
68         globalClassNameClassMap.put("BigDecimal", java.math.BigDecimal JavaDoc.class);
69         globalClassNameClassMap.put("java.math.BigDecimal", java.math.BigDecimal JavaDoc.class);
70         globalClassNameClassMap.put("Double", java.lang.Double JavaDoc.class);
71         globalClassNameClassMap.put("java.lang.Double", java.lang.Double JavaDoc.class);
72         globalClassNameClassMap.put("Float", java.lang.Float JavaDoc.class);
73         globalClassNameClassMap.put("java.lang.Float", java.lang.Float JavaDoc.class);
74         globalClassNameClassMap.put("Long", java.lang.Long JavaDoc.class);
75         globalClassNameClassMap.put("java.lang.Long", java.lang.Long JavaDoc.class);
76         globalClassNameClassMap.put("Integer", java.lang.Integer JavaDoc.class);
77         globalClassNameClassMap.put("java.lang.Integer", java.lang.Integer JavaDoc.class);
78
79         globalClassNameClassMap.put("Timestamp", java.sql.Timestamp JavaDoc.class);
80         globalClassNameClassMap.put("java.sql.Timestamp", java.sql.Timestamp JavaDoc.class);
81         globalClassNameClassMap.put("Time", java.sql.Time JavaDoc.class);
82         globalClassNameClassMap.put("java.sql.Time", java.sql.Time JavaDoc.class);
83         globalClassNameClassMap.put("Date", java.sql.Date JavaDoc.class);
84         globalClassNameClassMap.put("java.sql.Date", java.sql.Date JavaDoc.class);
85
86         globalClassNameClassMap.put("Locale", java.util.Locale JavaDoc.class);
87         globalClassNameClassMap.put("java.util.Locale", java.util.Locale JavaDoc.class);
88         
89         globalClassNameClassMap.put("java.util.Date", java.util.Date JavaDoc.class);
90         globalClassNameClassMap.put("Collection", java.util.Collection JavaDoc.class);
91         globalClassNameClassMap.put("java.util.Collection", java.util.Collection JavaDoc.class);
92         globalClassNameClassMap.put("List", java.util.List JavaDoc.class);
93         globalClassNameClassMap.put("java.util.List", java.util.List JavaDoc.class);
94         globalClassNameClassMap.put("Set", java.util.Set JavaDoc.class);
95         globalClassNameClassMap.put("java.util.Set", java.util.Set JavaDoc.class);
96         globalClassNameClassMap.put("Map", java.util.Map JavaDoc.class);
97         globalClassNameClassMap.put("java.util.Map", java.util.Map JavaDoc.class);
98         globalClassNameClassMap.put("HashMap", java.util.HashMap JavaDoc.class);
99         globalClassNameClassMap.put("java.util.HashMap", java.util.HashMap JavaDoc.class);
100
101         try {
102             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
103
104             // note: loadClass is necessary for these since this class doesn't know anything about the Entity Engine at compile time
105
globalClassNameClassMap.put("GenericValue", loader.loadClass("org.ofbiz.entity.GenericValue"));
106             globalClassNameClassMap.put("org.ofbiz.entity.GenericValue", loader.loadClass("org.ofbiz.entity.GenericValue"));
107             globalClassNameClassMap.put("GenericPK", loader.loadClass("org.ofbiz.entity.GenericPK"));
108             globalClassNameClassMap.put("org.ofbiz.entity.GenericPK", loader.loadClass("org.ofbiz.entity.GenericPK"));
109             globalClassNameClassMap.put("GenericEntity", loader.loadClass("org.ofbiz.entity.GenericEntity"));
110             globalClassNameClassMap.put("org.ofbiz.entity.GenericEntity", loader.loadClass("org.ofbiz.entity.GenericEntity"));
111         } catch (ClassNotFoundException JavaDoc e) {
112             Debug.logError(e, "Could not pre-initialize dynamically loaded class: ", module);
113         }
114     }
115         
116     public CachedClassLoader(URL JavaDoc[] url, ClassLoader JavaDoc parent, String JavaDoc contextName) {
117         super(url, parent);
118         this.contextName = contextName;
119         if (Debug.verboseOn()) {
120             Package JavaDoc[] paks = this.getPackages();
121             StringBuffer JavaDoc pakList = new StringBuffer JavaDoc();
122             for (int i = 0; i < paks.length; i++) {
123                 pakList.append(paks[i].getName());
124                 if (i < (paks.length - 1)) {
125                     pakList.append(":");
126                 }
127             }
128             Debug.logVerbose("Cached ClassLoader Packages : " + pakList.toString(), module);
129         }
130     }
131     
132     public CachedClassLoader(ClassLoader JavaDoc parent, String JavaDoc contextName) {
133         this(new URL JavaDoc[0], parent, contextName);
134     }
135     
136     public CachedClassLoader(URL JavaDoc[] url, ClassLoader JavaDoc parent) {
137         this(url, parent, "__globalContext");
138     }
139     
140     public String JavaDoc toString() {
141         return "org.ofbiz.base.util.CachedClassLoader(" + contextName + ") / " + getParent().toString();
142     }
143     
144     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
145         return loadClass(name, false);
146     }
147     
148     protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
149         //check glocal common classes, ie for all instances
150
Class JavaDoc theClass = (Class JavaDoc) globalClassNameClassMap.get(name);
151         
152         //check local classes, ie for this instance
153
if (theClass == null) theClass = (Class JavaDoc) localClassNameClassMap.get(name);
154
155         //make sure it is not a known bad class name
156
if (theClass == null) {
157             if (localBadClassNameSet.contains(name) || globalBadClassNameSet.contains(name)) {
158                 if (Debug.verboseOn()) Debug.logVerbose("Cached loader got a known bad class name: [" + name + "]", module);
159                 throw new ClassNotFoundException JavaDoc("Cached loader got a known bad class name: " + name);
160             }
161         }
162         
163         if (theClass == null) {
164             if (Debug.verboseOn()) Debug.logVerbose("Cached loader cache miss for class name: [" + name + "]", module);
165             
166             synchronized (this) {
167                 theClass = (Class JavaDoc) localClassNameClassMap.get(name);
168                 if (theClass == null) {
169                     try {
170                         theClass = super.loadClass(name, resolve);
171                         if (isGlobalPath(name)) {
172                             globalClassNameClassMap.put(name, theClass);
173                         } else {
174                             localClassNameClassMap.put(name, theClass);
175                         }
176                     } catch (ClassNotFoundException JavaDoc e) {
177                         //Debug.logInfo(e, module);
178
if (Debug.verboseOn()) Debug.logVerbose("Remembering invalid class name: [" + name + "]", module);
179                         if (isGlobalPath(name)) {
180                             globalBadClassNameSet.add(name);
181                         } else {
182                             localBadClassNameSet.add(name);
183                         }
184                         throw e;
185                     }
186                 }
187             }
188         }
189         return theClass;
190     }
191     
192     public URL JavaDoc getResource(String JavaDoc name) {
193         //check glocal common resources, ie for all instances
194
URL JavaDoc theResource = (URL JavaDoc) globalResourceMap.get(name);
195         
196         //check local resources, ie for this instance
197
if (theResource == null) theResource = (URL JavaDoc) localResourceMap.get(name);
198
199         //make sure it is not a known bad resource name
200
if (theResource == null) {
201             if (localBadResourceNameSet.contains(name) || globalBadResourceNameSet.contains(name)) {
202                 if (Debug.verboseOn()) Debug.logVerbose("Cached loader got a known bad resource name: [" + name + "]", module);
203                 return null;
204             }
205         }
206         
207         if (theResource == null) {
208             if (Debug.verboseOn()) Debug.logVerbose("Cached loader cache miss for resource name: [" + name + "]", module);
209             //Debug.logInfo("Cached loader cache miss for resource name: [" + name + "]", module);
210

211             synchronized (this) {
212                 theResource = (URL JavaDoc) localResourceMap.get(name);
213                 if (theResource == null) {
214                     theResource = super.getResource(name);
215                     if (theResource == null) {
216                         if (Debug.verboseOn()) Debug.logVerbose("Remembering invalid resource name: [" + name + "]", module);
217                         //Debug.logInfo("Remembering invalid resource name: [" + name + "]", module);
218
if (isGlobalPath(name)) {
219                             globalBadResourceNameSet.add(name);
220                         } else {
221                             localBadResourceNameSet.add(name);
222                         }
223                     } else {
224                         if (isGlobalPath(name)) {
225                             globalResourceMap.put(name, theResource);
226                         } else {
227                             localResourceMap.put(name, theResource);
228                         }
229                     }
230                 }
231             }
232         }
233         return theResource;
234     }
235     
236     protected boolean isGlobalPath(String JavaDoc name) {
237         if (name.startsWith("java.") || name.startsWith("java/") || name.startsWith("/java/")) return true;
238         if (name.startsWith("javax.") || name.startsWith("javax/") || name.startsWith("/javax/")) return true;
239         if (name.startsWith("sun.") || name.startsWith("sun/") || name.startsWith("/sun/")) return true;
240         if (name.startsWith("org.ofbiz.")) return true;
241         return false;
242     }
243 }
244
Popular Tags