KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > loading > DelegatingClassLoader


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.loading;
23
24 import java.net.URL JavaDoc;
25 import java.net.URLClassLoader JavaDoc;
26 import java.net.URLStreamHandlerFactory JavaDoc;
27
28 /**
29  * A URL classloader that delegates to its parent, avoiding
30  * synchronization.
31  *
32  * A standard flag is provided so it can be used as a parent class,
33  * but later subclassed and to revert to standard class loading
34  * if the subclass wants to load classes.
35  *
36  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
37  * @version $Revision: 1958 $
38  */

39 public class DelegatingClassLoader
40    extends URLClassLoader JavaDoc
41 {
42    /** The value returned by {@link getURLs}. */
43    public static final URL JavaDoc[] EMPTY_URL_ARRAY = {};
44
45    /** Whether to use standard loading */
46    protected boolean standard = false;
47
48    /**
49     * Constructor
50     *
51     * @param parent the parent classloader, cannot be null.
52     */

53    public DelegatingClassLoader(ClassLoader JavaDoc parent)
54    {
55       super(EMPTY_URL_ARRAY, parent);
56       if (parent == null)
57          throw new IllegalArgumentException JavaDoc("No parent");
58    }
59
60    /**
61     * Constructor
62     *
63     * @param parent, the parent classloader, cannot be null.
64     * @param factory the url stream factory.
65     */

66    public DelegatingClassLoader(ClassLoader JavaDoc parent, URLStreamHandlerFactory JavaDoc factory)
67    {
68       super(EMPTY_URL_ARRAY, parent, factory);
69       if (parent == null)
70          throw new IllegalArgumentException JavaDoc("No parent");
71    }
72
73    /**
74     * Load a class, by asking the parent
75     *
76     * @param className the class name to load
77     * @param resolve whether to link the class
78     * @return the loaded class
79     * @throws ClassNotFoundException when the class could not be found
80     */

81    protected Class JavaDoc loadClass(String JavaDoc className, boolean resolve)
82       throws ClassNotFoundException JavaDoc
83    {
84       // Revert to standard rules
85
if (standard)
86          return super.loadClass(className, resolve);
87
88       // Ask the parent
89
Class JavaDoc clazz = getParent().loadClass(className);
90
91       // Link the class
92
if (resolve)
93          resolveClass(clazz);
94
95       return clazz;
96    }
97 }
98
Popular Tags