KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > appserver > PluginClassLoaderWrapper


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.help.internal.appserver;
12 import java.io.*;
13 import java.net.*;
14 import java.util.*;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.osgi.util.*;
18 import org.osgi.framework.*;
19 /**
20  * Wrapper for a plugin class loader. This class is only needed because the
21  * current PluginClassLoader is not clearly exposed as a URLClassLoader and its
22  * getURLs() method does not properly return the list of url's (it misses
23  * required jars, etc.)
24  */

25 public class PluginClassLoaderWrapper extends URLClassLoader {
26     private String JavaDoc plugin;
27     private Bundle bundle;
28     public PluginClassLoaderWrapper(String JavaDoc plugin) {
29         super(new URL[0]);
30         this.plugin = plugin;
31         this.bundle = Platform.getBundle(plugin);
32     }
33     public Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
34         return bundle.loadClass(className);
35     }
36     public URL getResource(String JavaDoc resName) {
37         return bundle.getResource(resName);
38     }
39     /**
40      * This is a workaround for the jsp compiler that needs to know the
41      * classpath.
42      */

43     public URL[] getURLs() {
44         Set urls = getPluginClasspath(plugin);
45         return (URL[]) urls.toArray(new URL[urls.size()]);
46     }
47     private Set getPluginClasspath(String JavaDoc pluginId) {
48         // Collect set of plug-ins
49
Set plugins = new HashSet();
50         addPluginWithPrereqs(pluginId, plugins);
51         // Collect URLs for each plug-in
52
Set urls = new HashSet();
53         for (Iterator it = plugins.iterator(); it.hasNext();) {
54             String JavaDoc id = (String JavaDoc) it.next();
55             try {
56                 Bundle b = Platform.getBundle(id);
57                 if (b != null) {
58                     // declared classpath
59
String JavaDoc headers = (String JavaDoc) b.getHeaders().get(
60                             Constants.BUNDLE_CLASSPATH);
61                     ManifestElement[] paths = ManifestElement.parseHeader(
62                             Constants.BUNDLE_CLASSPATH, headers);
63                     if (paths != null) {
64                         for (int i = 0; i < paths.length; i++) {
65                             String JavaDoc path = paths[i].getValue();
66                             URL url = b.getEntry(path);
67                             if (url != null)
68                                 try {
69                                     urls.add(FileLocator.toFileURL(url));
70                                 } catch (IOException ioe) {
71                                 }
72                         }
73                     }
74                     // dev classpath
75
String JavaDoc[] devpaths = DevClassPathHelper
76                             .getDevClassPath(pluginId);
77                     if (devpaths != null) {
78                         for (int i = 0; i < devpaths.length; i++) {
79                             URL url = b.getEntry(devpaths[i]);
80                             if (url != null)
81                                 try {
82                                     urls.add(FileLocator.toFileURL(url));
83                                 } catch (IOException ioe) {
84                                 }
85                         }
86                     }
87                 }
88             } catch (BundleException e) {
89             }
90         }
91         return urls;
92     }
93     /**
94      * Ensures set contains plugin ID of given plugin and all its prereqs. Does
95      * nothing if set contains given plug-in.
96      */

97     private void addPluginWithPrereqs(String JavaDoc pluginId, Set pluginIds) {
98         if (pluginIds.contains(pluginId)) {
99             return;
100         }
101         String JavaDoc[] immidiatePrereqs = getDirectPrereqs(pluginId);
102         for (int i = 0; i < immidiatePrereqs.length; i++) {
103             addPluginWithPrereqs(immidiatePrereqs[i], pluginIds);
104         }
105         pluginIds.add(pluginId);
106     }
107     /**
108      * Obtain plug-ins immidiately required by given plug-in
109      *
110      * @param pluginId
111      * @return
112      */

113     private String JavaDoc[] getDirectPrereqs(String JavaDoc pluginId) {
114         try {
115             Bundle bundle = Platform.getBundle(pluginId);
116             if (bundle != null) {
117                 String JavaDoc header = (String JavaDoc) bundle.getHeaders().get(
118                         Constants.REQUIRE_BUNDLE);
119                 ManifestElement[] requires = ManifestElement.parseHeader(
120                         Constants.REQUIRE_BUNDLE, header);
121                 if (requires != null) {
122                     String JavaDoc[] reqs = new String JavaDoc[requires.length];
123                     for (int i = 0; i < requires.length; i++) {
124                         reqs[i] = requires[i].getValue();
125                     }
126                     return reqs;
127                 }
128             }
129         } catch (BundleException e) {
130         }
131         return new String JavaDoc[0];
132     }
133 }
134
Popular Tags