KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > discovery > resource > ClassLoaders


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  */

57
58 package org.apache.commons.discovery.resource;
59
60 import java.util.Vector JavaDoc;
61
62 import org.apache.commons.discovery.jdk.JDKHooks;
63
64
65 /**
66  * There are many different contexts in which
67  * loaders can be used. This provides a holder
68  * for a set of class loaders, so that they
69  * don't have to be build back up everytime...
70  *
71  * @author Richard A. Sitze
72  * @author Craig R. McClanahan
73  * @author Costin Manolache
74  */

75 public class ClassLoaders
76 {
77     protected Vector JavaDoc classLoaders = new Vector JavaDoc();
78     
79     /** Construct a new class loader set
80      */

81     public ClassLoaders() {
82     }
83     
84     public int size() {
85         return classLoaders.size();
86     }
87     
88     public ClassLoader JavaDoc get(int idx) {
89         return (ClassLoader JavaDoc)classLoaders.elementAt(idx);
90     }
91
92     /**
93      * Specify a new class loader to be used in searching.
94      * The order of loaders determines the order of the result.
95      * It is recommended to add the most specific loaders first.
96      */

97     public void put(ClassLoader JavaDoc classLoader) {
98         if (classLoader != null) {
99             classLoaders.addElement(classLoader);
100         }
101     }
102     
103
104     /**
105      * Specify a new class loader to be used in searching.
106      * The order of loaders determines the order of the result.
107      * It is recommended to add the most specific loaders first.
108      *
109      * @param prune if true, verify that the class loader is
110      * not an Ancestor (@see isAncestor) before
111      * adding it to our list.
112      */

113     public void put(ClassLoader JavaDoc classLoader, boolean prune) {
114         if (classLoader != null && !(prune && isAncestor(classLoader))) {
115             classLoaders.addElement(classLoader);
116         }
117     }
118     
119     
120     /**
121      * Check to see if <code>classLoader</code> is an
122      * ancestor of any contained class loader.
123      *
124      * This can be used to eliminate redundant class loaders
125      * IF all class loaders defer to parent class loaders
126      * before resolving a class.
127      *
128      * It may be that this is not always true. Therefore,
129      * this check is not done internally to eliminate
130      * redundant class loaders, but left to the discretion
131      * of the user.
132      */

133     public boolean isAncestor(final ClassLoader JavaDoc classLoader) {
134         /* bootstrap classloader, at root of all trees! */
135         if (classLoader == null)
136             return true;
137
138         for (int idx = 0; idx < size(); idx++) {
139             for(ClassLoader JavaDoc walker = get(idx);
140                 walker != null;
141                 walker = walker.getParent())
142             {
143                 if (walker == classLoader) {
144                     return true;
145                 }
146             }
147         }
148         return false;
149     }
150
151
152     /**
153      * Utility method. Returns a preloaded ClassLoaders instance
154      * containing the following class loaders, in order:
155      *
156      * <ul>
157      * <li>spi.getClassLoader</li>
158      * <li>seeker.getClassLoader</li>
159      * <li>System Class Loader</li>
160      * </ul>
161      *
162      * Note that the thread context class loader is NOT present.
163      * This is a reasonable set of loaders to try if the resource to be found
164      * should be restricted to a libraries containing the SPI and Factory.
165      *
166      * @param spi WHAT is being looked for (an implementation of this class,
167      * a default property file related to this class).
168      * @param factory WHO is performing the lookup.
169      * @param prune Determines if ancestors are allowed to be loaded or not.
170      */

171     public static ClassLoaders getLibLoaders(Class JavaDoc spi, Class JavaDoc factory, boolean prune) {
172         ClassLoaders loaders = new ClassLoaders();
173         
174         loaders.put(spi.getClassLoader());
175         loaders.put(factory.getClassLoader(), prune);
176         loaders.put(JDKHooks.getJDKHooks().getSystemClassLoader(), prune);
177         
178         return loaders;
179     }
180     
181     /**
182      * Utility method. Returns a preloaded ClassLoaders instance
183      * containing the following class loaders, in order:
184      *
185      * <ul>
186      * <li>Thread Context Class Loader</li>
187      * <li>spi.getClassLoader</li>
188      * <li>seeker.getClassLoader</li>
189      * <li>System Class Loader</li>
190      * </ul>
191      *
192      * Note that the thread context class loader IS present.
193      * This is a reasonable set of loaders to try if the resource to be found
194      * may be provided by an application.
195      *
196      * @param spi WHAT is being looked for (an implementation of this class,
197      * a default property file related to this class).
198      * @param factory WHO is performing the lookup (factory).
199      * @param prune Determines if ancestors are allowed to be loaded or not.
200      */

201     public static ClassLoaders getAppLoaders(Class JavaDoc spi, Class JavaDoc factory, boolean prune) {
202         ClassLoaders loaders = new ClassLoaders();
203
204         loaders.put(JDKHooks.getJDKHooks().getThreadContextClassLoader());
205         loaders.put(spi.getClassLoader(), prune);
206         loaders.put(factory.getClassLoader(), prune);
207         loaders.put(JDKHooks.getJDKHooks().getSystemClassLoader(), prune);
208         
209         return loaders;
210     }
211 }
212
Popular Tags