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

Collapse All | Expand All

(-)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 (-9 / +65 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
		IPath basePath = new Path(platformHome);
58
		if (configurationArea == null) {
57
		if (configurationArea == null) {
59
			return null;
58
			return null;
60
		}
59
		}
61
		try {
60
		try {
62
			URL bundlesTxt = new URL(configurationArea.getProtocol(), configurationArea.getHost(), configurationArea.getFile().concat(BUNDLE_INFO_PATH));
61
			BundleInfo[] bundles = readBundles(platformHome, configurationArea);
63
			File home = basePath.toFile();
62
			if (bundles == null) {
64
			BundleInfo bundles[] = getBundlesFromFile(bundlesTxt, home);
65
			if (bundles == null || bundles.length == 0) {
66
				return null;
63
				return null;
67
			}
64
			}
68
			int length = bundles.length;
65
			int length = bundles.length;
69
			URL srcBundlesTxt = new URL(configurationArea.getProtocol(), configurationArea.getHost(), configurationArea.getFile().concat(SRC_INFO_PATH));
66
			BundleInfo[] srcBundles = readSourceBundles(platformHome, configurationArea);
70
			BundleInfo srcBundles[] = getBundlesFromFile(srcBundlesTxt, home);
71
72
			if (srcBundles != null) {
67
			if (srcBundles != null) {
73
				length += srcBundles.length;
68
				length += srcBundles.length;
74
			}
69
			}
75
76
			URL[] urls = new URL[length];
70
			URL[] urls = new URL[length];
77
			copyURLs(urls, 0, bundles);
71
			copyURLs(urls, 0, bundles);
78
			if (srcBundles != null && srcBundles.length > 0) {
72
			if (srcBundles != null && srcBundles.length > 0) {
79
				copyURLs(urls, bundles.length, srcBundles);
73
				copyURLs(urls, bundles.length, srcBundles);
80
			}
74
			}
81
			return urls;
75
			return urls;
76
		} catch (MalformedURLException e) {
77
			PDECore.log(e);
78
			return null;
79
		}
80
	}
81
82
	/**
83
	 * Returns bundles defined by the 'bundles.info' relative to the given
84
	 * home and configuration area, or <code>null</code> if none.
85
	 * The "bundles.info" file is assumed to be at a fixed location relative to the
86
	 * configuration area URL.
87
	 * 
88
	 * @param platformHome absolute path in the local file system to an installation
89
	 * @param configurationArea url location of the configuration directory to search
90
	 *  for bundles.info
91
	 * @return all bundles in the installation or <code>null</code> if not able
92
	 * 	to locate a bundles.info
93
	 */
94
	public static BundleInfo[] readBundles(String platformHome, URL configurationArea) {
95
		IPath basePath = new Path(platformHome);
96
		if (configurationArea == null) {
97
			return null;
98
		}
99
		try {
100
			URL bundlesTxt = new URL(configurationArea.getProtocol(), configurationArea.getHost(), configurationArea.getFile().concat(BUNDLE_INFO_PATH));
101
			File home = basePath.toFile();
102
			BundleInfo bundles[] = getBundlesFromFile(bundlesTxt, home);
103
			if (bundles == null || bundles.length == 0) {
104
				return null;
105
			}
106
			return bundles;
107
		} catch (MalformedURLException e) {
108
			PDECore.log(e);
109
			return null;
110
		} catch (IOException e) {
111
			PDECore.log(e);
112
			return null;
113
		}
114
	}
82
115
116
	/**
117
	 * Returns source bundles defined by the 'source.info' file in the
118
	 * specified location, or <code>null</code> if none. The "source.info" file
119
	 * is assumed to be at a fixed location relative to the configuration area URL.
120
	 * 
121
	 * @param platformHome absolute path in the local file system to an installation
122
	 * @param configurationArea url location of the configuration directory to search for bundles.info and source.info
123
	 * @return all source bundles in the installation or <code>null</code> if not able
124
	 * 	to locate a source.info
125
	 */
126
	public static BundleInfo[] readSourceBundles(String platformHome, URL configurationArea) {
127
		IPath basePath = new Path(platformHome);
128
		if (configurationArea == null) {
129
			return null;
130
		}
131
		try {
132
			File home = basePath.toFile();
133
			URL srcBundlesTxt = new URL(configurationArea.getProtocol(), configurationArea.getHost(), configurationArea.getFile().concat(SRC_INFO_PATH));
134
			BundleInfo srcBundles[] = getBundlesFromFile(srcBundlesTxt, home);
135
			if (srcBundles == null || srcBundles.length == 0) {
136
				return null;
137
			}
138
			return srcBundles;
83
		} catch (MalformedURLException e) {
139
		} catch (MalformedURLException e) {
84
			PDECore.log(e);
140
			PDECore.log(e);
85
			return null;
141
			return null;
(-)src/org/eclipse/pde/internal/core/PDECore.java (-2 / +15 lines)
Lines 13-18 Link Here
13
import java.io.IOException;
13
import java.io.IOException;
14
import java.lang.reflect.InvocationTargetException;
14
import java.lang.reflect.InvocationTargetException;
15
import java.net.URL;
15
import java.net.URL;
16
import java.util.Hashtable;
16
import org.eclipse.core.resources.IWorkspace;
17
import org.eclipse.core.resources.IWorkspace;
17
import org.eclipse.core.resources.ResourcesPlugin;
18
import org.eclipse.core.resources.ResourcesPlugin;
18
import org.eclipse.core.runtime.*;
19
import org.eclipse.core.runtime.*;
Lines 21-29 Link Here
21
import org.eclipse.pde.internal.core.builders.FeatureRebuilder;
22
import org.eclipse.pde.internal.core.builders.FeatureRebuilder;
22
import org.eclipse.pde.internal.core.builders.PluginRebuilder;
23
import org.eclipse.pde.internal.core.builders.PluginRebuilder;
23
import org.eclipse.pde.internal.core.schema.SchemaRegistry;
24
import org.eclipse.pde.internal.core.schema.SchemaRegistry;
25
import org.eclipse.pde.internal.core.target.impl.TargetPlatformService;
26
import org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService;
24
import org.eclipse.update.configurator.ConfiguratorUtils;
27
import org.eclipse.update.configurator.ConfiguratorUtils;
25
import org.osgi.framework.BundleContext;
28
import org.osgi.framework.*;
26
import org.osgi.framework.ServiceReference;
27
29
28
public class PDECore extends Plugin {
30
public class PDECore extends Plugin {
29
	public static final String PLUGIN_ID = "org.eclipse.pde.core"; //$NON-NLS-1$
31
	public static final String PLUGIN_ID = "org.eclipse.pde.core"; //$NON-NLS-1$
Lines 117-122 Link Here
117
119
118
	private PluginRebuilder fPluginRebuilder;
120
	private PluginRebuilder fPluginRebuilder;
119
121
122
	/**
123
	 * Target platform service.
124
	 */
125
	private ServiceRegistration fTargetPlatformService;
126
120
	public PDECore() {
127
	public PDECore() {
121
		inst = this;
128
		inst = this;
122
	}
129
	}
Lines 225-230 Link Here
225
		fPluginRebuilder.start();
232
		fPluginRebuilder.start();
226
		fFeatureRebuilder = new FeatureRebuilder();
233
		fFeatureRebuilder = new FeatureRebuilder();
227
		fFeatureRebuilder.start();
234
		fFeatureRebuilder.start();
235
236
		fTargetPlatformService = context.registerService(ITargetPlatformService.class.getName(), TargetPlatformService.getDefault(), new Hashtable());
228
	}
237
	}
229
238
230
	public BundleContext getBundleContext() {
239
	public BundleContext getBundleContext() {
Lines 264-269 Link Here
264
			fModelManager.shutdown();
273
			fModelManager.shutdown();
265
			fModelManager = null;
274
			fModelManager = null;
266
		}
275
		}
276
		if (fTargetPlatformService != null) {
277
			fTargetPlatformService.unregister();
278
			fTargetPlatformService = null;
279
		}
267
	}
280
	}
268
281
269
	/**
282
	/**
(-)META-INF/MANIFEST.MF (+2 lines)
Lines 32-37 Link Here
32
 org.eclipse.pde.internal.core.search;x-friends:="org.eclipse.pde.ui",
32
 org.eclipse.pde.internal.core.search;x-friends:="org.eclipse.pde.ui",
33
 org.eclipse.pde.internal.core.site;x-friends:="org.eclipse.pde.ui",
33
 org.eclipse.pde.internal.core.site;x-friends:="org.eclipse.pde.ui",
34
 org.eclipse.pde.internal.core.target;x-friends:="org.eclipse.pde.ui",
34
 org.eclipse.pde.internal.core.target;x-friends:="org.eclipse.pde.ui",
35
 org.eclipse.pde.internal.core.target.impl;x-internal:=true,
36
 org.eclipse.pde.internal.core.target.provisional;x-friends:="org.eclipse.pde.ui",
35
 org.eclipse.pde.internal.core.text;
37
 org.eclipse.pde.internal.core.text;
36
  x-friends:="org.eclipse.pde.ui,
38
  x-friends:="org.eclipse.pde.ui,
37
   org.eclipse.pde.ds.core,
39
   org.eclipse.pde.ds.core,
(-)src/org/eclipse/pde/internal/core/target/impl/TargetDefinition.java (+248 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.InputStream;
14
import java.io.OutputStream;
15
import java.util.*;
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
19
import org.eclipse.pde.internal.core.target.provisional.*;
20
21
/**
22
 * Target definition implementation.
23
 * 
24
 * @since 3.5
25
 */
26
class TargetDefinition implements ITargetDefinition {
27
28
	// name and description
29
	private String fName;
30
	private String fDescription;
31
32
	// arguments
33
	private String fProgramArgs;
34
	private String fVMArgs;
35
36
	// environment settings
37
	private String fEE;
38
	private String fArch;
39
	private String fOS;
40
	private String fWS;
41
	private String fNL;
42
43
	// bundle containers
44
	private IBundleContainer[] fContainers;
45
46
	// handle
47
	private ITargetHandle fHandle;
48
49
	/**
50
	 * Constructs a target definition based on the given handle. 
51
	 */
52
	TargetDefinition(ITargetHandle handle) {
53
		fHandle = handle;
54
	}
55
56
	/* (non-Javadoc)
57
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getArch()
58
	 */
59
	public String getArch() {
60
		return fArch;
61
	}
62
63
	/* (non-Javadoc)
64
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getBundleContainers()
65
	 */
66
	public IBundleContainer[] getBundleContainers() {
67
		return fContainers;
68
	}
69
70
	/* (non-Javadoc)
71
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getDescription()
72
	 */
73
	public String getDescription() {
74
		return fDescription;
75
	}
76
77
	/* (non-Javadoc)
78
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getExecutionEnvironment()
79
	 */
80
	public String getExecutionEnvironment() {
81
		return fEE;
82
	}
83
84
	/* (non-Javadoc)
85
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getNL()
86
	 */
87
	public String getNL() {
88
		return fNL;
89
	}
90
91
	/* (non-Javadoc)
92
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getName()
93
	 */
94
	public String getName() {
95
		return fName;
96
	}
97
98
	/* (non-Javadoc)
99
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getOS()
100
	 */
101
	public String getOS() {
102
		return fOS;
103
	}
104
105
	/* (non-Javadoc)
106
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getProgramArguments()
107
	 */
108
	public String getProgramArguments() {
109
		return fProgramArgs;
110
	}
111
112
	/* (non-Javadoc)
113
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getVMArguments()
114
	 */
115
	public String getVMArguments() {
116
		return fVMArgs;
117
	}
118
119
	/* (non-Javadoc)
120
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getWS()
121
	 */
122
	public String getWS() {
123
		return fWS;
124
	}
125
126
	/* (non-Javadoc)
127
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setArch(java.lang.String)
128
	 */
129
	public void setArch(String arch) {
130
		fArch = arch;
131
	}
132
133
	/* (non-Javadoc)
134
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setDescription(java.lang.String)
135
	 */
136
	public void setDescription(String description) {
137
		fDescription = description;
138
	}
139
140
	/* (non-Javadoc)
141
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setExecutionEnvironment(java.lang.String)
142
	 */
143
	public void setExecutionEnvironment(String environment) {
144
		fEE = environment;
145
	}
146
147
	/* (non-Javadoc)
148
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setNL(java.lang.String)
149
	 */
150
	public void setNL(String nl) {
151
		fNL = nl;
152
	}
153
154
	/* (non-Javadoc)
155
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setName(java.lang.String)
156
	 */
157
	public void setName(String name) {
158
		fName = name;
159
	}
160
161
	/* (non-Javadoc)
162
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setOS(java.lang.String)
163
	 */
164
	public void setOS(String os) {
165
		fOS = os;
166
	}
167
168
	/* (non-Javadoc)
169
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setProgramArguments(java.lang.String)
170
	 */
171
	public void setProgramArguments(String args) {
172
		fProgramArgs = args;
173
	}
174
175
	/* (non-Javadoc)
176
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setVMArguments(java.lang.String)
177
	 */
178
	public void setVMArguments(String args) {
179
		fVMArgs = args;
180
	}
181
182
	/* (non-Javadoc)
183
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setWS(java.lang.String)
184
	 */
185
	public void setWS(String ws) {
186
		fWS = ws;
187
	}
188
189
	/* (non-Javadoc)
190
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#setBundleContainers(org.eclipse.pde.internal.core.target.provisional.IBundleContainer[])
191
	 */
192
	public void setBundleContainers(IBundleContainer[] containers) {
193
		fContainers = containers;
194
	}
195
196
	/* (non-Javadoc)
197
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#resolveBundles(org.eclipse.core.runtime.IProgressMonitor)
198
	 */
199
	public BundleInfo[] resolveBundles(IProgressMonitor monitor) throws CoreException {
200
		IBundleContainer[] containers = getBundleContainers();
201
		List bundles = new ArrayList();
202
		for (int i = 0; i < containers.length; i++) {
203
			BundleInfo[] infos = containers[i].resolveBundles(monitor);
204
			bundles.addAll(Arrays.asList(infos));
205
		}
206
		return (BundleInfo[]) bundles.toArray(new BundleInfo[bundles.size()]);
207
	}
208
209
	/* (non-Javadoc)
210
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#resolveSourceBundles(org.eclipse.core.runtime.IProgressMonitor)
211
	 */
212
	public BundleInfo[] resolveSourceBundles(IProgressMonitor monitor) throws CoreException {
213
		IBundleContainer[] containers = getBundleContainers();
214
		List source = new ArrayList();
215
		for (int i = 0; i < containers.length; i++) {
216
			BundleInfo[] infos = containers[i].resolveSourceBundles(monitor);
217
			source.addAll(Arrays.asList(infos));
218
		}
219
		return (BundleInfo[]) source.toArray(new BundleInfo[source.size()]);
220
	}
221
222
	/* (non-Javadoc)
223
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetDefinition#getHandle()
224
	 */
225
	public ITargetHandle getHandle() {
226
		return fHandle;
227
	}
228
229
	/**
230
	 * Build contents from the given stream.
231
	 * 
232
	 * @param stream input stream
233
	 * @throws CoreException if an error occurs
234
	 */
235
	void setContents(InputStream stream) throws CoreException {
236
237
	}
238
239
	/**
240
	 * Persists contents to the given stream.
241
	 * 
242
	 * @param stream output stream
243
	 * @throws CoreException if an error occurs
244
	 */
245
	void write(OutputStream stream) throws CoreException {
246
247
	}
248
}
(-)src/org/eclipse/pde/internal/core/target/provisional/ITargetHandle.java (+45 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
15
/**
16
 * A handle to a target definition.
17
 * 
18
 * @since 3.5
19
 */
20
public interface ITargetHandle {
21
22
	/**
23
	 * Returns the target definition this handle references.
24
	 * 
25
	 * @return target definition
26
	 * @throws CoreException if the underlying target definition does not exist
27
	 */
28
	public ITargetDefinition getTargetDefinition() throws CoreException;
29
30
	/**
31
	 * Returns a memento for this handle.
32
	 * 
33
	 * @return a memento for this handle
34
	 * @exception CoreException if unable to generate a memento
35
	 */
36
	public String getMemento() throws CoreException;
37
38
	/**
39
	 * Returns whether or not the underlying target definition exists.
40
	 * 
41
	 * @return whether or not the underlying target definition exists
42
	 */
43
	public boolean exists();
44
45
}
(-)src/org/eclipse/pde/internal/core/target/impl/TargetPlatformService.java (+116 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.URI;
14
import java.net.URISyntaxException;
15
import org.eclipse.core.resources.IFile;
16
import org.eclipse.core.runtime.*;
17
import org.eclipse.pde.internal.core.PDECore;
18
import org.eclipse.pde.internal.core.target.provisional.*;
19
20
/**
21
 * Target platform service implementation.
22
 * 
23
 * @since 3.5
24
 */
25
public class TargetPlatformService implements ITargetPlatformService {
26
27
	private static ITargetPlatformService fgDefault;
28
29
	private TargetPlatformService() {
30
	}
31
32
	public synchronized static ITargetPlatformService getDefault() {
33
		if (fgDefault == null) {
34
			fgDefault = new TargetPlatformService();
35
		}
36
		return fgDefault;
37
	}
38
39
	/* (non-Javadoc)
40
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService#deleteTarget(org.eclipse.pde.internal.core.target.provisional.ITargetHandle)
41
	 */
42
	public void deleteTarget(ITargetHandle handle) throws CoreException {
43
		// TODO Auto-generated method stub
44
45
	}
46
47
	/* (non-Javadoc)
48
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService#getTarget(org.eclipse.core.resources.IFile)
49
	 */
50
	public ITargetHandle getTarget(IFile file) {
51
		return new FileTargetHandle(file);
52
	}
53
54
	/* (non-Javadoc)
55
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService#getTargetHandle(java.lang.String)
56
	 */
57
	public ITargetHandle getTargetHandle(String memento) throws CoreException {
58
		try {
59
			URI uri = new URI(memento);
60
			String scheme = uri.getScheme();
61
			if (FileTargetHandle.SCHEME.equals(scheme)) {
62
				return FileTargetHandle.restoreHandle(uri);
63
			} else if (LocalTargetHandle.SCHEME.equals(scheme)) {
64
				return LocalTargetHandle.restoreHandle(uri);
65
			}
66
		} catch (URISyntaxException e) {
67
			throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, "Unable to restore target memento", e));
68
		}
69
		throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, "Unrecognized target memento scheme", null));
70
	}
