Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f70e6383c2efffde9eabe4a8c00d8c5d0b3e8a1d (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
/*******************************************************************************
 * Copyright (c) 2008, 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.internal.transforms;

import java.io.IOException;
import org.eclipse.osgi.internal.framework.EquinoxContainer;
import org.eclipse.osgi.internal.hookregistry.*;
import org.eclipse.osgi.internal.log.EquinoxLogServices;
import org.eclipse.osgi.storage.BundleInfo.Generation;
import org.eclipse.osgi.storage.bundlefile.BundleFile;
import org.eclipse.osgi.storage.bundlefile.BundleFileWrapper;
import org.osgi.framework.*;

/**
 * The framework extension that is capable of applying transforms to bundle content.
 */
public class TransformerHook implements BundleFileWrapperFactoryHook, HookConfigurator, ActivatorHookFactory, BundleActivator {
	private volatile TransformerList transformers;
	private volatile TransformInstanceListData templates;
	private EquinoxLogServices logServices;

	/**
	 * @throws IOException  
	 */
	public BundleFileWrapper wrapBundleFile(BundleFile bundleFile, Generation generation, boolean base) {
		if (transformers == null || templates == null)
			return null;
		return new TransformedBundleFile(this, generation, bundleFile);
	}

	public void addHooks(HookRegistry hookRegistry) {
		hookRegistry.addActivatorHookFactory(this);
		hookRegistry.addBundleFileWrapperFactoryHook(this);
		logServices = hookRegistry.getContainer().getLogServices();
	}

	public void start(BundleContext context) throws BundleException {
		try {
			this.transformers = new TransformerList(context, logServices);
		} catch (InvalidSyntaxException e) {
			throw new BundleException("Problem registering service tracker: transformers", e); //$NON-NLS-1$
		}
		try {
			this.templates = new TransformInstanceListData(context, logServices);
		} catch (InvalidSyntaxException e) {
			transformers.close();
			transformers = null;
			throw new BundleException("Problem registering service tracker: templates", e); //$NON-NLS-1$
		}

	}

	public void stop(BundleContext context) {
		transformers.close();
		templates.close();
	}

	void log(int severity, String msg, Throwable t) {
		if (logServices == null) {
			System.err.println(msg);
			t.printStackTrace();
			return;
		}
		logServices.log(EquinoxContainer.NAME, severity, msg, t);
	}

	public BundleActivator createActivator() {
		return this;
	}

	public String[] getTransformTypes() {
		TransformInstanceListData current = templates;
		return current == null ? new String[0] : current.getTransformTypes();
	}

	public StreamTransformer getTransformer(String type) {
		TransformerList current = transformers;
		return current == null ? null : current.getTransformer(type);
	}

	public TransformTuple[] getTransformsFor(String type) {
		TransformInstanceListData current = templates;
		return current == null ? null : current.getTransformsFor(type);
	}

	public boolean hasTransformers() {
		TransformerList current = transformers;
		return current == null ? false : current.hasTransformers();
	}

	public boolean hasTransformsFor(Bundle bundle) {
		TransformInstanceListData current = templates;
		return current == null ? false : current.hasTransformsFor(bundle);
	}
}

Back to the top