KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > adaptor > core > AbstractClassLoader


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.adaptor.core;
13
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.security.*;
17 import java.util.Enumeration JavaDoc;
18 import org.eclipse.osgi.framework.adaptor.*;
19 import org.eclipse.osgi.framework.debug.Debug;
20
21 /**
22  * The AbstractClassLoader provides some basic functionality that all
23  * BundleClassLoaders must provide. It properly delegates resource and
24  * class lookups to a parent classloader and the to a ClassLoaderDelegate.
25  * <p>
26  * Clients may extend this class.
27  * </p>
28  * @since 3.1
29  */

30 public abstract class AbstractClassLoader extends ClassLoader JavaDoc implements BundleClassLoader {
31     /**
32      * The delegate used to get classes and resources from. The delegate
33      * must always be queried first before the local ClassLoader is searched for
34      * a class or resource.
35      */

36     protected ClassLoaderDelegate delegate;
37
38     /**
39      * The host ProtectionDomain to use to define classes.
40      */

41     protected ProtectionDomain hostdomain;
42
43     /**
44      * The host classpath entries for this classloader
45      */

46     protected String JavaDoc[] hostclasspath;
47
48     /**
49      * BundleClassLoader constructor.
50      * @param delegate The ClassLoaderDelegate for this bundle.
51      * @param domain The ProtectionDomain for this bundle.
52      * @param parent The parent classloader to use. Must not be null.
53      * @param classpath The classpath entries to use for the host.
54      */

55     public AbstractClassLoader(ClassLoaderDelegate delegate, ProtectionDomain domain, String JavaDoc[] classpath, ClassLoader JavaDoc parent) {
56         super(parent);
57         this.delegate = delegate;
58         this.hostdomain = domain;
59         this.hostclasspath = classpath;
60     }
61
62     /**
63      * Loads a class for the bundle. First delegate.findClass(name) is called.
64      * The delegate will query the system class loader, bundle imports, bundle
65      * local classes, bundle hosts and fragments. The delegate will call
66      * BundleClassLoader.findLocalClass(name) to find a class local to this
67      * bundle.
68      * @param name the name of the class to load.
69      * @param resolve indicates whether to resolve the loaded class or not.
70      * @return The Class object.
71      * @throws ClassNotFoundException if the class is not found.
72      */

73     protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
74         if (Debug.DEBUG && Debug.DEBUG_LOADER)
75             Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
76
try {
77             // Just ask the delegate. This could result in findLocalClass(name) being called.
78
Class JavaDoc clazz = delegate.findClass(name);
79             // resolve the class if asked to.
80
if (resolve)
81                 resolveClass(clazz);
82             return (clazz);
83         } catch (Error JavaDoc e) {
84             if (Debug.DEBUG && Debug.DEBUG_LOADER) {
85                 Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ") failed."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
86
Debug.printStackTrace(e);
87             }
88             throw e;
89         } catch (ClassNotFoundException JavaDoc e) {
90             // If the class is not found do not try to look for it locally.
91
// The delegate would have already done that for us.
92
if (Debug.DEBUG && Debug.DEBUG_LOADER) {
93                 Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ") failed."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
94
Debug.printStackTrace(e);
95             }
96             throw e;
97         }
98     }
99
100     /**
101      * Finds a class local to this bundle. The bundle class path is used
102      * to search for the class. The delegate must not be used. This method
103      * is abstract to force extending classes to implement this method instead
104      * of using the ClassLoader.findClass(String) method.
105      * @param name The classname of the class to find
106      * @return The Class object.
107      * @throws ClassNotFoundException if the class is not found.
108      */

109     abstract protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc;
110
111     /**
112      * Gets a resource for the bundle. First delegate.findResource(name) is
113      * called. The delegate will query the system class loader, bundle imports,
114      * bundle local resources, bundle hosts and fragments. The delegate will
115      * call BundleClassLoader.findLocalResource(name) to find a resource local
116      * to this bundle.
117      * @param name The resource path to get.
118      * @return The URL of the resource or null if it does not exist.
119      */

120     public URL JavaDoc getResource(String JavaDoc name) {
121         if (Debug.DEBUG && Debug.DEBUG_LOADER) {
122             Debug.println("BundleClassLoader[" + delegate + "].getResource(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123
}
124
125         URL JavaDoc url = delegate.findResource(name);
126         if (url != null)
127             return (url);
128
129         if (Debug.DEBUG && Debug.DEBUG_LOADER) {
130             Debug.println("BundleClassLoader[" + delegate + "].getResource(" + name + ") failed."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
131
}
132
133         return (null);
134     }
135
136     /**
137      * Finds a resource local to this bundle. Simply calls
138      * findResourceImpl(name) to find the resource.
139      * @param name The resource path to find.
140      * @return The URL of the resource or null if it does not exist.
141      */

142     abstract protected URL JavaDoc findResource(String JavaDoc name);
143
144     /**
145      * Finds all resources with the specified name. This method must call
146      * delegate.findResources(name) to find all the resources.
147      * @param name The resource path to find.
148      * @return An Enumeration of all resources found or null if the resource.
149      * @throws IOException
150      */

151     protected Enumeration JavaDoc findResources(String JavaDoc name) throws IOException JavaDoc {
152         return (delegate.findResources(name));
153     }
154
155     /**
156      * Finds a library for this bundle. Simply calls
157      * delegate.findLibrary(libname) to find the library.
158      * @param libname The library to find.
159      * @return The URL of the resource or null if it does not exist.
160      */

161     protected String JavaDoc findLibrary(String JavaDoc libname) {
162         return delegate.findLibrary(libname);
163     }
164
165     /**
166      * Finds a local resource in the BundleClassLoader without
167      * consulting the delegate.
168      * @param resource the resource path to find.
169      * @return a URL to the resource or null if the resource does not exist.
170      */

171     public URL JavaDoc findLocalResource(String JavaDoc resource) {
172         return findResource(resource);
173     }
174
175     /**
176      * Finds a local class in the BundleClassLoader without
177      * consulting the delegate.
178      * @param classname the classname to find.
179      * @return The class object found.
180      * @throws ClassNotFoundException if the classname does not exist locally.
181      */

182     public Class JavaDoc findLocalClass(String JavaDoc classname) throws ClassNotFoundException JavaDoc {
183         return findClass(classname);
184     }
185
186     /**
187      * Returns a local entry for the specified path
188      * @param path the entry path
189      * @return a bundle entry for the specified path or <code>null</code> if the
190      * path does not exist
191      */

192     // TODO should rename to findLocalEntry ??
193
abstract public Object JavaDoc findLocalObject(String JavaDoc path);
194
195     /**
196      * Returns an Enumeration of local entries for the specified path. The returned
197      * Enumeration is ordered using the correct local search order of the classpath for
198      * this classloader.
199      * @param path the entry path
200      * @return an enumeration of entries for the specified path or <code>null</code> if
201      * the path does not exist.
202      */

203     abstract public Enumeration JavaDoc findLocalObjects(String JavaDoc path);
204
205     /**
206      * @see BundleClassLoader#getDelegate()
207      */

208     public ClassLoaderDelegate getDelegate() {
209         return delegate;
210     }
211
212     /**
213      * @see BundleClassLoader#close()
214      */

215     public void close() {
216         // do nothing
217
}
218
219 }
220
Popular Tags