Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b9fa617537750d3246f8492bde2647c541156581 (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
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 *  Copyright (c) 2000, 2013 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.pde.internal.core.bundle;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.HostSpecification;
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.pde.core.IModelChangedEvent;
import org.eclipse.pde.core.ModelChangedEvent;
import org.eclipse.pde.internal.core.*;
import org.eclipse.pde.internal.core.ibundle.IBundle;
import org.eclipse.pde.internal.core.ibundle.IBundleModel;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;

public abstract class BundleModel extends AbstractModel implements IBundleModel {

	private static final long serialVersionUID = 1L;

	private Bundle fBundle;

	public BundleModel() {
		fBundle = new Bundle();
		fBundle.setModel(this);
	}

	@Override
	public IBundle getBundle() {
		if (!isLoaded())
			load();
		return fBundle;
	}

	@Override
	public String getInstallLocation() {
		return null;
	}

	@Override
	public abstract void load();

	@Override
	public boolean isFragmentModel() {
		return fBundle.getHeader(Constants.FRAGMENT_HOST) != null;
	}

	@Override
	public void load(InputStream source, boolean outOfSync) {
		try {
			setLoaded(true); // Must be set before loading the manifest otherwise calls to getModel() cause a stack overflow
			fBundle.load(ManifestElement.parseBundleManifest(source, null));
			if (!outOfSync)
				updateTimeStamp();

		} catch (BundleException e) {
			PDECore.log(e);
		} catch (IOException e) {
			PDECore.log(e);
		}
	}

	public void load(BundleDescription desc, PDEState state) {
		long id = desc.getBundleId();
		Properties properties = new Properties();
		properties.put(Constants.BUNDLE_SYMBOLICNAME, desc.getSymbolicName());
		String value = state.getPluginName(id);
		if (value != null)
			properties.put(Constants.BUNDLE_NAME, value);
		value = state.getProviderName(id);
		if (value != null)
			properties.put(Constants.BUNDLE_VENDOR, value);
		value = state.getClassName(id);
		if (value != null)
			properties.put(Constants.BUNDLE_ACTIVATOR, value);
		value = state.getBundleLocalization(id);
		if (value != null)
			properties.put(Constants.BUNDLE_LOCALIZATION, value);
		if (state.hasExtensibleAPI(id))
			properties.put(ICoreConstants.EXTENSIBLE_API, "true"); //$NON-NLS-1$
		if (state.isPatchFragment(id))
			properties.put(ICoreConstants.PATCH_FRAGMENT, "true"); //$NON-NLS-1$
		String[] libraries = state.getLibraryNames(id);
		if (libraries.length > 0) {
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < libraries.length; i++) {
				if (buffer.length() > 0) {
					buffer.append(","); //$NON-NLS-1$
					buffer.append(System.getProperty("line.separator")); //$NON-NLS-1$
					buffer.append(" "); //$NON-NLS-1$
				}
				buffer.append(libraries[i]);
			}
			properties.put(Constants.BUNDLE_CLASSPATH, buffer.toString());
		}
		if (desc.getHost() != null) {
			properties.put(Constants.FRAGMENT_HOST, writeFragmentHost(desc.getHost()));
		}
		setLoaded(true); // Must set loaded before creating headers as calls to getModel() throw stack overflows
		fBundle.load(properties);
		updateTimeStamp();
	}

	private String writeFragmentHost(HostSpecification host) {
		String id = host.getName();
		String version = host.getVersionRange().toString();
		StringBuffer buffer = new StringBuffer();
		if (id != null)
			buffer.append(id);

		if (version != null && version.trim().length() > 0) {
			buffer.append(";" + Constants.BUNDLE_VERSION_ATTRIBUTE + "=\"" + version + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		}
		return buffer.toString();
	}

	@Override
	public void reload(InputStream source, boolean outOfSync) {
		load(source, outOfSync);
		fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.WORLD_CHANGED, new Object[0], null));
	}

	@Override
	public String toString() {
		if (fBundle != null) {
			StringBuffer buf = new StringBuffer();
			buf.append(fBundle.getHeader(Constants.BUNDLE_SYMBOLICNAME));
			buf.append(" ("); //$NON-NLS-1$
			buf.append(fBundle.getHeader(Constants.BUNDLE_VERSION));
			buf.append(')');
		}
		return "Unknown bundle model"; //$NON-NLS-1$
	}
}

Back to the top