KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > plugins > PluginClassLoader


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.core.internal.plugins;
12
13 import java.io.IOException JavaDoc;
14 import java.net.*;
15 import org.eclipse.core.internal.runtime.InternalPlatform;
16 import org.eclipse.core.runtime.ILibrary;
17 import org.eclipse.core.runtime.Platform;
18 import org.osgi.framework.Bundle;
19
20 public class PluginClassLoader extends URLClassLoader {
21     private Bundle bundle; //We should be able to get rid of this field, since the info is in the descriptor
22
private PluginDescriptor descriptor;
23
24     PluginClassLoader(PluginDescriptor descriptor) {
25         super(computeURLs(descriptor));
26         this.descriptor = descriptor;
27         this.bundle = InternalPlatform.getDefault().getBundle(descriptor.getUniqueIdentifier());
28         if (bundle == null)
29             throw new IllegalArgumentException JavaDoc();
30     }
31
32     private static URL[] computeURLs(PluginDescriptor descriptor) {
33         Bundle bundle = InternalPlatform.getDefault().getBundle(descriptor.getUniqueIdentifier());
34         if (bundle == null)
35             throw new IllegalArgumentException JavaDoc();
36
37         ILibrary[] libs = descriptor.getRuntimeLibraries();
38         String JavaDoc[] devPath = computeDevPath(bundle);
39         URL pluginBase = descriptor.getInstallURL();
40         try {
41             pluginBase = Platform.resolve(descriptor.getInstallURL());
42         } catch (IOException JavaDoc e1) {
43             //Ignore
44
}
45
46         URL[] urls = new URL[devPath.length + libs.length];
47         int j = 0;
48         for (int i = 0; i < devPath.length; i++) {
49             try {
50                 urls[j++] = new URL(pluginBase, devPath[i]);
51             } catch (MalformedURLException e) {
52                 //Ignore the exception
53
}
54         }
55         for (int i = 0; i < libs.length; i++) {
56             try {
57                 urls[j++] = new URL(pluginBase, libs[i].getPath().toOSString());
58             } catch (MalformedURLException e) {
59                 //Ignore the exception
60
}
61         }
62         return urls;
63     }
64
65     private static String JavaDoc[] computeDevPath(Bundle bundle) {
66         if (!DevClassPathHelper.inDevelopmentMode())
67             return new String JavaDoc[0];
68
69         String JavaDoc pluginId = bundle.getSymbolicName();
70         if (pluginId == null)
71             return new String JavaDoc[0];
72         return DevClassPathHelper.getDevClassPath(pluginId);
73     }
74
75     protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
76         return bundle.loadClass(name); // if no CNFE is thrown, activate the bundle (if needed)
77
}
78
79     public URL findResource(String JavaDoc name) {
80         return bundle.getResource(name);
81     }
82
83     public PluginDescriptor getPluginDescriptor() {
84         return descriptor;
85     }
86 }
87
Popular Tags