1 9 10 package org.picocontainer.defaults; 11 12 import java.net.URL ; 13 import java.net.URLClassLoader ; 14 import java.security.AccessController ; 15 import java.security.CodeSource ; 16 import java.security.PermissionCollection ; 17 import java.security.Permissions ; 18 import java.security.PrivilegedAction ; 19 import java.util.Map ; 20 21 30 public class CustomPermissionsURLClassLoader extends URLClassLoader { 31 private final Map permissionsMap; 32 33 public CustomPermissionsURLClassLoader(URL [] urls, Map permissionsMap, ClassLoader parent) { 34 super(urls, parent); 35 this.permissionsMap = permissionsMap; 36 } 37 38 public Class loadClass(String name) throws ClassNotFoundException { 39 try { 40 return super.loadClass(name); 41 } catch (ClassNotFoundException e) { 42 throw decorateException(name, e); 43 } 44 } 45 46 protected Class findClass(String name) throws ClassNotFoundException { 47 try { 48 return super.findClass(name); 49 } catch (ClassNotFoundException e) { 50 throw decorateException(name, e); 51 } 52 } 53 54 private ClassNotFoundException decorateException(String name, ClassNotFoundException e) { 55 if (name.startsWith("class ")) { 56 return new ClassNotFoundException ("Class '" + name + "' is not a classInstance.getName(). " + 57 "It's a classInstance.toString(). The clue is that it starts with 'class ', no classname contains a space."); 58 } 59 ClassLoader classLoader = this; 60 StringBuffer sb = new StringBuffer ("'").append(name).append("' classloader stack ["); 61 while (classLoader != null) { 62 sb.append(classLoader.toString()).append("\n"); 63 final ClassLoader cl = classLoader; 64 classLoader = (ClassLoader ) AccessController.doPrivileged(new PrivilegedAction () { 65 public Object run() { 66 return cl.getParent(); 67 } 68 }); 69 70 } 71 return new ClassNotFoundException (sb.append("]").toString(), e); 72 } 73 74 public String toString() { 75 String result = CustomPermissionsURLClassLoader.class.getName() + " " + System.identityHashCode(this) + ":"; 76 URL [] urls = getURLs(); 77 for (int i = 0; i < urls.length; i++) { 78 URL url = urls[i]; 79 result += "\n\t" + url.toString(); 80 } 81 82 return result; 83 } 84 85 public PermissionCollection getPermissions(CodeSource codeSource) { 86 return (Permissions ) permissionsMap.get(codeSource.getLocation()); 87 } 88 89 } 90 91 | Popular Tags |