Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e2bf89c9c23f519fd5f913e9afc69b562caa5662 (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
/*******************************************************************************
 * Copyright (c) 2013, 2017 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.storage;

import java.io.*;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import org.eclipse.osgi.framework.util.CaseInsensitiveDictionaryMap;
import org.eclipse.osgi.storage.bundlefile.BundleEntry;
import org.eclipse.osgi.storage.bundlefile.BundleFile;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.BundleException;

public class SystemBundleFile extends BundleFile {

	public SystemBundleFile() {
		super(null);
	}

	@Override
	public File getFile(String path, boolean nativeCode) {
		return null;
	}

	@Override
	public BundleEntry getEntry(String path) {
		if (BundleInfo.OSGI_BUNDLE_MANIFEST.equals(path)) {
			return new BundleEntry() {

				public InputStream getInputStream() throws IOException {
					return getManifestURL().openStream();
				}

				public long getSize() {
					return 0;
				}

				public String getName() {
					return BundleInfo.OSGI_BUNDLE_MANIFEST;
				}

				public long getTime() {
					return 0;
				}

				public URL getLocalURL() {
					return getManifestURL();
				}

				public URL getFileURL() {
					return null;
				}
			};
		}
		return null;
	}

	@Override
	public Enumeration<String> getEntryPaths(String path, boolean recurse) {
		return null;
	}

	@Override
	public void close() throws IOException {
		// nothing
	}

	@Override
	public void open() throws IOException {
		// nothing
	}

	@Override
	public boolean containsDir(String dir) {
		return false;
	}

	URL getManifestURL() {
		ClassLoader cl = getClass().getClassLoader();
		try {
			// get all manifests in your classloader delegation
			Enumeration<URL> manifests = cl != null ? cl.getResources(BundleInfo.OSGI_BUNDLE_MANIFEST) : ClassLoader.getSystemResources(BundleInfo.OSGI_BUNDLE_MANIFEST);
			while (manifests.hasMoreElements()) {
				URL url = manifests.nextElement();
				try {
					// check each manifest until we find one with the Eclipse-SystemBundle: true header
					Map<String, String> headers = ManifestElement.parseBundleManifest(url.openStream(), new CaseInsensitiveDictionaryMap<String, String>());
					if ("true".equals(headers.get(Storage.ECLIPSE_SYSTEMBUNDLE))) //$NON-NLS-1$
						return url;
				} catch (BundleException e) {
					// ignore and continue to next URL
				}
			}
		} catch (IOException e) {
			// ignore and return null
		}
		return null;
	}
}

Back to the top