1 11 package org.eclipse.update.internal.core; 12 13 import java.io.IOException ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.update.core.ContentReference; 22 import org.eclipse.update.core.IFeature; 23 import org.eclipse.update.core.IFeatureContentConsumer; 24 import org.eclipse.update.core.IFeatureFactory; 25 import org.eclipse.update.core.IFeatureReference; 26 import org.eclipse.update.core.IInstallHandler; 27 import org.eclipse.update.core.INonPluginEntry; 28 import org.eclipse.update.core.IPluginEntry; 29 import org.eclipse.update.core.ISite; 30 import org.eclipse.update.core.ISiteFeatureReference; 31 import org.eclipse.update.core.IVerificationListener; 32 import org.eclipse.update.core.IVerifier; 33 import org.eclipse.update.core.InstallMonitor; 34 import org.eclipse.update.core.Site; 35 import org.eclipse.update.core.Utilities; 36 import org.eclipse.update.core.model.ContentEntryModel; 37 import org.eclipse.update.core.model.FeatureModel; 38 import org.eclipse.update.core.model.FeatureReferenceModel; 39 import org.eclipse.update.core.model.InstallAbortedException; 40 import org.eclipse.update.internal.operations.UpdateUtils; 41 42 43 46 public class SiteFile extends Site { 47 48 51 private List pluginEntries = new ArrayList (0); 52 53 56 public ISiteContentConsumer createSiteContentConsumer(IFeature targetFeature) throws CoreException { 57 SiteFileContentConsumer consumer = new SiteFileContentConsumer(targetFeature); 58 consumer.setSite(this); 59 return consumer; 60 } 61 62 64 public String getDefaultPackagedFeatureType() { 65 return DEFAULT_INSTALLED_FEATURE_TYPE; 66 } 67 68 71 public IFeatureReference install(IFeature sourceFeature, IVerificationListener verificationListener, IProgressMonitor progress) throws CoreException { 72 return install(sourceFeature,null,verificationListener,progress); 73 } 74 75 78 public IFeatureReference install(IFeature sourceFeature, IFeatureReference[] optionalfeatures, IVerificationListener verificationListener, IProgressMonitor progress) throws CoreException { 79 80 if (sourceFeature == null) 81 return null; 82 83 InstallMonitor monitor; 85 if (progress == null) 86 monitor = null; 87 else if (progress instanceof InstallMonitor) 88 monitor = (InstallMonitor) progress; 89 else 90 monitor = new InstallMonitor(progress); 91 92 IFeature localFeature = createExecutableFeature(sourceFeature); 94 95 IFeatureReference localFeatureReference = null; 96 localFeatureReference = sourceFeature.install(localFeature, optionalfeatures, verificationListener, monitor); 97 98 return localFeatureReference; 99 } 100 101 104 public IFeatureReference install(IFeature sourceFeature, IFeatureReference[] optionalfeatures, IFeatureContentConsumer parentContentConsumer, IVerifier parentVerifier, IVerificationListener verificationListener, IProgressMonitor progress) 105 throws InstallAbortedException, CoreException { 106 107 if (sourceFeature == null) 108 return null; 109 110 InstallMonitor monitor; 112 if (progress == null) 113 monitor = null; 114 else if (progress instanceof InstallMonitor) 115 monitor = (InstallMonitor) progress; 116 else 117 monitor = new InstallMonitor(progress); 118 119 IFeature localFeature = createExecutableFeature(sourceFeature); 121 parentContentConsumer.addChild(localFeature); 122 123 IVerifier vr = sourceFeature.getFeatureContentProvider().getVerifier(); 125 if (vr != null) 126 vr.setParent(parentVerifier); 127 128 IFeatureReference localFeatureReference = null; 129 localFeatureReference = sourceFeature.install(localFeature, optionalfeatures, verificationListener, monitor); 130 131 return localFeatureReference; 132 } 133 134 137 public void remove(IFeature feature, IProgressMonitor progress) throws CoreException { 138 139 if (feature == null) { 140 UpdateCore.warn("Feature to remove is null"); return; 142 } 143 144 ErrorRecoveryLog recoveryLog = ErrorRecoveryLog.getLog(); 145 146 InstallMonitor monitor; 148 if (progress == null) 149 monitor = null; 150 else if (progress instanceof InstallMonitor) 151 monitor = (InstallMonitor) progress; 152 else 153 monitor = new InstallMonitor(progress); 154 155 InstallHandlerProxy handler = new InstallHandlerProxy(IInstallHandler.HANDLER_ACTION_UNINSTALL, feature, feature.getInstallHandlerEntry(), monitor); 157 boolean success = false; 158 Throwable originalException = null; 159 160 try { 161 162 recoveryLog.open(ErrorRecoveryLog.START_REMOVE_LOG); 164 165 aboutToRemove(feature); 166 167 recoveryLog.append(ErrorRecoveryLog.END_ABOUT_REMOVE); 169 170 handler.uninstallInitiated(); 171 172 IPluginEntry[] pluginsToRemove = getPluginEntriesOnlyReferencedBy(feature); 175 176 if (monitor != null) { 177 monitor.beginTask(Messages.SiteFile_Removing + feature.getLabel(), pluginsToRemove.length + 1); 178 } 179 180 ISiteFeatureReference[] featureReferences = getFeatureReferences(); 182 if (featureReferences != null) { 183 for (int indexRef = 0; indexRef < featureReferences.length; indexRef++) { 184 IFeatureReference element = featureReferences[indexRef]; 185 if (element.getVersionedIdentifier().equals(feature.getVersionedIdentifier())) { 186 removeFeatureReferenceModel((FeatureReferenceModel) element); 187 break; 188 } 189 } 190 } 191 192 if (InstallRegistry.getInstance().get("feature_"+feature.getVersionedIdentifier()) == null) { UpdateCore.log(NLS.bind(Messages.SiteFile_featureNotRemoved, (new String [] { feature.getVersionedIdentifier().toString() })), null); 194 } else { 195 ContentReference[] references = feature.getFeatureContentProvider().getFeatureEntryArchiveReferences(monitor); 197 for (int i = 0; i < references.length; i++) { 198 try { 199 UpdateManagerUtils.removeFromFileSystem(references[i].asFile()); 200 if (monitor != null) 201 monitor.worked(1); 202 } catch (IOException e) { 203 throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_CannotRemoveFeature, (new String [] { feature.getVersionedIdentifier().getIdentifier(), getURL().toExternalForm() })), e); 204 } 205 } 206 InstallRegistry.unregisterFeature(feature); 207 } 208 209 for (int i = 0; i < pluginsToRemove.length; i++) { 212 remove(feature, pluginsToRemove[i], monitor); 213 } 214 215 IFeatureReference[] childrenRef = feature.getIncludedFeatureReferences(); 217 for (int i = 0; i < childrenRef.length; i++) { 218 IFeature childFeature = null; 219 try { 220 childFeature = childrenRef[i].getFeature(null); 221 } catch (CoreException e) { 222 UpdateCore.warn("Unable to retrieve feature to remove for:" + childrenRef[i]); } 224 if (childFeature != null && !getCurrentConfiguredSite().isConfigured(childFeature)) 226 remove(childrenRef[i].getFeature(null), monitor); 227 } 228 229 removeFeatureFromCache(feature.getURL()); 231 232 handler.completeUninstall(); 233 234 success = true; 235 } catch (Throwable t) { 236 originalException = t; 237 } finally { 238 Throwable newException = null; 239 try { 240 if (success) { 241 recoveryLog.close(ErrorRecoveryLog.END_REMOVE_LOG); 243 recoveryLog.delete(); 244 } else { 245 recoveryLog.close(ErrorRecoveryLog.END_REMOVE_LOG); 246 } 247 handler.uninstallCompleted(success); 248 } catch (Throwable t) { 249 newException = t; 250 } 251 if (originalException != null) throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String [] { feature.getLabel() })), originalException); 253 if (newException != null) 254 throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String [] { feature.getLabel() })), newException); 255 } 256 } 257 258 267 public long getDownloadSizeFor(IFeature feature) { 268 long result = 0; 269 276 try { 277 IFeatureReference[] children = feature.getIncludedFeatureReferences(); 280 IFeature currentFeature = null; 281 for (int i = 0; i < children.length; i++) { 282 currentFeature = UpdateUtils.getIncludedFeature(feature, children[i]); 283 if (currentFeature != null) { 284 result += getDownloadSizeFor(currentFeature); 285 if(result == ContentEntryModel.UNKNOWN_SIZE) 286 return result; 287 } 288 } 289 290 IPluginEntry[] entriesToInstall = feature.getPluginEntries(); 291 IPluginEntry[] siteEntries = this.getPluginEntries(); 292 entriesToInstall = UpdateManagerUtils.diff(entriesToInstall, siteEntries); 293 INonPluginEntry[] nonPluginEntriesToInstall = feature.getNonPluginEntries(); 295 296 result += feature.getFeatureContentProvider().getDownloadSizeFor(entriesToInstall, nonPluginEntriesToInstall); 297 } catch (CoreException e) { 298 UpdateCore.warn(null, e); 299 result = ContentEntryModel.UNKNOWN_SIZE; 300 } 301 return result; 302 } 303 304 315 public long getInstallSizeFor(IFeature feature) { 316 long result = 0; 317 318 try { 319 List pluginsToInstall = new ArrayList (); 320 321 pluginsToInstall.addAll(Arrays.asList(feature.getPluginEntries())); 323 IFeatureReference[] children = feature.getIncludedFeatureReferences(); 324 IFeature currentFeature = null; 325 for (int i = 0; i < children.length; i++) { 326 currentFeature = UpdateUtils.getIncludedFeature(feature, children[i]); 327 if (currentFeature != null) { 328 result += getInstallSizeFor(currentFeature); 331 if (result == ContentEntryModel.UNKNOWN_SIZE) 332 return result; 333 } 334 } 335 336 IPluginEntry[] entriesToInstall = new IPluginEntry[0]; 337 if (pluginsToInstall.size() > 0) { 338 entriesToInstall = new IPluginEntry[pluginsToInstall.size()]; 339 pluginsToInstall.toArray(entriesToInstall); 340 } 341 342 IPluginEntry[] siteEntries = this.getPluginEntries(); 343 entriesToInstall = UpdateManagerUtils.diff(entriesToInstall, siteEntries); 344 345 INonPluginEntry[] nonPluginEntriesToInstall = feature.getNonPluginEntries(); 347 348 result += feature.getFeatureContentProvider().getInstallSizeFor(entriesToInstall, nonPluginEntriesToInstall); 351 } catch (CoreException e) { 352 UpdateCore.warn(null, e); 353 result = ContentEntryModel.UNKNOWN_SIZE; 354 } 355 356 return result; 357 } 358 359 367 public void addPluginEntry(IPluginEntry pluginEntry) { 368 pluginEntries.add(pluginEntry); 369 } 370 371 public IPluginEntry[] getPluginEntries() { 372 IPluginEntry[] result = new IPluginEntry[0]; 373 if (!(pluginEntries == null || pluginEntries.isEmpty())) { 374 result = new IPluginEntry[pluginEntries.size()]; 375 pluginEntries.toArray(result); 376 } 377 return result; 378 } 379 380 381 public int getPluginEntryCount() { 382 return getPluginEntries().length; 383 } 384 385 388 private IFeature createExecutableFeature(IFeature sourceFeature) throws CoreException { 389 IFeature result = null; 390 IFeatureFactory factory = FeatureTypeFactory.getInstance().getFactory(DEFAULT_INSTALLED_FEATURE_TYPE); 391 result = factory.createFeature(null, this, null); 392 393 ((FeatureModel) result).setFeatureIdentifier(sourceFeature.getVersionedIdentifier().getIdentifier()); 395 ((FeatureModel) result).setFeatureVersion(sourceFeature.getVersionedIdentifier().getVersion().toString()); 396 return result; 397 } 398 399 402 private void remove(IFeature feature, IPluginEntry pluginEntry, InstallMonitor monitor) throws CoreException { 403 404 if (pluginEntry == null) 405 return; 406 407 if (InstallRegistry.getInstance().get("plugin_"+pluginEntry.getVersionedIdentifier()) == null) { UpdateCore.log(NLS.bind(Messages.SiteFile_pluginNotRemoved, (new String [] { pluginEntry.getVersionedIdentifier().toString() })), null); 409 return; 410 } 411 412 ContentReference[] references = feature.getFeatureContentProvider().getPluginEntryArchiveReferences(pluginEntry, monitor); 413 for (int i = 0; i < references.length; i++) { 414 try { 415 UpdateManagerUtils.removeFromFileSystem(references[i].asFile()); 416 if (monitor != null) 417 monitor.worked(1); 418 } catch (IOException e) { 419 throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_CannotRemovePlugin, (new String [] { pluginEntry.getVersionedIdentifier().toString(), getURL().toExternalForm() })), e); 420 } 421 } 422 pluginEntries.remove(pluginEntry); 423 InstallRegistry.unregisterPlugin(pluginEntry); 424 } 425 426 429 private void aboutToRemove(IFeature feature) throws CoreException { 430 431 ErrorRecoveryLog recoveryLog = ErrorRecoveryLog.getLog(); 432 if (!ErrorRecoveryLog.RECOVERY_ON) 434 return; 435 436 if (feature != null) { 438 439 ContentReference[] references = feature.getFeatureContentProvider().getFeatureEntryArchiveReferences(null); 441 for (int i = 0; i < references.length; i++) { 442 try { 443 recoveryLog.appendPath(ErrorRecoveryLog.FEATURE_ENTRY, references[i].asFile().getAbsolutePath()); 444 } catch (IOException e) { 445 String id = UpdateCore.getPlugin().getBundle().getSymbolicName(); 446 throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_CannotRemoveFeature, (new String [] { feature.getVersionedIdentifier().getIdentifier(), getURL().toExternalForm() })), e); 447 } 448 } 449 IPluginEntry[] pluginsToRemove = getPluginEntriesOnlyReferencedBy(feature); 451 for (int i = 0; i < pluginsToRemove.length; i++) { 452 references = feature.getFeatureContentProvider().getPluginEntryArchiveReferences(pluginsToRemove[i], null); 453 for (int j = 0; j < references.length; j++) { 454 try { 455 recoveryLog.appendPath(ErrorRecoveryLog.BUNDLE_JAR_ENTRY, references[j].asFile().getAbsolutePath()); 456 } catch (IOException e) { 457 throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_CannotRemovePlugin, (new String [] { pluginsToRemove[i].getVersionedIdentifier().toString(), getURL().toExternalForm() })), e); 458 } 459 } 460 } 461 } 462 463 IFeatureReference[] childrenRef = feature.getIncludedFeatureReferences(); 465 IFeature childFeature = null; 466 for (int i = 0; i < childrenRef.length; i++) { 467 try { 468 childFeature = childrenRef[i].getFeature(null); 469 } catch (CoreException e) { 470 UpdateCore.warn("Unable to retrieve feature to remove for:" + childrenRef[i]); } 472 aboutToRemove(childFeature); 473 } 474 } 475 476 } 477 | Popular Tags |