KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > baseadaptor > DefaultClassLoader


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.internal.baseadaptor;
13
14 import java.io.IOException JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.security.*;
18 import java.security.cert.Certificate JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import org.eclipse.osgi.baseadaptor.BaseData;
21 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry;
22 import org.eclipse.osgi.baseadaptor.bundlefile.BundleFile;
23 import org.eclipse.osgi.baseadaptor.loader.*;
24 import org.eclipse.osgi.framework.adaptor.BundleData;
25 import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate;
26 import org.eclipse.osgi.framework.debug.Debug;
27 import org.eclipse.osgi.internal.provisional.verifier.CertificateChain;
28 import org.eclipse.osgi.internal.provisional.verifier.CertificateVerifier;
29
30 /**
31  * The default implemention of <code>BaseClassLoader</code>. This implementation extends
32  * <code>ClassLoader</code>.
33  * @see BaseClassLoader
34  * @see ClasspathManager
35  */

36 public class DefaultClassLoader extends ClassLoader JavaDoc implements BaseClassLoader {
37     /**
38      * A PermissionCollection for AllPermissions; shared across all ProtectionDomains when security is disabled
39      */

40     protected static final PermissionCollection ALLPERMISSIONS;
41     static {
42         AllPermission allPerm = new AllPermission();
43         ALLPERMISSIONS = allPerm.newPermissionCollection();
44         if (ALLPERMISSIONS != null)
45             ALLPERMISSIONS.add(allPerm);
46     }
47
48     protected ClassLoaderDelegate delegate;
49     protected ProtectionDomain domain;
50     protected ClasspathManager manager;
51
52     /**
53      * Constructs a new DefaultClassLoader.
54      * @param parent the parent classloader
55      * @param delegate the delegate for this classloader
56      * @param domain the domain for this classloader
57      * @param bundledata the bundledata for this classloader
58      * @param classpath the classpath for this classloader
59      */

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

78     protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
79         if (Debug.DEBUG && Debug.DEBUG_LOADER)
80             Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
81
try {
82             // Just ask the delegate. This could result in findLocalClass(name) being called.
83
Class JavaDoc clazz = delegate.findClass(name);
84             // resolve the class if asked to.
85
if (resolve)
86                 resolveClass(clazz);
87             return (clazz);
88         } catch (Error JavaDoc e) {
89             if (Debug.DEBUG && Debug.DEBUG_LOADER) {
90                 Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ") failed."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
91
Debug.printStackTrace(e);
92             }
93             throw e;
94         } catch (ClassNotFoundException JavaDoc e) {
95             // If the class is not found do not try to look for it locally.
96
// The delegate would have already done that for us.
97
if (Debug.DEBUG && Debug.DEBUG_LOADER) {
98                 Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ") failed."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
99
Debug.printStackTrace(e);
100             }
101             throw e;
102         }
103     }
104
105     /**
106      * Gets a resource for the bundle. First delegate.findResource(name) is
107      * called. The delegate will query the system class loader, bundle imports,
108      * bundle local resources, bundle hosts and fragments. The delegate will
109      * call BundleClassLoader.findLocalResource(name) to find a resource local
110      * to this bundle.
111      * @param name The resource path to get.
112      * @return The URL of the resource or null if it does not exist.
113      */

