1 11 package org.eclipse.update.internal.core; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.osgi.util.NLS; 18 import org.eclipse.update.configuration.*; 19 import org.eclipse.update.core.*; 20 import org.osgi.framework.*; 21 import org.osgi.service.packageadmin.*; 22 23 26 27 public class SiteStatusAnalyzer { 28 29 private static List allConfiguredFeatures; 30 private LocalSite siteLocal; 31 32 35 public SiteStatusAnalyzer(LocalSite siteLocal) { 36 this.siteLocal = siteLocal; 37 } 38 39 42 public class PluginIdentifier { 43 private VersionedIdentifier id; 44 private String label; 45 private boolean isFragment = false; 46 47 public PluginIdentifier(VersionedIdentifier id, String label, boolean fragment) { 48 this.id = id; 49 this.label = label; 50 this.isFragment = fragment; 51 } 52 53 public VersionedIdentifier getVersionedIdentifier() { 54 return id; 55 } 56 57 public boolean isFragment() { 58 return isFragment; 59 } 60 61 public String getLabel() { 62 return label; 63 } 64 } 65 72 private IStatus getStatus(IFeature feature) { 73 74 ISite featureSite = feature.getSite(); 76 if (featureSite == null) { 77 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION) 78 UpdateCore.debug("Cannot determine status of feature:" + feature.getLabel() + ". Site is NULL."); String msg = NLS.bind(Messages.SiteLocal_UnableToDetermineFeatureStatusSiteNull, (new Object [] { feature.getURL()})); 80 return createStatus(IStatus.ERROR, IFeature.STATUS_AMBIGUOUS, msg, null); 81 } 82 83 ConfiguredSite cSite = (ConfiguredSite) featureSite.getCurrentConfiguredSite(); 85 if (cSite == null) { 86 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION) 87 UpdateCore.warn("Cannot determine status of feature: " + feature.getLabel() + ". Configured Site is NULL."); String msg = NLS.bind(Messages.SiteLocal_UnableToDetermineFeatureStatusConfiguredSiteNull, (new Object [] { feature.getURL()})); 89 return createStatus(IStatus.ERROR, IFeature.STATUS_AMBIGUOUS, msg, null); 90 } 91 92 IFeatureReference ref = cSite.getSite().getFeatureReference(feature); 94 if (ref != null) { 95 if (!cSite.getConfigurationPolicy().isConfigured(ref)) 96 return createStatus(IStatus.OK, IFeature.STATUS_DISABLED, "", null); } else { 98 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION) 99 UpdateCore.warn("Unable to find reference for feature " + feature + " in site " + cSite.getSite().getURL()); } 101 102 IStatus status = cSite.getBrokenStatus(feature); 104 if (status.getSeverity() != IStatus.OK) { 105 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION) 106 UpdateCore.debug("Feature broken:" + feature.getLabel() + ".Site:" + cSite.toString()); return status; 108 } 109 110 IPluginEntry[] featuresEntries = feature.getPluginEntries(); 112 return status( feature, featuresEntries); 113 } 114 115 122 public IStatus getFeatureStatus(IFeature feature) throws CoreException { 123 124 IFeature childFeature = null; 125 IStatus childStatus; 126 127 IFeatureReference[] children = feature.getIncludedFeatureReferences(); 128 129 String msg = Messages.SiteLocal_FeatureDisable; 132 int code = IFeature.STATUS_DISABLED; 133 IStatus featureStatus = getStatus(feature); 134 MultiStatus multiTemp = new MultiStatus(featureStatus.getPlugin(), code, msg, null); 135 if (featureStatus.getSeverity() == IStatus.ERROR) { 136 if (featureStatus.isMultiStatus()) { 137 multiTemp.addAll(featureStatus); 138 } else { 139 multiTemp.add(featureStatus); 140 } 141 } 142 if (featureStatus.getCode() > code) 144 code = featureStatus.getCode(); 145 146 if (!(code == IFeature.STATUS_DISABLED)) { 148 for (int i = 0; i < children.length; i++) { 149 if (!UpdateManagerUtils.isOptional(children[i])) { 150 try { 151 childFeature = children[i].getFeature(null); 152 } catch (CoreException e) { 153 childFeature = null; 154 if (!UpdateManagerUtils.isOptional(children[i])) 155 UpdateCore.warn("Error retrieving feature:" + children[i]); } 157 158 if (childFeature == null) { 159 UpdateCore.warn("getFeatureStatus: Feature is null for:" + children[i]); Object featureAsPrintableObject = children[i].getURL(); 162 featureAsPrintableObject = children[i].getVersionedIdentifier(); 163 String msg1 = NLS.bind(Messages.SiteLocal_NestedFeatureUnavailable, (new Object [] { featureAsPrintableObject })); 164 multiTemp.add(createStatus(IStatus.ERROR, IFeature.STATUS_UNHAPPY, msg1, null)); 165 if (IFeature.STATUS_UNHAPPY > code) 166 code = IFeature.STATUS_UNHAPPY; 167 } else { 168 childStatus = getFeatureStatus(childFeature); 169 if (childStatus.getCode() == IFeature.STATUS_DISABLED) { 172 VersionedIdentifier versionID = childFeature.getVersionedIdentifier(); 173 String featureVer = (versionID == null) ? "" : versionID.getVersion().toString(); String msg1 = NLS.bind(Messages.SiteLocal_NestedFeatureDisable, (new String [] { childFeature.getLabel(), featureVer })); 175 multiTemp.add(createStatus(IStatus.ERROR, childStatus.getCode(), msg1, null)); 176 if (IFeature.STATUS_UNHAPPY > code) 177 code = IFeature.STATUS_UNHAPPY; 178 } 179 if (childStatus.getSeverity() != IStatus.OK) { 180 VersionedIdentifier versionID = childFeature.getVersionedIdentifier(); 181 String featureVer = (versionID == null) ? "" : versionID.getVersion().toString(); String msg1 = NLS.bind(Messages.SiteLocal_NestedFeatureUnHappy, (new String [] { childFeature.getLabel(), featureVer })); 183 multiTemp.add(createStatus(IStatus.ERROR, childStatus.getCode(), msg1, null)); 184 if (childStatus.getCode() > code) 185 code = childStatus.getCode(); 186 } 187 } 188 } 189 } 190 } 191 192 switch (code) { 194 case IFeature.STATUS_HAPPY : 195 msg = Messages.SiteLocal_FeatureHappy; 196 break; 197 case IFeature.STATUS_UNHAPPY : 198 msg = Messages.SiteLocal_FeatureUnHappy; 199 break; 200 case IFeature.STATUS_AMBIGUOUS : 201 msg = Messages.SiteLocal_FeatureAmbiguous; 202 break; 203 case IFeature.STATUS_DISABLED : 204 msg = Messages.SiteLocal_FeatureDisable; 205 break; 206 default : 207 msg = Messages.SiteLocal_FeatureStatusUnknown; 208 break; 209 } 210 MultiStatus multi = new MultiStatus(featureStatus.getPlugin(), code, msg, null); 211 multi.addAll(multiTemp); 212 return multi; 213 } 214 215 218 private IStatus status(IFeature pluginsOriginatorFeature, IPluginEntry[] featurePlugins) { 219 VersionedIdentifier featurePluginID; 220 221 String happyMSG = Messages.SiteLocal_FeatureHappy; 222 String ambiguousMSG = Messages.SiteLocal_FeatureAmbiguous; 223 IStatus featureStatus = createStatus(IStatus.OK, IFeature.STATUS_HAPPY, "", null); MultiStatus multi = new MultiStatus(featureStatus.getPlugin(), IFeature.STATUS_AMBIGUOUS, ambiguousMSG, null); 225 PackageAdmin pkgAdmin = UpdateCore.getPlugin().getPackageAdmin(); 226 227 for (int i = 0; i < featurePlugins.length; i++) { 230 MultiStatus tempmulti = new MultiStatus(featureStatus.getPlugin(), IFeature.STATUS_AMBIGUOUS, ambiguousMSG, null); 231 featurePluginID = featurePlugins[i].getVersionedIdentifier(); 232 boolean found = false; 233 234 String singleVersionRange = '[' + featurePluginID.getVersion().toString() + ',' + featurePluginID.getVersion().toString() + ']'; 235 Bundle[] bundles = pkgAdmin.getBundles(featurePluginID.getIdentifier(), singleVersionRange); 236 if (bundles != null && bundles.length == 1) { 237 found = true; 238 continue; 239 } 240 241 bundles = pkgAdmin.getBundles(featurePluginID.getIdentifier(), null); 244 for (int j = 0; bundles != null && j < bundles.length && !found; j++ ) { 245 String bundleVersion = (String )bundles[j].getHeaders().get(Constants.BUNDLE_VERSION); 246 IFeature feature = getFeatureForId(new VersionedIdentifier(bundles[j].getSymbolicName(), bundleVersion )); 247 if ((feature != null) && (!isFeaturePatchOfThisFeature(pluginsOriginatorFeature, feature))) { 248 String msg = null; 249 if (feature == null) { 250 Object [] values = new Object [] {bundles[j].getSymbolicName(), featurePluginID.getVersion(), bundleVersion}; 251 msg = NLS.bind(Messages.SiteLocal_TwoVersionSamePlugin1, values); 252 } else { 253 String label = feature.getLabel(); 254 String featureVersion = feature.getVersionedIdentifier().getVersion().toString(); 255 Object [] values = new Object [] { bundles[j].getSymbolicName(), featurePluginID.getVersion(), bundleVersion, label, featureVersion }; 256 msg = NLS.bind(Messages.SiteLocal_TwoVersionSamePlugin2, values); 257 } 258 259 UpdateCore.warn("Found another version of the same plugin on the path:" + bundles[j].getSymbolicName() + " " + bundleVersion); tempmulti.add(createStatus(IStatus.ERROR, IFeature.STATUS_AMBIGUOUS, msg, null)); 261 } else { 262 found = true; 263 } 264 265 } 266 267 268 if (!found) { 272 if (tempmulti.getChildren().length > 0) { 273 multi.addAll(tempmulti); 274 } else { 275 if (multi.getCode() != IFeature.STATUS_UNHAPPY) { 276 String unhappyMSG = Messages.SiteLocal_FeatureUnHappy; 277 MultiStatus newMulti = new MultiStatus(featureStatus.getPlugin(), IFeature.STATUS_UNHAPPY, unhappyMSG, null); 278 newMulti.addAll(multi); 279 multi = newMulti; 280 } 281 String msg = NLS.bind(Messages.SiteLocal_NoPluginVersion, (new String [] { featurePluginID.getIdentifier() })); 282 multi.add(createStatus(IStatus.ERROR, IFeature.STATUS_UNHAPPY, msg, null)); 283 } 284 } 285 } 286 287 if (!multi.isOK()) 288 return multi; 289 290 return createStatus(IStatus.OK, IFeature.STATUS_HAPPY, happyMSG, null); 292 } 293 private boolean isFeaturePatchOfThisFeature(IFeature pluginsOriginatorFeature, IFeature feature) { 294 295 if (!feature.isPatch()) 296 return false; 297 298 IImport[] featureImports = feature.getImports(); 299 300 if (featureImports == null) { 301 return false; 302 } 303 304 for(int i = 0; i < featureImports.length; i++) { 305 if (featureImports[i].isPatch() && featureImports[i].getVersionedIdentifier().equals(pluginsOriginatorFeature.getVersionedIdentifier())) { 306 return true; 307 } 308 } 309 return false; 310 } 311 312 315 private IStatus createStatus(int statusSeverity, int statusCode, String msg, Exception e) { 316 String id = UpdateCore.getPlugin().getBundle().getSymbolicName(); 317 318 StringBuffer completeString = new StringBuffer (""); if (msg != null) 320 completeString.append(msg); 321 if (e != null) { 322 completeString.append("\r\n["); completeString.append(e.toString()); 324 completeString.append("]\r\n"); } 326 return new Status(statusSeverity, id, statusCode, completeString.toString(), e); 327 } 328 329 330 333 private IFeature[] getAllConfiguredFeatures() { 334 if (allConfiguredFeatures == null) { 335 336 allConfiguredFeatures = new ArrayList (); 337 IConfiguredSite[] allConfiguredSites = siteLocal.getCurrentConfiguration().getConfiguredSites(); 338 339 for (int i = 0; i < allConfiguredSites.length; i++) { 340 IFeatureReference[] refs = allConfiguredSites[i].getConfiguredFeatures(); 341 IFeature feature = null; 342 for (int j = 0; j < refs.length; j++) { 343 feature = null; 344 try { 345 feature = refs[j].getFeature(null); 346 } catch (CoreException e) { 347 } 348 if (feature != null) { 349 allConfiguredFeatures.add(feature); 350 } 351 } 352 } 353 } 354 355 IFeature[] features = new IFeature[allConfiguredFeatures.size()]; 356 if (allConfiguredFeatures.size() > 0) { 357 allConfiguredFeatures.toArray(features); 358 } 359 return features; 360 } 361 362 365 private IFeature getFeatureForId(VersionedIdentifier id) { 366 367 if (id == null) 368 return null; 369 370 IFeature[] allFeatures = getAllConfiguredFeatures(); 371 IFeature currentFeature = null; 372 IPluginEntry[] allPlugins = null; 373 IPluginEntry currentPlugin = null; 374 for (int i = 0; i < allFeatures.length; i++) { 375 currentFeature = allFeatures[i]; 376 allPlugins = currentFeature.getPluginEntries(); 377 for (int j = 0; j < allPlugins.length; j++) { 378 currentPlugin = allPlugins[j]; 379 if (id.equals(currentPlugin.getVersionedIdentifier())) 380 return currentFeature; 381 } 382 } 383 return null; 384 } 385 } 386 | Popular Tags |