71
72
	/* (non-Javadoc)
73
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService#getTargets(org.eclipse.core.runtime.IProgressMonitor)
74
	 */
75
	public ITargetHandle[] getTargets(IProgressMonitor monitor) {
76
		// TODO Auto-generated method stub
77
		return null;
78
	}
79
80
	/* (non-Javadoc)
81
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService#newDirectoryContainer(java.lang.String)
82
	 */
83
	public IBundleContainer newDirectoryContainer(String path) {
84
		return new DirectoryBundleContainer(path);
85
	}
86
87
	/* (non-Javadoc)
88
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService#newProfileContainer(java.lang.String)
89
	 */
90
	public IBundleContainer newProfileContainer(String home) {
91
		return newProfileContainer(home, null);
92
	}
93
94
	/* (non-Javadoc)
95
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService#newProfileContainer(java.lang.String, java.lang.String)
96
	 */
97
	public IBundleContainer newProfileContainer(String home, String configurationLocation) {
98
		return new ProfileBundleContainer(home, configurationLocation);
99
	}
100
101
	/* (non-Javadoc)
102
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService#newTarget()
103
	 */
104
	public ITargetDefinition newTarget() {
105
		return new TargetDefinition(new LocalTargetHandle());
106
	}
107
108
	/* (non-Javadoc)
109
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService#saveTargetDefinition(org.eclipse.pde.internal.core.target.provisional.ITargetDefinition)
110
	 */
