View | Details | Raw Unified | Return to bug 256910 | Differences between
and this patch

Collapse All | Expand All

(-)META-INF/MANIFEST.MF (-1 / +2 lines)
Lines 19-24 Link Here
19
 org.eclipse.jdt.launching,
19
 org.eclipse.jdt.launching,
20
 org.eclipse.pde.core,
20
 org.eclipse.pde.core,
21
 org.eclipse.text,
21
 org.eclipse.text,
22
 org.eclipse.pde.runtime
22
 org.eclipse.pde.runtime,
23
 org.eclipse.equinox.frameworkadmin;bundle-version="1.0.100"
23
Eclipse-LazyStart: true
24
Eclipse-LazyStart: true
24
Bundle-RequiredExecutionEnvironment: J2SE-1.4
25
Bundle-RequiredExecutionEnvironment: J2SE-1.4
(-)src/org/eclipse/pde/ui/tests/target/TargetDefinitionTests.java (+280 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.ui.tests.target;
12
13
import java.io.*;
14
import java.net.URL;
15
import java.util.*;
16
import java.util.zip.ZipEntry;
17
import java.util.zip.ZipFile;
18
import junit.framework.TestCase;
19
import org.eclipse.core.runtime.*;
20
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
21
import org.eclipse.pde.core.plugin.TargetPlatform;
22
import org.eclipse.pde.internal.core.*;
23
import org.eclipse.pde.internal.core.target.provisional.*;
24
import org.eclipse.pde.internal.ui.tests.macro.MacroPlugin;
25
26
/**
27
 * Tests for target definitions.
28
 * 
29
 * @since 3.5 
30
 */
31
public class TargetDefinitionTests extends TestCase {
32
	
33
	/**
34
	 * Retrieves all bundles (source and code) in the given target definition
35
	 * returning them as a set of URLs.
36
	 * 
37
	 * @param target target definition
38
	 * @return all bundle URLs
39
	 */
40
	protected Set getAllBundleURLs(ITargetDefinition target) throws Exception {
41
		BundleInfo[] code = target.resolveBundles(null);
42
		BundleInfo[] source = target.resolveSourceBundles(null);
43
		Set urls = new HashSet(code.length + source.length);
44
		for (int i = 0; i < code.length; i++) {
45
			urls.add(code[i].getLocation().toURL());
46
		}
47
		for (int i = 0; i < source.length; i++) {
48
			urls.add(source[i].getLocation().toURL());
49
		}
50
		return urls;
51
	}
52
	
53
	/**
54
	 * Extracts the classic plug-ins archive, if not already done, and returns a path to the
55
	 * root directory containing the plug-ins.
56
	 * 
57
	 * @return path to the plug-ins directory
58
	 * @throws Exception
59
	 */
60
	protected IPath extracClassicPlugins() throws Exception {
61
		// extract the 3.0.2 skeleton
62
		IPath stateLocation = MacroPlugin.getDefault().getStateLocation();
63
		IPath location = stateLocation.append("classic-plugins");
64
		if (location.toFile().exists()) {
65
			return location;
66
		}
67
		URL zipURL = MacroPlugin.getBundleContext().getBundle().getEntry("/tests/targets/classic-plugins.zip");
68
		Path zipPath = new Path(new File(FileLocator.toFileURL(zipURL).getFile()).getAbsolutePath());
69
		ZipFile zipFile = new ZipFile(zipPath.toFile());
70
		Enumeration entries = zipFile.entries();
71
		while (entries.hasMoreElements()) {
72
			ZipEntry entry = (ZipEntry) entries.nextElement();
73
			if (!entry.isDirectory()) {
74
				IPath entryPath = stateLocation.append(entry.getName());
75
				File dir = entryPath.removeLastSegments(1).toFile();
76
				dir.mkdirs();
77
				File file = entryPath.toFile();
78
				file.createNewFile();
79
				InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
80
				byte[] bytes = getInputStreamAsByteArray(inputStream, -1);
81
				inputStream.close();
82
				BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
83
				outputStream.write(bytes);
84
				outputStream.close();
85
			}
86
		}
87
		zipFile.close();
88
		return location;
89
	}
90
91
	/**
92
	 * Tests that a target definition equivalent to the default target platform
93
	 * contains the same bundles as the default target platform (this is an 
94
	 * explicit location with no target weaving).
95
	 * 
96
	 * @throws Exception
97
	 */
98
	public void testDefaultTargetPlatform() throws Exception {
99
		// the new way
100
		ITargetDefinition definition = TargetFactory.newTargetDefinition();
101
		IBundleContainer container = TargetFactory.newProfileContainer(TargetPlatform.getDefaultLocation());
102
		definition.setBundleContainers(new IBundleContainer[]{container});
103
		Set urls = getAllBundleURLs(definition);
104
		
105
		// the old way
106
		IPath location = new Path(TargetPlatform.getDefaultLocation());
107
		URL[] pluginPaths = P2Utils.readBundlesTxt(location.toOSString(), location.append("configuration").toFile().toURL());
108
		assertEquals("Should have same number of bundles", pluginPaths.length, urls.size());
109
		for (int i = 0; i < pluginPaths.length; i++) {
110
			URL url = pluginPaths[i];
111
			assertTrue("Missing plug-in " + url.toString(), urls.contains(url));
112
		}
113
		
114
	}
115
	
116
	/**
117
	 * Tests that a target definition equivalent to the default target platform
118
	 * contains the same bundles as the default target platform using the
119
	 * platform's configuration location (which will do target weaving). This
120
	 * is really only tested when run as a JUnit plug-in test suite from
121
	 * within Eclipse.
122
	 * 
123
	 * @throws Exception
124
	 */
125
	public void testWovenTargetPlatform() throws Exception {
126
		// the new way
127
		ITargetDefinition definition = TargetFactory.newTargetDefinition();
128
		IBundleContainer container = TargetFactory.newProfileContainer(TargetPlatform.getDefaultLocation(),
129
				new File(Platform.getConfigurationLocation().getURL().getFile()).getAbsolutePath());
130
		definition.setBundleContainers(new IBundleContainer[]{container});
131
		Set urls = getAllBundleURLs(definition);
132
		
133
		// the old way
134
		URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation());
135
		assertEquals("Should have same number of bundles", pluginPaths.length, urls.size());
136
		for (int i = 0; i < pluginPaths.length; i++) {
137
			URL url = pluginPaths[i];
138
			assertTrue("Missing plug-in " + url.toString(), urls.contains(url));
139
		}
140
		
141
	}	
142
143
	/**
144
	 * Tests that a bundle directory container is equivalent to scanning locations.
145
	 * 
146
	 * @throws Exception
147
	 */
148
	public void testDirectoryBundleContainer() throws Exception {
149
		// the new way
150
		ITargetDefinition definition = TargetFactory.newTargetDefinition();
151
		IBundleContainer container = TargetFactory.newDirectoryContainer(TargetPlatform.getDefaultLocation() + "/plugins");
152
		definition.setBundleContainers(new IBundleContainer[]{container});
153
		Set urls = getAllBundleURLs(definition);
154
		
155
		Preferences store = PDECore.getDefault().getPluginPreferences();
156
		boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION);
157
		try {
158
			store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false);
159
			// the old way
160
			URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation());
