File: SystemBundleLoader.java

package info (click to toggle)
libequinox-osgi-java 3.9.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,068 kB
  • sloc: java: 57,768; makefile: 9
file content (277 lines) | stat: -rw-r--r-- 8,833 bytes parent folder | download | duplicates (6)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
 * Copyright (c) 2003, 2010 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.osgi.internal.loader;

import java.io.IOException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.*;
import org.eclipse.osgi.framework.adaptor.*;
import org.eclipse.osgi.framework.internal.core.BundleFragment;
import org.eclipse.osgi.framework.internal.core.BundleHost;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.wiring.BundleWiring;

/**
 * The System Bundle's BundleLoader.  This BundleLoader is used by ImportClassLoaders
 * to load a resource that is exported by the System Bundle.
 */
public class SystemBundleLoader extends BundleLoader {
	public static final String EQUINOX_EE = "x-equinox-ee"; //$NON-NLS-1$
	final ClassLoader classLoader;
	private final Set<String> eePackages;
	private final Set<String> extPackages;
	private final ClassLoader extClassLoader;

	/**
	 * @param bundle The system bundle.
	 * @param proxy The BundleLoaderProxy for the system bundle
	 * @throws BundleException On any error.
	 */
	protected SystemBundleLoader(BundleHost bundle, BundleLoaderProxy proxy) throws BundleException {
		super(bundle, proxy);
		ExportPackageDescription[] exports = proxy.getBundleDescription().getSelectedExports();
		if (exports == null || exports.length == 0)
			eePackages = null;
		else {
			eePackages = new HashSet<String>(exports.length);
			for (int i = 0; i < exports.length; i++)
				if (((Integer) exports[i].getDirective(EQUINOX_EE)).intValue() >= 0)
					eePackages.add(exports[i].getName());
		}
		this.classLoader = getClass().getClassLoader();
		extPackages = new HashSet<String>(0); // not common; start with 0
		BundleFragment[] fragments = bundle.getFragments();
		if (fragments != null)
			for (int i = 0; i < fragments.length; i++)
				addExtPackages(fragments[i]);
		ClassLoader extCL = ClassLoader.getSystemClassLoader();
		if (extCL == null)
			extClassLoader = null;
		else {
			while (extCL.getParent() != null)
				extCL = extCL.getParent();
			// make sure extCL is not already on the parent chain of the system classloader
			boolean found = false;
			ClassLoader systemExtCL = this.classLoader;
			while (systemExtCL.getParent() != null && !found) {
				if (systemExtCL.getParent() == extCL)
					found = true;
				else
					systemExtCL = systemExtCL.getParent();
			}
			extClassLoader = found ? null : extCL;
		}
	}

	private void addExtPackages(BundleFragment fragment) {
		if ((fragment.getBundleData().getType() & BundleData.TYPE_EXTCLASSPATH_EXTENSION) == 0)
			return;
		ExportPackageDescription[] extExports = fragment.getBundleDescription().getExportPackages();
		for (int j = 0; j < extExports.length; j++)
			extPackages.add(extExports[j].getName());
	}

	synchronized public void attachFragment(BundleFragment fragment) throws BundleException {
		super.attachFragment(fragment);
		synchronized (extPackages) {
			addExtPackages(fragment);
		}
	}

	/**
	 * The ClassLoader that loads OSGi framework classes is used to find the class.
	 * This method never gets called because there is no BundleClassLoader for the framework.
	 */
	public Class<?> findClass(String name) throws ClassNotFoundException {
		Class<?> result = findLocalClass(name);
		if (result == null)
			throw new ClassNotFoundException(name);
		return result;
	}

	/**
	 * This method will always return null.
	 * This method never gets called because there is no BundleClassLoader for the framework.
	 */
	public String findLibrary(String name) {
		return null;
	}

	/**
	 * The ClassLoader that loads OSGi framework classes is used to find the class. 
	 */
	Class<?> findLocalClass(String name) {
		try {
			return classLoader.loadClass(name);
		} catch (ClassNotFoundException e) {
			if (extClassLoader != null)
				synchronized (extPackages) {
					if (extPackages.size() > 0 && extPackages.contains(BundleLoader.getPackageName(name)))
						try {
							return extClassLoader.loadClass(name);
						} catch (ClassNotFoundException e2) {
							return null;
						}
				}
		}
		return null;
	}

