1 12 package org.eclipse.equinox.internal.jsp.jasper; 13 14 import java.io.IOException ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.net.URLClassLoader ; 18 import java.util.*; 19 20 import org.osgi.framework.Bundle; 21 import org.osgi.framework.Constants; 22 23 35 public class JspClassLoader extends URLClassLoader { 36 37 private static final Bundle JASPERBUNDLE = Activator.getBundle(org.apache.jasper.servlet.JspServlet.class); 38 private static final ClassLoader PARENT = JspClassLoader.class.getClassLoader().getParent(); 39 private static final String JAVA_PACKAGE = "java."; private static final ClassLoader EMPTY_CLASSLOADER = new ClassLoader () { 41 public URL getResource(String name) { 42 return null; 43 } 44 public Enumeration findResources(String name) throws IOException { 45 return new Enumeration() { 46 public boolean hasMoreElements() { 47 return false; 48 } 49 public Object nextElement() { 50 return null; 51 } 52 }; 53 } 54 public Class loadClass(String name) throws ClassNotFoundException { 55 throw new ClassNotFoundException (name); 56 } 57 }; 58 59 public JspClassLoader(Bundle bundle) { 60 super(new URL [0], new BundleProxyClassLoader(bundle, new BundleProxyClassLoader(JASPERBUNDLE, new JSPContextFinder(EMPTY_CLASSLOADER)))); 61 addBundleClassPathJars(bundle); 62 Bundle[] fragments = Activator.getFragments(bundle); 63 if (fragments != null) { 64 for (int i = 0; i < fragments.length; i++) { 65 addBundleClassPathJars(fragments[i]); 66 } 67 } 68 } 69 70 private void addBundleClassPathJars(Bundle bundle) { 71 Dictionary headers = bundle.getHeaders(); 72 String classPath = (String ) headers.get(Constants.BUNDLE_CLASSPATH); 73 if (classPath != null) { 74 StringTokenizer tokenizer = new StringTokenizer(classPath, ","); while (tokenizer.hasMoreTokens()) { 76 String candidate = tokenizer.nextToken().trim(); 77 if (candidate.endsWith(".jar")) { URL entry = bundle.getEntry(candidate); 79 if (entry != null) { 80 URL jarEntryURL; 81 try { 82 jarEntryURL = new URL ("jar:" + entry.toString() + "!/"); super.addURL(jarEntryURL); 84 } catch (MalformedURLException e) { 85 } 87 } 88 } 89 } 90 } 91 } 92 93 protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { 94 if (PARENT != null && name.startsWith(JAVA_PACKAGE)) 95 return PARENT.loadClass(name); 96 return super.loadClass(name, resolve); 97 } 98 99 protected Class findClass(String name) throws ClassNotFoundException { 101 throw new ClassNotFoundException (name); 102 } 103 } 104 | Popular Tags |