111
	public void saveTargetDefinition(ITargetDefinition definition) throws CoreException {
112
		// TODO Auto-generated method stub
113
114
	}
115
116
}
(-)src/org/eclipse/pde/internal/core/target/impl/FileTargetHandle.java (+102 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.InputStream;
14
import java.net.URI;
15
import java.net.URISyntaxException;
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.resources.ResourcesPlugin;
18
import org.eclipse.core.runtime.*;
19
import org.eclipse.pde.internal.core.PDECore;
20
import org.eclipse.pde.internal.core.target.provisional.ITargetHandle;
21
22
/**
23
 * A handle to a target stored in the workspace as a <code>.target</code> file.
24
 * 
25
 * @since 3.5
26
 */
27
class FileTargetHandle extends AbstractTargetHandle {
28
29
	private IFile fFile;
30
31
	/**
32
	 * Scheme for resource target handle
33
	 */
34
	static final String SCHEME = "resource"; //$NON-NLS-1$
35
36
	/**
37
	 * Returns a handle for the given URI.
38
	 * 
39
	 * @param uri URI
40
	 * @return target handle
41
	 */
42
	static ITargetHandle restoreHandle(URI uri) {
43
		String part = uri.getSchemeSpecificPart();
44
		Path path = new Path(part);
45
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
46
		return new FileTargetHandle(file);
47
	}
48
49
	/**
50
	 * Constructs a handle to a target in the given file.
51
	 * 
52
	 * @param file underlying file - may or may not exist
53
	 */
54
	FileTargetHandle(IFile file) {
55
		fFile = file;
56
	}
57
58
	/* (non-Javadoc)
59
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetHandle#getMemento()
60
	 */
61
	public String getMemento() throws CoreException {
62
		try {
63
			URI uri = new URI(SCHEME, fFile.getFullPath().toPortableString(), null);
64
			return uri.toString();
65
		} catch (URISyntaxException e) {
66
			throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, "Unable to generate memento for target platform", e));
67
		}
68
	}