114     public URL JavaDoc getResource(String JavaDoc name) {
115         if (Debug.DEBUG && Debug.DEBUG_LOADER) {
116             Debug.println("BundleClassLoader[" + delegate + "].getResource(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
117
}
118
119         URL JavaDoc url = delegate.findResource(name);
120         if (url != null)
121             return (url);
122
123         if (Debug.DEBUG && Debug.DEBUG_LOADER) {
124             Debug.println("BundleClassLoader[" + delegate + "].getResource(" + name + ") failed."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
125
}
126
127         return (null);
128     }
129
130     /**
131      * Finds all resources with the specified name. This method must call
132      * delegate.findResources(name) to find all the resources.
133      * @param name The resource path to find.
134      * @return An Enumeration of all resources found or null if the resource.
135      * @throws IOException
136      */

137     protected Enumeration JavaDoc findResources(String JavaDoc name) throws IOException JavaDoc {
138         return (delegate.findResources(name));
139     }
140
141     /**
142      * Finds a library for this bundle. Simply calls
143      * delegate.findLibrary(libname) to find the library.
144      * @param libname The library to find.
145      * @return The URL of the resource or null if it does not exist.
146      */

147     protected String JavaDoc findLibrary(String JavaDoc libname) {
148         // let the manager find the library for us
149
return manager.findLibrary(libname);
150     }
151
152     public ProtectionDomain getDomain() {
153         return domain;
154     }
155
156     public ClasspathEntry createClassPathEntry(BundleFile bundlefile, ProtectionDomain cpDomain) {
157         return new ClasspathEntry(bundlefile, createProtectionDomain(bundlefile, cpDomain));
158     }
159
160     public Class JavaDoc defineClass(String JavaDoc name, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry) {
161         return defineClass(name, classbytes, 0, classbytes.length, classpathEntry.getDomain());
162     }
163
164     public Class JavaDoc publicFindLoaded(String JavaDoc classname) {
165         return findLoadedClass(classname);
166     }
167
168     public Object JavaDoc publicGetPackage(String JavaDoc pkgname) {
169         return getPackage(pkgname);
170     }
171
172     public Object JavaDoc publicDefinePackage(String JavaDoc name, String JavaDoc specTitle, String JavaDoc specVersion, String JavaDoc specVendor, String JavaDoc implTitle, String JavaDoc implVersion, String JavaDoc implVendor, URL JavaDoc sealBase) {
173         return definePackage(name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, sealBase);
174     }
175
176     public void initialize() {
177         manager.initialize();
178     }
179
180     public URL JavaDoc findLocalResource(String JavaDoc resource) {
181         return manager.findLocalResource(resource);
182     }
183
184     public Enumeration JavaDoc findLocalResources(String JavaDoc resource) {
185         return manager.findLocalResources(resource);
186     }
187
188     public Class JavaDoc findLocalClass(String JavaDoc classname) throws ClassNotFoundException JavaDoc {
189         return manager.findLocalClass(classname);
190     }
191
192     public void close() {
193         manager.close();
194     }
195
196     public void attachFragment(BundleData sourcedata, ProtectionDomain sourcedomain, String JavaDoc[] sourceclasspath) {
197         manager.attachFragment(sourcedata, sourcedomain, sourceclasspath);
198     }
199
200     public ClassLoaderDelegate getDelegate() {
201         return delegate;
202     }
203
204     /**
205      * Creates a ProtectionDomain which uses specified BundleFile and the permissions of the baseDomain
206      * @param bundlefile The source bundlefile the domain is for.
207      * @param baseDomain The source domain.
208      * @return a ProtectionDomain which uses specified BundleFile and the permissions of the baseDomain
209      */

210     public static ProtectionDomain createProtectionDomain(BundleFile bundlefile, ProtectionDomain baseDomain) {
211         // create a protection domain which knows about the codesource for this classpath entry (bug 89904)
212
try {
213             // use the permissions supplied by the domain passed in from the framework
214
PermissionCollection permissions;
215             if (baseDomain != null)
216                 permissions = baseDomain.getPermissions();
217             else
218                 // no domain specified. Better use a collection that has all permissions
219
// this is done just incase someone sets the security manager later
220
permissions = ALLPERMISSIONS;
221             Certificate JavaDoc[] certs = null;
222             if (bundlefile instanceof CertificateVerifier) {
223                 CertificateChain[] chains = ((CertificateVerifier) bundlefile).getChains();
224                 certs = chains == null || chains.length == 0 ? null : chains[0].getCertificates();
225             }
226             return new ProtectionDomain(new CodeSource(bundlefile.getBaseFile().toURL(), certs), permissions);
227         } catch (MalformedURLException JavaDoc e) {
228             // Failed to create our own domain; just return the baseDomain
229
return baseDomain;
230         }
231     }
232
233     public ClasspathManager getClasspathManager() {
234         return manager;
235     }
236 }
237
Popular Tags