KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > tomcat > TomcatClassLoader


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.tomcat;
18
19 import java.net.URL JavaDoc;
20 import java.net.URLClassLoader JavaDoc;
21
22 /**
23 * @version $Rev$ $Date$
24 */

25 public class TomcatClassLoader extends URLClassLoader JavaDoc {
26    private final boolean contextPriorityClassLoader;
27    private final ClassLoader JavaDoc parent;
28    private final ClassLoader JavaDoc resourceClassLoader;
29
30    public TomcatClassLoader(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.apache.jasper.") ||
57                name.startsWith("org.apache.tomcat.") ||
58                name.startsWith("org.apache.naming.") ||
59                name.startsWith("org.apache.catalina.") ||
60                name.startsWith("org.xml.") ||
61                name.startsWith("org.w3c.")) {
62            return super.loadClass(name);
63        }
64
65        // first check if this class has already been loaded
66
Class JavaDoc clazz = findLoadedClass(name);
67        if (clazz != null) {
68            return clazz;
69        }
70
71        // try to load the class from this class loader
72
try {
73            clazz = findClass(name);
74        } catch (ClassNotFoundException JavaDoc ignored) {
75        }
76        if (clazz != null) {
77            return clazz;
78        }
79
80        // that didn't work... try the parent
81
return parent.loadClass(name);
82    }
83
84    public URL JavaDoc getResource(String JavaDoc name) {
85        return resourceClassLoader.getResource(name);
86    }
87
88    private class ResourceClassLoader extends URLClassLoader JavaDoc {
89
90        public ResourceClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc classLoader) {
91            super(urls, classLoader);
92        }
93
94        public URL JavaDoc getResource(String JavaDoc name) {
95            if (!contextPriorityClassLoader ||
96                    name.startsWith("java/") ||
97                    name.startsWith("javax/") ||
98                    name.startsWith("org/apache/geronimo/") ||
99                    name.startsWith("org/apache/jasper") ||
100                    name.startsWith("org/apache/tomcat") ||
101                    name.startsWith("org/apache/naming") ||
102                    name.startsWith("org/apache/catalina") ||
103                    name.startsWith("org/xml/") ||
104                    name.startsWith("org/w3c/")) {
105                return super.getResource(name);
106            }
107
108            // try to load the resource from this class loader
109
URL JavaDoc url = findResource(name);
110            if (url != null) {
111                return url;
112            }
113
114            // that didn't work... try the parent
115
return parent.getResource(name);
116        }
117    }
118 }
119
Popular Tags