161
			assertEquals("Should have same number of bundles", pluginPaths.length, urls.size());
162
			for (int i = 0; i < pluginPaths.length; i++) {
163
				URL url = pluginPaths[i];
164
				assertTrue("Missing plug-in " + url.toString(), urls.contains(url));
165
			}
166
		}
167
		finally {
168
			store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore);
169
		}
170
	}
171
	
172
	/**
173
	 * Tests reading a 3.0.2 install with a mix of classic and OSGi plug-ins.
174
	 * 
175
	 * @throws Exception
176
	 */
177
	public void testClassicPlugins() throws Exception {
178
		// extract the 3.0.2 skeleton
179
		IPath location = extracClassicPlugins();
180
		
181
		// the new way
182
		ITargetDefinition definition = TargetFactory.newTargetDefinition();
183
		IBundleContainer container = TargetFactory.newDirectoryContainer(location.toOSString());
184
		definition.setBundleContainers(new IBundleContainer[]{container});
185
		Set urls = getAllBundleURLs(definition);
186
		
187
		Preferences store = PDECore.getDefault().getPluginPreferences();
188
		boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION);
189
		try {
190
			store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false);
191
			// the old way
192
			URL[] pluginPaths = PluginPathFinder.getPluginPaths(location.toOSString());
193
			for (int i = 0; i < pluginPaths.length; i++) {
194
				URL url = pluginPaths[i];
195
				if (!urls.contains(url)) {
196
					System.err.println(url.toString());
197
				}
198
			}
199
			assertEquals("Wrong number of bundles", pluginPaths.length, urls.size());
200
		}
201
		finally {
202
			store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore);
203
		}		
204
	}
205
	
206
	/**
207
	 * Tests identification of source bundles in a 3.0.2 install.
208
	 * 
209
	 * @throws Exception
210
	 */
211
	public void testClassicSourcePlugins() throws Exception {
212
		// extract the 3.0.2 skeleton
213
		IPath location = extracClassicPlugins();
214
		
215
		// the new way
216
		ITargetDefinition definition = TargetFactory.newTargetDefinition();
217
		IBundleContainer container = TargetFactory.newDirectoryContainer(location.toOSString());
218
		definition.setBundleContainers(new IBundleContainer[]{container});
219
		BundleInfo[] bundles = definition.resolveSourceBundles(null);
220
		assertEquals("Wrong number of source bundles", 3, bundles.length);
221
		Set names = new HashSet();
222
		for (int i = 0; i < bundles.length; i++) {
223
			names.add(bundles[i].getSymbolicName());
224
		}
225
		String[] expected = new String[]{"org.eclipse.platform.source", "org.eclipse.jdt.source", "org.eclipse.pde.source"};
226
		for (int i = 0; i < expected.length; i++) {
227
			assertTrue("Missing source for " + expected[i], names.contains(expected[i]));	
228
		}
229
	}
230
231
	/**
232
	 * Returns the given input stream as a byte array
233
	 * @param stream the stream to get as a byte array
234
	 * @param length the length to read from the stream or -1 for unknown
235
	 * @return the given input stream as a byte array
236
	 * @throws IOException
237
	 */
238
	public static byte[] getInputStreamAsByteArray(InputStream stream, int length) throws IOException {
239
		byte[] contents;
240
		if (length == -1) {
241
			contents = new byte[0];
242
			int contentsLength = 0;
243
			int amountRead = -1;
244
			do {
245
				// read at least 8K
246
				int amountRequested = Math.max(stream.available(), 8192);
247
				// resize contents if needed
248
				if (contentsLength + amountRequested > contents.length) {
249
					System.arraycopy(contents,
250
							0,
251
							contents = new byte[contentsLength + amountRequested],
252
							0,
253
							contentsLength);
254
				}
255
				// read as many bytes as possible
256
				amountRead = stream.read(contents, contentsLength, amountRequested);
257
				if (amountRead > 0) {
258
					// remember length of contents
259
					contentsLength += amountRead;
260
				}
261
			} while (amountRead != -1);
262
			// resize contents if necessary
263
			if (contentsLength < contents.length) {
264
				System.arraycopy(contents, 0, contents = new byte[contentsLength], 0, contentsLength);
265
			}
266
		} else {
267
			contents = new byte[length];
268
			int len = 0;
269
			int readSize = 0;
270
			while ((readSize != -1) && (len != length)) {
271
				// See PR 1FMS89U
272
				// We record first the read size. In this case length is the actual
273
				// read size.
274
				len += readSize;
275
				readSize = stream.read(contents, len, length - len);
276
			}
277
		}
278
		return contents;
279
	}	
280
}
(-)src/org/eclipse/pde/internal/core/ICoreConstants.java (+3 lines)
Lines 145-148 Link Here
145
	public static IPath BUILD_PROPERTIES_PATH = new Path(BUILD_FILENAME_DESCRIPTOR);
145
	public static IPath BUILD_PROPERTIES_PATH = new Path(BUILD_FILENAME_DESCRIPTOR);
146
	public static IPath OSGI_INF_PATH = new Path(OSGI_INF_FOLDER_NAME);
146
	public static IPath OSGI_INF_PATH = new Path(OSGI_INF_FOLDER_NAME);
147
147
148
	// Extension point identifiers
