KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > core > util > CompositeClassLoader


1 package com.thoughtworks.xstream.core.util;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 /**
9  * ClassLoader that is composed of other classloaders. Each loader will be used to try to load the particular class, until
10  * one of them succeeds. <b>Note:</b> The loaders will always be called in the REVERSE order they were added in.
11  *
12  * <p>The Composite class loader also has registered the classloader that loaded xstream.jar
13  * and (if available) the thread's context classloader.</p>
14  *
15  * <h1>Example</h1>
16  * <pre><code>CompositeClassLoader loader = new CompositeClassLoader();
17  * loader.add(MyClass.class.getClassLoader());
18  * loader.add(new AnotherClassLoader());
19  * &nbsp;
20  * loader.loadClass("com.blah.ChickenPlucker");
21  * </code></pre>
22  *
23  * <p>The above code will attempt to load a class from the following classloaders (in order):</p>
24  *
25  * <ul>
26  * <li>AnotherClassLoader (and all its parents)</li>
27  * <li>The classloader for MyClas (and all its parents)</li>
28  * <li>The thread's context classloader (and all its parents)</li>
29  * <li>The classloader for XStream (and all its parents)</li>
30  * </ul>
31  *
32  * @author Joe Walnes
33  * @since 1.0.3
34  */

35 public class CompositeClassLoader extends ClassLoader JavaDoc {
36
37     private final List JavaDoc classLoaders = Collections.synchronizedList(new ArrayList JavaDoc());
38
39     public CompositeClassLoader() {
40         add(Object JavaDoc.class.getClassLoader()); // bootstrap loader.
41
add(getClass().getClassLoader()); // whichever classloader loaded this jar.
42
}
43
44     /**
45      * Add a loader to the n
46      * @param classLoader
47      */

48     public void add(ClassLoader JavaDoc classLoader) {
49         if (classLoader != null) {
50             classLoaders.add(0, classLoader);
51         }
52     }
53
54     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
55         for (Iterator JavaDoc iterator = classLoaders.iterator(); iterator.hasNext();) {
56             ClassLoader JavaDoc classLoader = (ClassLoader JavaDoc) iterator.next();
57             try {
58                 return classLoader.loadClass(name);
59             } catch (ClassNotFoundException JavaDoc notFound) {
60                 // ok.. try another one
61
}
62         }
63         // One last try - the context class loader associated with the current thread. Often used in j2ee servers.
64
// Note: The contextClassLoader cannot be added to the classLoaders list up front as the thread that constructs
65
// XStream is potentially different to thread that uses it.
66
ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
67         if (contextClassLoader != null) {
68             return contextClassLoader.loadClass(name);
69         } else {
70             throw new ClassNotFoundException JavaDoc(name);
71         }
72     }
73
74 }
75
Popular Tags