69
70
	/* (non-Javadoc)
71
	 * @see org.eclipse.pde.internal.core.target.impl.AbstractTargetHandle#getInputStream()
72
	 */
73
	protected InputStream getInputStream() throws CoreException {
74
		return fFile.getContents();
75
	}
76
77
	/* (non-Javadoc)
78
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetHandle#exists()
79
	 */
80
	public boolean exists() {
81
		return fFile.exists();
82
	}
83
84
	/* (non-Javadoc)
85
	 * @see java.lang.Object#equals(java.lang.Object)
86
	 */
87
	public boolean equals(Object obj) {
88
		if (obj instanceof FileTargetHandle) {
89
			FileTargetHandle handle = (FileTargetHandle) obj;
90
			return fFile.equals(handle.fFile);
91
		}
92
		return false;
93
	}
94
95
	/* (non-Javadoc)
96
	 * @see java.lang.Object#hashCode()
97
	 */
98
	public int hashCode() {
99
		return fFile.hashCode() + getClass().hashCode();
100
	}
101
102
}
(-)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
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/impl/LocalTargetHandle.java (+141 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.net.URI;
15
import java.net.URISyntaxException;
16
import org.eclipse.core.runtime.*;
17
import org.eclipse.pde.internal.core.PDECore;
18
import org.eclipse.pde.internal.core.target.provisional.ITargetHandle;
19
20
/**
21
 * A handle to a target stored with workspace metadata.
22
 * 
23
 * @since 3.5
24
 */
25
class LocalTargetHandle extends AbstractTargetHandle {
26
27
	/**
28
	 * Time stamp when target was created.
29
	 */
30
	private long fTimeStamp;
31
32
	/**
33
	 * The last time stamp handed out.
34
	 */
35
	private static long fgLastStamp = -1;
36
37
	/**
38
	 * URI scheme for local targets
39
	 */
40
	static final String SCHEME = "local"; //$NON-NLS-1$
41
42
	/**
43
	 * Path to the local directory where API descriptions are cached
44
	 * per project.
45
	 */
46
	static final IPath LOCAL_TARGET_CONTAINER_PATH = PDECore.getDefault().getStateLocation().append(".local_targets"); //$NON-NLS-1$
47
48
	/**
49
	 * Reconstructs a handle from the specified URI.
50
	 * 
51
	 * @param uri URI
52
	 * @return handle to a target in local metadata
53
	 * @exception if unable to restore
54
	 */
55
	static ITargetHandle restoreHandle(URI uri) throws CoreException {
56
		String part = uri.getSchemeSpecificPart();
57
		try {
58
			long stamp = Long.parseLong(part);
59
			return new LocalTargetHandle(stamp);
60
		} catch (NumberFormatException e) {
61
			throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, "Unable to restore target handle", e));
62
		}
63
	}
