Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2007, 2009 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.api.tools.internal.model; |
12 |
|
13 |
import java.io.BufferedReader; |
14 |
import java.io.File; |
15 |
import java.io.FileInputStream; |
16 |
import java.io.FileOutputStream; |
17 |
import java.io.IOException; |
18 |
import java.io.InputStream; |
19 |
import java.io.StringReader; |
20 |
import java.net.MalformedURLException; |
21 |
import java.net.URL; |
22 |
import java.util.ArrayList; |
23 |
import java.util.Dictionary; |
24 |
import java.util.Enumeration; |
25 |
import java.util.HashSet; |
26 |
import java.util.Hashtable; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import java.util.Set; |
31 |
import java.util.jar.JarFile; |
32 |
import java.util.jar.Manifest; |
33 |
import java.util.zip.ZipEntry; |
34 |
import java.util.zip.ZipFile; |
35 |
|
36 |
import javax.xml.parsers.FactoryConfigurationError; |
37 |
import javax.xml.parsers.ParserConfigurationException; |
38 |
import javax.xml.parsers.SAXParser; |
39 |
import javax.xml.parsers.SAXParserFactory; |
40 |
|
41 |
import org.eclipse.core.runtime.CoreException; |
42 |
import org.eclipse.core.runtime.IStatus; |
43 |
import org.eclipse.core.runtime.Path; |
44 |
import org.eclipse.core.runtime.Status; |
45 |
import org.eclipse.osgi.service.resolver.BundleDescription; |
46 |
import org.eclipse.osgi.service.resolver.BundleSpecification; |
47 |
import org.eclipse.osgi.service.resolver.ExportPackageDescription; |
48 |
import org.eclipse.osgi.service.resolver.HostSpecification; |
49 |
import org.eclipse.osgi.service.resolver.ResolverError; |
50 |
import org.eclipse.osgi.service.resolver.State; |
51 |
import org.eclipse.osgi.service.resolver.StateObjectFactory; |
52 |
import org.eclipse.osgi.util.ManifestElement; |
53 |
import org.eclipse.osgi.util.NLS; |
54 |
import org.eclipse.pde.api.tools.internal.ApiBaselineManager; |
55 |
import org.eclipse.pde.api.tools.internal.ApiDescription; |
56 |
import org.eclipse.pde.api.tools.internal.ApiDescriptionProcessor; |
57 |
import org.eclipse.pde.api.tools.internal.BundleVersionRange; |
58 |
import org.eclipse.pde.api.tools.internal.CompositeApiDescription; |
59 |
import org.eclipse.pde.api.tools.internal.IApiCoreConstants; |
60 |
import org.eclipse.pde.api.tools.internal.RequiredComponentDescription; |
61 |
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
62 |
import org.eclipse.pde.api.tools.internal.provisional.Factory; |
63 |
import org.eclipse.pde.api.tools.internal.provisional.IApiAccess; |
64 |
import org.eclipse.pde.api.tools.internal.provisional.IApiDescription; |
65 |
import org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore; |
66 |
import org.eclipse.pde.api.tools.internal.provisional.IRequiredComponentDescription; |
67 |
import org.eclipse.pde.api.tools.internal.provisional.ProfileModifiers; |
68 |
import org.eclipse.pde.api.tools.internal.provisional.VisibilityModifiers; |
69 |
import org.eclipse.pde.api.tools.internal.provisional.descriptors.IPackageDescriptor; |
70 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
71 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
72 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; |
73 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer; |
74 |
import org.eclipse.pde.api.tools.internal.util.FileManager; |
75 |
import org.eclipse.pde.api.tools.internal.util.SourceDefaultHandler; |
76 |
import org.eclipse.pde.api.tools.internal.util.Util; |
77 |
import org.eclipse.pde.internal.core.TargetWeaver; |
78 |
import org.osgi.framework.BundleException; |
79 |
import org.osgi.framework.Constants; |
80 |
import org.osgi.framework.Version; |
81 |
import org.xml.sax.InputSource; |
82 |
import org.xml.sax.SAXException; |
83 |
|
84 |
/** |
85 |
* Implementation of an API component based on a bundle in the file system. |
86 |
* |
87 |
* @since 1.0.0 |
88 |
*/ |
89 |
public class BundleComponent extends Component { |
90 |
|
91 |
static final String TMP_API_FILE_PREFIX = "api"; //$NON-NLS-1$ |
92 |
static final String TMP_API_FILE_POSTFIX = "tmp"; //$NON-NLS-1$ |
93 |
|
94 |
/** |
95 |
* Dictionary parsed from MANIFEST.MF |
96 |
*/ |
97 |
private Dictionary fManifest; |
98 |
|
99 |
/** |
100 |
* Manifest headers that are maintained after {@link BundleDescription} creation. |
101 |
* Only these headers are maintained in the manifest dictionary to reduce footprint. |
102 |
*/ |
103 |
private static final String[] MANIFEST_HEADERS = new String[] { |
104 |
IApiCoreConstants.ECLIPSE_SOURCE_BUNDLE, |
105 |
Constants.BUNDLE_CLASSPATH, |
106 |
Constants.BUNDLE_NAME, |
107 |
Constants.BUNDLE_VERSION |
108 |
}; |
109 |
|
110 |
/** |
111 |
* Whether there is an underlying .api_description file |
112 |
*/ |
113 |
private boolean fHasApiDescription = false; |
114 |
|
115 |
/** |
116 |
* Root location of component in the file system |
117 |
*/ |
118 |
private String fLocation; |
119 |
|
120 |
/** |
121 |
* Underlying bundle description (OSGi model of a bundle) |
122 |
*/ |
123 |
private BundleDescription fBundleDescription; |
124 |
|
125 |
/** |
126 |
* Symbolic name of this bundle |
127 |
*/ |
128 |
private String fSymbolicName = null; |
129 |
|
130 |
/** |
131 |
* Bundle version |
132 |
*/ |
133 |
private Version fVersion = null; |
134 |
|
135 |
/** |
136 |
* Cached value for the lowest EEs |
137 |
*/ |
138 |
private String[] lowestEEs; |
139 |
|
140 |
/** |
141 |
* Flag to know if this component is a binary bundle in the workspace |
142 |
* i.e. an imported binary bundle |
143 |
*/ |
144 |
private boolean fWorkspaceBinary = false; |
145 |
|
146 |
/** |
147 |
* The id of this component |
148 |
*/ |
149 |
private long fBundleId = 0L; |
150 |
|
151 |
/** |
152 |
* Constructs a new API component from the specified location in the file system |
153 |
* in the given profile. |
154 |
* |
155 |
* @param baseline owning API baseline |
156 |
* @param location directory or jar file |
157 |
* @param bundleid |
158 |
* @exception CoreException if unable to create a component from the specified location |
159 |
*/ |
160 |
public BundleComponent(IApiBaseline baseline, String location, long bundleid) throws CoreException { |
161 |
super(baseline); |
162 |
fLocation = location; |
163 |
fBundleId = bundleid; |
164 |
fWorkspaceBinary = isBinary() && ApiBaselineManager.WORKSPACE_API_BASELINE_ID.equals(baseline.getName()); |
165 |
} |
166 |
|
167 |
/* (non-Javadoc) |
168 |
* @see org.eclipse.pde.api.tools.internal.descriptors.AbstractApiComponent#dispose() |
169 |
*/ |
170 |
public void dispose() { |
171 |
try { |
172 |
super.dispose(); |
173 |
} finally { |
174 |
synchronized(this) { |
175 |
fManifest = null; |
176 |
fBundleDescription = null; |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
/** |
182 |
* Returns this bundle's manifest as a dictionary. |
183 |
* |
184 |
* @return manifest dictionary |
185 |
* @exception CoreException if something goes terribly wrong |
186 |
*/ |
187 |
protected synchronized Dictionary getManifest() throws CoreException { |
188 |
if(fManifest == null) { |
189 |
try { |
190 |
fManifest = (Dictionary) loadManifest(new File(fLocation)); |
191 |
} catch (IOException e) { |
192 |
abort("Unable to load manifest due to IO error", e); //$NON-NLS-1$ |
193 |
} |
194 |
} |
195 |
return fManifest; |
196 |
} |
197 |
|
198 |
/** |
199 |
* Reduce the manifest to only contain required headers after {@link BundleDescription} creation. |
200 |
*/ |
201 |
protected synchronized void doManifestCompaction() { |
202 |
Dictionary temp = fManifest; |
203 |
fManifest = new Hashtable(MANIFEST_HEADERS.length, 1); |
204 |
for (int i = 0; i < MANIFEST_HEADERS.length; i++) { |
205 |
String header = MANIFEST_HEADERS[i]; |
206 |
Object value = temp.get(header); |
207 |
if (value != null) { |
208 |
fManifest.put(header, value); |
209 |
} |
210 |
} |
211 |
} |
212 |
|
213 |
/** |
214 |
* Returns if the bundle at the specified location is a valid bundle or not. |
215 |
* Validity is determined via the existence of a readable manifest file |
216 |
* @param location |
217 |
* @return true if the bundle at the given location is valid false otherwise |
218 |
* @throws IOException |
219 |
*/ |
220 |
public boolean isValidBundle() throws CoreException { |
221 |
Dictionary manifest = getManifest(); |
222 |
return manifest != null && (manifest.get(Constants.BUNDLE_NAME) != null && manifest.get(Constants.BUNDLE_VERSION) != null); |
223 |
} |
224 |
|
225 |
/* (non-Javadoc) |
226 |
* @see java.lang.Object#equals(java.lang.Object) |
227 |
*/ |
228 |
public boolean equals(Object obj) { |
229 |
if(obj instanceof BundleComponent) { |
230 |
BundleComponent comp = (BundleComponent) obj; |
231 |
return getName().equals(comp.getName()) && |
232 |
getSymbolicName().equals(comp.getSymbolicName()) && |
233 |
getVersion().equals(comp.getVersion()); |
234 |
} |
235 |
return false; |
236 |
} |
237 |
|
238 |
/** |
239 |
* Initializes the component |
240 |
* @throws CoreException on failure |
241 |
*/ |
242 |
protected synchronized void init() { |
243 |
if(fBundleDescription != null) { |
244 |
return; |
245 |
} |
246 |
try { |
247 |
Dictionary manifest = getManifest(); |
248 |
if (isWorkspaceBinary()) { |
249 |
// must account for bundles in development mode - look for class files in output |
250 |
// folders rather than jars |
251 |
TargetWeaver.weaveManifest(manifest); |
252 |
} |
253 |
fBundleDescription = getBundleDescription(manifest, fLocation, fBundleId); |
254 |
if(fBundleDescription == null) { |
255 |
ApiPlugin.log(new Status( |
256 |
IStatus.ERROR, |
257 |
ApiPlugin.PLUGIN_ID, |
258 |
"Unable to resolve the BundleDescription for the component from: " + fLocation, //$NON-NLS-1$ |
259 |
null)); |
260 |
} |
261 |
fSymbolicName = fBundleDescription.getSymbolicName(); |
262 |
fVersion = fBundleDescription.getVersion(); |
263 |
setName((String)manifest.get(Constants.BUNDLE_NAME)); |
264 |
} catch (BundleException e) { |
265 |
ApiPlugin.log(new Status( |
266 |
IStatus.ERROR, |
267 |
ApiPlugin.PLUGIN_ID, |
268 |
"Unable to create API component from specified location: " + fLocation, //$NON-NLS-1$ |
269 |
e)); |
270 |
} |
271 |
catch (CoreException ce) { |
272 |
ApiPlugin.log(ce); |
273 |
} |
274 |
// compact manifest after initialization - only keep used headers |
275 |
doManifestCompaction(); |
276 |
} |
277 |
|
278 |
/** |
279 |
* Returns if this component is a a binary bundle in the workspace |
280 |
* i.e. an imported binary bundle |
281 |
* @return true if the component is a binary bundle in the workspace, false otherwise |
282 |
*/ |
283 |
public boolean isWorkspaceBinary() { |
284 |
return fWorkspaceBinary; |
285 |
} |
286 |
|
287 |
/** |
288 |
* Returns the {@link State} from the backing baseline |
289 |
* @return the state from the backing {@link ApiBaseline} |
290 |
*/ |
291 |
protected State getState() { |
292 |
return ((ApiBaseline)getBaseline()).getState(); |
293 |
} |
294 |
|
295 |
/** |
296 |
* Returns the {@link BundleDescription} for the given manifest + state or throws an exception, never |
297 |
* returns <code>null</code> |
298 |
* @param manifest |
299 |
* @param location |
300 |
* @param id |
301 |
* @return the {@link BundleDescription} or throws an exception |
302 |
* @throws BundleException |
303 |
*/ |
304 |
protected BundleDescription getBundleDescription(Dictionary manifest, String location, long id) throws BundleException { |
305 |
State state = getState(); |
306 |
BundleDescription bundle = lookupBundle(state, manifest); |
307 |
if(bundle != null) { |
308 |
return bundle; |
309 |
} |
310 |
StateObjectFactory factory = StateObjectFactory.defaultFactory; |
311 |
bundle = factory.createBundleDescription(state, manifest, fLocation, id); |
312 |
state.addBundle(bundle); |
313 |
return bundle; |
314 |
} |
315 |
|
316 |
/** |
317 |
* Tries to look up the bundle described by the given manifest in the given state |
318 |
* @param manifest |
319 |
* @return the bundle for the given manifest, <code>null</code> otherwise |
320 |
*/ |
321 |
protected BundleDescription lookupBundle(State state, Dictionary manifest) { |
322 |
Version version = null; |
323 |
try { |
324 |
//just in case the version is not a number |
325 |
String ver = (String)manifest.get(Constants.BUNDLE_VERSION); |
326 |
version = ver != null ? new Version(ver) : null; |
327 |
} |
328 |
catch (NumberFormatException nfe) { |
329 |
version = null; |
330 |
} |
331 |
return state.getBundle((String)manifest.get(Constants.BUNDLE_SYMBOLICNAME), version); |
332 |
} |
333 |
|
334 |
/** |
335 |
* Returns whether this API component represents a binary bundle versus a project bundle. |
336 |
* |
337 |
* @return whether this API component represents a binary bundle |
338 |
*/ |
339 |
protected boolean isBinary() { |
340 |
return true; |
341 |
} |
342 |
|
343 |
/* (non-Javadoc) |
344 |
* @see org.eclipse.pde.api.tools.internal.AbstractApiComponent#createApiDescription() |
345 |
*/ |
346 |
protected IApiDescription createApiDescription() throws CoreException { |
347 |
BundleDescription[] fragments = getBundleDescription().getFragments(); |
348 |
if (fragments.length == 0) { |
349 |
return createLocalApiDescription(); |
350 |
} |
351 |
// build a composite description |
352 |
IApiDescription[] descriptions = new IApiDescription[fragments.length + 1]; |
353 |
for (int i = 0; i < fragments.length; i++) { |
354 |
BundleDescription fragment = fragments[i]; |
355 |
BundleComponent component = (BundleComponent) getBaseline().getApiComponent(fragment.getSymbolicName()); |
356 |
descriptions[i + 1] = component.getApiDescription(); |
357 |
} |
358 |
descriptions[0] = createLocalApiDescription(); |
359 |
return new CompositeApiDescription(descriptions); |
360 |
} |
361 |
|
362 |
/** |
363 |
* Creates and returns this component's API description based on packages |
364 |
* supplied by this component, exported packages, and associated directives. |
365 |
* |
366 |
* @return API description |
367 |
* @throws CoreException if unable to initialize |
368 |
*/ |
369 |
protected IApiDescription createLocalApiDescription() throws CoreException { |
370 |
IApiDescription apiDesc = new ApiDescription(getSymbolicName()); |
371 |
// first mark all packages as internal |
372 |
initializeApiDescription(apiDesc, getBundleDescription(), getLocalPackageNames()); |
373 |
try { |
374 |
String xml = loadApiDescription(new File(fLocation)); |
375 |
setHasApiDescription(xml != null); |
376 |
if (xml != null) { |
377 |
ApiDescriptionProcessor.annotateApiSettings(null, apiDesc, xml); |
378 |
} |
379 |
} catch (IOException e) { |
380 |
abort("Unable to load .api_description file ", e); //$NON-NLS-1$ |
381 |
} |
382 |
return apiDesc; |
383 |
} |
384 |
|
385 |
/** |
386 |
* Returns the names of all packages that originate from this bundle. |
387 |
* Does not include packages that originate from fragments or a host. |
388 |
* |
389 |
* @return local package names |
390 |
* @throws CoreException |
391 |
*/ |
392 |
protected Set getLocalPackageNames() throws CoreException { |
393 |
Set names = new HashSet(); |
394 |
IApiTypeContainer[] containers = getApiTypeContainers(); |
395 |
IApiComponent comp = null; |
396 |
for (int i = 0; i < containers.length; i++) { |
397 |
comp = (IApiComponent) containers[i].getAncestor(IApiElement.COMPONENT); |
398 |
if (comp != null && comp.getSymbolicName().equals(getSymbolicName())) { |
399 |
String[] packageNames = containers[i].getPackageNames(); |
400 |
for (int j = 0; j < packageNames.length; j++) { |
401 |
names.add(packageNames[j]); |
402 |
} |
403 |
} |
404 |
} |
405 |
return names; |
406 |
} |
407 |
|
408 |
|
409 |
/** |
410 |
* Initializes the given API description based on package exports in the manifest. |
411 |
* The API description for a bundle only contains packages that originate from |
412 |
* this bundle (so a host will not contain API descriptions for packages that |
413 |
* originate from fragments). However, a host's API description will be represented |
414 |
* by a proxy that delegates to the host and all of its fragments to provide |
415 |
* a complete description of the host. |
416 |
* |
417 |
* @param apiDesc API description to initialize |
418 |
* @param bundle the bundle to load from |
419 |
* @param packages the complete set of packages names originating from the backing |
420 |
* component |
421 |
* @throws CoreException if an error occurs |
422 |
*/ |
423 |
public static void initializeApiDescription(IApiDescription apiDesc, BundleDescription bundle, Set packages) throws CoreException { |
424 |
Iterator iterator = packages.iterator(); |
425 |
while (iterator.hasNext()) { |
426 |
String name = (String) iterator.next(); |
427 |
apiDesc.setVisibility(Factory.packageDescriptor(name), VisibilityModifiers.PRIVATE); |
428 |
} |
429 |
// then process exported packages that originate from this bundle |
430 |
// considering host and fragment package exports |
431 |
List supplied = new ArrayList(); |
432 |
ExportPackageDescription[] exportPackages = bundle.getExportPackages(); |
433 |
addSuppliedPackages(packages, supplied, exportPackages); |
434 |
HostSpecification host = bundle.getHost(); |
435 |
if (host != null) { |
436 |
BundleDescription[] hosts = host.getHosts(); |
437 |
for (int i = 0; i < hosts.length; i++) { |
438 |
addSuppliedPackages(packages, supplied, hosts[i].getExportPackages()); |
439 |
} |
440 |
} |
441 |
BundleDescription[] fragments = bundle.getFragments(); |
442 |
for (int i = 0; i < fragments.length; i++) { |
443 |
addSuppliedPackages(packages, supplied, fragments[i].getExportPackages()); |
444 |
} |
445 |
|
446 |
annotateExportedPackages(apiDesc, (ExportPackageDescription[]) supplied.toArray(new ExportPackageDescription[supplied.size()])); |
447 |
} |
448 |
|
449 |
/** |
450 |
* Adds package exports to the given list if the associated package originates |
451 |
* from this bundle. |
452 |
* |
453 |
* @param packages names of packages supplied by this bundle |
454 |
* @param supplied list to append package exports to |
455 |
* @param exportPackages package exports to consider |
456 |
*/ |
457 |
protected static void addSuppliedPackages(Set packages, List supplied, ExportPackageDescription[] exportPackages) { |
458 |
for (int i = 0; i < exportPackages.length; i++) { |
459 |
ExportPackageDescription pkg = exportPackages[i]; |
460 |
String name = pkg.getName(); |
461 |
if (name.equals(".")) { //$NON-NLS-1$ |
462 |
// translate . to default package |
463 |
name = Util.DEFAULT_PACKAGE_NAME; |
464 |
} |
465 |
if (packages.contains(name)) { |
466 |
supplied.add(pkg); |
467 |
} |
468 |
} |
469 |
} |
470 |
|
471 |
/** |
472 |
* Annotates the API description with exported packages. |
473 |
* |
474 |
* @param apiDesc description to annotate |
475 |
* @param exportedPackages packages that are exported |
476 |
*/ |
477 |
protected static void annotateExportedPackages(IApiDescription apiDesc, ExportPackageDescription[] exportedPackages) { |
478 |
for(int i = 0; i < exportedPackages.length; i++) { |
479 |
ExportPackageDescription pkg = exportedPackages[i]; |
480 |
boolean internal = ((Boolean) pkg.getDirective("x-internal")).booleanValue(); //$NON-NLS-1$ |
481 |
String[] friends = (String[]) pkg.getDirective("x-friends"); //$NON-NLS-1$ |
482 |
String pkgName = pkg.getName(); |
483 |
if (pkgName.equals(".")) { //$NON-NLS-1$ |
484 |
// default package |
485 |
pkgName = ""; //$NON-NLS-1$ |
486 |
} |
487 |
IPackageDescriptor pkgDesc = Factory.packageDescriptor(pkgName); |
488 |
if(internal) { |
489 |
apiDesc.setVisibility(pkgDesc, VisibilityModifiers.PRIVATE); |
490 |
} |
491 |
if (friends != null) { |
492 |
apiDesc.setVisibility(pkgDesc, VisibilityModifiers.PRIVATE); |
493 |
for(int j = 0; j < friends.length; j++) { |
494 |
//annotate the api description for x-friends access levels |
495 |
apiDesc.setAccessLevel( |
496 |
Factory.componentDescriptor(friends[j]), |
497 |
Factory.packageDescriptor(pkgName), |
498 |
IApiAccess.FRIEND); |
499 |
} |
500 |
} |
501 |
if (!internal && friends == null) { |
502 |
//there could have been directives that have nothing to do with |
503 |
//visibility, so we need to add the package as API in that case |
504 |
apiDesc.setVisibility(pkgDesc, VisibilityModifiers.API); |
505 |
} |
506 |
} |
507 |
} |
508 |
|
509 |
/* (non-Javadoc) |
510 |
* @see org.eclipse.pde.api.tools.internal.AbstractApiComponent#createApiFilterStore() |
511 |
*/ |
512 |
protected IApiFilterStore createApiFilterStore() throws CoreException { |
513 |
//always return a new empty store since we do not support filtering from bundles |
514 |
return null; |
515 |
} |
516 |
|
517 |
/** |
518 |
* @see org.eclipse.pde.api.tools.internal.AbstractApiTypeContainer#createApiTypeContainers() |
519 |
*/ |
520 |
protected synchronized List createApiTypeContainers() throws CoreException { |
521 |
if (fBundleDescription == null) { |
522 |
baselineDisposed(getBaseline()); |
523 |
} |
524 |
List containers = new ArrayList(5); |
525 |
try { |
526 |
List all = new ArrayList(); |
527 |
// build the classpath from bundle and all fragments |
528 |
all.add(this); |
529 |
boolean considerFragments = true; |
530 |
if (Util.ORG_ECLIPSE_SWT.equals(getSymbolicName())) { |
531 |
// if SWT is a project to be built/analyzed don't consider its fragments |
532 |
considerFragments = !isApiEnabled(); |
533 |
} |
534 |
if (considerFragments) { |
535 |
BundleDescription[] fragments = fBundleDescription.getFragments(); |
536 |
for (int i = 0; i < fragments.length; i++) { |
537 |
BundleDescription fragment = fragments[i]; |
538 |
BundleComponent component = (BundleComponent) getBaseline().getApiComponent(fragment.getSymbolicName()); |
539 |
if (component != null) { |
540 |
// force initialization of the fragment so we can retrieve its class file containers |
541 |
component.getApiTypeContainers(); |
542 |
all.add(component); |
543 |
} |
544 |
} |
545 |
} |
546 |
Iterator iterator = all.iterator(); |
547 |
Set entryNames = new HashSet(5); |
548 |
BundleComponent other = null; |
549 |
while (iterator.hasNext()) { |
550 |
BundleComponent component = (BundleComponent) iterator.next(); |
551 |
String[] paths = getClasspathEntries(component.getManifest()); |
552 |
for (int i = 0; i < paths.length; i++) { |
553 |
String path = paths[i]; |
554 |
// don't re-process the same entry twice (except default entries ".") |
555 |
if (!(".".equals(path))) { //$NON-NLS-1$ |
556 |
if (entryNames.contains(path)) { |
557 |
continue; |
558 |
} |
559 |
} |
560 |
IApiTypeContainer container = component.createApiTypeContainer(path); |
561 |
if (container == null) { |
562 |
for(Iterator iter = all.iterator(); iter.hasNext();) { |
563 |
other = (BundleComponent) iter.next(); |
564 |
if (other != component) { |
565 |
container = other.createApiTypeContainer(path); |
566 |
} |
567 |
} |
568 |
} |
569 |
if (container != null) { |
570 |
containers.add(container); |
571 |
if (!(".".equals(path))) { //$NON-NLS-1$ |
572 |
entryNames.add(path); |
573 |
} |
574 |
} |
575 |
} |
576 |
} |
577 |
} catch (BundleException e) { |
578 |
abort("Unable to parse bundle classpath", e); //$NON-NLS-1$ |
579 |
} catch (IOException e) { |
580 |
abort("Unable to initialize class file containers", e); //$NON-NLS-1$ |
581 |
} |
582 |
return containers; |
583 |
} |
584 |
|
585 |
/** |
586 |
* Returns whether this API component is enabled for API analysis by the API builder. |
587 |
* |
588 |
* @return whether this API component is enabled for API analysis by the API builder. |
589 |
*/ |
590 |
protected boolean isApiEnabled() { |
591 |
return false; |
592 |
} |
593 |
|
594 |
/** |
595 |
* Returns classpath entries defined in the given manifest. |
596 |
* |
597 |
* @param manifest |
598 |
* @return classpath entries as bundle relative paths |
599 |
* @throws BundleException |
600 |
*/ |
601 |
protected String[] getClasspathEntries(Dictionary manifest) throws BundleException { |
602 |
ManifestElement[] classpath = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, (String) manifest.get(Constants.BUNDLE_CLASSPATH)); |
603 |
String elements[] = null; |
604 |
if (classpath == null) { |
605 |
// default classpath is '.' |
606 |
elements = new String[]{"."}; //$NON-NLS-1$ |
607 |
} else { |
608 |
elements = new String[classpath.length]; |
609 |
for (int i = 0; i < classpath.length; i++) { |
610 |
elements[i] = classpath[i].getValue(); |
611 |
} |
612 |
} |
613 |
return elements; |
614 |
} |
615 |
|
616 |
/** |
617 |
* Creates and returns an {@link IApiTypeContainer} at the specified path in |
618 |
* this bundle, or <code>null</code> if the {@link IApiTypeContainer} does not |
619 |
* exist. The path is the name (path) of entries specified by the |
620 |
* <code>Bundle-ClassPath:</code> header. |
621 |
* |
622 |
* @param path relative path to a class file container in this bundle |
623 |
* @return {@link IApiTypeContainer} or <code>null</code> |
624 |
* @exception IOException |
625 |
*/ |
626 |
protected IApiTypeContainer createApiTypeContainer(String path) throws IOException, CoreException { |
627 |
File bundle = new File(fLocation); |
628 |
if (bundle.isDirectory()) { |
629 |
// bundle is folder |
630 |
File entry = new File(bundle, path); |
631 |
if (entry.exists()) { |
632 |
if (entry.isFile()) { |
633 |
return new ArchiveApiTypeContainer(this, entry.getCanonicalPath()); |
634 |
} else { |
635 |
return new DirectoryApiTypeContainer(this, entry.getCanonicalPath()); |
636 |
} |
637 |
} |
638 |
} else { |
639 |
// bundle is jar'd |
640 |
ZipFile zip = null; |
641 |
try { |
642 |
if (path.equals(".")) { //$NON-NLS-1$ |
643 |
return new ArchiveApiTypeContainer(this, fLocation); |
644 |
} else { |
645 |
//classpath element can be jar or folder |
646 |
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=279729 |
647 |
zip = new ZipFile(fLocation); |
648 |
ZipEntry entry = zip.getEntry(path); |
649 |
if (entry != null) { |
650 |
File tmpfolder = new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$ |
651 |
if(entry.isDirectory()) { |
652 |
//extract the dir and all children |
653 |
File dir = File.createTempFile(TMP_API_FILE_PREFIX, TMP_API_FILE_POSTFIX); |
654 |
dir.deleteOnExit(); |
655 |
//hack to create a temp directory |
656 |
// see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4735419 |
657 |
if(dir.delete()) { |
658 |
dir.mkdir(); |
659 |
FileManager.getManager().recordTempFileRoot(dir.getCanonicalPath()); |
660 |
} |
661 |
extractDirectory(zip, entry.getName(), dir); |
662 |
if(dir.isDirectory() && dir.exists()) { |
663 |
return new DirectoryApiTypeContainer(this, dir.getCanonicalPath()); |
664 |
} |
665 |
} |
666 |
else { |
667 |
File file = extractEntry(zip, entry, tmpfolder); |
668 |
if(Util.isArchive(file.getName())) { |
669 |
File parent = file.getParentFile(); |
670 |
if(!parent.equals(tmpfolder)) { |
671 |
FileManager.getManager().recordTempFileRoot(parent.getCanonicalPath()); |
672 |
} |
673 |
else { |
674 |
FileManager.getManager().recordTempFileRoot(file.getCanonicalPath()); |
675 |
} |
676 |
return new ArchiveApiTypeContainer(this, file.getCanonicalPath()); |
677 |
} |
678 |
} |
679 |
} |
680 |
} |
681 |
} finally { |
682 |
if (zip != null) { |
683 |
zip.close(); |
684 |
} |
685 |
} |
686 |
} |
687 |
return null; |
688 |
} |
689 |
|
690 |
/** |
691 |
* Extracts a directory from the archive given a path prefix for entries to retrieve. |
692 |
* <code>null</code> can be passed in as a prefix, causing all entries to be be extracted from |
693 |
* the archive. |
694 |
* |
695 |
* @param zip the {@link ZipFile} to extract from |
696 |
* @param pathprefix the prefix'ing path to include for extraction |
697 |
* @param parent the parent directory to extract to |
698 |
* @throws IOException if the {@link ZipFile} cannot be read or extraction fails to write the file(s) |
699 |
*/ |
700 |
void extractDirectory(ZipFile zip, String pathprefix, File parent) throws IOException { |
701 |
Enumeration entries = zip.entries(); |
702 |
String prefix = (pathprefix == null ? Util.EMPTY_STRING : pathprefix); |
703 |
ZipEntry entry = null; |
704 |
File file = null; |
705 |
while (entries.hasMoreElements()) { |
706 |
entry = (ZipEntry) entries.nextElement(); |
707 |
if(entry.getName().startsWith(prefix)) { |
708 |
file = new File(parent, entry.getName()); |
709 |
if (entry.isDirectory()) { |
710 |
file.mkdir(); |
711 |
continue; |
712 |
} |
713 |
extractEntry(zip, entry, parent); |
714 |
} |
715 |
} |
716 |
} |
717 |
|
718 |
/** |
719 |
* Extracts a non-directory entry from a zip file and returns the File handle |
720 |
* @param zip the zip to extract from |
721 |
* @param entry the entry to extract |
722 |
* @param parent the parent directory to add the extracted entry to |
723 |
* @return the file handle to the extracted entry, <code>null</code> otherwise |
724 |
* @throws IOException |
725 |
*/ |
726 |
File extractEntry(ZipFile zip, ZipEntry entry, File parent) throws IOException { |
727 |
InputStream inputStream = null; |
728 |
File file; |
729 |
FileOutputStream outputStream = null; |
730 |
try { |
731 |
inputStream = zip.getInputStream(entry); |
732 |
file = new File(parent, entry.getName()); |
733 |
File lparent = file.getParentFile(); |
734 |
if(!lparent.exists()) { |
735 |
lparent.mkdirs(); |
736 |
} |
737 |
outputStream = new FileOutputStream(file); |
738 |
byte[] bytes = new byte[8096]; |
739 |
while (inputStream.available() > 0) { |
740 |
int read = inputStream.read(bytes); |
741 |
if (read > 0) { |
742 |
outputStream.write(bytes, 0, read); |
743 |
} |
744 |
} |
745 |
} finally { |
746 |
if (inputStream != null) { |
747 |
try { |
748 |
inputStream.close(); |
749 |
} catch(IOException e) { |
750 |
ApiPlugin.log(e); |
751 |
} |
752 |
} |
753 |
if (outputStream != null) { |
754 |
try { |
755 |
outputStream.close(); |
756 |
} catch(IOException e) { |
757 |
ApiPlugin.log(e); |
758 |
} |
759 |
} |
760 |
} |
761 |
return file; |
762 |
} |
763 |
|
764 |
/** |
765 |
* Parses a bunlde's manifest into a dictionary. The bundle may be in a jar |
766 |
* or in a directory at the specified location. |
767 |
* |
768 |
* @param bundleLocation root location of the bundle |
769 |
* @return bundle manifest dictionary or <code>null</code> if none |
770 |
* @throws IOException if unable to parse |
771 |
*/ |
772 |
protected Map loadManifest(File bundleLocation) throws IOException { |
773 |
ZipFile jarFile = null; |
774 |
InputStream manifestStream = null; |
775 |
String extension = new Path(bundleLocation.getName()).getFileExtension(); |
776 |
try { |
777 |
if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { //$NON-NLS-1$ |
778 |
jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ); |
779 |
ZipEntry manifestEntry = jarFile.getEntry(JarFile.MANIFEST_NAME); |
780 |
if (manifestEntry != null) { |
781 |
manifestStream = jarFile.getInputStream(manifestEntry); |
782 |
} |
783 |
} else { |
784 |
File file = new File(bundleLocation, JarFile.MANIFEST_NAME); |
785 |
if (file.exists()) |
786 |
manifestStream = new FileInputStream(file); |
787 |
} |
788 |
if (manifestStream == null) { |
789 |
return null; |
790 |
} |
791 |
return ManifestElement.parseBundleManifest(manifestStream, new Hashtable(10)); |
792 |
} catch (BundleException e) { |
793 |
ApiPlugin.log(e); |
794 |
} finally { |
795 |
closingZipFileAndStream(manifestStream, jarFile); |
796 |
} |
797 |
return null; |
798 |
} |
799 |
|
800 |
/** |
801 |
* Reads and returns this bunlde's manifest in a Manifest object. |
802 |
* The bundle may be in a jar or in a directory at the specified location. |
803 |
* |
804 |
* @param bundleLocation root location of the bundle |
805 |
* @return manifest or <code>null</code> if not present |
806 |
* @throws IOException if unable to parse |
807 |
*/ |
808 |
protected Manifest readManifest(File bundleLocation) throws IOException { |
809 |
ZipFile jarFile = null; |
810 |
InputStream manifestStream = null; |
811 |
try { |
812 |
String extension = new Path(bundleLocation.getName()).getFileExtension(); |
813 |
if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { //$NON-NLS-1$ |
814 |
jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ); |
815 |
ZipEntry manifestEntry = jarFile.getEntry(JarFile.MANIFEST_NAME); |
816 |
if (manifestEntry != null) { |
817 |
manifestStream = jarFile.getInputStream(manifestEntry); |
818 |
} |
819 |
} else { |
820 |
File file = new File(bundleLocation, JarFile.MANIFEST_NAME); |
821 |
if (file.exists()) |
822 |
manifestStream = new FileInputStream(file); |
823 |
} |
824 |
if (manifestStream == null) { |
825 |
return null; |
826 |
} |
827 |
return new Manifest(manifestStream); |
828 |
} finally { |
829 |
closingZipFileAndStream(manifestStream, jarFile); |
830 |
} |
831 |
} |
832 |
|
833 |
void closingZipFileAndStream(InputStream stream, ZipFile jarFile) { |
834 |
try { |
835 |
if (stream != null) { |
836 |
stream.close(); |
837 |
} |
838 |
} catch (IOException e) { |
839 |
ApiPlugin.log(e); |
840 |
} |
841 |
try { |
842 |
if (jarFile != null) { |
843 |
jarFile.close(); |
844 |
} |
845 |
} catch (IOException e) { |
846 |
ApiPlugin.log(e); |
847 |
} |
848 |
} |
849 |
|
850 |
/** |
851 |
* Reads and returns the file contents corresponding to the given file name. |
852 |
* The bundle may be in a jar or in a directory at the specified location. |
853 |
* |
854 |
* @param xmlFileName the given file name |
855 |
* @param bundleLocation the root location of the bundle |
856 |
* @return the file contents or <code>null</code> if not present |
857 |
*/ |
858 |
protected String readFileContents(String xmlFileName, File bundleLocation) { |
859 |
ZipFile jarFile = null; |
860 |
InputStream stream = null; |
861 |
try { |
862 |
String extension = new Path(bundleLocation.getName()).getFileExtension(); |
863 |
if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { //$NON-NLS-1$ |
864 |
jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ); |
865 |
ZipEntry manifestEntry = jarFile.getEntry(xmlFileName); |
866 |
if (manifestEntry != null) { |
867 |
stream = jarFile.getInputStream(manifestEntry); |
868 |
} |
869 |
} else { |
870 |
File file = new File(bundleLocation, xmlFileName); |
871 |
if (file.exists()) { |
872 |
stream = new FileInputStream(file); |
873 |
} |
874 |
} |
875 |
if (stream == null) { |
876 |
return null; |
877 |
} |
878 |
return new String(Util.getInputStreamAsCharArray(stream, -1, IApiCoreConstants.UTF_8)); |
879 |
} catch(IOException e) { |
880 |
//TODO abort |
881 |
ApiPlugin.log(e); |
882 |
} finally { |
883 |
closingZipFileAndStream(stream, jarFile); |
884 |
} |
885 |
return null; |
886 |
} |
887 |
|
888 |
/** |
889 |
* Parses a bundle's .api_description XML into a string. The file may be in a jar |
890 |
* or in a directory at the specified location. |
891 |
* |
892 |
* @param bundleLocation root location of the bundle |
893 |
* @return API description XML as a string or <code>null</code> if none |
894 |
* @throws IOException if unable to parse |
895 |
*/ |
896 |
protected String loadApiDescription(File bundleLocation) throws IOException { |
897 |
ZipFile jarFile = null; |
898 |
InputStream stream = null; |
899 |
String contents = null; |
900 |
try { |
901 |
String extension = new Path(bundleLocation.getName()).getFileExtension(); |
902 |
if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { //$NON-NLS-1$ |
903 |
jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ); |
904 |
ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.API_DESCRIPTION_XML_NAME); |
905 |
if (manifestEntry != null) { |
906 |
// new file is present |
907 |
stream = jarFile.getInputStream(manifestEntry); |
908 |
} |
909 |
} else { |
910 |
File file = new File(bundleLocation, IApiCoreConstants.API_DESCRIPTION_XML_NAME); |
911 |
if (file.exists()) { |
912 |
// use new file |
913 |
stream = new FileInputStream(file); |
914 |
} |
915 |
} |
916 |
if (stream == null) { |
917 |
return null; |
918 |
} |
919 |
char[] charArray = Util.getInputStreamAsCharArray(stream, -1, IApiCoreConstants.UTF_8); |
920 |
contents = new String(charArray); |
921 |
} finally { |
922 |
closingZipFileAndStream(stream, jarFile); |
923 |
} |
924 |
return contents; |
925 |
} |
926 |
|
927 |
|
928 |
/** |
929 |
* Returns a URL describing a file inside a bundle. |
930 |
* |
931 |
* @param bundleLocation root location of the bundle. May be a |
932 |
* directory or a file (jar) |
933 |
* @param filePath bundle relative path to desired file |
934 |
* @return URL to the file |
935 |
* @throws MalformedURLException |
936 |
*/ |
937 |
protected URL getFileInBundle(File bundleLocation, String filePath) throws MalformedURLException { |
938 |
String extension = new Path(bundleLocation.getName()).getFileExtension(); |
939 |
StringBuffer urlSt = new StringBuffer(); |
940 |
if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { //$NON-NLS-1$ |
941 |
urlSt.append("jar:file:"); //$NON-NLS-1$ |
942 |
urlSt.append(bundleLocation.getAbsolutePath()); |
943 |
urlSt.append("!/"); //$NON-NLS-1$ |
944 |
urlSt.append(filePath); |
945 |
} else { |
946 |
urlSt.append("file:"); //$NON-NLS-1$ |
947 |
urlSt.append(bundleLocation.getAbsolutePath()); |
948 |
urlSt.append(File.separatorChar); |
949 |
urlSt.append(filePath); |
950 |
} |
951 |
return new URL(urlSt.toString()); |
952 |
} |
953 |
|
954 |
/* (non-Javadoc) |
955 |
* @see org.eclipse.pde.api.tools.manifest.IApiComponent#getExecutionEnvironments() |
956 |
*/ |
957 |
public synchronized String[] getExecutionEnvironments() throws CoreException { |
958 |
if (fBundleDescription == null) { |
959 |
baselineDisposed(getBaseline()); |
960 |
} |
961 |
return fBundleDescription.getExecutionEnvironments(); |
962 |
} |
963 |
|
964 |
/* (non-Javadoc) |
965 |
* @see org.eclipse.pde.api.tools.manifest.IApiComponent#getSymbolicName() |
966 |
*/ |
967 |
public final String getSymbolicName() { |
968 |
init(); |
969 |
return fSymbolicName; |
970 |
} |
971 |
|
972 |
/* (non-Javadoc) |
973 |
* @see org.eclipse.pde.api.tools.manifest.IApiComponent#getRequiredComponents() |
974 |
*/ |
975 |
public synchronized IRequiredComponentDescription[] getRequiredComponents() throws CoreException { |
976 |
if (fBundleDescription == null) { |
977 |
baselineDisposed(getBaseline()); |
978 |
} |
979 |
BundleSpecification[] requiredBundles = fBundleDescription.getRequiredBundles(); |
980 |
IRequiredComponentDescription[] req = new IRequiredComponentDescription[requiredBundles.length]; |
981 |
for (int i = 0; i < requiredBundles.length; i++) { |
982 |
BundleSpecification bundle = requiredBundles[i]; |
983 |
req[i] = new RequiredComponentDescription(bundle.getName(), |
984 |
new BundleVersionRange(bundle.getVersionRange()), |
985 |
bundle.isOptional(), |
986 |
bundle.isExported()); |
987 |
} |
988 |
return req; |
989 |
} |
990 |
|
991 |
/* (non-Javadoc) |
992 |
* @see org.eclipse.pde.api.tools.manifest.IApiComponent#getVersion() |
993 |
*/ |
994 |
public synchronized String getVersion() { |
995 |
init(); |
996 |
return fVersion.toString(); |
997 |
} |
998 |
|
999 |
/** |
1000 |
* Returns this component's bundle description. |
1001 |
* |
1002 |
* @return bundle description |
1003 |
*/ |
1004 |
public synchronized BundleDescription getBundleDescription() throws CoreException { |
1005 |
init(); |
1006 |
if (fBundleDescription == null) { |
1007 |
baselineDisposed(getBaseline()); |
1008 |
} |
1009 |
return fBundleDescription; |
1010 |
} |
1011 |
|
1012 |
/* (non-Javadoc) |
1013 |
* @see java.lang.Object#toString() |
1014 |
*/ |
1015 |
public String toString() { |
1016 |
if (fBundleDescription != null) { |
1017 |
try { |
1018 |
StringBuffer buffer = new StringBuffer(); |
1019 |
buffer.append(fBundleDescription.toString()); |
1020 |
buffer.append(" - "); //$NON-NLS-1$ |
1021 |
buffer.append("[fragment: ").append(isFragment()).append("] "); //$NON-NLS-1$ //$NON-NLS-2$ |
1022 |
buffer.append("[host: ").append(fBundleDescription.getFragments().length > 0).append("] "); //$NON-NLS-1$ //$NON-NLS-2$ |
1023 |
buffer.append("[system bundle: ").append(isSystemComponent()).append("] "); //$NON-NLS-1$ //$NON-NLS-2$ |
1024 |
buffer.append("[source bundle: ").append(isSourceComponent()).append("] "); //$NON-NLS-1$ //$NON-NLS-2$ |
1025 |
buffer.append("[dev bundle: ").append(fWorkspaceBinary).append("]"); //$NON-NLS-1$ //$NON-NLS-2$ |
1026 |
return buffer.toString(); |
1027 |
} |
1028 |
catch(CoreException ce) {} |
1029 |
} |
1030 |
else { |
1031 |
StringBuffer buffer = new StringBuffer(); |
1032 |
buffer.append("Un-initialized Bundle Component"); //$NON-NLS-1$ |
1033 |
buffer.append("[location: ").append(fLocation).append("]"); //$NON-NLS-1$ //$NON-NLS-2$ |
1034 |
buffer.append("[dev bundle: ").append(fWorkspaceBinary).append("]"); //$NON-NLS-1$ //$NON-NLS-2$ |
1035 |
return buffer.toString(); |
1036 |
} |
1037 |
return super.toString(); |
1038 |
} |
1039 |
|
1040 |
/* (non-Javadoc) |
1041 |
* @see org.eclipse.pde.api.tools.model.component.IApiComponent#getLocation() |
1042 |
*/ |
1043 |
public String getLocation() { |
1044 |
return fLocation; |
1045 |
} |
1046 |
|
1047 |
/* (non-Javadoc) |
1048 |
* @see org.eclipse.pde.api.tools.model.component.IApiComponent#isSystemComponent() |
1049 |
*/ |
1050 |
public boolean isSystemComponent() { |
1051 |
return false; |
1052 |
} |
1053 |
|
1054 |
/* (non-Javadoc) |
1055 |
* @see IApiComponent#isSourceComponent() |
1056 |
*/ |
1057 |
public synchronized boolean isSourceComponent() throws CoreException { |
1058 |
getManifest(); |
1059 |
if (fManifest == null) { |
1060 |
baselineDisposed(getBaseline()); |
1061 |
} |
1062 |
ManifestElement[] sourceBundle = null; |
1063 |
try { |
1064 |
sourceBundle = ManifestElement.parseHeader(IApiCoreConstants.ECLIPSE_SOURCE_BUNDLE, (String) fManifest.get(IApiCoreConstants.ECLIPSE_SOURCE_BUNDLE)); |
1065 |
} catch (BundleException e) { |
1066 |
// ignore |
1067 |
} |
1068 |
if (sourceBundle != null) { |
1069 |
// this is a source bundle with the new format |
1070 |
return true; |
1071 |
} |
1072 |
// check for the old format |
1073 |
String pluginXMLContents = readFileContents(IApiCoreConstants.PLUGIN_XML_NAME,new File(getLocation())); |
1074 |
if (pluginXMLContents != null) { |
1075 |
if (containsSourceExtensionPoint(pluginXMLContents)) { |
1076 |
return true; |
1077 |
} |
1078 |
} |
1079 |
// check if it contains a fragment.xml with the appropriate extension point |
1080 |
pluginXMLContents = readFileContents(IApiCoreConstants.FRAGMENT_XML_NAME,new File(getLocation())); |
1081 |
if (pluginXMLContents != null) { |
1082 |
if (containsSourceExtensionPoint(pluginXMLContents)) { |
1083 |
return true; |
1084 |
} |
1085 |
} |
1086 |
// parse XML contents to find extension points |
1087 |
return false; |
1088 |
} |
1089 |
|
1090 |
/** |
1091 |
* Check if the given source contains an source extension point. |
1092 |
* |
1093 |
* @param pluginXMLContents the given file contents |
1094 |
* @return true if it contains a source extension point, false otherwise |
1095 |
*/ |
1096 |
private boolean containsSourceExtensionPoint(String pluginXMLContents) { |
1097 |
SAXParserFactory factory = null; |
1098 |
try { |
1099 |
factory = SAXParserFactory.newInstance(); |
1100 |
} catch (FactoryConfigurationError e) { |
1101 |
return false; |
1102 |
} |
1103 |
SAXParser saxParser = null; |
1104 |
try { |
1105 |
saxParser = factory.newSAXParser(); |
1106 |
} catch (ParserConfigurationException e) { |
1107 |
// ignore |
1108 |
} catch (SAXException e) { |
1109 |
// ignore |
1110 |
} |
1111 |
|
1112 |
if (saxParser == null) { |
1113 |
return false; |
1114 |
} |
1115 |
|
1116 |
// Parse |
1117 |
InputSource inputSource = new InputSource(new BufferedReader(new StringReader(pluginXMLContents))); |
1118 |
try { |
1119 |
SourceDefaultHandler defaultHandler = new SourceDefaultHandler(); |
1120 |
saxParser.parse(inputSource, defaultHandler); |
1121 |
return defaultHandler.isSource(); |
1122 |
} catch (SAXException e) { |
1123 |
// ignore |
1124 |
} catch (IOException e) { |
1125 |
// ignore |
1126 |
} |
1127 |
return false; |
1128 |
} |
1129 |
|
1130 |
/* (non-Javadoc) |
1131 |
* @see org.eclipse.pde.api.tools.IApiComponent#isFragment() |
1132 |
*/ |
1133 |
public synchronized boolean isFragment() throws CoreException { |
1134 |
init(); |
1135 |
if (fBundleDescription == null) { |
1136 |
baselineDisposed(getBaseline()); |
1137 |
} |
1138 |
return fBundleDescription.getHost() != null; |
1139 |
} |
1140 |
|
1141 |
/* (non-Javadoc) |
1142 |
* @see org.eclipse.pde.api.tools.internal.model.Component#getHost() |
1143 |
*/ |
1144 |
public synchronized IApiComponent getHost() throws CoreException { |
1145 |
init(); |
1146 |
if (fBundleDescription == null) { |
1147 |
baselineDisposed(getBaseline()); |
1148 |
} |
1149 |
HostSpecification host = fBundleDescription.getHost(); |
1150 |
if(host != null) { |
1151 |
return getBaseline().getApiComponent(host.getName()); |
1152 |
} |
1153 |
return null; |
1154 |
} |
1155 |
|
1156 |
/* (non-Javadoc) |
1157 |
* @see org.eclipse.pde.api.tools.IApiComponent#hasFragments() |
1158 |
*/ |
1159 |
public synchronized boolean hasFragments() throws CoreException { |
1160 |
init(); |
1161 |
if (fBundleDescription == null) { |
1162 |
baselineDisposed(getBaseline()); |
1163 |
} |
1164 |
return fBundleDescription.getFragments().length != 0; |
1165 |
} |
1166 |
|
1167 |
/** |
1168 |
* Sets whether this bundle has an underlying API description file. |
1169 |
* |
1170 |
* @param hasApiDescription whether this bundle has an underlying API description file |
1171 |
*/ |
1172 |
protected void setHasApiDescription(boolean hasApiDescription) { |
1173 |
fHasApiDescription = hasApiDescription; |
1174 |
} |
1175 |
|
1176 |
/* (non-Javadoc) |
1177 |
* @see org.eclipse.pde.api.tools.internal.provisional.IApiComponent#hasApiDescription() |
1178 |
*/ |
1179 |
public boolean hasApiDescription() { |
1180 |
// ensure initialized |
1181 |
try { |
1182 |
getApiDescription(); |
1183 |
} catch (CoreException e) { |
1184 |
} |
1185 |
return fHasApiDescription; |
1186 |
} |
1187 |
|
1188 |
/* (non-Javadoc) |
1189 |
* @see org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent#getLowestEEs() |
1190 |
*/ |
1191 |
public String[] getLowestEEs() throws CoreException { |
1192 |
if (lowestEEs != null) return lowestEEs; |
1193 |
String[] temp = null; |
1194 |
String[] executionEnvironments = getExecutionEnvironments(); |
1195 |
int length = executionEnvironments.length; |
1196 |
switch(length) { |
1197 |
case 0 : |
1198 |
return null; |
1199 |
case 1 : |
1200 |
temp = new String[] { executionEnvironments[0] }; |
1201 |
break; |
1202 |
default : |
1203 |
int values = ProfileModifiers.NO_PROFILE_VALUE; |
1204 |
for (int i = 0; i < length; i++) { |
1205 |
values |= ProfileModifiers.getValue(executionEnvironments[i]); |
1206 |
} |
1207 |
if (ProfileModifiers.isJRE(values)) { |
1208 |
if (ProfileModifiers.isJRE_1_1(values)) { |
1209 |
temp = new String[] { ProfileModifiers.JRE_1_1_NAME }; |
1210 |
} else if (ProfileModifiers.isJ2SE_1_2(values)) { |
1211 |
temp = new String[] { ProfileModifiers.J2SE_1_2_NAME }; |
1212 |
} else if (ProfileModifiers.isJ2SE_1_3(values)) { |
1213 |
temp = new String[] { ProfileModifiers.J2SE_1_3_NAME }; |
1214 |
} else if (ProfileModifiers.isJ2SE_1_4(values)) { |
1215 |
temp = new String[] { ProfileModifiers.J2SE_1_4_NAME }; |
1216 |
} else if (ProfileModifiers.isJ2SE_1_5(values)) { |
1217 |
temp = new String[] { ProfileModifiers.J2SE_1_5_NAME }; |
1218 |
} else { |
1219 |
// this is 1.6 |
1220 |
temp = new String[] { ProfileModifiers.JAVASE_1_6_NAME }; |
1221 |
} |
1222 |
} |
1223 |
if (ProfileModifiers.isCDC_Foundation(values)) { |
1224 |
if (ProfileModifiers.isCDC_1_0_FOUNDATION_1_0(values)) { |
1225 |
if (temp != null) { |
1226 |
temp = new String[] { temp[0], ProfileModifiers.CDC_1_0_FOUNDATION_1_0_NAME }; |
1227 |
} else { |
1228 |
temp = new String[] { ProfileModifiers.CDC_1_0_FOUNDATION_1_0_NAME }; |
1229 |
} |
1230 |
} else { |
1231 |
if (temp != null) { |
1232 |
temp = new String[] { temp[0], ProfileModifiers.CDC_1_1_FOUNDATION_1_1_NAME }; |
1233 |
} else { |
1234 |
temp = new String[] { ProfileModifiers.CDC_1_1_FOUNDATION_1_1_NAME }; |
1235 |
} |
1236 |
} |
1237 |
} |
1238 |
if (ProfileModifiers.isOSGi(values)) { |
1239 |
if (ProfileModifiers.isOSGI_MINIMUM_1_0(values)) { |
1240 |
if (temp != null) { |
1241 |
int tempLength = temp.length; |
1242 |
System.arraycopy(temp, 0, (temp = new String[tempLength + 1]), 0, tempLength); |
1243 |
temp[tempLength] = ProfileModifiers.OSGI_MINIMUM_1_0_NAME; |
1244 |
} else { |
1245 |
temp = new String[] { ProfileModifiers.OSGI_MINIMUM_1_0_NAME }; |
1246 |
} |
1247 |
} else if (ProfileModifiers.isOSGI_MINIMUM_1_1(values)) { |
1248 |
if (temp != null) { |
1249 |
int tempLength = temp.length; |
1250 |
System.arraycopy(temp, 0, (temp = new String[tempLength + 1]), 0, tempLength); |
1251 |
temp[tempLength] = ProfileModifiers.OSGI_MINIMUM_1_1_NAME; |
1252 |
} else { |
1253 |
temp = new String[] { ProfileModifiers.OSGI_MINIMUM_1_1_NAME }; |
1254 |
} |
1255 |
} else { |
1256 |
// OSGI_MINIMUM_1_2 |
1257 |
if (temp != null) { |
1258 |
int tempLength = temp.length; |
1259 |
System.arraycopy(temp, 0, (temp = new String[tempLength + 1]), 0, tempLength); |
1260 |
temp[tempLength] = ProfileModifiers.OSGI_MINIMUM_1_2_NAME; |
1261 |
} else { |
1262 |
temp = new String[] { ProfileModifiers.OSGI_MINIMUM_1_2_NAME }; |
1263 |
} |
1264 |
} |
1265 |
} |
1266 |
} |
1267 |
lowestEEs = temp; |
1268 |
return temp; |
1269 |
} |
1270 |
|
1271 |
/* (non-Javadoc) |
1272 |
* @see org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent#getErrors() |
1273 |
*/ |
1274 |
public synchronized ResolverError[] getErrors() throws CoreException { |
1275 |
init(); |
1276 |
ApiBaseline baseline = (ApiBaseline) getBaseline(); |
1277 |
if (fBundleDescription == null) { |
1278 |
baselineDisposed(baseline); |
1279 |
} |
1280 |
if (baseline != null) { |
1281 |
ResolverError[] resolverErrors = baseline.getState().getResolverErrors(fBundleDescription); |
1282 |
if (resolverErrors.length == 0) { |
1283 |
return null; |
1284 |
} |
1285 |
return resolverErrors; |
1286 |
} |
1287 |
return null; |
1288 |
} |
1289 |
|
1290 |
/** |
1291 |
* @param baseline the baseline that is disposed |
1292 |
* @throws CoreException with the baseline disposed information |
1293 |
*/ |
1294 |
protected void baselineDisposed(IApiBaseline baseline) throws CoreException { |
1295 |
throw new CoreException( |
1296 |
new Status( |
1297 |
IStatus.ERROR, |
1298 |
ApiPlugin.PLUGIN_ID, |
1299 |
ApiPlugin.REPORT_BASELINE_IS_DISPOSED, |
1300 |
NLS.bind(Messages.BundleApiComponent_baseline_disposed, baseline.getName()), |
1301 |
null)); |
1302 |
} |
1303 |
} |