1 16 17 package org.apache.commons.logging; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 49 50 public class PathableClassLoader extends URLClassLoader { 51 52 private static final URL [] NO_URLS = new URL [0]; 53 54 68 private HashMap lookasides = null; 69 70 73 private boolean parentFirst = true; 74 75 78 public PathableClassLoader(ClassLoader parent) { 79 super(NO_URLS, parent); 80 } 81 82 87 public void addURL(URL url) { 88 super.addURL(url); 89 } 90 91 107 public void setParentFirst(boolean state) { 108 parentFirst = state; 109 } 110 111 120 public void useSystemLoader(String prefix) { 121 useExplicitLoader(prefix, ClassLoader.getSystemClassLoader()); 122 123 } 124 125 128 public void useExplicitLoader(String prefix, ClassLoader loader) { 129 if (lookasides == null) { 130 lookasides = new HashMap (); 131 } 132 lookasides.put(prefix, loader); 133 } 134 135 138 public void addLogicalLib(String [] logicalLibs) { 139 for(int i=0; i<logicalLibs.length; ++i) { 140 addLogicalLib(logicalLibs[i]); 141 } 142 } 143 144 158 public void addLogicalLib(String logicalLib) { 159 String filename = System.getProperty(logicalLib); 160 if (filename == null) { 161 throw new UnknownError ( 162 "Logical lib [" + logicalLib + "] is not defined" 163 + " as a System property."); 164 } 165 166 try { 167 URL url = new File (filename).toURL(); 168 addURL(url); 169 } catch(java.net.MalformedURLException e) { 170 throw new UnknownError ( 171 "Invalid file [" + filename + "] for logical lib [" + logicalLib + "]"); 172 } 173 } 174 175 182 protected Class loadClass(String name, boolean resolve) 183 throws ClassNotFoundException { 184 if (name.startsWith("java.") || name.startsWith("javax.")) { 186 return super.loadClass(name, resolve); 187 } 188 189 if (lookasides != null) { 190 for(Iterator i = lookasides.entrySet().iterator(); i.hasNext(); ) { 191 Map.Entry entry = (Map.Entry ) i.next(); 192 String prefix = (String ) entry.getKey(); 193 if (name.startsWith(prefix) == true) { 194 ClassLoader loader = (ClassLoader ) entry.getValue(); 195 Class clazz = Class.forName(name, resolve, loader); 196 return clazz; 197 } 198 } 199 } 200 201 if (parentFirst) { 202 return super.loadClass(name, resolve); 203 } else { 204 211 try { 212 Class clazz = findLoadedClass(name); 213 if (clazz == null) { 214 clazz = super.findClass(name); 215 } 216 if (resolve) { 217 resolveClass(clazz); 218 } 219 return clazz; 220 } catch(ClassNotFoundException e) { 221 return super.loadClass(name, resolve); 222 } 223 } 224 } 225 226 231 public URL getResource(String name) { 232 if (parentFirst) { 233 return super.getResource(name); 234 } else { 235 URL local = super.findResource(name); 236 if (local != null) { 237 return local; 238 } 239 return super.getResource(name); 240 } 241 } 242 243 251 public Enumeration getResourcesInOrder(String name) throws IOException { 252 if (parentFirst) { 253 return super.getResources(name); 254 } else { 255 Enumeration localUrls = super.findResources(name); 256 257 ClassLoader parent = getParent(); 258 if (parent == null) { 259 return localUrls; 270 } 271 Enumeration parentUrls = parent.getResources(name); 272 273 ArrayList localItems = toList(localUrls); 274 ArrayList parentItems = toList(parentUrls); 275 localItems.addAll(parentItems); 276 return Collections.enumeration(localItems); 277 } 278 } 279 280 288 private ArrayList toList(Enumeration en) { 289 ArrayList results = new ArrayList (); 290 if (en != null) { 291 while (en.hasMoreElements()){ 292 Object element = en.nextElement(); 293 results.add(element); 294 } 295 } 296 return results; 297 } 298 299 304 public InputStream getResourceAsStream(String name) { 305 if (parentFirst) { 306 return super.getResourceAsStream(name); 307 } else { 308 URL local = super.findResource(name); 309 if (local != null) { 310 try { 311 return local.openStream(); 312 } catch(IOException e) { 313 return null; 316 } 317 } 318 return super.getResourceAsStream(name); 319 } 320 } 321 } 322 | Popular Tags |