1 11 package org.eclipse.equinox.internal.jsp.jasper; 12 13 import java.io.IOException ; 14 import java.net.URL ; 15 import java.security.AccessController ; 16 import java.security.PrivilegedAction ; 17 import java.util.*; 18 19 20 public class JSPContextFinder extends ClassLoader implements PrivilegedAction { 24 static final class Finder extends SecurityManager { 25 public Class [] getClassContext() { 26 return super.getClassContext(); 27 } 28 } 29 private static ThreadLocal cycleDetector = new ThreadLocal (); 32 static Finder contextFinder; 33 static { 34 AccessController.doPrivileged(new PrivilegedAction () { 35 public Object run() { 36 contextFinder = new Finder(); 37 return null; 38 } 39 }); 40 } 41 42 public JSPContextFinder(ClassLoader contextClassLoader) { 43 super(contextClassLoader); 44 } 45 46 ArrayList basicFindClassLoaders() { 51 Class [] stack = contextFinder.getClassContext(); 52 ArrayList result = new ArrayList(1); 53 ClassLoader previousLoader = null; 54 for (int i = 1; i < stack.length; i++) { 55 ClassLoader tmp = stack[i].getClassLoader(); 56 if (checkClass(stack[i]) && tmp != null && tmp != this) { 57 if (checkClassLoader(tmp)) { 58 if (previousLoader != tmp) { 59 result.add(tmp); 60 previousLoader = tmp; 61 } 62 } 63 if (Activator.getBundle(stack[i]) != null) 65 break; 66 } 67 } 68 return result; 69 } 70 71 private boolean checkClass(Class clazz) { 72 return clazz != JSPContextFinder.class && 73 clazz != BundleProxyClassLoader.class && 74 clazz != JspClassLoader.class; 75 } 76 77 private boolean checkClassLoader(ClassLoader classloader) { 81 if (classloader == null || classloader == getParent()) 82 return false; 83 for (ClassLoader parent = classloader.getParent(); parent != null; parent = parent.getParent()) 84 if (parent == this) 85 return false; 86 return true; 87 } 88 89 private ArrayList findClassLoaders() { 90 if (System.getSecurityManager() == null) 91 return basicFindClassLoaders(); 92 return (ArrayList) AccessController.doPrivileged(this); 93 } 94 95 public Object run() { 96 return basicFindClassLoaders(); 97 } 98 99 private boolean startLoading(String name) { 102 Set classesAndResources = (Set) cycleDetector.get(); 103 if (classesAndResources != null && classesAndResources.contains(name)) 104 return false; 105 106 if (classesAndResources == null) { 107 classesAndResources = new HashSet(3); 108 cycleDetector.set(classesAndResources); 109 } 110 classesAndResources.add(name); 111 return true; 112 } 113 114 private void stopLoading(String name) { 115 ((Set) cycleDetector.get()).remove(name); 116 } 117 118 protected Class loadClass(String arg0, boolean arg1) throws ClassNotFoundException { 119 if (startLoading(arg0) == false) 121 throw new ClassNotFoundException (arg0); 122 123 try { 124 ArrayList toConsult = findClassLoaders(); 125 for (Iterator loaders = toConsult.iterator(); loaders.hasNext();) 126 try { 127 return ((ClassLoader ) loaders.next()).loadClass(arg0); 128 } catch (ClassNotFoundException e) { 129 } 131 return super.loadClass(arg0, arg1); 132 } finally { 133 stopLoading(arg0); 134 } 135 } 136 137 public URL getResource(String arg0) { 138 if (startLoading(arg0) == false) 140 return null; 141 try { 142 ArrayList toConsult = findClassLoaders(); 143 for (Iterator loaders = toConsult.iterator(); loaders.hasNext();) { 144 URL result = ((ClassLoader ) loaders.next()).getResource(arg0); 145 if (result != null) 146 return result; 147 } 149 return super.getResource(arg0); 150 } finally { 151 stopLoading(arg0); 152 } 153 } 154 155 protected Enumeration findResources(String arg0) throws IOException { 156 if (startLoading(arg0) == false) 158 return null; 159 try { 160 ArrayList toConsult = findClassLoaders(); 161 for (Iterator loaders = toConsult.iterator(); loaders.hasNext();) { 162 Enumeration result = ((ClassLoader ) loaders.next()).getResources(arg0); 163 if (result != null && result.hasMoreElements()) 164 return result; 165 } 167 return super.findResources(arg0); 168 } finally { 169 stopLoading(arg0); 170 } 171 } 172 } 173 | Popular Tags |