64
65
	/**
66
	 * Constructs a new target handle to a local file, based on a time stamp.
67
	 */
68
	LocalTargetHandle() {
69
		fTimeStamp = System.currentTimeMillis();
70
		if (fTimeStamp == fgLastStamp) {
71
			fTimeStamp++;
72
		}
73
		fgLastStamp = fTimeStamp;
74
	}
75
76
	/**
77
	 * Reconstructs a handle.
78
	 * 
79
	 * @param stamp time stamp
80
	 */
81
	private LocalTargetHandle(long stamp) {
82
		fTimeStamp = stamp;
83
	}
84
85
	/* (non-Javadoc)
86
	 * @see org.eclipse.pde.internal.core.target.impl.AbstractTargetHandle#getInputStream()
87
	 */
88
	protected InputStream getInputStream() throws CoreException {
89
		try {
90
			return new BufferedInputStream(new FileInputStream(getFile()));
91
		} catch (FileNotFoundException e) {
92
			throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, "Target file not found", e));
93
		}
94
	}
95
96
	/* (non-Javadoc)
97
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetHandle#getMemento()
98
	 */
99
	public String getMemento() throws CoreException {
100
		try {
101
			URI uri = new URI(SCHEME, Long.toString(fTimeStamp), null);
102
			return uri.toString();
103
		} catch (URISyntaxException e) {
104
			throw new CoreException(new Status(IStatus.ERROR, PDECore.PLUGIN_ID, "Unable to generate memento for target platform", e));
105
		}
106
	}
107
108
	/* (non-Javadoc)
109
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetHandle#exists()
110
	 */
111
	public boolean exists() {
112
		return getFile().exists();
113
	}
114
115
	/**
116
	 * Returns the local file associated with this target definition.
117
	 * 
118
	 * @return target file
119
	 */
120
	private File getFile() {
121
		return LOCAL_TARGET_CONTAINER_PATH.append(Long.toString(fTimeStamp)).toFile();
122
	}
123
124
	/* (non-Javadoc)
125
	 * @see java.lang.Object#equals(java.lang.Object)
126
	 */
127
	public boolean equals(Object obj) {
128
		if (obj instanceof LocalTargetHandle) {
129
			LocalTargetHandle handle = (LocalTargetHandle) obj;
130
			return handle.fTimeStamp == fTimeStamp;
131
		}
132
		return false;
133
	}
134
135
	/* (non-Javadoc)
136
	 * @see java.lang.Object#hashCode()
137
	 */
138
	public int hashCode() {
139
		return (int) fTimeStamp;
140
	}
