Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ff76b4b896733b07caceeaa9ed3a5f2218dc3fa (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.compatibility.plugins;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.eclipse.osgi.framework.util.CaseInsensitiveDictionaryMap;
import org.eclipse.osgi.internal.hookregistry.ActivatorHookFactory;
import org.eclipse.osgi.internal.hookregistry.BundleFileWrapperFactoryHook;
import org.eclipse.osgi.internal.hookregistry.HookConfigurator;
import org.eclipse.osgi.internal.hookregistry.HookRegistry;
import org.eclipse.osgi.service.pluginconversion.PluginConversionException;
import org.eclipse.osgi.service.pluginconversion.PluginConverter;
import org.eclipse.osgi.storage.BundleInfo.Generation;
import org.eclipse.osgi.storage.bundlefile.BundleEntry;
import org.eclipse.osgi.storage.bundlefile.BundleFile;
import org.eclipse.osgi.storage.bundlefile.BundleFileWrapper;
import org.eclipse.osgi.storage.bundlefile.FileBundleEntry;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;

public class PluginConverterHook implements HookConfigurator {
	@Override
	public void addHooks(final HookRegistry hookRegistry) {
		PluginConverterImpl tempConverter;
		try {
			tempConverter = new PluginConverterImpl(hookRegistry);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		final PluginConverterImpl converter = tempConverter;
		hookRegistry.addBundleFileWrapperFactoryHook(new BundleFileWrapperFactoryHook() {

			@Override
			public BundleFileWrapper wrapBundleFile(BundleFile bundleFile, Generation generation, boolean base) {
				if (!base) {
					return null;
				}
				return new BundleFileWrapper(bundleFile) {

					@Override
					public BundleEntry getEntry(String path) {
						BundleEntry entry = getBundleFile().getEntry(path);
						if (!PluginConverterImpl.OSGI_BUNDLE_MANIFEST.equals(path)) {
							return entry;
						}
						Map<String, String> headers = null;
						if (entry != null) {
							try {
								headers = ManifestElement.parseBundleManifest(entry.getInputStream(), new CaseInsensitiveDictionaryMap<String, String>());
							} catch (Exception e) {
								throw new RuntimeException(e);
							}
							if (headers.containsKey(Constants.BUNDLE_MANIFESTVERSION)) {
								return entry;
							}
							if (headers.containsKey(Constants.BUNDLE_SYMBOLICNAME)) {
								return entry;
							}
						}
						try {
							File manifest = converter.convertManifest(getBaseFile(), null, true, null, true, null, false);
							if (manifest == null) {
								return entry;
							}
							return new FileBundleEntry(manifest, PluginConverterImpl.OSGI_BUNDLE_MANIFEST);
						} catch (PluginConversionException e) {
							throw new RuntimeException(e);
						}
					}
				};
			}
		});

		hookRegistry.addActivatorHookFactory(new ActivatorHookFactory() {

			@Override
			public BundleActivator createActivator() {
				return new BundleActivator() {
					ServiceRegistration<PluginConverter> reg;

					@Override
					public void start(BundleContext context) throws Exception {
						reg = context.registerService(PluginConverter.class, converter, null);
					}

					@Override
					public void stop(BundleContext context) throws Exception {
						reg.unregister();
					}
				};
			}
		});
	}
}

Back to the top