Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cfa08d524a399b28cd18cc9a839ec34cd56d5443 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.appserver;
import java.io.*;
import java.net.*;
import java.util.*;

import org.eclipse.core.runtime.*;
import org.eclipse.osgi.util.*;
import org.osgi.framework.*;
/**
 * Wrapper for a plugin class loader. This class is only needed because the
 * current PluginClassLoader is not clearly exposed as a URLClassLoader and its
 * getURLs() method does not properly return the list of url's (it misses
 * required jars, etc.)
 */
public class PluginClassLoaderWrapper extends URLClassLoader {
	private String plugin;
	private Bundle bundle;
	public PluginClassLoaderWrapper(String plugin) {
		super(new URL[0]);
		this.plugin = plugin;
		this.bundle = Platform.getBundle(plugin);
	}
	public Class loadClass(String className) throws ClassNotFoundException {
		return bundle.loadClass(className);
	}
	public URL getResource(String resName) {
		return bundle.getResource(resName);
	}
	/**
	 * This is a workaround for the jsp compiler that needs to know the
	 * classpath.
	 */
	public URL[] getURLs() {
		Set urls = getPluginClasspath(plugin);
		return (URL[]) urls.toArray(new URL[urls.size()]);
	}
	private Set getPluginClasspath(String pluginId) {
		// Collect set of plug-ins
		Set plugins = new HashSet();
		addPluginWithPrereqs(pluginId, plugins);
		// Collect URLs for each plug-in
		Set urls = new HashSet();
		for (Iterator it = plugins.iterator(); it.hasNext();) {
			String id = (String) it.next();
			try {
				Bundle b = Platform.getBundle(id);
				if (b != null) {
					// declared classpath
					String headers = (String) b.getHeaders().get(
							Constants.BUNDLE_CLASSPATH);
					ManifestElement[] paths = ManifestElement.parseHeader(
							Constants.BUNDLE_CLASSPATH, headers);
					if (paths != null) {
						for (int i = 0; i < paths.length; i++) {
							String path = paths[i].getValue();
							URL url = b.getEntry(path);
							if (url != null)
								try {
									urls.add(FileLocator.toFileURL(url));
								} catch (IOException ioe) {
								}
						}
					}
					// dev classpath
					String[] devpaths = DevClassPathHelper
							.getDevClassPath(pluginId);
					if (devpaths != null) {
						for (int i = 0; i < devpaths.length; i++) {
							URL url = b.getEntry(devpaths[i]);
							if (url != null)
								try {
									urls.add(FileLocator.toFileURL(url));
								} catch (IOException ioe) {
								}
						}
					}
				}
			} catch (BundleException e) {
			}
		}
		return urls;
	}
	/**
	 * Ensures set contains plugin ID of given plugin and all its prereqs. Does
	 * nothing if set contains given plug-in.
	 */
	private void addPluginWithPrereqs(String pluginId, Set pluginIds) {
		if (pluginIds.contains(pluginId)) {
			return;
		}
		String[] immidiatePrereqs = getDirectPrereqs(pluginId);
		for (int i = 0; i < immidiatePrereqs.length; i++) {
			addPluginWithPrereqs(immidiatePrereqs[i], pluginIds);
		}
		pluginIds.add(pluginId);
	}
	/**
	 * Obtain plug-ins immidiately required by given plug-in
	 * 
	 * @param pluginId
	 * @return
	 */
	private String[] getDirectPrereqs(String pluginId) {
		try {
			Bundle bundle = Platform.getBundle(pluginId);
			if (bundle != null) {
				String header = (String) bundle.getHeaders().get(
						Constants.REQUIRE_BUNDLE);
				ManifestElement[] requires = ManifestElement.parseHeader(
						Constants.REQUIRE_BUNDLE, header);
				if (requires != null) {
					String[] reqs = new String[requires.length];
					for (int i = 0; i < requires.length; i++) {
						reqs[i] = requires[i].getValue();
					}
					return reqs;
				}
			}
		} catch (BundleException e) {
		}
		return new String[0];
	}
}

Back to the top