KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > internal > jsp > jasper > JspClassLoader


1 /*******************************************************************************
2  * Copyright (c) 2006-2007 Cognos Incorporated, IBM Corporation and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Cognos Incorporated - initial API and implementation
10  * IBM Corporation - bug fixes and enhancements
11  *******************************************************************************/

12 package org.eclipse.equinox.internal.jsp.jasper;
13
14 import java.io.IOException JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.net.URLClassLoader JavaDoc;
18 import java.util.*;
19
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.Constants;
22
23 /**
24  * Jasper requires that this class loader be an instance of URLClassLoader.
25  * At runtime it uses the URLClassLoader's getURLs method to find jar files that are in turn searched for TLDs. In a webapp
26  * these jar files would normally be located in WEB-INF/lib. In the OSGi context, this behaviour is provided by returning the
27  * URLs of the jar files contained on the Bundle-ClassPath. Other than jar file tld resources this classloader is not used for
28  * loading classes which should be done by the other contained class loaders.
29  *
30  * The rest of the ClassLoader is as follows:
31  * 1) Thread-ContextClassLoader (top - parent) -- see ContextFinder
32  * 2) Jasper Bundle
33  * 3) The Bundle referenced at JSPServlet creation
34  */

35 public class JspClassLoader extends URLClassLoader JavaDoc {
36
37     private static final Bundle JASPERBUNDLE = Activator.getBundle(org.apache.jasper.servlet.JspServlet.class);
38     private static final ClassLoader JavaDoc PARENT = JspClassLoader.class.getClassLoader().getParent();
39     private static final String JavaDoc JAVA_PACKAGE = "java."; //$NON-NLS-1$
40
private static final ClassLoader JavaDoc EMPTY_CLASSLOADER = new ClassLoader JavaDoc() {
41         public URL JavaDoc getResource(String JavaDoc name) {
42             return null;
43         }
44         public Enumeration findResources(String JavaDoc name) throws IOException JavaDoc {
45             return new Enumeration() {
46                 public boolean hasMoreElements() {
47                     return false;
48                 }
49                 public Object JavaDoc nextElement() {
50                     return null;
51                 }
52             };
53         }
54         public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
55             throw new ClassNotFoundException JavaDoc(name);
56         }
57     };
58
59     public JspClassLoader(Bundle bundle) {
60         super(new URL JavaDoc[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 JavaDoc classPath = (String JavaDoc) headers.get(Constants.BUNDLE_CLASSPATH);
73         if (classPath != null) {
74             StringTokenizer tokenizer = new StringTokenizer(classPath, ","); //$NON-NLS-1$
75
while (tokenizer.hasMoreTokens()) {
76                 String JavaDoc candidate = tokenizer.nextToken().trim();
77                 if (candidate.endsWith(".jar")) { //$NON-NLS-1$
78
URL JavaDoc entry = bundle.getEntry(candidate);
79                     if (entry != null) {
80                         URL JavaDoc jarEntryURL;
81                         try {
82                             jarEntryURL = new URL JavaDoc("jar:" + entry.toString() + "!/"); //$NON-NLS-1$ //$NON-NLS-2$
83
super.addURL(jarEntryURL);
84                         } catch (MalformedURLException JavaDoc e) {
85                             // TODO should log this.
86
}
87                     }
88                 }
89             }
90         }
91     }
92
93     protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
94         if (PARENT != null && name.startsWith(JAVA_PACKAGE))
95             return PARENT.loadClass(name);
96         return super.loadClass(name, resolve);
97     }
98
99     // Classes should "not" be loaded by this classloader from the URLs - it is just used for TLD resource discovery.
100
protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
101         throw new ClassNotFoundException JavaDoc(name);
102     }
103 }
104
Popular Tags