141
}
(-)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/ITargetPlatformService.java (+110 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.resources.IFile;
14
import org.eclipse.core.runtime.CoreException;
15
import org.eclipse.core.runtime.IProgressMonitor;
16
17
/**
18
 * A service to manage target platform definitions available to the workspace.
19
 * 
20
 * @since 3.5
21
 */
22
public interface ITargetPlatformService {
23
24
	/**
25
	 * Returns handles to all target definitions known in the workspace.
26
	 * 
27
	 * @return handles to all target definitions known in the workspace
28
	 */
29
	public ITargetHandle[] getTargets(IProgressMonitor monitor);
30
31
	/**
32
	 * Returns a handle to a target definition backed by the underlying file.
33
	 * The target definition may or may not exist. If the file does not exist
34
	 * then this is a new target definition which becomes one of the known
35
	 * workspace target definitions when it is saved.
36
	 * 
37
	 * @param file target definition file that may or may not exist 
38
	 * @return target handle
39
	 */
40
	public ITargetHandle getTarget(IFile file);
41
42
	/**
43
	 * Returns a new target definition to be stored with local metadata. The target
44
	 * becomes one of the know workspace target definitions when it is saved.
45
	 * 
46
	 * @return new empty target definition
47
	 */
48
	public ITargetDefinition newTarget();
49
50
	/**
51
	 * Persists the given target definition. The target becomes one of known
52
	 * workspace target definitions when it is saved.
53
	 * <p>
54
	 * The target is persisted in a location determined by its handle. A handle
55
	 * may refer to an {@link IFile} or a workspace metadata location. Any existing
56
	 * target definition at the same location is overwritten.
57
	 * </p>
58
	 * @param definition definition to persist
59
	 * @throws CoreException if unable to persist the definition
60
	 */
61
	public void saveTargetDefinition(ITargetDefinition definition) throws CoreException;
62
63
	/**
64
	 * Deletes the target definition associated with the given handle.
65
	 * 
66
	 * @param handle target handle
67
	 * @throws CoreException if the associated target does not exist or deletion fails
68
	 */
69
	public void deleteTarget(ITargetHandle handle) throws CoreException;
70
71
	/**
72
	 * Creates and returns a target handle from the given memento. The memento must
73
	 * have been generated from {@link ITargetHandle#getMemento()}.
74
	 * 
75
	 * @param memento a target handle memento
76
	 * @return target handle
77
	 * @throws CoreException if the target handle format is invalid
78
	 */
79
	public ITargetHandle getTargetHandle(String memento) throws CoreException;
80
81
	/**
82
	 * Creates and returns a bundle container that contains all bundles in the
83
	 * specified directory.
84
	 * 
85
	 * @param path absolute path in the local file system, may contain string variables
86
	 * @return bundle container
87
	 */
88
	public IBundleContainer newDirectoryContainer(String path);
89
90
	/**
91
	 * Creates and returns a bundle container that contains all bundles installed in
92
	 * a profile at the specified location with a default configuration area.
93
	 * 
94
	 * @param home absolute path in the local file system to the root of an installed profile
95
	 * @return bundle container
96
	 */
97
	public IBundleContainer newProfileContainer(String home);
98
99
	/**
100
	 * Creates and returns a bundle container that contains all bundles installed in
101
	 * a profile at the specified location with the specified configuration area.
102
	 * 
103
	 * @param home absolute path in the local file system to the root of an installed profile
104
	 * @param configurationLocation absolute path in the local file system to the
105
	 *  configuration area for the specified installation
106
	 * @return bundle container
107
	 */
108
	public IBundleContainer newProfileContainer(String home, String configurationLocation);
109
110
}
(-)src/org/eclipse/pde/internal/core/target/impl/AbstractTargetHandle.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.impl;
12
13
import java.io.InputStream;
14
import org.eclipse.core.runtime.CoreException;
15
import org.eclipse.pde.internal.core.target.provisional.ITargetDefinition;
16
import org.eclipse.pde.internal.core.target.provisional.ITargetHandle;
17
18
/**
19
 * Common implementation of target handles.
20
 * 
21
 * @since 3.5
22
 */
23
abstract class AbstractTargetHandle implements ITargetHandle {
24
25
	/* (non-Javadoc)
26
	 * @see org.eclipse.pde.internal.core.target.provisional.ITargetHandle#getTargetDefinition()
27
	 */
28
	public ITargetDefinition getTargetDefinition() throws CoreException {
29
		TargetDefinition definition = new TargetDefinition(this);
30
		if (exists()) {
31
			definition.setContents(getInputStream());
32
		}
33
		return definition;
34
	}
35
36
	/**
37
	 * Returns an input stream of the target definition's contents.
38
	 * 
39
	 * @return stream of content
40
	 * @throws CoreException if an error occurs
41
	 */
42
	protected abstract InputStream getInputStream() throws CoreException;
43
}
(-)src/org/eclipse/pde/internal/core/target/provisional/ITargetDefinition.java (+212 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, or <code>null</code> if none
28
	 * 
29
	 * @return name or <code>null</code>
30
	 */
31
	public String getName();
32
33
	/**
34
	 * Sets the name of this target.
35
	 * 
36
	 * @param name target name or <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
206
	/**
207
	 * Returns a handle to this target definition.
208
	 * 
209
	 * @return target handle
210
	 */
211
	public ITargetHandle getHandle();
212
}
(-)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
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
}
(-)META-INF/MANIFEST.MF (-1 / +2 lines)
Lines 20-25 Link Here
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.core.filesystem
23
 org.eclipse.core.filesystem,
24
 org.eclipse.equinox.frameworkadmin
