1 11 package org.eclipse.update.internal.core; 12 import java.io.IOException ; 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.FileLocator; 23 import org.eclipse.core.runtime.PluginVersionIdentifier; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.update.configuration.IConfiguredSite; 26 import org.eclipse.update.configurator.IPlatformConfiguration; 27 import org.eclipse.update.core.IFeature; 28 import org.eclipse.update.core.IFeatureReference; 29 import org.eclipse.update.core.IIncludedFeatureReference; 30 import org.eclipse.update.core.Utilities; 31 import org.eclipse.update.core.VersionedIdentifier; 32 import org.eclipse.update.core.model.ModelObject; 33 34 37 38 public class SiteReconciler extends ModelObject { 39 40 43 public SiteReconciler(LocalSite siteLocal) { 44 } 46 47 48 51 52 URL resolveSiteEntry(IPlatformConfiguration.ISiteEntry newSiteEntry) throws CoreException { 53 URL resolvedURL = null; 54 try { 55 resolvedURL = FileLocator.resolve(newSiteEntry.getURL()); 56 } catch (IOException e) { 57 throw Utilities.newCoreException(NLS.bind(Messages.SiteLocal_UnableToResolve, (new String [] { newSiteEntry.getURL().toExternalForm() })), e); 58 } 59 return resolvedURL; 60 } 61 62 63 67 public static void checkConfiguredFeaturesOld(IConfiguredSite configuredSite) throws CoreException { 68 69 71 ConfiguredSite cSite = (ConfiguredSite) configuredSite; 72 IFeatureReference[] configuredFeatures = cSite.getConfiguredFeatures(); 73 ConfigurationPolicy cPolicy = cSite.getConfigurationPolicy(); 74 75 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) { 77 UpdateCore.debug("Compare features within :" + configuredSite.getSite().getURL()); } 79 80 for (int indexConfiguredFeatures = 0; indexConfiguredFeatures < configuredFeatures.length - 1; indexConfiguredFeatures++) { 81 82 IFeatureReference featureToCompare = configuredFeatures[indexConfiguredFeatures]; 83 84 for (int restOfConfiguredFeatures = indexConfiguredFeatures + 1; restOfConfiguredFeatures < configuredFeatures.length; restOfConfiguredFeatures++) { 87 int result = compare(featureToCompare, configuredFeatures[restOfConfiguredFeatures]); 88 if (result != 0) { 89 if (result == 1) { 90 cPolicy.unconfigure(configuredFeatures[restOfConfiguredFeatures], true, false); 91 } 92 if (result == 2) { 93 cPolicy.unconfigure(featureToCompare, true, false); 94 } 95 } 96 } 97 } 98 } 99 100 106 private static int compare(IFeatureReference featureRef1, IFeatureReference featureRef2) throws CoreException { 107 108 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) { 110 UpdateCore.debug("Compare: " + featureRef1 + " && " + featureRef2); } 112 113 if (featureRef1 == null) 114 return 0; 115 116 IFeature feature1 = null; 117 IFeature feature2 = null; 118 try { 119 feature1 = featureRef1.getFeature(null); 120 feature2 = featureRef2.getFeature(null); 121 } catch (CoreException e) { 122 UpdateCore.warn(null, e); 123 return 0; 124 } 125 126 if (feature1 == null || feature2 == null) { 127 return 0; 128 } 129 130 VersionedIdentifier id1 = feature1.getVersionedIdentifier(); 131 VersionedIdentifier id2 = feature2.getVersionedIdentifier(); 132 133 if (id1 == null || id2 == null) { 134 return 0; 135 } 136 137 if (id1.getIdentifier() != null && id1.getIdentifier().equals(id2.getIdentifier())) { 138 PluginVersionIdentifier version1 = id1.getVersion(); 139 PluginVersionIdentifier version2 = id2.getVersion(); 140 if (version1 != null) { 141 if (version1.isGreaterThan(version2)) { 142 return 1; 143 } else { 144 return 2; 145 } 146 } else { 147 return 2; 148 } 149 } 150 return 0; 151 } 152 153 158 public static void checkConfiguredFeatures(IConfiguredSite configuredSite) { 159 160 168 ConfiguredSite cSite = (ConfiguredSite) configuredSite; 170 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) { 172 UpdateCore.debug("Validate configuration of site " + cSite.getSite().getURL()); } 174 IFeatureReference[] configuredRefs = cSite.getConfiguredFeatures(); 175 ArrayList allPossibleConfiguredFeatures = new ArrayList (); 176 for (int i = 0; i < configuredRefs.length; i++) { 177 try { 178 IFeature feature = configuredRefs[i].getFeature(null); 179 allPossibleConfiguredFeatures.add(feature); 180 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) { 182 UpdateCore.debug(" configured feature " + feature.getVersionedIdentifier().toString()); } 184 } catch (CoreException e) { 185 UpdateCore.warn("", e); } 187 } 188 189 ArrayList topFeatures = computeTopFeatures(allPossibleConfiguredFeatures); 191 192 ArrayList topNonEfixFeatures = getNonEfixFeatures(topFeatures); 194 195 ArrayList configuredFeatures = expandFeatures(topNonEfixFeatures, configuredSite); 197 198 if (topFeatures.size() != topNonEfixFeatures.size()) { 201 Map patches = getPatchesAsFeature(allPossibleConfiguredFeatures); 202 if (!patches.isEmpty()) { 203 List efixesToEnable = getPatchesToEnable(patches, configuredFeatures); 205 for (Iterator iter = efixesToEnable.iterator(); iter.hasNext();) { 208 IFeature element = (IFeature) iter.next(); 209 ArrayList expandedEfix = new ArrayList (); 210 expandEfixFeature(element, expandedEfix, configuredSite); 211 configuredFeatures.addAll(expandedEfix); 212 } 213 } 214 } 215 216 ArrayList extras = diff(allPossibleConfiguredFeatures, configuredFeatures); 218 219 ConfigurationPolicy cPolicy = cSite.getConfigurationPolicy(); 221 for (int i = 0; i < extras.size(); i++) { 222 IFeature feature = (IFeature) extras.get(i); 223 IFeatureReference ref = cSite.getSite().getFeatureReference(feature); 224 try { 225 cPolicy.unconfigure(ref, true, false); 226 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) { 228 UpdateCore.debug("Unconfiguring \"extra\" feature " + feature.getVersionedIdentifier().toString()); } 230 } catch (CoreException e) { 231 UpdateCore.warn("", e); } 233 } 234 } 235 236 239 private static ArrayList computeTopFeatures(ArrayList features) { 240 241 Map topFeatures = new HashMap (features.size()); 242 for (Iterator it = features.iterator(); it.hasNext();) { 244 IFeature f = ((IFeature) it.next()); 245 topFeatures.put(f.getVersionedIdentifier(), f); 246 } 247 for (Iterator it = features.iterator(); it.hasNext();) { 249 try { 250 IIncludedFeatureReference[] children = ((IFeature) it.next()).getIncludedFeatureReferences(); 251 for (int j = 0; j < children.length; j++) { 252 try { 253 topFeatures.remove(children[j].getVersionedIdentifier()); 254 } catch (CoreException e1) { 255 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_WARNINGS) 256 UpdateCore.warn("", e1); } 258 } 259 } catch (CoreException e) { 260 UpdateCore.warn("", e); } 262 } 263 ArrayList list = new ArrayList (); 264 list.addAll(topFeatures.values()); 265 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) { 267 UpdateCore.debug("Computed top-level features"); for (int i = 0; i < topFeatures.size(); i++) { 269 UpdateCore.debug(" " + ((IFeature) list.get(i)).getVersionedIdentifier().toString()); } 271 } 272 return list; 273 } 274 275 278 private static ArrayList expandFeatures(ArrayList features, IConfiguredSite configuredSite) { 279 ArrayList result = new ArrayList (); 280 281 for (int i = 0; i < features.size(); i++) { 283 expandFeature((IFeature) features.get(i), result, configuredSite); 284 } 285 286 return result; 287 } 288 289 292 private static void expandFeature(IFeature feature, ArrayList features, IConfiguredSite configuredSite) { 293 294 if (!features.contains(feature)) { 296 features.add(feature); 297 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) { 299 UpdateCore.debug("Retaining configured feature " + feature.getVersionedIdentifier().toString()); } 301 } 302 303 IIncludedFeatureReference[] children = null; 305 try { 306 children = feature.getIncludedFeatureReferences(); 307 } catch (CoreException e) { 308 UpdateCore.warn("", e); return; 310 } 311 312 for (int j = 0; j < children.length; j++) { 313 IFeature child = null; 314 try { 315 child = children[j].getFeature(null); 316 } catch (CoreException e) { 317 if (!UpdateManagerUtils.isOptional(children[j])) 318 UpdateCore.warn("", e); } 321 if (child != null) 322 expandFeature(child, features, configuredSite); 323 } 324 } 325 326 329 private static ArrayList diff(ArrayList left, ArrayList right) { 330 ArrayList result = new ArrayList (); 331 332 for (int i = 0; i < left.size(); i++) { 334 IFeature feature = (IFeature) left.get(i); 335 if (!right.contains(feature)) 336 result.add(feature); 337 } 338 return result; 339 } 340 341 344 private static Map getPatchesAsFeature(ArrayList allConfiguredFeatures) { 345 Map patches = new HashMap (); 347 if (allConfiguredFeatures != null) { 348 Iterator iter = allConfiguredFeatures.iterator(); 349 while (iter.hasNext()) { 350 List patchedFeaturesID = new ArrayList (); 351 IFeature element = (IFeature) iter.next(); 352 for (int i = 0; i < element.getImports().length; i++) { 354 if (element.getImports()[i].isPatch()) { 355 VersionedIdentifier id = element.getImports()[i].getVersionedIdentifier(); 356 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) 357 UpdateCore.debug("Found patch " + element + " for feature identifier " + id); patchedFeaturesID.add(id); 359 } 360 } 361 362 if (!patchedFeaturesID.isEmpty()) { 363 patches.put(element, patchedFeaturesID); 364 } 365 } 366 } 367 368 return patches; 369 } 370 371 374 private static List getPatchesToEnable(Map efixes, ArrayList configuredFeatures) { 375 376 ArrayList enabledVersionedIdentifier = new ArrayList (); 377 Iterator iter = configuredFeatures.iterator(); 378 while (iter.hasNext()) { 379 IFeature element = (IFeature) iter.next(); 380 enabledVersionedIdentifier.add(element.getVersionedIdentifier()); 381 } 382 383 List result = new ArrayList (); 385 iter = efixes.keySet().iterator(); 386 while (iter.hasNext()) { 387 boolean toEnable = false; 388 IFeature efixFeature = (IFeature) iter.next(); 389 List patchedFeatures = (List ) efixes.get(efixFeature); 390 Iterator patchedFeaturesIter = patchedFeatures.iterator(); 393 while (patchedFeaturesIter.hasNext() && !toEnable) { 394 VersionedIdentifier patchedFeatureID = (VersionedIdentifier) patchedFeaturesIter.next(); 395 if (enabledVersionedIdentifier.contains(patchedFeatureID)) { 396 toEnable = true; 397 } 398 } 399 400 if (!toEnable) { 401 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) 402 UpdateCore.debug("The Patch " + efixFeature + " does not patch any enabled features: it will be disabled"); } else { 404 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) 405 UpdateCore.debug("The patch " + efixFeature + " will be enabled."); result.add(efixFeature); 407 } 408 } 409 return result; 410 } 411 412 415 private static ArrayList getNonEfixFeatures(ArrayList topFeatures) { 416 Map efixFeatures = getPatchesAsFeature(topFeatures); 417 Set keySet = efixFeatures.keySet(); 418 if (keySet == null || keySet.isEmpty()) 419 return topFeatures; 420 421 Iterator iter = topFeatures.iterator(); 422 ArrayList result = new ArrayList (); 423 while (iter.hasNext()) { 424 IFeature element = (IFeature) iter.next(); 425 if (!keySet.contains(element)) { 426 result.add(element); 427 } 428 } 429 return result; 430 } 431 432 433 434 435 438 private static void expandEfixFeature(IFeature feature, ArrayList features, IConfiguredSite configuredSite) { 439 440 if (!features.contains(feature)) { 442 features.add(feature); 443 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) { 445 UpdateCore.debug("Retaining configured feature " + feature.getVersionedIdentifier().toString()); } 447 } 448 449 IIncludedFeatureReference[] children = null; 451 try { 452 children = feature.getIncludedFeatureReferences(); 453 } catch (CoreException e) { 454 UpdateCore.warn("", e); return; 456 } 457 458 for (int j = 0; j < children.length; j++) { 459 IFeature child = null; 460 try { 461 child = children[j].getFeature(null); 462 } catch (CoreException e) { 463 if (!children[j].isOptional()) 464 UpdateCore.warn("", e); } 467 if (child != null){ 468 if (!UpdateCore.isPatch(child)) 469 expandEfixFeature(child, features, configuredSite); 470 } 471 } 472 } 473 474 } 475 | Popular Tags |