KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > classpath > BshClassLoader


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34 package bsh.classpath;
35
36 import java.net.*;
37 import java.util.*;
38 import java.io.*;
39 import bsh.BshClassManager;
40
41 /**
42     One of the things BshClassLoader does is to address a deficiency in
43     URLClassLoader that prevents us from specifying individual classes
44     via URLs.
45 */

46 public class BshClassLoader extends URLClassLoader
47 {
48     BshClassManager classManager;
49
50     /**
51         @param bases URLs JARClassLoader seems to require absolute paths
52     */

53     public BshClassLoader( BshClassManager classManager, URL [] bases ) {
54         super( bases );
55         this.classManager = classManager;
56     }
57
58     /**
59         @param bases URLs JARClassLoader seems to require absolute paths
60     */

61     public BshClassLoader( BshClassManager classManager, BshClassPath bcp ) {
62         this( classManager, bcp.getPathComponents() );
63     }
64
65     /**
66         For use by children
67         @param bases URLs JARClassLoader seems to require absolute paths
68     */

69     protected BshClassLoader( BshClassManager classManager ) {
70         this( classManager, new URL [] { } );
71     }
72
73     // public version of addURL
74
public void addURL( URL url ) {
75         super.addURL( url );
76     }
77
78     /**
79         This modification allows us to reload classes which are in the
80         Java VM user classpath. We search first rather than delegate to
81         the parent classloader (or bootstrap path) first.
82
83         An exception is for BeanShell core classes which are always loaded from
84         the same classloader as the interpreter.
85     */

86     public Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
87         throws ClassNotFoundException JavaDoc
88     {
89         Class JavaDoc c = null;
90
91         /*
92             Check first for classes loaded through this loader.
93             The VM will not allow a class to be loaded twice.
94         */

95         c = findLoadedClass(name);
96         if ( c != null )
97             return c;
98
99 // This is copied from ClassManagerImpl
100
// We should refactor this somehow if it sticks around
101
if ( name.startsWith( ClassManagerImpl.BSH_PACKAGE ) )
102             try {
103                 return bsh.Interpreter.class.getClassLoader().loadClass( name );
104             } catch ( ClassNotFoundException JavaDoc e ) {}
105
106         /*
107             Try to find the class using our classloading mechanism.
108             Note: I wish we didn't have to catch the exception here... slow
109         */

110         try {
111             c = findClass( name );
112         } catch ( ClassNotFoundException JavaDoc e ) { }
113
114         if ( c == null )
115             throw new ClassNotFoundException JavaDoc("here in loaClass");
116
117         if ( resolve )
118             resolveClass( c );
119
120         return c;
121     }
122
123     /**
124         Find the correct source for the class...
125
126         Try designated loader if any
127         Try our URLClassLoader paths if any
128         Try base loader if any
129         Try system ???
130     */

131     // add some caching for not found classes?
132
protected Class JavaDoc findClass( String JavaDoc name )
133         throws ClassNotFoundException JavaDoc
134     {
135         // Deal with this cast somehow... maybe have this class use
136
// ClassManagerImpl type directly.
137
// Don't add the method to BshClassManager... it's really an impl thing
138
ClassManagerImpl bcm = (ClassManagerImpl)getClassManager();
139
140         // Should we try to load the class ourselves or delegate?
141
// look for overlay loader
142

143         // Deal with this cast somehow... maybe have this class use
144
// ClassManagerImpl type directly.
145
// Don't add the method to BshClassManager... it's really an impl thing
146
ClassLoader JavaDoc cl = bcm.getLoaderForClass( name );
147
148         Class JavaDoc c;
149
150         // If there is a designated loader and it's not us delegate to it
151
if ( cl != null && cl != this )
152             try {
153                 return cl.loadClass( name );
154             } catch ( ClassNotFoundException JavaDoc e ) {
155                 throw new ClassNotFoundException JavaDoc(
156                     "Designated loader could not find class: "+e );
157             }
158
159         // Let URLClassLoader try any paths it may have
160
if ( getURLs().length > 0 )
161             try {
162                 return super.findClass(name);
163             } catch ( ClassNotFoundException JavaDoc e ) {
164                 //System.out.println(
165
// "base loader here caught class not found: "+name );
166
}
167
168
169         // If there is a baseLoader and it's not us delegate to it
170
cl = bcm.getBaseLoader();
171
172         if ( cl != null && cl != this )
173             try {
174                 return cl.loadClass( name );
175             } catch ( ClassNotFoundException JavaDoc e ) { }
176
177         // Try system loader
178
return bcm.plainClassForName( name );
179     }
180
181     /*
182         The superclass does something like this
183
184         c = findLoadedClass(name);
185         if null
186             try
187                 if parent not null
188                     c = parent.loadClass(name, false);
189                 else
190                     c = findBootstrapClass(name);
191             catch ClassNotFoundException
192                 c = findClass(name);
193     */

194
195     BshClassManager getClassManager() { return classManager; }
196
197 }
198
Popular Tags