24
Eclipse-LazyStart: true
25
Eclipse-LazyStart: true
25
Bundle-RequiredExecutionEnvironment: J2SE-1.4
26
Bundle-RequiredExecutionEnvironment: J2SE-1.4
(-)src/org/eclipse/pde/ui/tests/target/TargetDefinitionTests.java (+340 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 org.eclipse.core.runtime.CoreException;
14
15
import org.eclipse.pde.internal.core.target.provisional.ITargetHandle;
16
17
import org.eclipse.core.resources.IFile;
18
19
import org.eclipse.core.resources.ResourcesPlugin;
20
21
import org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService;
22
23
import java.io.*;
24
import java.net.URL;
25
import java.util.*;
26
import java.util.zip.ZipEntry;
27
import java.util.zip.ZipFile;
28
import junit.framework.TestCase;
29
import org.eclipse.core.runtime.*;
30
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo;
31
import org.eclipse.pde.core.plugin.TargetPlatform;
32
import org.eclipse.pde.internal.core.*;
33
import org.eclipse.pde.internal.core.target.provisional.*;
34
import org.eclipse.pde.internal.ui.tests.macro.MacroPlugin;
35
import org.osgi.framework.ServiceReference;
36
37
/**
38
 * Tests for target definitions.
39
 * 
40
 * @since 3.5 
41
 */
42
public class TargetDefinitionTests extends TestCase {
43
	
44
	/**
45
	 * Retrieves all bundles (source and code) in the given target definition
46
	 * returning them as a set of URLs.
47
	 * 
48
	 * @param target target definition
49
	 * @return all bundle URLs
50
	 */
51
	protected Set getAllBundleURLs(ITargetDefinition target) throws Exception {
52
		BundleInfo[] code = target.resolveBundles(null);
53
		BundleInfo[] source = target.resolveSourceBundles(null);
54
		Set urls = new HashSet(code.length + source.length);
55
		for (int i = 0; i < code.length; i++) {
56
			urls.add(code[i].getLocation().toURL());
57
		}
58
		for (int i = 0; i < source.length; i++) {
59
			urls.add(source[i].getLocation().toURL());
60
		}
61
		return urls;
62
	}
63
	
64
	/**
65
	 * Extracts the classic plug-ins archive, if not already done, and returns a path to the
66
	 * root directory containing the plug-ins.
67
	 * 
68
	 * @return path to the plug-ins directory
69
	 * @throws Exception
70
	 */
71
	protected IPath extracClassicPlugins() throws Exception {
72
		// extract the 3.0.2 skeleton
73
		IPath stateLocation = MacroPlugin.getDefault().getStateLocation();
74
		IPath location = stateLocation.append("classic-plugins");
75
		if (location.toFile().exists()) {
76
			return location;
77
		}
78
		URL zipURL = MacroPlugin.getBundleContext().getBundle().getEntry("/tests/targets/classic-plugins.zip");
79
		Path zipPath = new Path(new File(FileLocator.toFileURL(zipURL).getFile()).getAbsolutePath());
80
		ZipFile zipFile = new ZipFile(zipPath.toFile());
81
		Enumeration entries = zipFile.entries();
82
		while (entries.hasMoreElements()) {
83
			ZipEntry entry = (ZipEntry) entries.nextElement();
84
			if (!entry.isDirectory()) {
85
				IPath entryPath = stateLocation.append(entry.getName());
86
				File dir = entryPath.removeLastSegments(1).toFile();
87
				dir.mkdirs();
88
				File file = entryPath.toFile();
89
				file.createNewFile();
90
				InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
91
				byte[] bytes = getInputStreamAsByteArray(inputStream, -1);
92
				inputStream.close();
93
				BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
94
				outputStream.write(bytes);
95
				outputStream.close();
96
			}
97
		}
98
		zipFile.close();
99
		return location;
100
	}
101
	
102
	/**
103
	 * Returns the target platform service or <code>null</code> if none
104
	 * 
105
	 * @return target platform service
106
	 */
107
	protected ITargetPlatformService getTargetService() {
108
		ServiceReference reference = MacroPlugin.getBundleContext().getServiceReference(ITargetPlatformService.class.getName());
109
		assertNotNull("Missing target platform service", reference);
110
		if (reference == null)
111
			return null;
112
		return (ITargetPlatformService) MacroPlugin.getBundleContext().getService(reference);
113
	}
114
115
	/**
116
	 * Tests that a target definition equivalent to the default target platform
117
	 * contains the same bundles as the default target platform (this is an 
118
	 * explicit location with no target weaving).
119
	 * 
120
	 * @throws Exception
121
	 */
122
	public void testDefaultTargetPlatform() throws Exception {
123
		// the new way
124
		ITargetDefinition definition = getTargetService().newTarget();
125
		IBundleContainer container = getTargetService().newProfileContainer(TargetPlatform.getDefaultLocation());
126
		definition.setBundleContainers(new IBundleContainer[]{container});
127
		Set urls = getAllBundleURLs(definition);
128
		
129
		// the old way
130
		IPath location = new Path(TargetPlatform.getDefaultLocation());
131
		URL[] pluginPaths = P2Utils.readBundlesTxt(location.toOSString(), location.append("configuration").toFile().toURL());
132
		assertEquals("Should have same number of bundles", pluginPaths.length, urls.size());
133
		for (int i = 0; i < pluginPaths.length; i++) {
134
			URL url = pluginPaths[i];
135
			assertTrue("Missing plug-in " + url.toString(), urls.contains(url));
136
		}
137
		
138
	}
139
	
140
	/**
141
	 * Tests that a target definition equivalent to the default target platform
142
	 * contains the same bundles as the default target platform using the
143
	 * platform's configuration location (which will do target weaving). This
144
	 * is really only tested when run as a JUnit plug-in test suite from
145
	 * within Eclipse.
146
	 * 
147
	 * @throws Exception
148
	 */
149
	public void testWovenTargetPlatform() throws Exception {
150
		// the new way
151
		ITargetDefinition definition = getTargetService().newTarget();
152
		IBundleContainer container = getTargetService().newProfileContainer(TargetPlatform.getDefaultLocation(),
153
				new File(Platform.getConfigurationLocation().getURL().getFile()).getAbsolutePath());
154
		definition.setBundleContainers(new IBundleContainer[]{container});
155
		Set urls = getAllBundleURLs(definition);
156
		
157
		// the old way
158
		URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation());
159
		assertEquals("Should have same number of bundles", pluginPaths.length, urls.size());
160
		for (int i = 0; i < pluginPaths.length; i++) {
161
			URL url = pluginPaths[i];
162
			assertTrue("Missing plug-in " + url.toString(), urls.contains(url));
163
		}
164
		
165
	}	
166
167
	/**
168
	 * Tests that a bundle directory container is equivalent to scanning locations.
169
	 * 
170
	 * @throws Exception
171
	 */
172
	public void testDirectoryBundleContainer() throws Exception {
173
		// the new way
174
		ITargetDefinition definition = getTargetService().newTarget();
175
		IBundleContainer container = getTargetService().newDirectoryContainer(TargetPlatform.getDefaultLocation() + "/plugins");
176
		definition.setBundleContainers(new IBundleContainer[]{container});
177
		Set urls = getAllBundleURLs(definition);
178
		
179
		Preferences store = PDECore.getDefault().getPluginPreferences();
180
		boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION);
181
		try {
182
			store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false);
183
			// the old way
184
			URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation());
185
			assertEquals("Should have same number of bundles", pluginPaths.length, urls.size());
186
			for (int i = 0; i < pluginPaths.length; i++) {
187
				URL url = pluginPaths[i];
188
				assertTrue("Missing plug-in " + url.toString(), urls.contains(url));
189
			}
190
		}
191
		finally {
192
			store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore);
193
		}
194
	}
195
	
