KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty > JettyClassLoader


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jetty;
18
19 import java.net.URL JavaDoc;
20 import java.net.URLClassLoader JavaDoc;
21
22 /**
23  * @version $Rev$ $Date$
24  */

25 public class JettyClassLoader extends URLClassLoader JavaDoc {
26     private final boolean contextPriorityClassLoader;
27     private final ClassLoader JavaDoc parent;
28     private final ClassLoader JavaDoc resourceClassLoader;
29
30     public JettyClassLoader(URL JavaDoc[] urls, URL JavaDoc resourceURL, ClassLoader JavaDoc parent, boolean contextPriorityClassLoader) {
31         super(urls, parent);
32
33         if (parent == null) {
34             throw new IllegalArgumentException JavaDoc("Parent class loader is null");
35         }
36         URL JavaDoc[] resourceURLS;
37         if (resourceURL != null) {
38             resourceURLS = new URL JavaDoc[urls.length + 1];
39             System.arraycopy(urls, 0, resourceURLS, 0, urls.length);
40             resourceURLS[resourceURLS.length - 1] = resourceURL;
41         } else {
42             resourceURLS = urls;
43         }
44         resourceClassLoader = new ResourceClassLoader(resourceURLS, parent);
45
46         // hold on to the parent so we don't have to go throught the security check each time
47
this.parent = parent;
48         this.contextPriorityClassLoader = contextPriorityClassLoader;
49     }
50
51     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
52         if (!contextPriorityClassLoader ||
53                 name.startsWith("java.") ||
54                 name.startsWith("javax.") ||
55                 name.startsWith("org.apache.geronimo.") ||
56                 name.startsWith("org.mortbay.") ||
57                 name.startsWith("org.xml.") ||
58                 name.startsWith("org.w3c.")) {
59             return super.loadClass(name);
60         }
61
62         // first check if this class has already been loaded
63
Class JavaDoc clazz = findLoadedClass(name);
64         if (clazz != null) {
65             return clazz;
66         }
67
68         // try to load the class from this class loader
69
try {
70             clazz = findClass(name);
71         } catch (ClassNotFoundException JavaDoc ignored) {
72         }
73         if (clazz != null) {
74             return clazz;
75         }
76
77         // that didn't work... try the parent
78
return parent.loadClass(name);
79     }
80
81     public URL JavaDoc getResource(String JavaDoc name) {
82         return resourceClassLoader.getResource(name);
83     }
84
85     private class ResourceClassLoader extends URLClassLoader JavaDoc {
86
87         public ResourceClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc classLoader) {
88             super(urls, classLoader);
89         }
90
91         public URL JavaDoc getResource(String JavaDoc name) {
92             if (!contextPriorityClassLoader ||
93                     name.startsWith("java/") ||
94                     name.startsWith("javax/") ||
95                     name.startsWith("org/apache/geronimo/") ||
96                     name.startsWith("org/mortbay/") ||
97                     name.startsWith("org/xml/") ||
98                     name.startsWith("org/w3c/")) {
99                 return super.getResource(name);
100             }
101
102             // try to load the resource from this class loader
103
URL JavaDoc url = findResource(name);
104             if (url != null) {
105                 return url;
106             }
107
108             // that didn't work... try the parent
109
return parent.getResource(name);
110         }
111     }
112 }
113
Popular Tags