1 24 package org.ofbiz.base.util; 25 26 import java.net.URL ; 27 import java.net.URLClassLoader ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Map ; 31 32 39 public class CachedClassLoader extends URLClassLoader { 40 41 public static final String module = CachedClassLoader.class.getName(); 42 43 private String contextName; 44 45 public static Map globalClassNameClassMap = new HashMap (); 46 public static HashSet globalBadClassNameSet = new HashSet (); 47 48 public Map localClassNameClassMap = new HashMap (); 49 public HashSet localBadClassNameSet = new HashSet (); 50 51 public static Map globalResourceMap = new HashMap (); 52 public static HashSet globalBadResourceNameSet = new HashSet (); 53 54 public Map localResourceMap = new HashMap (); 55 public HashSet localBadResourceNameSet = new HashSet (); 56 57 static { 58 globalClassNameClassMap.put("Object", java.lang.Object .class); 60 globalClassNameClassMap.put("java.lang.Object", java.lang.Object .class); 61 62 globalClassNameClassMap.put("String", java.lang.String .class); 63 globalClassNameClassMap.put("java.lang.String", java.lang.String .class); 64 65 globalClassNameClassMap.put("Boolean", java.lang.Boolean .class); 66 globalClassNameClassMap.put("java.lang.Boolean", java.lang.Boolean .class); 67 68 globalClassNameClassMap.put("BigDecimal", java.math.BigDecimal .class); 69 globalClassNameClassMap.put("java.math.BigDecimal", java.math.BigDecimal .class); 70 globalClassNameClassMap.put("Double", java.lang.Double .class); 71 globalClassNameClassMap.put("java.lang.Double", java.lang.Double .class); 72 globalClassNameClassMap.put("Float", java.lang.Float .class); 73 globalClassNameClassMap.put("java.lang.Float", java.lang.Float .class); 74 globalClassNameClassMap.put("Long", java.lang.Long .class); 75 globalClassNameClassMap.put("java.lang.Long", java.lang.Long .class); 76 globalClassNameClassMap.put("Integer", java.lang.Integer .class); 77 globalClassNameClassMap.put("java.lang.Integer", java.lang.Integer .class); 78 79 globalClassNameClassMap.put("Timestamp", java.sql.Timestamp .class); 80 globalClassNameClassMap.put("java.sql.Timestamp", java.sql.Timestamp .class); 81 globalClassNameClassMap.put("Time", java.sql.Time .class); 82 globalClassNameClassMap.put("java.sql.Time", java.sql.Time .class); 83 globalClassNameClassMap.put("Date", java.sql.Date .class); 84 globalClassNameClassMap.put("java.sql.Date", java.sql.Date .class); 85 86 globalClassNameClassMap.put("Locale", java.util.Locale .class); 87 globalClassNameClassMap.put("java.util.Locale", java.util.Locale .class); 88 89 globalClassNameClassMap.put("java.util.Date", java.util.Date .class); 90 globalClassNameClassMap.put("Collection", java.util.Collection .class); 91 globalClassNameClassMap.put("java.util.Collection", java.util.Collection .class); 92 globalClassNameClassMap.put("List", java.util.List .class); 93 globalClassNameClassMap.put("java.util.List", java.util.List .class); 94 globalClassNameClassMap.put("Set", java.util.Set .class); 95 globalClassNameClassMap.put("java.util.Set", java.util.Set .class); 96 globalClassNameClassMap.put("Map", java.util.Map .class); 97 globalClassNameClassMap.put("java.util.Map", java.util.Map .class); 98 globalClassNameClassMap.put("HashMap", java.util.HashMap .class); 99 globalClassNameClassMap.put("java.util.HashMap", java.util.HashMap .class); 100 101 try { 102 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 103 104 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 e) { 112 Debug.logError(e, "Could not pre-initialize dynamically loaded class: ", module); 113 } 114 } 115 116 public CachedClassLoader(URL [] url, ClassLoader parent, String contextName) { 117 super(url, parent); 118 this.contextName = contextName; 119 if (Debug.verboseOn()) { 120 Package [] paks = this.getPackages(); 121 StringBuffer pakList = new StringBuffer (); 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 parent, String contextName) { 133 this(new URL [0], parent, contextName); 134 } 135 136 public CachedClassLoader(URL [] url, ClassLoader parent) { 137 this(url, parent, "__globalContext"); 138 } 139 140 public String toString() { 141 return "org.ofbiz.base.util.CachedClassLoader(" + contextName + ") / " + getParent().toString(); 142 } 143 144 public Class loadClass(String name) throws ClassNotFoundException { 145 return loadClass(name, false); 146 } 147 148 protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { 149 Class theClass = (Class ) globalClassNameClassMap.get(name); 151 152 if (theClass == null) theClass = (Class ) localClassNameClassMap.get(name); 154 155 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 ("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 ) 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 e) { 177 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 getResource(String name) { 193 URL theResource = (URL ) globalResourceMap.get(name); 195 196 if (theResource == null) theResource = (URL ) localResourceMap.get(name); 198 199 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 211 synchronized (this) { 212 theResource = (URL ) 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 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 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 |