196
	/**
197
	 * Tests reading a 3.0.2 install with a mix of classic and OSGi plug-ins.
198
	 * 
199
	 * @throws Exception
200
	 */
201
	public void testClassicPlugins() throws Exception {
202
		// extract the 3.0.2 skeleton
203
		IPath location = extracClassicPlugins();
204
		
205
		// the new way
206
		ITargetDefinition definition = getTargetService().newTarget();
207
		IBundleContainer container = getTargetService().newDirectoryContainer(location.toOSString());
208
		definition.setBundleContainers(new IBundleContainer[]{container});
209
		Set urls = getAllBundleURLs(definition);
210
		
211
		Preferences store = PDECore.getDefault().getPluginPreferences();
212
		boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION);
213
		try {
214
			store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false);
215
			// the old way
216
			URL[] pluginPaths = PluginPathFinder.getPluginPaths(location.toOSString());
217
			for (int i = 0; i < pluginPaths.length; i++) {
218
				URL url = pluginPaths[i];
219
				if (!urls.contains(url)) {
220
					System.err.println(url.toString());
221
				}
222
			}
223
			assertEquals("Wrong number of bundles", pluginPaths.length, urls.size());
224
		}
225
		finally {
226
			store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore);
227
		}		
228
	}
229
	
230
	/**
231
	 * Tests identification of source bundles in a 3.0.2 install.
232
	 * 
233
	 * @throws Exception
234
	 */
235
	public void testClassicSourcePlugins() throws Exception {
236
		// extract the 3.0.2 skeleton
237
		IPath location = extracClassicPlugins();
238
		
239
		// the new way
240
		ITargetDefinition definition = getTargetService().newTarget();
241
		IBundleContainer container = getTargetService().newDirectoryContainer(location.toOSString());
242
		definition.setBundleContainers(new IBundleContainer[]{container});
243
		BundleInfo[] bundles = definition.resolveSourceBundles(null);
244
		assertEquals("Wrong number of source bundles", 3, bundles.length);
245
		Set names = new HashSet();
246
		for (int i = 0; i < bundles.length; i++) {
247
			names.add(bundles[i].getSymbolicName());
248
		}
249
		String[] expected = new String[]{"org.eclipse.platform.source", "org.eclipse.jdt.source", "org.eclipse.pde.source"};
250
		for (int i = 0; i < expected.length; i++) {
251
			assertTrue("Missing source for " + expected[i], names.contains(expected[i]));	
252
		}
253
	}
254
255
	/**
256
	 * Returns the given input stream as a byte array
257
	 * @param stream the stream to get as a byte array
258
	 * @param length the length to read from the stream or -1 for unknown
259
	 * @return the given input stream as a byte array
260
	 * @throws IOException
261
	 */
262
	public static byte[] getInputStreamAsByteArray(InputStream stream, int length) throws IOException {
263
		byte[] contents;
264
		if (length == -1) {
265
			contents = new byte[0];
266
			int contentsLength = 0;
267
			int amountRead = -1;
268
			do {
269
				// read at least 8K
270
				int amountRequested = Math.max(stream.available(), 8192);
271
				// resize contents if needed
272
				if (contentsLength + amountRequested > contents.length) {
273
					System.arraycopy(contents,
274
							0,
275
							contents = new byte[contentsLength + amountRequested],
276
							0,
277
							contentsLength);
278
				}
279
				// read as many bytes as possible
280
				amountRead = stream.read(contents, contentsLength, amountRequested);
281
				if (amountRead > 0) {
282
					// remember length of contents
283
					contentsLength += amountRead;
284
				}
285
			} while (amountRead != -1);
286
			// resize contents if necessary
287
			if (contentsLength < contents.length) {
288
				System.arraycopy(contents, 0, contents = new byte[contentsLength], 0, contentsLength);
289
			}
290
		} else {
291
			contents = new byte[length];
292
			int len = 0;
293
			int readSize = 0;
294
			while ((readSize != -1) && (len != length)) {
295
				// See PR 1FMS89U
296
				// We record first the read size. In this case length is the actual
297
				// read size.
298
				len += readSize;
299
				readSize = stream.read(contents, len, length - len);
300
			}
301
		}
302
		return contents;
303
	}	
304
	
305
	/**
306
	 * Tests restoration of a handle to target definition in an IFile 
307
	 * @throws CoreException 
308
	 */
309
	public void testFileTargetHandleMemento() throws CoreException {
310
		ITargetPlatformService service = getTargetService();
311
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("does/not/exist"));
312
		ITargetHandle handle = service.getTarget(file);
313
		assertFalse("Target should not exist", handle.exists());
314
		String memento = handle.getMemento();
315
		assertNotNull("Missing memento", memento);
316
		ITargetHandle handle2 = service.getTargetHandle(memento);
317
		assertEquals("Restore failed", handle, handle2);
318
		IFile file2 = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("does/not/exist/either"));
319
		ITargetHandle handle3 = service.getTarget(file2);
320
		assertFalse("Should be different targets", handle.equals(handle3));
321
	}
322
	
323
	/**
324
	 * Tests restoration of a handle to target definition in local metadata 
325
	 * 
326
	 * @throws CoreException 
327
	 * @throws InterruptedException 
328
	 */
329
	public void testLocalTargetHandleMemento() throws CoreException, InterruptedException {
330
		ITargetPlatformService service = getTargetService();
331
		ITargetHandle handle = service.newTarget().getHandle();
332
		assertFalse("Target should not exist", handle.exists());
333
		String memento = handle.getMemento();
334
		assertNotNull("Missing memento", memento);
335
		ITargetHandle handle2 = service.getTargetHandle(memento);
336
		assertEquals("Restore failed", handle, handle2);
337
		ITargetHandle handle3 = service.newTarget().getHandle();
338
		assertFalse("Should be different targets", handle.equals(handle3));
339
	}	
340
}

Return to bug 256910