1 11 package org.eclipse.pde.internal.build.site; 12 13 import java.io.*; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 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 30 public class BuildTimeSite extends Site implements ISite, IPDEBuildConstants, IXMLConstants { 31 private PDEState state; 32 private Properties repositoryVersions; private boolean reportResolutionErrors; 34 private Properties platformProperties; 35 36 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 } 62 } 63 return repositoryVersions; 64 } 65 66 public PDEState getRegistry() throws CoreException { 67 if (state == null) { 68 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 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 if (isConfigError(all[i], resolutionErrors, AbstractScriptGenerator.getConfigInfos())) 112 continue; 113 114 String 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 private boolean isConfigError(BundleDescription bundle, ResolverError[] errors, List configs) { 132 Dictionary environment = new Hashtable(3); 133 String 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()); environment.put("osgi.ws", aConfig.getWs()); environment.put("osgi.arch", aConfig.getArch()); if (BundleHelper.getDefault().createFilter(filterSpec).match(environment)) { 141 return false; 142 } 143 } 144 return true; 145 } 146 return false; 147 } 148 149 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 getResolutionErrorMessage(ResolverError[] errors) { 159 String errorMessage = ""; 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 getResolutionFailureMessage(VersionConstraint unsatisfied) { 168 if (unsatisfied.isResolved()) 169 throw new IllegalArgumentException (); 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 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 featureId, String 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 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 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 featureURL; 221 SiteFeatureReferenceModel featureRef; 222 if (featureXML.exists()) { 223 try { 229 featureURL = new URL ("file:" + featureXML.getAbsolutePath() + '/'); 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 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 ) 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 ) 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 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 |