1 11 package org.eclipse.update.internal.core; 12 import java.io.*; 13 import java.net.*; 14 import java.util.Arrays ; 15 import java.util.Collection ; 16 import java.util.Date ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.update.configuration.*; 25 import org.eclipse.update.configurator.*; 26 import org.eclipse.update.core.*; 27 import org.eclipse.update.core.model.*; 28 import org.eclipse.update.internal.model.*; 29 30 33 public class ConfigurationPolicy extends ConfigurationPolicyModel { 34 35 38 public ConfigurationPolicy() { 39 } 40 41 44 public ConfigurationPolicy(ConfigurationPolicy configPolicy) { 45 super(); 46 setPolicy(configPolicy.getPolicy()); 47 setConfiguredFeatureReferences(configPolicy.getConfiguredFeatures()); 48 setUnconfiguredFeatureReferences(configPolicy.getUnconfiguredFeatures()); 49 setConfiguredSiteModel(configPolicy.getConfiguredSiteModel()); 50 } 51 52 55 private boolean isUnconfigured(IFeatureReference featureReference) { 56 57 if (featureReference == null) 58 return false; 59 60 IFeatureReference[] refs = getUnconfiguredFeatures(); 62 for (int i = 0; i < refs.length; i++) { 63 if (featureReference.equals(refs[i])) { 64 return true; 65 } 66 } 67 return false; 68 } 69 70 73 public boolean isConfigured(IFeatureReference featureReference) { 74 75 if (featureReference == null) 76 return false; 77 78 IFeatureReference[] refs = getConfiguredFeatures(); 80 for (int i = 0; i < refs.length; i++) { 81 if (featureReference.equals(refs[i])) { 82 return true; 83 } 84 } 85 return false; 86 } 87 88 91 public void configure(IFeatureReference featureReference, boolean callInstallHandler, boolean createActivity) throws CoreException { 92 93 if (isConfigured(featureReference)) return; 95 96 if (featureReference == null) { 97 UpdateCore.warn("The feature reference to configure is null"); return; 99 } 100 101 IFeature feature = null; 102 try { 103 feature = featureReference.getFeature(null); 104 } catch (CoreException e) { 105 if (!UpdateManagerUtils.isOptional(featureReference)) { 106 URL url = featureReference.getURL(); 107 String urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; UpdateCore.warn("Error retrieving feature:" + urlString, e); return; 110 } 111 } 112 if (feature == null) { 113 URL url = featureReference.getURL(); 114 String urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; UpdateCore.warn("The feature to unconfigure is null: feature reference is:" + urlString); } 117 118 InstallHandlerProxy handler = null; 120 if (callInstallHandler && feature.getInstallHandlerEntry() != null) 121 handler = new InstallHandlerProxy(IInstallHandler.HANDLER_ACTION_CONFIGURE, feature, feature.getInstallHandlerEntry(), null); 122 boolean success = false; 123 Throwable originalException = null; 124 125 try { 127 if (handler != null) 128 handler.configureInitiated(); 129 130 ConfigurationActivity activity = null; 131 if (createActivity) { 132 activity = new ConfigurationActivity(IActivity.ACTION_CONFIGURE); 133 activity.setLabel(feature.getVersionedIdentifier().toString()); 134 activity.setDate(new Date ()); 135 } 136 137 addConfiguredFeatureReference((FeatureReferenceModel) featureReference); 138 139 if (activity != null) { 141 InstallConfiguration installConfig = (InstallConfiguration) SiteManager.getLocalSite().getCurrentConfiguration(); 142 activity.setStatus(IActivity.STATUS_OK); 143 installConfig.addActivity(activity); 144 } 145 146 if (handler != null) 147 handler.completeConfigure(); 148 149 success = true; 150 } catch (Throwable t) { 151 originalException = t; 152 } finally { 153 Throwable newException = null; 154 try { 155 if (handler != null) 156 handler.configureCompleted(success); 157 } catch (Throwable t) { 158 newException = t; 159 } 160 if (originalException != null) throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String [] { feature.getLabel() })), originalException); 162 if (newException != null) 163 throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String [] { feature.getLabel() })), newException); 164 } 165 } 166 167 171 public boolean unconfigure(IFeatureReference featureReference, boolean callInstallHandler, boolean createActivity) throws CoreException { 172 173 if (isUnconfigured(featureReference)) { 174 UpdateCore.warn("Feature already unconfigured"); return true; 176 } 177 178 if (featureReference == null) { 179 UpdateCore.warn("The feature reference to unconfigure is null"); return false; 181 } 182 183 IFeature feature = null; 184 try { 185 feature = featureReference.getFeature(null); 186 } catch (CoreException e) { 187 if (!UpdateManagerUtils.isOptional(featureReference)) { 188 URL url = featureReference.getURL(); 189 String urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; UpdateCore.warn("Error retrieving feature:" + urlString, e); return false; 192 } 193 } 194 195 if (feature == null) { 196 URL url = featureReference.getURL(); 197 String urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; UpdateCore.warn("The feature to unconfigure is null: feature reference is:" + urlString); return false; 200 } 201 202 InstallHandlerProxy handler = null; 204 if (callInstallHandler && feature.getInstallHandlerEntry() != null) { 205 handler = new InstallHandlerProxy(IInstallHandler.HANDLER_ACTION_UNCONFIGURE, feature, feature.getInstallHandlerEntry(), null); 206 } 207 208 boolean success = false; 209 Throwable originalException = null; 210 211 try { 213 214 ConfigurationActivity activity = null; 215 if (createActivity) { 216 activity = new ConfigurationActivity(IActivity.ACTION_UNCONFIGURE); 217 activity.setLabel(feature.getVersionedIdentifier().toString()); 218 activity.setDate(new Date ()); 219 } 220 221 InstallConfiguration installConfig = null; 222 223 if (activity != null) 226 installConfig = ((InstallConfiguration) SiteManager.getLocalSite().getCurrentConfiguration()); 227 228 if (handler != null) 233 handler.unconfigureInitiated(); 234 addUnconfiguredFeatureReference((FeatureReferenceModel) featureReference); 235 if (handler != null) 236 handler.completeUnconfigure(); 237 238 if (activity != null) { 240 activity.setStatus(IActivity.STATUS_OK); 241 installConfig.addActivity(activity); 242 } 243 success = true; 244 } catch (Throwable t) { 251 originalException = t; 252 } finally { 253 Throwable newException = null; 254 try { 255 if (handler != null) 256 handler.unconfigureCompleted(success); 257 } catch (Throwable t) { 258 newException = t; 259 } 260 if (originalException != null) throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String [] { feature.getLabel() })), originalException); 262 if (newException != null) 263 throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String [] { feature.getLabel() })), newException); 264 } 265 266 if (!success) { 267 URL url = featureReference.getURL(); 268 String urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; UpdateCore.warn("Unable to unconfigure:" + urlString); } 271 return success; 272 } 273 274 280 public String [] getPluginPath(ISite site) throws CoreException { 281 if (getPolicy() == IPlatformConfiguration.ISitePolicy.MANAGED_ONLY) 283 return new String [0]; 284 285 String [] pluginPaths; 286 IFeatureReference[] unconfiguredFeatures = getUnconfiguredFeatures(); 291 IFeatureReference[] configuredFeatures = getConfiguredFeatures(); 293 if (!isEnabled()) { 294 if (getPolicy() == IPlatformConfiguration.ISitePolicy.USER_INCLUDE) { 295 pluginPaths = new String [0]; 297 } else { 298 pluginPaths = getAllKnownPluginStrings(site, 300 configuredFeatures, unconfiguredFeatures); 301 } 302 } else { 303 PatchedFeature[] patchedFeatures = buildPatchedFeatures(configuredFeatures); 305 if (getPolicy() == IPlatformConfiguration.ISitePolicy.USER_INCLUDE) { 306 pluginPaths = getConfiguredPluginStrings(site, patchedFeatures); 308 } else { 309 pluginPaths = subtract(getAllKnownPluginStrings(site, 313 configuredFeatures, unconfiguredFeatures), 314 getConfiguredPluginStrings(site, patchedFeatures)); 315 } 316 } 317 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) { 319 UpdateCore 320 .debug("GetPluginPath for: " + ((site == null) ? "<No site>" : site.getURL() .toString())); 323 for (int i = 0; i < pluginPaths.length; i++) { 324 UpdateCore.debug("To write:" + pluginPaths[i]); } 326 } 327 return pluginPaths; 328 } 329 330 337 private PatchedFeature[] buildPatchedFeatures(IFeatureReference[] features) { 338 Map map = new HashMap (); 340 for (int f = 0; f < features.length; f++) { 342 IFeatureReference featureRef = features[f]; 343 try { 344 if(featureRef.isPatch()){ 345 continue; 346 } 347 VersionedIdentifier vi = featureRef.getVersionedIdentifier(); 348 map.put(vi, new PatchedFeature(features[f])); 349 } catch (CoreException e) { 350 UpdateCore.warn(null, e); 351 } 352 } 353 for (int f = 0; f < features.length; f++) { 355 IFeatureReference patchCandidate = features[f]; 356 try { 357 IFeature feature = patchCandidate.getFeature(null); 358 IImport[] imports = feature.getImports(); 359 for (int i = 0; i < imports.length; i++) { 360 IImport oneImport = imports[i]; 361 if (!oneImport.isPatch()) 362 continue; 363 VersionedIdentifier patchedIdentifier = 365 oneImport.getVersionedIdentifier(); 366 PatchedFeature pf=(PatchedFeature) map.get(patchedIdentifier); 367 if (pf!=null) { 368 pf.addPatch(patchCandidate); 369 } else { 370 } 372 } 373 } catch (CoreException e) { 374 UpdateCore.warn(null, e); 375 } 376 } 377 Collection patchedFeatures=map.values(); 378 return (PatchedFeature[])patchedFeatures.toArray(new PatchedFeature[patchedFeatures.size()]); 379 } 380 381 384 public IFeatureReference[] getConfiguredFeatures() { 385 FeatureReferenceModel[] result = getConfiguredFeaturesModel(); 386 if (result.length == 0) 387 return new IFeatureReference[0]; 388 else 389 return (IFeatureReference[]) result; 390 } 391 392 395 public IFeatureReference[] getUnconfiguredFeatures() { 396 FeatureReferenceModel[] result = getUnconfiguredFeaturesModel(); 397 if (result.length == 0) 398 return new IFeatureReference[0]; 399 else 400 return (IFeatureReference[]) result; 401 } 402 403 407 public IConfiguredSite getConfiguredSite() { 408 return (IConfiguredSite) getConfiguredSiteModel(); 409 } 410 411 414 public void removeFeatureReference(IFeatureReference featureRef) { 415 if (featureRef instanceof FeatureReferenceModel) { 416 removeFeatureReference((FeatureReferenceModel) featureRef); 417 } 418 } 419 420 426 private String [] getConfiguredPluginStrings(ISite site, PatchedFeature[] features) throws CoreException { 427 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION){ 428 UpdateCore.warn("CONFIGURED PLUGINS"); } 430 431 Set featurePlugins = new HashSet (); 434 for (int i = 0; i < features.length; i++) { 435 FeaturePlugin[] plugins = features[i].getPlugins(); 436 featurePlugins.addAll(Arrays.asList(plugins)); 437 } 438 Set pluginStrings = getPluginStrings(site, (FeaturePlugin[]) featurePlugins.toArray(new FeaturePlugin[featurePlugins.size()])); 439 return (String []) pluginStrings.toArray(new String [pluginStrings.size()]); 440 } 441 444 private String [] getAllKnownPluginStrings(ISite site, IFeatureReference[] configured,IFeatureReference[] unconfigured) throws CoreException { 445 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION){ 446 UpdateCore.warn("ALL PLUGINS"); } 448 IFeatureReference[] all=new IFeatureReference[configured.length+unconfigured.length]; 450 System.arraycopy(configured, 0, all, 0, configured.length); 451 System.arraycopy(unconfigured, 0, all, configured.length, unconfigured.length); 452 Set patchedPlugins = new HashSet (); 454 for (int i=0; i< all.length; i++) { 455 try { 456 IFeature feature = all[i].getFeature(null); 457 if (feature == null) { 458 UpdateCore.warn("Null Feature", new Exception ()); continue; 460 } 461 462 IPluginEntry[] entries = feature.getPluginEntries(); 463 for (int entr = 0; entr < entries.length; entr++) { 465 patchedPlugins.add(new FeaturePlugin(entries[entr], feature)); 466 } 467 468 } catch (CoreException e) { 469 UpdateCore.warn(null, e); 470 } 471 } 472 Set pluginStrings = getPluginStrings(site, (FeaturePlugin[])patchedPlugins.toArray(new FeaturePlugin[patchedPlugins.size()])); 473 return (String []) pluginStrings.toArray(new String [pluginStrings.size()]); 474 } 475 481 private Set getPluginStrings(ISite site, FeaturePlugin[] plugins) throws CoreException { 482 Set pluginStrings=new HashSet (); 483 for (int i=0; i< plugins.length; i++) { 484 IPluginEntry entry = plugins[i].getEntry(); 485 IFeature feature=plugins[i].getFeature(); 486 487 ContentReference[] featureContentReference = null; 489 try { 490 featureContentReference = feature.getFeatureContentProvider().getPluginEntryArchiveReferences(entry, null 491 ); 492 } catch (CoreException e) { 493 UpdateCore.warn(null, e); 494 } 495 496 if (featureContentReference != null) { 498 for (int j = 0; j < featureContentReference.length; j++) { 499 URL url = site.getSiteContentProvider().getArchiveReference(featureContentReference[j].getIdentifier()); 500 if (url != null) { 501 String path = UpdateManagerUtils.getURLAsString(site.getURL(), url); 503 if(!path.endsWith(".jar")) path += (path.endsWith(File.separator) || path.endsWith("/")) ? "" : "/"; pluginStrings.add(path); 507 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION) 508 UpdateCore.warn("Add plugin: " + path + " to the list"); } 510 } 511 } 512 } 513 return pluginStrings; 514 } 515 516 519 private String [] subtract(String [] allStrings, String [] stringsToRemove) { 520 HashSet resultList = new HashSet (Arrays.asList(allStrings)); 521 resultList.removeAll(Arrays.asList(stringsToRemove)); 522 return (String [])resultList.toArray(new String [resultList.size()]); 523 } 524 } 525 | Popular Tags |