KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > site > BuildTimeSite


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build.site;
12
13 import java.io.*;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.service.resolver.*;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.pde.internal.build.*;
21 import org.eclipse.update.core.*;
22 import org.osgi.framework.Version;
23
24 /**
25  * This site represent a site at build time. A build time site is made of code
26  * to compile, and a potential installation of eclipse (or derived products)
27  * against which the code must be compiled. Moreover this site provide access to
28  * a pluginRegistry.
29  */

30 public class BuildTimeSite extends Site implements ISite, IPDEBuildConstants, IXMLConstants {
31     private PDEState state;
32     private Properties repositoryVersions; //version for the features
33
private boolean reportResolutionErrors;
34     private Properties platformProperties;
35
36     //Support for filtering what is added to the state
37
private List rootFeaturesForFilter;
38     private List rootPluginsForFiler;
39     private boolean filter = false;
40
41     public void setReportResolutionErrors(boolean value) {
42         reportResolutionErrors = value;
43     }
44     
45     public void setPlatformPropeties(Properties platformProperties) {
46         this.platformProperties = platformProperties;
47     }
48
49     public Properties getFeatureVersions() {
50         if (repositoryVersions == null) {
51             repositoryVersions = new Properties();
52             try {
53                 InputStream input = new BufferedInputStream(new FileInputStream(AbstractScriptGenerator.getWorkingDirectory() + '/' + DEFAULT_FEATURE_REPOTAG_FILENAME_DESCRIPTOR));
54                 try {
55                     repositoryVersions.load(input);
56                 } finally {
57                     input.close();
58                 }
59             } catch (IOException e) {
60                 //Ignore
61
}
62         }
63         return repositoryVersions;
64     }
65
66     public PDEState getRegistry() throws CoreException {
67         if (state == null) {
68             // create the registry according to the site where the code to
69
// compile is, and a existing installation of eclipse
70
BuildTimeSiteContentProvider contentProvider = (BuildTimeSiteContentProvider) getSiteContentProvider();
71
72             if (contentProvider.getInitialState() != null) {
73                 state = new PDEState(contentProvider.getInitialState());
74                 return state;
75             }
76
77             if (AbstractScriptGenerator.isBuildingOSGi()) {
78                 if (filter) {
79                     state = new FilteringState();
80                     ((FilteringState) state).setFilter(findAllReferencedPlugins());
81                 } else {
82                     state = new PDEState();
83                 }
84                 if (platformProperties != null)
85                     state.setPlatformProperties(platformProperties);
86             } else {
87                 state = new PluginRegistryConverter();
88             }
89             state.addBundles(contentProvider.getPluginPaths());
90
91             //Once all the elements have been added to the state, the filter is removed to allow for the generated plug-ins to be added
92
if (state instanceof FilteringState) {
93                 ((FilteringState) state).setFilter(null);
94             }
95             state.resolveState();
96             BundleDescription[] allBundles = state.getState().getBundles();
97             BundleDescription[] resolvedBundles = state.getState().getResolvedBundles();
98             if (allBundles.length == resolvedBundles.length)
99                 return state;
100
101             if (reportResolutionErrors) {
102                 MultiStatus errors = new MultiStatus(IPDEBuildConstants.PI_PDEBUILD, 1, Messages.exception_registryResolution, null);
103                 BundleDescription[] all = state.getState().getBundles();
104                 StateHelper helper = Platform.getPlatformAdmin().getStateHelper();
105                 for (int i = 0; i < all.length; i++) {
106                     if (!all[i].isResolved()) {
107                         ResolverError[] resolutionErrors = state.getState().getResolverErrors(all[i]);
108                         VersionConstraint[] versionErrors = helper.getUnsatisfiedConstraints(all[i]);
109
110                         //ignore problems when they are caused by bundles not being built for the right config
111
if (isConfigError(all[i], resolutionErrors, AbstractScriptGenerator.getConfigInfos()))
112                             continue;
113
114                         String JavaDoc errorMessage = "Bundle " + all[i].getSymbolicName() + ":\n" + getResolutionErrorMessage(resolutionErrors);
115                         for (int j = 0; j < versionErrors.length; j++) {
116                             errorMessage += '\t' + getResolutionFailureMessage(versionErrors[j]) + '\n';
117                         }
118                         errors.add(new Status(IStatus.WARNING, IPDEBuildConstants.PI_PDEBUILD, IStatus.WARNING, errorMessage, null));
119                     }
120                 }
121                 if (errors.getChildren().length > 0)
122                     BundleHelper.getDefault().getLog().log(errors);
123             }
124         }
125         if (!state.getState().isResolved())
126             state.state.resolve(true);
127         return state;
128     }
129
130     //Return whether the resolution error is caused because we are not building for the proper configurations.
131
private boolean isConfigError(BundleDescription bundle, ResolverError[] errors, List configs) {
132         Dictionary environment = new Hashtable(3);
133         String JavaDoc filterSpec = bundle.getPlatformFilter();
134         if (hasPlatformFilterError(errors) != null) {
135             for (Iterator iter = configs.iterator(); iter.hasNext();) {
136                 Config aConfig = (Config) iter.next();
137                 environment.put("osgi.os", aConfig.getOs()); //$NON-NLS-1$
138
environment.put("osgi.ws", aConfig.getWs()); //$NON-NLS-1$
139
environment.put("osgi.arch", aConfig.getArch()); //$NON-NLS-1$
140
if (BundleHelper.getDefault().createFilter(filterSpec).match(environment)) {
141                     return false;
142                 }
143             }
144             return true;
145         }
146         return false;
147     }
148
149     //Check if the set of errors contain a platform filter
150
private ResolverError hasPlatformFilterError(ResolverError[] errors) {
151         for (int i = 0; i < errors.length; i++) {
152             if ((errors[i].getType() & ResolverError.PLATFORM_FILTER) != 0)
153                 return errors[i];
154         }
155         return null;
156     }
157
158     private String JavaDoc getResolutionErrorMessage(ResolverError[] errors) {
159         String JavaDoc errorMessage = ""; //$NON-NLS-1$
160
for (int i = 0; i < errors.length; i++) {
161             if ((errors[i].getType() & (ResolverError.SINGLETON_SELECTION | ResolverError.FRAGMENT_CONFLICT | ResolverError.IMPORT_PACKAGE_USES_CONFLICT | ResolverError.REQUIRE_BUNDLE_USES_CONFLICT | ResolverError.MISSING_EXECUTION_ENVIRONMENT)) != 0)
162                 errorMessage += '\t' + errors[i].toString() + '\n';
163         }
164         return errorMessage;
165     }
166
167     public String JavaDoc getResolutionFailureMessage(VersionConstraint unsatisfied) {
168         if (unsatisfied.isResolved())
169             throw new IllegalArgumentException JavaDoc();
170         if (unsatisfied instanceof ImportPackageSpecification)
171             return NLS.bind(Messages.unsatisfied_import, displayVersionConstraint(unsatisfied));
172         if (unsatisfied instanceof BundleSpecification) {
173             if (((BundleSpecification) unsatisfied).isOptional())
174                 return NLS.bind(Messages.unsatisfied_optionalBundle, displayVersionConstraint(unsatisfied));
175             return NLS.bind(Messages.unsatisfied_required, displayVersionConstraint(unsatisfied));
176         }
177         return NLS.bind(Messages.unsatisfied_host, displayVersionConstraint(unsatisfied));
178     }
179
180     private String JavaDoc displayVersionConstraint(VersionConstraint constraint) {
181         VersionRange versionSpec = constraint.getVersionRange();
182         if (versionSpec == null)
183             return constraint.getName();
184         return constraint.getName() + '_' + versionSpec;
185     }
186
187     public IFeature findFeature(String JavaDoc featureId, String JavaDoc versionId, boolean throwsException) throws CoreException {
188         ISiteFeatureReference[] features = getFeatureReferences();
189         if (GENERIC_VERSION_NUMBER.equals(versionId))
190             versionId = null;
191         for (int i = 0; i < features.length; i++) {
192             IFeature verifiedFeature;
193             try {
194                 verifiedFeature = features[i].getFeature(null);
195             } catch (CoreException e) {
196                 String JavaDoc message = NLS.bind(Messages.exception_featureParse, features[i].getURL());
197                 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_MISSING, message, null));
198             }
199             if (verifiedFeature.getVersionedIdentifier().getIdentifier().equals(featureId))
200                 if (versionId == null || features[i].getVersionedIdentifier().getVersion().equals(new PluginVersionIdentifier(versionId)))
201                     return features[i].getFeature(null);
202         }
203         int qualifierIdx = -1;
204         if (versionId != null && (((qualifierIdx = versionId.indexOf('.' + IBuildPropertiesConstants.PROPERTY_QUALIFIER)) != -1) || ((qualifierIdx = versionId.indexOf(IBuildPropertiesConstants.PROPERTY_QUALIFIER)) != -1))) {
205             Version versionToMatch = Version.parseVersion(versionId.substring(0, qualifierIdx));
206             for (int i = 0; i < features.length; i++) {
207                 Version featureVersion = Version.parseVersion(features[i].getVersionedIdentifier().getVersion().toString());
208                 if (features[i].getVersionedIdentifier().getIdentifier().equals(featureId) && featureVersion.getMajor() == versionToMatch.getMajor() && featureVersion.getMinor() == versionToMatch.getMinor() && featureVersion.getMicro() >= versionToMatch.getMicro() && featureVersion.getQualifier().compareTo(versionToMatch.getQualifier()) >= 0)
209                     return features[i].getFeature(null);
210             }
211         }
212         if (throwsException) {
213             String JavaDoc message = NLS.bind(Messages.exception_missingFeature, featureId);
214             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_MISSING, message, null));
215         }
216         return null;
217     }
218
219     public void addFeatureReferenceModel(File featureXML) {
220         URL JavaDoc featureURL;
221         SiteFeatureReferenceModel featureRef;
222         if (featureXML.exists()) {
223             // Here we could not use toURL() on currentFeatureDir, because the
224
// URL has a slash after the colons (file:/c:/foo) whereas the
225
// plugins don't
226
// have it (file:d:/eclipse/plugins) and this causes problems later
227
// to compare URLs... and compute relative paths
228
try {
229                 featureURL = new URL JavaDoc("file:" + featureXML.getAbsolutePath() + '/'); //$NON-NLS-1$
230
featureRef = new SiteFeatureReference();
231                 featureRef.setSiteModel(this);
232                 featureRef.setURLString(featureURL.toExternalForm());
233                 featureRef.setType(BuildTimeFeatureFactory.BUILDTIME_FEATURE_FACTORY_ID);
234                 addFeatureReferenceModel(featureRef);
235             } catch (MalformedURLException JavaDoc e) {
236                 BundleHelper.getDefault().getLog().log(new Status(IStatus.WARNING, PI_PDEBUILD, WARNING_MISSING_SOURCE, NLS.bind(Messages.warning_cannotLocateSource, featureXML.getAbsolutePath()), e));
237             }
238         }
239     }
240
241     private SortedSet findAllReferencedPlugins() throws CoreException {
242         ArrayList rootFeatures = new ArrayList();
243         SortedSet allPlugins = new TreeSet();
244         for (Iterator iter = rootFeaturesForFilter.iterator(); iter.hasNext();) {
245             IFeature correspondingFeature = findFeature((String JavaDoc) iter.next(), null, true);
246             if (correspondingFeature == null)
247                 return null;
248             rootFeatures.add(correspondingFeature);
249         }
250         for (Iterator iter = rootPluginsForFiler.iterator(); iter.hasNext();) {
251             allPlugins.add(new ReachablePlugin((String JavaDoc) iter.next(), ReachablePlugin.WIDEST_RANGE));
252         }
253         int it = 0;
254         while (it < rootFeatures.size()) {
255             IFeature toAnalyse = null;
256             try {
257                 toAnalyse = (IFeature) rootFeatures.get(it++);
258             } catch (RuntimeException JavaDoc e) {
259                 e.printStackTrace();
260             }
261             IIncludedFeatureReference[] includedRefs = toAnalyse.getIncludedFeatureReferences();
262             for (int i = 0; i < includedRefs.length; i++) {
263                 rootFeatures.add(findFeature(includedRefs[i].getVersionedIdentifier().getIdentifier(), includedRefs[i].getVersionedIdentifier().getVersion().toString(), true));
264             }
265             IPluginEntry[] entries = toAnalyse.getPluginEntries();
266             for (int i = 0; i < entries.length; i++) {
267                 allPlugins.add(new ReachablePlugin(entries[i]));
268             }
269             IImport[] imports = toAnalyse.getImports();
270             for (int i = 0; i < imports.length; i++) {
271                 if (((Import) imports[i]).isFeatureImport()) {
272                     VersionedIdentifier requiredFeature = imports[i].getVersionedIdentifier();
273                     rootFeatures.add(findFeature(requiredFeature.getIdentifier(), requiredFeature.getVersion().toString(), true));
274                 } else {
275                     allPlugins.add(new ReachablePlugin(imports[i]));
276                 }
277             }
278         }
279         return allPlugins;
280     }
281
282     public void setFilter(boolean filter) {
283         this.filter = filter;
284     }
285
286     public void setRootFeaturesForFilter(List rootFeaturesForFilter) {
287         this.rootFeaturesForFilter = rootFeaturesForFilter;
288     }
289
290     public void setRootPluginsForFiler(List rootPluginsForFiler) {
291         this.rootPluginsForFiler = rootPluginsForFiler;
292     }
293 }
294
Popular Tags