149
	public static final String EXTENSION_POINT_SOURCE = PDECore.PLUGIN_ID + ".source"; //$NON-NLS-1$
150
148
}
151
}
(-)src/org/eclipse/pde/internal/core/P2Utils.java (-28 / +63 lines)
Lines 54-85 Link Here
54
	 * 	to locate a bundles.info
54
	 * 	to locate a bundles.info
55
	 */
55
	 */
56
	public static URL[] readBundlesTxt(String platformHome, URL configurationArea) {
56
	public static URL[] readBundlesTxt(String platformHome, URL configurationArea) {
57
		BundleInfo[] bundles = readBundles(platformHome, configurationArea);
58
		BundleInfo[] source = readSourceBundles(platformHome, configurationArea);
59
		if (bundles != null) {
60
			int length = bundles.length;
61
			if (source != null) {
62
				length += source.length;
63
			}
64
			URL[] urls = new URL[length];
65
			int index = 0;
66
			try {
67
				for (int i = 0; i < bundles.length; i++) {
68
					urls[index++] = bundles[i].getLocation().toURL();
69
				}
70
				if (source != null) {
71
					for (int i = 0; i < source.length; i++) {
72
						urls[index++] = source[i].getLocation().toURL();
73
					}
74
				}
75
				return urls;
76
			} catch (MalformedURLException e) {
77
				PDECore.log(e);
78
			}
79
		}
80
		return null;
81
	}
82
83
	/**
84
	 * Returns bundles defined by the 'bundles.info' file in the
85
	 * specified location, or <code>null</code> if none. The "bundles.info" file
86
	 * is assumed to be at a fixed location relative to the configuration area URL.
87
	 * If bundle URLs found in the bundles.info are relative, they will be appended
88
	 * to platformHome to make them absolute.
89
	 * 
90
	 * @param platformHome absolute path in the local file system to an installation
91
	 * @param configurationArea url location of the configuration directory to search for bundles.info
92
	 * @return URLs of all bundles in the installation or <code>null</code> if not able
93
	 * 	to locate a bundles.info
94
	 */
95
	public static BundleInfo[] readBundles(String platformHome, URL configurationArea) {
57
		IPath basePath = new Path(platformHome);
96
		IPath basePath = new Path(platformHome);
58
		if (configurationArea == null) {
97
		if (configurationArea == null) {
59
			return null;
98
			return null;
60
		}
99
		}
61
		try {
100
		try {
62
			URL bundlesTxt = new URL(configurationArea.getProtocol(), configurationArea.getHost(), configurationArea.getFile().concat(BUNDLE_INFO_PATH));
101
			URL bundlesTxt = new URL(configurationArea.getProtocol(), configurationArea.getHost(), configurationArea.getFile().concat(BUNDLE_INFO_PATH));
63
			File home = basePath.toFile();
102
			return getBundlesFromFile(bundlesTxt, basePath.toFile());
64
			BundleInfo bundles[] = getBundlesFromFile(bundlesTxt, home);
65
			if (bundles == null) {
66
				return null;
67
			}
68
			int length = bundles.length;
69
			URL srcBundlesTxt = new URL(configurationArea.getProtocol(), configurationArea.getHost(), configurationArea.getFile().concat(SRC_INFO_PATH));
70
			BundleInfo srcBundles[] = getBundlesFromFile(srcBundlesTxt, home);
71
72
			if (srcBundles != null) {
73
				length += srcBundles.length;
74
			}
75
76
			URL[] urls = new URL[length];
77
			copyURLs(urls, 0, bundles);
78
			if (srcBundles != null) {
79
				copyURLs(urls, bundles.length, srcBundles);
80
			}
81
			return urls;
82
83
		} catch (MalformedURLException e) {
103
		} catch (MalformedURLException e) {
84
			PDECore.log(e);
104
			PDECore.log(e);
85
			return null;
105
			return null;
Lines 90-105 Link Here
90
	}
110
	}
91
111
92
	/**
112
	/**
93
	 * Copies URLs from the given bundle info objects into the specified array starting at the given index.
113
	 * Returns bundles defined by the 'source.info' file in the
114
	 * specified location, or <code>null</code> if none. The "source.info" file
115
	 * is assumed to be at a fixed location relative to the configuration area URL.
116
	 * If bundle URLs found in the source.info are relative, they will be appended
117
	 * to platformHome to make them absolute.
94
	 * 
118
	 * 
95
	 * @param dest array to copy URLs into
119
	 * @param platformHome absolute path in the local file system to an installation
96
	 * @param start index to start copying into
120
	 * @param configurationArea url location of the configuration directory to search for source.info
97
	 * @param infos associated bundle infos
121
	 * @return URLs of all source bundles in the installation or <code>null</code> if not able
98
	 * @throws MalformedURLException
122
	 * 	to locate a source.info
99
	 */
123
	 */
100
	private static void copyURLs(URL[] dest, int start, BundleInfo[] infos) throws MalformedURLException {
124
	public static BundleInfo[] readSourceBundles(String platformHome, URL configurationArea) {
101
		for (int i = 0; i < infos.length; i++) {
125
		IPath basePath = new Path(platformHome);
102
			dest[start++] = infos[i].getLocation().toURL();
126
		if (configurationArea == null) {
127
			return null;
128
		}
129
		try {
130
			URL srcBundlesTxt = new URL(configurationArea.getProtocol(), configurationArea.getHost(), configurationArea.getFile().concat(SRC_INFO_PATH));
131
			return getBundlesFromFile(srcBundlesTxt, basePath.toFile());
132
		} catch (MalformedURLException e) {
133
			PDECore.log(e);
134
			return null;
135
		} catch (IOException e) {
136
			PDECore.log(e);
137
			return null;
103
		}
138
		}
104
	}
139
	}
105
140
(-)META-INF/MANIFEST.MF (-1 / +4 lines)
Lines 37-42 Link Here
37
 org.eclipse.pde.internal.core.search;x-friends:="org.eclipse.pde.ui",
37
 org.eclipse.pde.internal.core.search;x-friends:="org.eclipse.pde.ui",
38
 org.eclipse.pde.internal.core.site;x-friends:="org.eclipse.pde.ui",
38
 org.eclipse.pde.internal.core.site;x-friends:="org.eclipse.pde.ui",
39
 org.eclipse.pde.internal.core.target;x-friends:="org.eclipse.pde.ui",
39
 org.eclipse.pde.internal.core.target;x-friends:="org.eclipse.pde.ui",
40
 org.eclipse.pde.internal.core.target.impl;x-internal:=true,
41
 org.eclipse.pde.internal.core.target.provisional;x-internal:=true,
40
 org.eclipse.pde.internal.core.text;
42
 org.eclipse.pde.internal.core.text;
41
  x-friends:="org.eclipse.pde.ui,
43
  x-friends:="org.eclipse.pde.ui,
42
   org.eclipse.pde.ds.core,
44
   org.eclipse.pde.ds.core,
Lines 64-70 Link Here
64
 org.eclipse.pde.build;bundle-version="[3.2.0,4.1.0)",
66
 org.eclipse.pde.build;bundle-version="[3.2.0,4.1.0)",
65
 org.eclipse.ant.core;bundle-version="[3.1.0,4.0.0)",
67
 org.eclipse.ant.core;bundle-version="[3.1.0,4.0.0)",
66
 org.eclipse.equinox.simpleconfigurator.manipulator;bundle-version="[1.0.100,2.0.0)",
68
 org.eclipse.equinox.simpleconfigurator.manipulator;bundle-version="[1.0.100,2.0.0)",
67
 org.eclipse.equinox.frameworkadmin;bundle-version="[1.0.100,2.0.0)"
69
 org.eclipse.equinox.frameworkadmin;bundle-version="[1.0.100,2.0.0)",
70
 org.eclipse.equinox.frameworkadmin.equinox;bundle-version="1.0.100"
68
Eclipse-LazyStart: true
71
Eclipse-LazyStart: true
69
Bundle-RequiredExecutionEnvironment: J2SE-1.4
72
Bundle-RequiredExecutionEnvironment: J2SE-1.4
70
Import-Package: com.ibm.icu.util
73
Import-Package: com.ibm.icu.util
(-)src/org/eclipse/pde/internal/core/target/impl/TargetDefinition.java (+210 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.core.target.impl;
12
13
import java.util.*;
14
import org.eclipse.core.runtime.CoreException;
15
import org.eclipse.core.runtime.IProgressMonitor;
16
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
17
import org.eclipse.pde.internal.core.target.provisional.IBundleContainer;
18
import org.eclipse.pde.internal.core.target.provisional.ITargetDefinition;
19
20
/**
21
 * Target definition implementation.
22
 * 
23
 * @since 3.5
24
 */
25
public class TargetDefinition implements ITargetDefinition {
26
27
	// name and description
28
	private String fName;
29
	private String fDescription;
30
31
	// arguments
32
	private String fProgramArgs;
33
	private String fVMArgs;
34
35
	// environment settings
36
	private String fEE;
37
	private String fArch;
38
	private String fOS;
39
	private String fWS;
40
	private String fNL;
41
42
	// bundle containers
43
	private IBundleContainer[] fContainers;
44
45
	/* (non-Javadoc)
46
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getArch()
47
	 */
48
	public String getArch() {
49
		return fArch;
50
	}
51
52
	/* (non-Javadoc)
53
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getBundleContainers()
54
	 */
55
	public IBundleContainer[] getBundleContainers() {
56
		return fContainers;
57
	}
58
59
	/* (non-Javadoc)
60
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getDescription()
61
	 */
62
	public String getDescription() {
63
		return fDescription;
64
	}
65
66
	/* (non-Javadoc)
67
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getExecutionEnvironment()
68
	 */
69
	public String getExecutionEnvironment() {
70
		return fEE;
71
	}
72
73
	/* (non-Javadoc)
74
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getNL()
75
	 */
76
	public String getNL() {
77
		return fNL;
78
	}
79
80
	/* (non-Javadoc)
81
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getName()
82
	 */
83
	public String getName() {
84
		return fName;
85
	}
86
87
	/* (non-Javadoc)
88
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getOS()
89
	 */
90
	public String getOS() {
91
		return fOS;
92
	}
93
94
	/* (non-Javadoc)
95
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getProgramArguments()
96
	 */
97
	public String getProgramArguments() {
98
		return fProgramArgs;
99
	}
100
101
	/* (non-Javadoc)
102
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getVMArguments()
103
	 */
104
	public String getVMArguments() {
105
		return fVMArgs;
106
	}
107
108
	/* (non-Javadoc)
109
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getWS()
110
	 */
111
	public String getWS() {
112
		return fWS;
113
	}
114
115
	/* (non-Javadoc)
116
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setArch(java.lang.String)
117
	 */
118
	public void setArch(String arch) {
119
		fArch = arch;
120
	}
121
122
	/* (non-Javadoc)
123
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setDescription(java.lang.String)
124
	 */
125
	public void setDescription(String description) {
126
		fDescription = description;
127
	}
128
129
	/* (non-Javadoc)
130
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setExecutionEnvironment(java.lang.String)
131
	 */
132
	public void setExecutionEnvironment(String environment) {
133
		fEE = environment;
134
	}
135
136
	/* (non-Javadoc)
137
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setNL(java.lang.String)
138
	 */
139
	public void setNL(String nl) {
140
		fNL = nl;
141
	}
142
143
	/* (non-Javadoc)
144
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setName(java.lang.String)
145
	 */
146
	public void setName(String name) {
147
		fName = name;
148
	}
149
150
	/* (non-Javadoc)
151
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setOS(java.lang.String)
152
	 */
153
	public void setOS(String os) {
154
		fOS = os;
155
	}
156
157
	/* (non-Javadoc)
158
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setProgramArguments(java.lang.String)
159
	 */
160
	public void setProgramArguments(String args) {
161
		fProgramArgs = args;
162
	}
163
164
	/* (non-Javadoc)
165
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setVMArguments(java.lang.String)
166
	 */
167
	public void setVMArguments(String args) {
168
		fVMArgs = args;
169
	}
170
171
	/* (non-Javadoc)
172
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setWS(java.lang.String)
173
	 */
174
	public void setWS(String ws) {
175
		fWS = ws;
176
	}
177
178
	/* (non-Javadoc)
179
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setBundleContainers(org.eclipse.pde.internal.core.target.provisional.IBundleContainer[])
180
	 */
181
	public void setBundleContainers(IBundleContainer[] containers) {
182
		fContainers = containers;
183
	}
184
185
	/* (non-Javadoc)
186
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#resolveBundles(org.eclipse.core.runtime.IProgressMonitor)
187
	 */
188
	public BundleInfo[] resolveBundles(IProgressMonitor monitor) throws CoreException {
189
		IBundleContainer[] containers = getBundleContainers();
190
		List bundles = new ArrayList();
191
		for (int i = 0; i < containers.length; i++) {
192
			BundleInfo[] infos = containers[i].resolveBundles(monitor);
193
			bundles.addAll(Arrays.asList(infos));
194
		}
195
		return (BundleInfo[]) bundles.toArray(new BundleInfo[bundles.size()]);
196
	}
197
198
	/* (non-Javadoc)
199
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#resolveSourceBundles(org.eclipse.core.runtime.IProgressMonitor)
200
	 */
201
	public BundleInfo[] resolveSourceBundles(IProgressMonitor monitor) throws CoreException {
202
		IBundleContainer[] containers = getBundleContainers();
203
		List source = new ArrayList();
204
		for (int i = 0; i < containers.length; i++) {
205
			BundleInfo[] infos = containers[i].resolveSourceBundles(monitor);
206
			source.addAll(Arrays.asList(infos));
207
		}
208
		return (BundleInfo[]) source.toArray(new BundleInfo[source.size()]);
209
	}
210
}
(-)src/org/eclipse/pde/internal/core/target/impl/ProfileBundleContainer.java (+95 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.core.target.impl;
12
13
import java.net.MalformedURLException;
14
import java.net.URL;
15
import org.eclipse.core.runtime.*;
16
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
17
import org.eclipse.osgi.util.NLS;
18
import org.eclipse.pde.internal.core.P2Utils;
19
import org.eclipse.pde.internal.core.PDECore;
20
import org.eclipse.pde.internal.core.target.provisional.IBundleContainer;
21
22
/**
23
 * A bundle container representing an installed profile.
24
 * 
25
 * @since 3.5 
26
 */
27
public class ProfileBundleContainer implements IBundleContainer {
28
29
	/**
30
	 * Path to home/root install location
31
	 */
32
	private String fHome;
33
34
	/**
35
	 * Alternate configuration location or <code>null</code> if default
36
	 */
37
	private String fConfiguration;
38
39
	/**
40
	 * Creates a new bundle container for the profile at the specified location.
41
	 * 
42
	 * @param home path in local file system
43
	 * @param configurationLocation alternate configuration location or <code>null</code> for default
44
	 */
45
	public ProfileBundleContainer(String home, String configurationLocation) {
46
		fHome = home;
47
		fConfiguration = configurationLocation;
48
	}
49
50
	/* (non-Javadoc)
51
	 * @see org.eclipse.pde.internal.core.target.provisional.IBundleContainer#resolveBundles(org.eclipse.core.runtime.IProgressMonitor)
52
	 */
53
	public BundleInfo[] resolveBundles(IProgressMonitor monitor) throws CoreException {
54
		URL configUrl = getConfigurationArea();
55
		BundleInfo[] infos = P2Utils.readBundles(new Path(fHome).toOSString(), configUrl);
56
		if (infos == null) {
57
			throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, NLS.bind("Unable to resolve bundles in {0}", fHome)));
58
		}
59
		return infos;
60
	}
61
62
	/* (non-Javadoc)
63
	 * @see org.eclipse.pde.internal.core.target.provisional.IBundleContainer#resolveSourceBundles(org.eclipse.core.runtime.IProgressMonitor)
64
	 */
65
	public BundleInfo[] resolveSourceBundles(IProgressMonitor monitor) throws CoreException {
66
		URL configUrl = getConfigurationArea();
67
		BundleInfo[] source = P2Utils.readSourceBundles(new Path(fHome).toOSString(), configUrl);
68
		if (source == null) {
69
			source = new BundleInfo[0];
70
		}
71
		return source;
72
	}
73
74
	/**
75
	 * Returns a URL to the configuration area associated with this profile.
76
	 * 
77
	 * @return configuration area URL
78
	 * @throws CoreException if unable to generate a URL
79
	 */
80
	private URL getConfigurationArea() throws CoreException {
81
		IPath home = new Path(fHome);
82
		IPath configuration = null;
83
		if (fConfiguration == null) {
84
			configuration = home.append("configuration");
85
		} else {
86
			configuration = new Path(fConfiguration);
87
		}
88
		try {
89
			return configuration.toFile().toURL();
90
		} catch (MalformedURLException e) {
91
			throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, NLS.bind("Unable resolve configuration area in {0}", fHome), e));
92
		}
93
	}
94
95
}
(-)src/org/eclipse/pde/internal/core/target/provisional/IBundleContainer.java (+43 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.core.target.provisional;
12
13
import org.eclipse.core.runtime.CoreException;
14
import org.eclipse.core.runtime.IProgressMonitor;
15
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
16
17
/**
18
 * A collection of bundles. A bundle container abstracts the storage and location of the
19
 * underlying bundles and may contain a combination of executable and source bundles.
20
 * 
21
 * @since 3.5
22
 */
23
public interface IBundleContainer {
24
25
	/**
26
	 * Resolves and returns the executable bundles in this container, possibly empty.
27
	 * 
28
	 * @param monitor progress monitor or <code>null</code>
29
	 * @return executable bundles
30
	 * @exception CoreException if unable to resolve bundles
31
	 */
32
	public BundleInfo[] resolveBundles(IProgressMonitor monitor) throws CoreException;
33
34
	/**
35
	 * Resolves and returns the source bundles in this container, possibly empty.
36
	 * 
37
	 * @param monitor progress monitor or <code>null</code>
38
	 * @return source bundles
39
	 * @exception CoreException if unable to resolve bundles
40
	 */
41
	public BundleInfo[] resolveSourceBundles(IProgressMonitor monitor) throws CoreException;
42
43
}
(-)src/org/eclipse/pde/internal/core/target/provisional/TargetFactory.java (+65 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.core.target.provisional;
12
13
import org.eclipse.pde.internal.core.target.impl.*;
14
15
/**
16
 * Creates bundle containers and target definitions.
17
 * 
18
 * @since 3.5
19
 */
20
public class TargetFactory {
21
22
	/**
23
	 * Returns a new empty target definition.
24
	 * 
25
	 * @return target definition
26
	 */
27
	public static ITargetDefinition newTargetDefinition() {
28
		return new TargetDefinition();
29
	}
30
31
	/**
32
	 * Creates and returns a bundle container that contains all bundles in the
33
	 * specified directory.
34
	 * 
35
	 * @param path absolute path in the local file system, may contain string variables
36
	 * @return bundle container
37
	 */
38
	public static IBundleContainer newDirectoryContainer(String path) {
39
		return new DirectoryBundleContainer(path);
40
	}
41
42
	/**
43
	 * Creates and returns a bundle container that contains all bundles installed in
44
	 * a profile at the specified location with a default configuration area.
45
	 * 
46
	 * @param home absolute path in the local file system to the root of an installed profile
47
	 * @return bundle container
48
	 */
49
	public static IBundleContainer newProfileContainer(String home) {
50
		return newProfileContainer(home, null);
51
	}
52
53
	/**
54
	 * Creates and returns a bundle container that contains all bundles installed in
55
	 * a profile at the specified location with the specified configuration area.
56
	 * 
57
	 * @param home absolute path in the local file system to the root of an installed profile
58
	 * @param configurationLocation absolute path in the local file system to the
59
	 *  configuration area for the specified installation
60
	 * @return bundle container
61
	 */
62
	public static IBundleContainer newProfileContainer(String home, String configurationLocation) {
63
		return new ProfileBundleContainer(home, configurationLocation);
64
	}
65
}
(-)src/org/eclipse/pde/internal/core/target/provisional/ITargetDefinition.java (+205 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.core.target.provisional;
12
13
import org.eclipse.core.runtime.CoreException;
14
import org.eclipse.core.runtime.IProgressMonitor;
15
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
16
import org.eclipse.osgi.service.environment.Constants;
17
18
/**
19
 * Defines a target platform. A target platform is a collection of bundles configured
20
 * for a specific environment.
21
 * 
22
 * @since 3.5 
23
 */
24
public interface ITargetDefinition {
25
26
	/**
27
	 * Returns the name of this target.
28
	 * 
29
	 * @return name
30
	 */
31
	public String getName();
32
33
	/**
34
	 * Sets the name of this target.
35
	 * 
36
	 * @param name target name - cannot be <code>null</code>
37
	 */
38
	public void setName(String name);
39
40
	/**
41
	 * Returns the description of this target or <code>null</code> if none.
42
	 * 
43
	 * @return target description
44
	 */
45
	public String getDescription();
46
47
	/**
48
	 * Sets the description of this target, possibly <code>null</code>.
49
	 * 
50
	 * @param description target description or <code>null</code>
51
	 */
52
	public void setDescription(String description);
53
54
	/**
55
	 * Sets the execution environment this target requires to run. An execution 
56
	 * environment is specified by its associated OSGi profile identifier - for
57
	 * example, <code>J2SE-1.4</code>.
58
	 * 
59
	 * @param environment execution environment identifier
60
	 */
61
	public void setExecutionEnvironment(String environment);
62
63
	/**
64
	 * Returns the identifier of the execution environment this target requires to run.
65
	 * 
66
	 * @return execution environment identifier
67
	 */
68
	public String getExecutionEnvironment();
69
70
	/**
71
	 * Returns the identifier of the operating system this target is configured for,
72
	 * possibly <code>null</code>.
73
	 * 
74
	 * @return operating system identifier or <code>null</code> to default to the 
75
	 * 	running operating system
76
	 */
77
	public String getOS();
78
79
	/**
80
	 * Sets the operating system this target is configured for or <code>null</code> to
81
	 * default to the running operating system.
82
	 * 
83
	 * @param operating system identifier - one of the operating system constants
84
	 * 	defined by {@link Constants} or <code>null</code> to default to the running
85
	 * 	operating system
86
	 */
87
	public void setOS(String os);
88
89
	/**
90
	 * Returns the identifier of the window system this target is configured for,
91
	 * possibly <code>null</code>.
92
	 * 
93
	 * @return window system identifier - one of the window system constants
94
	 * 	defined by {@link Constants}, or <code>null</code> to default to the
95
	 * 	running window system
96
	 */
97
	public String getWS();
98
99
	/**
100
	 * Sets the window system this target is configured for or <code>null</code> to 
101
	 * default to the running window system.
102
	 * 
103
	 * @param window system identifier or <code>null</code> to default to the
104
	 * 	running window system
105
	 */
106
	public void setWS(String ws);
107
108
	/**
109
	 * Returns the identifier of the architecture this target is configured for,
110
	 * or <code>null</code> to default to the running architecture.
111
	 * 
112
	 * @return architecture identifier - one of the architecture constants
113
	 * 	defined by {@link Constants} or <code>null</code> to default to the running
114
	 * 	architecture
115
	 */
116
	public String getArch();
117
118
	/**
119
	 * Sets the architecture this target is configured for, or <code>null</code> to default
120
	 * to the running architecture.
121
	 * 
122
	 * @param architecture identifier or <code>null</code> to default to the
123
	 * 	running architecture.
124
	 */
125
	public void setArch(String arch);
126
127
	/**
128
	 * Returns the identifier of the locale this target is configured for, or <code>null</code>
129
	 * for default.
130
	 * 
131
	 * @return locale identifier or <code>null</code> for default
132
	 */
133
	public String getNL();
134
135
	/**
136
	 * Sets the locale this target is configured for or <code>null</code> for default.
137
	 * 
138
	 * @param locale identifier or <code>null</code> for default
139
	 */
140
	public void setNL(String nl);
141
142
	/**
143
	 * Returns the bundle containers defined by this target, possible <code>null</code>.
144
	 * 
145
	 * @return bundle containers or <code>null</code>
146
	 */
147
	public IBundleContainer[] getBundleContainers();
148
149
	/**
150
	 * Sets the bundle containers in this target definition or <code>null</code> if none.
151
	 * 
152
	 * @param containers bundle containers or <code>null</code>
153
	 */
154
	public void setBundleContainers(IBundleContainer[] containers);
155
156
	/**
157
	 * Resolves and returns all executable bundles in this target definition, possibly empty.
158
	 * 
159
	 * @param monitor progress monitor or <code>null</code>
160
	 * @return all executable bundles in this target definition
161
	 * @throws CoreException if unable to resolve
162
	 */
163
	public BundleInfo[] resolveBundles(IProgressMonitor monitor) throws CoreException;
164
165
	/**
166
	 * Resolves and returns all source bundles in this target definition, possibly empty.
167
	 * 
168
	 * @param monitor progress monitor or <code>null</code>
169
	 * @return all source bundles in this target definition
170
	 * @throws CoreException if unable to resolve
171
	 */
172
	public BundleInfo[] resolveSourceBundles(IProgressMonitor monitor) throws CoreException;
173
174
	/**
175
	 * Returns any program arguments that should be used when launching this target
176
	 * or <code>null</code> if none.
177
	 * 
178
	 * @return program arguments or <code>null</code> if none
179
	 */
180
	public String getProgramArguments();
181
182
	/**
183
	 * Sets any program arguments that should be used when launching this target
184
	 * or <code>null</code> if none.
185
	 * 
186
	 * @param args program arguments or <code>null</code>
187
	 */
188
	public void setProgramArguments(String args);
189
190
	/**
191
	 * Returns any VM arguments that should be used when launching this target
192
	 * or <code>null</code> if none.
193
	 * 
194
	 * @return VM arguments or <code>null</code> if none
195
	 */
196
	public String getVMArguments();
197
198
	/**
199
	 * Sets any VM arguments that should be used when launching this target
200
	 * or <code>null</code> if none.
201
	 * 
202
	 * @param args VM arguments or <code>null</code>
203
	 */
204
	public void setVMArguments(String args);
205
}
(-)src/org/eclipse/pde/internal/core/target/impl/DirectoryBundleContainer.java (+289 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.core.target.impl;
12
13
import java.io.*;
14
import java.util.*;
15
import java.util.jar.JarFile;
16
import java.util.zip.ZipEntry;
17
import java.util.zip.ZipFile;
18
import org.eclipse.core.runtime.*;
19
import org.eclipse.core.runtime.spi.RegistryContributor;
20
import org.eclipse.core.variables.IStringVariableManager;
21
import org.eclipse.core.variables.VariablesPlugin;
22
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
23
import org.eclipse.osgi.service.pluginconversion.PluginConversionException;
24
import org.eclipse.osgi.service.pluginconversion.PluginConverter;
25
import org.eclipse.osgi.util.ManifestElement;
26
import org.eclipse.osgi.util.NLS;
27
import org.eclipse.pde.internal.core.ICoreConstants;
28
import org.eclipse.pde.internal.core.PDECore;
29
import org.eclipse.pde.internal.core.target.provisional.IBundleContainer;
30
import org.osgi.framework.BundleException;
31
import org.osgi.framework.Constants;
32
33
/**
34
 * A directory of bundles.
35
 * 
36
 * @since 3.5
37
 */
38
public class DirectoryBundleContainer implements IBundleContainer {
39
40
	/**
41
	 * Path to this container's directory in the local file system.
42
	 * The path may contain string substitution variables.
43
	 */
44
	private String fPath;
45
46
	/**
47
	 * A registry can be build to identify old school source bundles.
48
	 */
49
	private IExtensionRegistry fRegistry;
50
51
	/**
52
	 * Constructs a directory bundle container at the given location.
53
	 * 
54
	 * @param path directory location in the local file system
55
	 */
56
	public DirectoryBundleContainer(String path) {
57
		fPath = path;
58
	}
59
60
	/* (non-Javadoc)
61
	 * @see org.eclipse.pde.internal.core.target.provisional.IBundleContainer#resolveBundles(org.eclipse.core.runtime.IProgressMonitor)
62
	 */
63
	public BundleInfo[] resolveBundles(IProgressMonitor monitor) throws CoreException {
64
		return resolveBundles(monitor, false);
65
	}
66
67
	/* (non-Javadoc)
68
	 * @see org.eclipse.pde.internal.core.target.provisional.IBundleContainer#resolveSourceBundles(org.eclipse.core.runtime.IProgressMonitor)
69
	 */
70
	public BundleInfo[] resolveSourceBundles(IProgressMonitor monitor) throws CoreException {
71
		return resolveBundles(monitor, true);
72
	}
73
74
	/**
75
	 * Resolves and returns source or code bundles based on the given flag.
76
	 * 
77
	 * @param monitor progress monitor or <code>null</code>
78
	 * @param source whether to retrieve source bundles
79
	 * @return bundles
80
	 * @throws CoreException
81
	 */
82
	private BundleInfo[] resolveBundles(IProgressMonitor monitor, boolean source) throws CoreException {
83
		File dir = getDirectory();
84
		if (dir.exists() && dir.isDirectory()) {
85
			try {
86
				File[] files = dir.listFiles();
87
				SubMonitor localMonitor = SubMonitor.convert(monitor, "Reading bundles...", files.length);
88
				List bundles = new ArrayList(files.length);
89
				for (int i = 0; i < files.length; i++) {
90
					if (localMonitor.isCanceled()) {
91
						throw new OperationCanceledException();
92
					}
93
					Map manifest = loadManifest(files[i]);
94
					if (manifest != null) {
95
						try {
96
							String header = (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME);
97
							if (header != null) {
98
								ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, header);
99
								if (elements != null) {
100
									String name = elements[0].getValue();
101
									if (name != null) {
102
										BundleInfo info = new BundleInfo();
103
										info.setSymbolicName(name);
104
										info.setLocation(files[i].toURI());
105
										header = (String) manifest.get(Constants.BUNDLE_VERSION);
106
										if (header != null) {
107
											elements = ManifestElement.parseHeader(Constants.BUNDLE_VERSION, header);
108
											if (elements != null) {
109
												info.setVersion(elements[0].getValue());
110
											}
111
										}
112
										if (source == isSourceBundle(files[i], name, manifest)) {
113
											bundles.add(info);
114
										}
115
									}
116
								}
117
							}
118
						} catch (BundleException e) {
119
							// ignore invalid bundles
120
						}
121
					}
122
					localMonitor.worked(1);
123
				}
124
				localMonitor.done();
125
				return (BundleInfo[]) bundles.toArray(new BundleInfo[bundles.size()]);
126
			} finally {
127
				if (fRegistry != null) {
128
					fRegistry.stop(this);
129
					fRegistry = null;
130
				}
131
			}
132
		}
133
		throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, NLS.bind("Directory does not exist: {0}", fPath)));
134
	}
135
136
	/**
137
	 * Returns whether the given bundle is a source bundle.
138
	 * 
139
	 * @param bundle location of the bundle in the file system
140
	 * @param symbolicName symbolic name of the bundle
141
	 * @param manifest the bundle's manifest
142
	 * @return whether the given bundle is a source bundle
143
	 */
144
	private boolean isSourceBundle(File bundle, String symbolicName, Map manifest) {
145
		if (manifest.containsKey(ICoreConstants.ECLIPSE_SOURCE_BUNDLE)) {
146
			// this is the new source bundle identifier
147
			return true;
148
		}
149
		// old source bundles were never jar'd
150
		if (bundle.isFile()) {
151
			return false;
152
		}
153
		// source bundles never have a class path
154
		if (manifest.containsKey(Constants.BUNDLE_CLASSPATH)) {
155
			return false;
156
		}
157
		// check for an "org.eclipse.pde.core.source" extension 
158
		File pxml = new File(bundle, ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR);
159
		if (pxml.exists()) {
160
			IExtensionRegistry registry = getRegistry();
161
			RegistryContributor contributor = new RegistryContributor(symbolicName, symbolicName, null, null);
162
			try {
163
				registry.addContribution(new BufferedInputStream(new FileInputStream(pxml)), contributor, false, null, null, this);
164
				IExtension[] extensions = registry.getExtensions(contributor);
165
				for (int i = 0; i < extensions.length; i++) {
166
					IExtension extension = extensions[i];
167
					if (ICoreConstants.EXTENSION_POINT_SOURCE.equals(extension.getExtensionPointUniqueIdentifier())) {
168
						return true;
169
					}
170
				}
171
			} catch (FileNotFoundException e) {
172
			}
173
		}
174
		return false;
175
	}
176
177
	/**
178
	 * Returns an extension registry used to identify source bundles.
179
	 * 
180
	 * @return extension registry
181
	 */
182
	private IExtensionRegistry getRegistry() {
183
		if (fRegistry == null) {
184
			fRegistry = RegistryFactory.createRegistry(null, this, this);
185
			// contribute PDE source extension point
186
			String bogusDef = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?eclipse version=\"3.2\"?>\n<plugin><extension-point id=\"source\" name=\"source\"/>\n</plugin>"; //$NON-NLS-1$
187
			RegistryContributor contributor = new RegistryContributor(PDECore.PLUGIN_ID, PDECore.PLUGIN_ID, null, null);
188
			fRegistry.addContribution(new ByteArrayInputStream(bogusDef.getBytes()), contributor, false, null, null, this);
189
		}
190
		return fRegistry;
191
	}
192
193
	/**
194
	 * Returns the directory to search for bundles in.
195
	 * 
196
	 * @return directory if unable to resolve variables in the path
197
	 */
198
	protected File getDirectory() throws CoreException {
199
		String path = fPath;
200
		IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
201
		path = manager.performStringSubstitution(fPath);
202
		return new File(path);
203
	}
204
205
	/**
206
	 * Parses a bunlde's manifest into a dictionary. The bundle may be in a jar
207
	 * or in a directory at the specified location.
208
	 * 
209
	 * @param bundleLocation root location of the bundle
210
	 * @return bundle manifest dictionary or <code>null</code> if none
211
	 * @throws CoreException if manifest has invalid syntax
212
	 */
213
	protected Map loadManifest(File bundleLocation) throws CoreException {
214
		ZipFile jarFile = null;
215
		InputStream manifestStream = null;
216
		String extension = new Path(bundleLocation.getName()).getFileExtension();
217
		try {
218
			if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { //$NON-NLS-1$
219
				jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ);
220
				ZipEntry manifestEntry = jarFile.getEntry(JarFile.MANIFEST_NAME);
221
				if (manifestEntry != null) {
222
					manifestStream = jarFile.getInputStream(manifestEntry);
223
				}
224
			} else {
225
				File file = new File(bundleLocation, JarFile.MANIFEST_NAME);
226
				if (file.exists()) {
227
					manifestStream = new FileInputStream(file);
228
				} else {
229
					File pxml = new File(bundleLocation, ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR);
230
					File fxml = new File(bundleLocation, ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR);
231
					if (pxml.exists() || fxml.exists()) {
232
						// support classic non-OSGi plug-in
233
						PluginConverter converter = (PluginConverter) PDECore.getDefault().acquireService(PluginConverter.class.getName());
234
						if (converter != null) {
235
							try {
236
								Dictionary convert = converter.convertManifest(bundleLocation, false, null, false, null);
237
								if (convert != null) {
238
									Map map = new HashMap(convert.size(), 1.0f);
239
									Enumeration keys = convert.keys();
240
									while (keys.hasMoreElements()) {
241
										Object key = keys.nextElement();
242
										map.put(key, convert.get(key));
243
									}
244
									return map;
245
								}
246
							} catch (PluginConversionException e) {
247
								throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, NLS.bind("Error converting manifest for {0}", bundleLocation.getAbsolutePath()), e));
248
							}
249
						}
250
					}
251
				}
252
			}
253
			if (manifestStream == null) {
254
				return null;
255
			}
256
			return ManifestElement.parseBundleManifest(manifestStream, new Hashtable(10));
257
		} catch (BundleException e) {
258
			PDECore.log(e);
259
		} catch (IOException e) {
260
			throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, NLS.bind("Error reading manifest for {0}", bundleLocation.getAbsolutePath()), e));
261
		} finally {
262
			closeZipFileAndStream(manifestStream, jarFile);
263
		}
264
		return null;
265
	}
266
267
	/**
268
	 * Closes the stream and jar file if not <code>null</code>.
269
	 * 
270
	 * @param stream stream to close or <code>null</code>
271
	 * @param jarFile jar to close or <code>null</code>
272
	 */
273
	private void closeZipFileAndStream(InputStream stream, ZipFile jarFile) {
274
		try {
275
			if (stream != null) {
276
				stream.close();
277
			}
278
		} catch (IOException e) {
279
			PDECore.log(e);
280
		}
281
		try {
282
			if (jarFile != null) {
283
				jarFile.close();
284
			}
285
		} catch (IOException e) {
286
			PDECore.log(e);
287
		}
288
	}
289
}

Return to bug 256910