	/**
	 * The ClassLoader that loads OSGi framework classes is used to find the resource.
	 */
	URL findLocalResource(String name) {
		URL result = classLoader.getResource(name);
		if (result == null && extClassLoader != null)
			synchronized (extPackages) {
				if (extPackages.size() > 0 && extPackages.contains(BundleLoader.getResourcePackageName(name)))
					result = extClassLoader.getResource(name);
			}
		return result;
	}

	/**
	 * The ClassLoader that loads OSGi framework classes is used to find the resource.
	 */
	Enumeration<URL> findLocalResources(String name) {
		Enumeration<URL> result = null;
		try {
			result = classLoader.getResources(name);
		} catch (IOException e) {
			// do nothing
		}
		if ((result == null || !result.hasMoreElements()) && extClassLoader != null)
			synchronized (extPackages) {
				if (extPackages.size() > 0 && extPackages.contains(BundleLoader.getResourcePackageName(name)))
					try {
						result = extClassLoader.getResources(name);
					} catch (IOException e) {
						// do nothing
					}
			}
		return result;
	}

	/**
	 * The ClassLoader that loads OSGi framework classes is used to find the resource.
	 * This method never gets called because there is no BundleClassLoader for the framework.
	 */
	public URL findResource(String name) {
		return findLocalResource(name);
	}

	/**
	 * The ClassLoader that loads OSGi framework classes is used to find the resource.
	 * This method never gets called because there is no BundleClassLoader for the framework.
	 * @throws IOException 
	 */
	public Enumeration<URL> findResources(String name) throws IOException {
		return findLocalResources(name);
	}

	/**
	 * Do nothing on a close.
	 */
	protected void close() {
		// Do nothing.
	}

	public boolean isEEPackage(String pkgName) {
		return eePackages.contains(pkgName);
	}

	BundleClassLoader createBCL(BundleProtectionDomain pd, String[] cp) {
		return new BundleClassLoader() {

			public Bundle getBundle() {
				return SystemBundleLoader.this.getBundle();
			}

			public Class<?> loadClass(String name) throws ClassNotFoundException {
				return SystemBundleLoader.this.loadClass(name);
			}

			public void initialize() {
				// nothing
			}

			/**
			 * @throws IOException  
			 */
			public Enumeration<URL> getResources(String name) throws IOException {
				return findLocalResources(name);
			}

			public URL getResource(String name) {
				return SystemBundleLoader.this.findLocalResource(name);
			}

			public ClassLoader getParent() {
				return SystemBundleLoader.this.classLoader.getParent();
			}

			public ClassLoaderDelegate getDelegate() {
				return SystemBundleLoader.this;
			}

			public Enumeration<URL> findLocalResources(String resource) {
				return SystemBundleLoader.this.findLocalResources(resource);
			}

			public URL findLocalResource(String resource) {
				return getResource(resource);
			}

			/**
			 * @throws ClassNotFoundException  
			 */
			public Class<?> findLocalClass(String classname) throws ClassNotFoundException {
				return SystemBundleLoader.this.findLocalClass(classname);
			}

			public void close() {
				// nothing
			}

			public void attachFragment(BundleData bundledata, ProtectionDomain domain, String[] classpath) {
				// nothing
			}

			public List<URL> findEntries(String path, String filePattern, int options) {
				Bundle systemBundle = SystemBundleLoader.this.getBundle();
				boolean recurse = (options & BundleWiring.FINDENTRIES_RECURSE) != 0;
				@SuppressWarnings("unchecked")
				List<URL> result = Collections.EMPTY_LIST;
				Enumeration<URL> entries = systemBundle.findEntries(path, filePattern, recurse);
				if (entries != null) {
					result = new ArrayList<URL>();
					while (entries.hasMoreElements())
						result.add(entries.nextElement());
				}
				return Collections.unmodifiableList(result);
			}

			@SuppressWarnings("unchecked")
			public Collection<String> listResources(String path, String filePattern, int options) {
				return Collections.EMPTY_LIST;
			}

			@SuppressWarnings("unchecked")
			public Collection<String> listLocalResources(String path, String filePattern, int options) {
				return Collections.EMPTY_LIST;
			}
		};
	}
}