KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > servlet > ParanoidClassLoader


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

16 package org.apache.cocoon.servlet;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLClassLoader JavaDoc;
22 import java.net.URLStreamHandlerFactory JavaDoc;
23
24 /**
25  * The <code>ParanoidClassLoader</code> reverses the search order for classes.
26  * It checks this classloader before it checks its parent.
27  *
28  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch </a>
29  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez </a>
30  * @version CVS $Id: ParanoidClassLoader.java 30932 2004-07-29 17:35:38Z
31  * vgritsenko $
32  */

33
34 public class ParanoidClassLoader extends URLClassLoader JavaDoc {
35
36     /**
37      * Default constructor has no parents or initial <code>URL</code>s.
38      */

39     public ParanoidClassLoader() {
40         this(null, null, null);
41     }
42
43     /**
44      * Alternate constructor to define a parent.
45      */

46     public ParanoidClassLoader(final ClassLoader JavaDoc parent) {
47         this(new URL JavaDoc[0], parent, null);
48     }
49
50     /**
51      * Alternate constructor to define initial <code>URL</code>s.
52      */

53     public ParanoidClassLoader(final URL JavaDoc[] urls) {
54         this(urls, null, null);
55     }
56
57     /**
58      * Alternate constructor to define a parent and initial <code>URL</code>
59      * s.
60      */

61     public ParanoidClassLoader(final URL JavaDoc[] urls, final ClassLoader JavaDoc parent) {
62         this(urls, parent, null);
63     }
64
65     /**
66      * Alternate constructor to define a parent, initial <code>URL</code>s,
67      * and a default <code>URLStreamHandlerFactory</code>.
68      */

69     public ParanoidClassLoader(final URL JavaDoc[] urls, final ClassLoader JavaDoc parent, final URLStreamHandlerFactory JavaDoc factory) {
70         super(urls, parent, factory);
71     }
72
73     /**
74      * Extends <code>URLClassLoader</code>'s initialization methods so we
75      * return a <code>ParanoidClassLoad</code> instead.
76      */

77     public static final URLClassLoader JavaDoc newInstance(final URL JavaDoc[] urls) {
78         return new ParanoidClassLoader(urls);
79     }
80
81     /**
82      * Extends <code>URLClassLoader</code>'s initialization methods so we
83      * return a <code>ParanoidClassLoad</code> instead.
84      */

85     public static final URLClassLoader JavaDoc newInstance(final URL JavaDoc[] urls, final ClassLoader JavaDoc parent) {
86         return new ParanoidClassLoader(urls, parent);
87     }
88
89     /**
90      * Loads the class from this <code>ClassLoader</class>. If the
91      * class does not exist in this one, we check the parent. Please
92      * note that this is the exact opposite of the
93      * <code>ClassLoader</code> spec. We use it to work around
94      * inconsistent class loaders from third party vendors.
95      *
96      * @param name the name of the class
97      * @param resolve if <code>true</code> then resolve the class
98      * @return the resulting <code>Class</code> object
99      * @exception ClassNotFoundException if the class could not be found
100      */

101     public final Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
102         // First check if it's already loaded
103
Class JavaDoc clazz = findLoadedClass(name);
104
105         if (clazz == null) {
106
107             try {
108                 clazz = findClass(name);
109                 //System.err.println("Paranoid load : " + name);
110
} catch (ClassNotFoundException JavaDoc cnfe) {
111                 ClassLoader JavaDoc parent = getParent();
112                 if (parent != null) {
113                     // Ask to parent ClassLoader (can also throw a CNFE).
114
clazz = parent.loadClass(name);
115                 } else {
116                     // Propagate exception
117
throw cnfe;
118                 }
119             }
120         }
121
122         if (resolve) {
123             resolveClass(clazz);
124         }
125
126         return clazz;
127     }
128
129     /**
130      * Gets a resource from this <code>ClassLoader</class>. If the
131      * resource does not exist in this one, we check the parent.
132      * Please note that this is the exact opposite of the
133      * <code>ClassLoader</code> spec. We use it to work around
134      * inconsistent class loaders from third party vendors.
135      *
136      * @param name of resource
137      */

138     public final URL JavaDoc getResource(final String JavaDoc name) {
139
140         URL JavaDoc resource = findResource(name);
141         ClassLoader JavaDoc parent = this.getParent();
142         if (resource == null && parent != null) {
143             resource = parent.getResource(name);
144         }
145
146         return resource;
147     }
148
149     /**
150      * Adds a new directory of class files.
151      *
152      * @param file
153      * for jar or directory
154      * @throws IOException
155      */

156     public final void addDirectory(File JavaDoc file) throws IOException JavaDoc {
157         this.addURL(file.getCanonicalFile().toURL());
158     }
159
160     /**
161      * Adds a new URL
162      */

163
164     public void addURL(URL JavaDoc url) {
165         super.addURL(url);
166     }
167 }
Popular Tags