1 11 package org.eclipse.update.internal.core; 12 13 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.io.OutputStreamWriter ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Date ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.MissingResourceException ; 30 import java.util.PropertyResourceBundle ; 31 import java.util.Set ; 32 import org.eclipse.core.runtime.CoreException; 33 import org.eclipse.core.runtime.FileLocator; 34 import org.eclipse.core.runtime.IProgressMonitor; 35 import org.eclipse.core.runtime.IStatus; 36 import org.eclipse.core.runtime.MultiStatus; 37 import org.eclipse.core.runtime.Platform; 38 import org.eclipse.osgi.util.NLS; 39 import org.eclipse.update.configuration.IActivity; 40 import org.eclipse.update.configuration.IConfiguredSite; 41 import org.eclipse.update.configuration.IConfiguredSiteChangedListener; 42 import org.eclipse.update.configuration.IInstallConfiguration; 43 import org.eclipse.update.configuration.IProblemHandler; 44 import org.eclipse.update.configurator.ConfiguratorUtils; 45 import org.eclipse.update.configurator.IPlatformConfiguration; 46 import org.eclipse.update.core.IFeature; 47 import org.eclipse.update.core.IFeatureReference; 48 import org.eclipse.update.core.IIncludedFeatureReference; 49 import org.eclipse.update.core.IPluginEntry; 50 import org.eclipse.update.core.ISite; 51 import org.eclipse.update.core.ISiteFeatureReference; 52 import org.eclipse.update.core.IVerificationListener; 53 import org.eclipse.update.core.SiteManager; 54 import org.eclipse.update.core.Utilities; 55 import org.eclipse.update.core.VersionedIdentifier; 56 import org.eclipse.update.core.model.InstallAbortedException; 57 import org.eclipse.update.internal.model.ConfiguredSiteModel; 58 import org.eclipse.update.internal.operations.UpdateUtils; 59 60 63 public class ConfiguredSite extends ConfiguredSiteModel implements IConfiguredSite { 64 65 private static final String PRODUCT_SITE_MARKER = ".eclipseproduct"; private static final String EXTENSION_SITE_MARKER = ".eclipseextension"; 68 private ListenersList listeners = new ListenersList(); 70 71 private IStatus verifyStatus; 73 74 private transient boolean justCreated = false; 76 77 80 public ConfiguredSite() { 81 } 82 83 87 public ConfiguredSite(IConfiguredSite configSite) { 88 ConfiguredSite cSite = (ConfiguredSite) configSite; 89 setSiteModel(cSite.getSiteModel()); 90 setConfigurationPolicyModel(new ConfigurationPolicy(cSite.getConfigurationPolicy())); 91 setUpdatable(cSite.isUpdatable()); 92 setEnabled(cSite.isEnabled()); 93 setPreviousPluginPath(cSite.getPreviousPluginPath()); 94 setPlatformURLString(cSite.getPlatformURLString()); 95 } 96 97 100 public void addConfiguredSiteChangedListener(IConfiguredSiteChangedListener listener) { 101 synchronized (listeners) { 102 listeners.add(listener); 103 } 104 } 105 106 109 public void removeConfiguredSiteChangedListener(IConfiguredSiteChangedListener listener) { 110 synchronized (listeners) { 111 listeners.remove(listener); 112 } 113 } 114 115 118 public IFeatureReference install(IFeature feature, IVerificationListener verificationListener, IProgressMonitor monitor) throws InstallAbortedException, CoreException { 119 return install(feature, null, verificationListener, monitor); 120 } 121 122 125 public IFeatureReference install(IFeature feature, IFeatureReference[] optionalFeatures, IVerificationListener verificationListener, IProgressMonitor monitor) throws InstallAbortedException, CoreException { 126 127 if (justCreated) justCreated=false; 129 130 if (!isUpdatable()) { 132 String errorMessage = NLS.bind(Messages.ConfiguredSite_NonInstallableSite, (new String [] { getSite().getURL().toExternalForm() })); 133 IStatus status = verifyUpdatableStatus(); 134 if (status != null) 135 errorMessage += " " + status.getMessage(); throw Utilities.newCoreException(errorMessage, null); 137 } 138 139 if (feature == null) { 141 String errorMessage = Messages.ConfiguredSite_NullFeatureToInstall; 142 throw Utilities.newCoreException(errorMessage, null); 143 } 144 145 IFeatureReference installedFeatureRef; 147 IFeature installedFeature = null; 148 149 ConfigurationActivity activity = new ConfigurationActivity(IActivity.ACTION_FEATURE_INSTALL); 151 activity.setLabel(feature.getVersionedIdentifier().toString()); 152 activity.setDate(new Date ()); 153 154 try { 155 installedFeatureRef = getSite().install(feature, optionalFeatures, verificationListener, monitor); 156 157 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_INSTALL) { 158 UpdateCore.debug("Sucessfully installed: " + installedFeatureRef.getURL().toExternalForm()); } 160 161 if (installedFeatureRef != null) { 162 try { 163 installedFeature = installedFeatureRef.getFeature(null); 164 } catch (CoreException e) { 165 UpdateCore.warn(null, e); 166 } 167 } 168 169 activity.setStatus(IActivity.STATUS_OK); 171 172 Object [] siteListeners = listeners.getListeners(); 174 for (int i = 0; i < siteListeners.length; i++) { 175 if (installedFeature != null) { 176 IConfiguredSiteChangedListener listener = ((IConfiguredSiteChangedListener) siteListeners[i]); 177 listener.featureInstalled(installedFeature); 178 } 179 } 180 } catch (CoreException e) { 181 activity.setStatus(IActivity.STATUS_NOK); 183 throw e; 184 } finally { 185 IInstallConfiguration current = SiteManager.getLocalSite().getCurrentConfiguration(); 186 ((InstallConfiguration) current).addActivity(activity); 187 } 188 if (installedFeature != null) 190 configure(installedFeature, optionalFeatures, true); 191 192 193 return installedFeatureRef; 194 } 195 196 199 public void remove(IFeature feature, IProgressMonitor monitor) throws CoreException { 200 201 if (!isUpdatable()) { 203 String errorMessage = NLS.bind(Messages.ConfiguredSite_NonUninstallableSite, (new String [] { getSite().getURL().toExternalForm() })); 204 throw Utilities.newCoreException(errorMessage, null); 205 } 206 207 ConfigurationActivity activity = new ConfigurationActivity(IActivity.ACTION_FEATURE_REMOVE); 209 activity.setLabel(feature.getVersionedIdentifier().toString()); 210 activity.setDate(new Date ()); 211 212 try { 213 IFeatureReference referenceToRemove = null; 214 ISiteFeatureReference[] featureRef = getSite().getFeatureReferences(); 215 ISiteFeatureReference ref = getSite().getFeatureReference(feature); 216 for (int i = 0; i < featureRef.length; i++) { 217 if (featureRef[i].equals(ref)) { 218 referenceToRemove = featureRef[i]; 219 break; 220 } 221 } 222 223 if (referenceToRemove != null) { 225 if (getConfigurationPolicy().isConfigured(referenceToRemove)) { 229 IFeature featureToRemove = referenceToRemove.getFeature(null); 230 String featureLabel = (featureToRemove == null) ? null : featureToRemove.getLabel(); 231 throw Utilities.newCoreException(NLS.bind(Messages.ConfiguredSite_UnableToRemoveConfiguredFeature, (new String [] { featureLabel })), null); 232 } 233 } else { 234 throw Utilities.newCoreException(NLS.bind(Messages.ConfiguredSite_UnableToFindFeature, (new String [] { feature.getURL().toString() })), 235 null); 236 } 237 238 getSite().remove(feature, monitor); 240 getConfigurationPolicy().removeFeatureReference(referenceToRemove); 241 activity.setStatus(IActivity.STATUS_OK); 243 Object [] siteListeners = listeners.getListeners(); 245 for (int i = 0; i < siteListeners.length; i++) { 246 ((IConfiguredSiteChangedListener) siteListeners[i]).featureRemoved(feature); 247 } 248 } catch (CoreException e) { 249 activity.setStatus(IActivity.STATUS_NOK); 250 throw e; 251 } finally { 252 IInstallConfiguration current = SiteManager.getLocalSite().getCurrentConfiguration(); 253 ((InstallConfiguration) current).addActivity(activity); 254 } 255 } 256 257 260 public void configure(IFeature feature) throws CoreException { 261 configure(feature, null, true 262 ); 263 } 264 265 268 private void configure(IFeature feature, IFeatureReference[] optionalFeatures, boolean callInstallHandler) throws CoreException { 269 270 if (feature == null) { 271 UpdateCore.warn("Attempting to configure a null feature in site:" + getSite().getURL().toExternalForm()); return; 273 } 274 275 ConfigurationPolicy configPolicy = getConfigurationPolicy(); 276 if (configPolicy == null) 277 return; 278 279 IIncludedFeatureReference[] childrenRef = feature.getIncludedFeatureReferences(); 281 if (optionalFeatures != null) { 282 childrenRef = childrenToConfigure(childrenRef, optionalFeatures); 283 } 284 285 for (int i = 0; i < childrenRef.length; i++) { 286 try { 287 IFeature child = childrenRef[i].getFeature(null); 288 configure(child, optionalFeatures, callInstallHandler); 289 } catch (CoreException e) { 290 if (!childrenRef[i].isOptional()) 292 UpdateCore.warn("Unable to configure child feature: " + childrenRef[i] + " " + e); throw e; 294 } 295 } 296 297 IFeatureReference featureReference = getSite().getFeatureReference(feature); 299 configPolicy.configure(featureReference, callInstallHandler, true); 300 301 Object [] siteListeners = listeners.getListeners(); 303 for (int i = 0; i < siteListeners.length; i++) { 304 ((IConfiguredSiteChangedListener) siteListeners[i]).featureConfigured(feature); 305 } 306 } 307 308 315 private IIncludedFeatureReference[] childrenToConfigure(IIncludedFeatureReference[] children, IFeatureReference[] optionalfeatures) { 316 317 List childrenToInstall = new ArrayList (); 318 for (int i = 0; i < children.length; i++) { 319 IIncludedFeatureReference optionalFeatureToConfigure = children[i]; 320 if (!optionalFeatureToConfigure.isOptional()) { 321 childrenToInstall.add(optionalFeatureToConfigure); 322 } else { 323 for (int j = 0; j < optionalfeatures.length; j++) { 324 try { 327 IFeature installedChildren = optionalfeatures[j].getFeature(null); 328 if (installedChildren.equals(optionalFeatureToConfigure.getFeature(null))) { 329 childrenToInstall.add(optionalFeatureToConfigure); 330 break; 331 } 332 } catch (CoreException e) { 333 UpdateCore.warn("", e); } 335 } 336 } 337 } 338 339 IIncludedFeatureReference[] result = new IIncludedFeatureReference[childrenToInstall.size()]; 340 if (childrenToInstall.size() > 0) { 341 childrenToInstall.toArray(result); 342 } 343 344 return result; 345 } 346 347 350 public boolean unconfigure(IFeature feature) throws CoreException { 351 return unconfigure(feature, true, false); 353 } 354 355 private boolean unconfigure(IFeature feature, boolean includePatches, boolean verifyEnableParent) throws CoreException { 356 IFeatureReference featureReference = getSite().getFeatureReference(feature); 357 358 if (featureReference == null) { 359 UpdateCore.warn("Unable to retrieve Feature Reference for feature" + feature); return false; 361 } 362 363 ConfigurationPolicy configPolicy = getConfigurationPolicy(); 364 if (configPolicy == null) 365 return false; 366 367 if (verifyEnableParent && !validateNoConfiguredParents(feature)) { 369 UpdateCore.warn("The feature " + feature.getVersionedIdentifier() + " to disable is needed by another enable feature"); return false; 371 } 372 373 boolean sucessfullyUnconfigured = false; 374 try { 375 sucessfullyUnconfigured = configPolicy.unconfigure(featureReference, true, true); 376 } catch (CoreException e) { 377 URL url = featureReference.getURL(); 378 String urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; UpdateCore.warn("Unable to unconfigure" + urlString, e); throw e; 381 } 382 if (sucessfullyUnconfigured) { 383 if (includePatches) 393 unconfigurePatches(feature); 394 395 IIncludedFeatureReference[] childrenRef = feature.getIncludedFeatureReferences(); 397 for (int i = 0; i < childrenRef.length; i++) { 398 try { 399 IFeature child = childrenRef[i].getFeature(null); unconfigure(child, includePatches, true); } catch (CoreException e) { 402 UpdateCore.warn("Unable to unconfigure child feature: " + childrenRef[i] + " " + e); } 405 } 406 407 Object [] siteListeners = listeners.getListeners(); 409 for (int i = 0; i < siteListeners.length; i++) { 410 IConfiguredSiteChangedListener listener = ((IConfiguredSiteChangedListener) siteListeners[i]); 411 listener.featureUnconfigured(feature); 412 } 413 414 return true; 415 } else { 416 URL url = featureReference.getURL(); 417 String urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; UpdateCore.warn("Unable to unconfigure:" + urlString); return false; 420 } 421 } 422 423 430 431 private void unconfigurePatches(IFeature feature) { 432 IFeatureReference[] frefs = getConfiguredFeatures(); 433 for (int i = 0; i < frefs.length; i++) { 434 IFeatureReference fref = frefs[i]; 435 try { 436 IFeature candidate = fref.getFeature(null); 437 if (candidate.equals(feature)) 438 continue; 439 440 if (UpdateUtils.isPatch(feature, candidate)) 441 unconfigure(candidate, false, false); 442 } catch (CoreException e) { 443 UpdateCore.warn("", e); } 445 } 446 } 447 448 451 public IFeatureReference[] getConfiguredFeatures() { 452 if (isEnabled()) 453 return getRawConfiguredFeatures(); 454 else 455 return new ISiteFeatureReference[0]; 456 } 457 458 461 private IFeatureReference[] getRawConfiguredFeatures() { 462 ConfigurationPolicy configPolicy = getConfigurationPolicy(); 463 if (configPolicy == null) 464 return new ISiteFeatureReference[0]; 465 466 return configPolicy.getConfiguredFeatures(); 467 } 468 469 472 public IFeatureReference[] getFeatureReferences() { 473 474 ConfigurationPolicy configPolicy = getConfigurationPolicy(); 475 if (configPolicy == null) 476 return new ISiteFeatureReference[0]; 477 478 IFeatureReference[] configuredFeatures = getConfiguredFeatures(); 479 int confLen = configuredFeatures.length; 480 IFeatureReference[] unconfiguredFeatures = configPolicy.getUnconfiguredFeatures(); 481 int unconfLen = unconfiguredFeatures.length; 482 483 IFeatureReference[] result = new IFeatureReference[confLen + unconfLen]; 484 if (confLen > 0) { 485 System.arraycopy(configuredFeatures, 0, result, 0, confLen); 486 } 487 if (unconfLen > 0) { 488 System.arraycopy(unconfiguredFeatures, 0, result, confLen, unconfLen); 489 } 490 491 return result; 492 } 493 494 501 public void revertTo(IConfiguredSite oldConfiguration, IProgressMonitor monitor, IProblemHandler handler) throws CoreException, InterruptedException { 502 503 ConfiguredSite oldConfiguredSite = (ConfiguredSite) oldConfiguration; 504 505 IFeatureReference[] configuredFeatures = oldConfiguredSite.validConfiguredFeatures(handler); 507 508 for (int i = 0; i < configuredFeatures.length; i++) { 509 getConfigurationPolicy().configure(configuredFeatures[i], true, true); 510 } 511 512 List featureToUnconfigure = oldConfiguredSite.calculateUnconfiguredFeatures(configuredFeatures); 515 516 Iterator iter = featureToUnconfigure.iterator(); 519 while (iter.hasNext()) { 520 IFeatureReference element = (IFeatureReference) iter.next(); 521 try { 522 getConfigurationPolicy().unconfigure(element, true, true); 524 } catch (CoreException e) { 525 String url = element.getURL().toString(); 527 ISite site = element.getSite(); 528 String siteString = (site != null) ? site.getURL().toExternalForm() : Messages.ConfiguredSite_NoSite; 529 UpdateCore.warn(NLS.bind(Messages.ConfiguredSite_CannotFindFeatureToUnconfigure, (new String [] { url, siteString })), e); 530 } 531 } 532 } 534 535 540 private IFeatureReference[] validConfiguredFeatures(IProblemHandler handler) throws InterruptedException { 541 542 IFeatureReference[] configuredFeatures = getConfiguredFeatures(); 543 if (configuredFeatures != null) { 544 for (int i = 0; i < configuredFeatures.length; i++) { 545 IFeature feature = null; 546 547 try { 549 feature = configuredFeatures[i].getFeature(null); 550 } catch (CoreException e) { 551 UpdateCore.warn(null, e); 553 String featureString = configuredFeatures[i].getURL().toExternalForm(); 554 if (!handler.reportProblem(NLS.bind(Messages.ConfiguredSite_CannotFindFeatureToConfigure, (new String [] { featureString })))) { 555 throw new InterruptedException (); 556 } 557 } 558 559 if (feature != null) { 561 List sitePluginIdentifiers = new ArrayList (); 563 ISite site = feature.getSite(); 564 IPluginEntry[] sitePluginEntries = null; 565 566 if (site != null) { 567 sitePluginEntries = site.getPluginEntries(); 568 for (int index = 0; index < sitePluginEntries.length; index++) { 569 IPluginEntry entry = sitePluginEntries[index]; 570 sitePluginIdentifiers.add(entry.getVersionedIdentifier()); 571 } 572 } 573 574 if (sitePluginEntries.length > 0) { 575 IPluginEntry[] featurePluginEntries = feature.getPluginEntries(); 576 for (int index = 0; index < featurePluginEntries.length; index++) { 577 IPluginEntry currentFeaturePluginEntry = featurePluginEntries[index]; 578 if (!contains(currentFeaturePluginEntry.getVersionedIdentifier(), sitePluginIdentifiers)) { 579 String msg = "Error verifying existence of plugin:" + currentFeaturePluginEntry.getVersionedIdentifier().toString(); UpdateCore.log(msg, new Exception ()); 583 584 String siteString = (site != null) ? site.getURL().toExternalForm() : Messages.ConfiguredSite_NoSite; 585 String errorLabel = NLS.bind(Messages.ConfiguredSite_CannotFindPluginEntry, (new String [] { currentFeaturePluginEntry.getVersionedIdentifier().toString(), siteString })); 586 if (handler == null) { 587 throw new InterruptedException (errorLabel); 588 } 589 if (!handler.reportProblem(errorLabel)) { 590 throw new InterruptedException (); 591 } 592 } } } 595 } 596 } } 598 return configuredFeatures; 599 } 600 601 612 private List calculateUnconfiguredFeatures(IFeatureReference[] configuredFeatures) throws CoreException { 613 614 Set featureToUnconfigureSet = new HashSet (); 615 616 IInstallConfiguration[] history = SiteManager.getLocalSite().getConfigurationHistory(); 621 622 for (int i = 0; i < history.length; i++) { 623 IInstallConfiguration element = history[i]; 624 IConfiguredSite[] configSites = element.getConfiguredSites(); 625 for (int j = 0; j < configSites.length; j++) { 626 ConfiguredSite configSite = (ConfiguredSite) configSites[j]; 627 if (configSite.getSite().equals(getSite())) { 628 featureToUnconfigureSet.addAll(Arrays.asList(configSite.getConfigurationPolicy().getUnconfiguredFeatures())); 629 featureToUnconfigureSet.addAll(Arrays.asList(configSite.getConfigurationPolicy().getConfiguredFeatures())); 630 } 631 } 632 } 633 634 List featureToUnconfigureList = remove(configuredFeatures, featureToUnconfigureSet); 637 638 return featureToUnconfigureList; 639 } 640 641 645 private List remove(IFeatureReference[] featureRefs, Set set) { 646 List result = new ArrayList (); 647 648 if (set == null) 649 return result; 650 651 Iterator iter = set.iterator(); 654 while (iter.hasNext()) { 655 IFeatureReference element = (IFeatureReference) iter.next(); 656 boolean found = false; 657 for (int i = 0; i < featureRefs.length; i++) { 658 if (element.equals(featureRefs[i])) { 659 found = true; 660 } 661 } 662 663 if (!found) 664 result.add(element); 665 } 666 return result; 667 } 668 669 673 private boolean contains(VersionedIdentifier id, List list) { 674 boolean found = false; 675 if (list != null && !list.isEmpty()) { 676 Iterator iter = list.iterator(); 677 while (iter.hasNext() && !found) { 678 VersionedIdentifier element = (VersionedIdentifier) iter.next(); 679 if (element.equals(id)) { 680 found = true; 681 } 682 } 683 } 684 return found; 685 } 686 687 690 public ConfigurationPolicy getConfigurationPolicy() { 691 return (ConfigurationPolicy) getConfigurationPolicyModel(); 692 } 693 694 697 public ISite getSite() { 698 return (ISite) getSiteModel(); 699 } 700 701 704 public IInstallConfiguration getInstallConfiguration() { 705 return (IInstallConfiguration) getInstallConfigurationModel(); 706 } 707 708 711 public IStatus getBrokenStatus(IFeature feature) { 712 713 IStatus featureStatus = createStatus(IStatus.OK, IFeature.STATUS_HAPPY, "", null); 715 IPluginEntry[] siteEntries = getSite().getPluginEntries(); 718 IPluginEntry[] featuresEntries = feature.getPluginEntries(); 719 IPluginEntry[] result = UpdateManagerUtils.diff(featuresEntries, siteEntries); 720 if (result != null && (result.length != 0)) { 721 String msg = Messages.SiteLocal_FeatureUnHappy; 722 MultiStatus multi = new MultiStatus(featureStatus.getPlugin(), IFeature.STATUS_UNHAPPY, msg, null); 723 724 for (int k = 0; k < result.length; k++) { 725 VersionedIdentifier id = result[k].getVersionedIdentifier(); 726 Object [] values = new String [] { "", "" }; if (id != null) { 728 values = new Object [] { id.getIdentifier(), id.getVersion()}; 729 } 730 String msg1 = NLS.bind(Messages.ConfiguredSite_MissingPluginsBrokenFeature, values); 731 UpdateCore.warn(msg1); 732 IStatus status = createStatus(IStatus.ERROR, IFeature.STATUS_UNHAPPY, msg1, null); 733 multi.add(status); 734 } 735 return multi; 736 } 737 738 740 String msg = Messages.SiteLocal_FeatureHappy; 741 return createStatus(IStatus.OK, IFeature.STATUS_HAPPY, msg, null); 742 } 743 744 747 public boolean isConfigured(IFeature feature) { 748 if (!isEnabled()) 749 return false; 750 751 if (getConfigurationPolicy() == null) 752 return false; 753 IFeatureReference featureReference = getSite().getFeatureReference(feature); 754 if (featureReference == null) { 755 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_WARNINGS) 756 UpdateCore.warn("Unable to retrieve featureReference for feature:" + feature); return false; 758 } 759 return getConfigurationPolicy().isConfigured(featureReference); 760 } 761 762 765 public String toString() { 766 if (getSite() == null) 767 return "No Site"; if (getSite().getURL() == null) 769 return "No URL"; return getSite().getURL().toExternalForm(); 771 } 772 773 776 public IStatus verifyUpdatableStatus() { 777 778 if (verifyStatus != null) 779 return verifyStatus; 780 781 URL siteURL = getSite().getURL(); 782 if (siteURL == null) { 783 verifyStatus = createStatus(IStatus.ERROR, Messages.ConfiguredSite_SiteURLNull, null); 784 return verifyStatus; 785 } 786 787 if (!"file".equalsIgnoreCase(siteURL.getProtocol())) { verifyStatus = createStatus(IStatus.ERROR, Messages.ConfiguredSite_NonLocalSite, null); 789 return verifyStatus; 790 } 791 792 String siteLocation = siteURL.getFile(); 793 File file = new File (siteLocation); 794 795 String productName = getProductName(file); 800 if (productName != null) { 801 if (!productName.equals(getProductIdentifier("id", getProductFile()))) { verifyStatus = createStatus(IStatus.ERROR, NLS.bind(Messages.ConfiguredSite_NotSameProductId, (new String [] { productName })), null); 803 return verifyStatus; 804 } 805 } else { 806 File container = getSiteContaining(file); 807 if (container != null && !siteLocation.equals(Platform.getInstallLocation().getURL().getFile())) { 810 verifyStatus = createStatus(IStatus.ERROR, NLS.bind(Messages.ConfiguredSite_ContainedInAnotherSite, (new String [] { container.getAbsolutePath() })), null); 811 return verifyStatus; 812 } 813 } 814 815 if (!canWrite(file)) { 816 verifyStatus = createStatus(IStatus.ERROR, Messages.ConfiguredSite_ReadOnlySite, null); 817 return verifyStatus; 818 } 819 820 verifyStatus = createStatus(IStatus.OK, "", null); setUpdatable(true); 822 return verifyStatus; 823 } 824 825 828 public static boolean canWrite(File file) { 829 if (file.canWrite() == false) 830 return false; 831 832 if (!file.isDirectory()) 833 return false; 834 835 File fileTest = null; 836 try { 837 fileTest = File.createTempFile("writtableArea", ".dll", file); } catch (IOException e) { 841 return false; 843 } finally { 844 if (fileTest != null) 845 fileTest.delete(); 846 } 847 return true; 848 } 849 850 855 private static File getSiteContaining(File file) { 856 857 if (file == null) 858 return null; 859 860 UpdateCore.warn("IsContained: Checking for markers at:" + file); if (file.exists() && file.isDirectory()) { 862 File productFile = new File (file, PRODUCT_SITE_MARKER); 863 File extensionFile = new File (file, EXTENSION_SITE_MARKER); 864 if (productFile.exists() || extensionFile.exists()) 865 return file; 866 } 874 return getSiteContaining(file.getParentFile()); 875 } 876 877 882 private static String getProductName(File file) { 883 884 if (file == null) 885 return null; 886 887 File markerFile = new File (file, EXTENSION_SITE_MARKER ); 888 if (!markerFile.exists()) { 889 return null; 890 } 891 892 File productFile = getProductFile(); 893 String productId = null; 894 String privateId = null; 895 if (productFile != null) { 896 productId = getProductIdentifier("id", productFile); privateId = getProductIdentifier("id", markerFile); if (productId == null) { 899 UpdateCore.warn("Product ID is null at:" + productFile); return null; 901 } 902 if (!productId.equalsIgnoreCase(privateId)) { 903 UpdateCore.warn("Product id at" + productFile + " Different than:" + markerFile); String name = getProductIdentifier("name", markerFile); String version = getProductIdentifier("version", markerFile); String markerID = (name == null) ? version : name + ":" + version; if (markerID == null) 908 markerID = ""; return markerID; 910 } else { 911 return privateId; 912 } 913 } else { 914 UpdateCore.warn("Product Marker doesn't exist:" + productFile); } 916 917 return null; 918 } 919 920 923 private static String getProductIdentifier(String identifier, File propertyFile) { 924 String result = null; 925 if (identifier == null) 926 return result; 927 InputStream in = null; 928 try { 929 in = new FileInputStream (propertyFile); 930 PropertyResourceBundle bundle = new PropertyResourceBundle (in); 931 result = bundle.getString(identifier); 932 } catch (IOException e) { 933 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_INSTALL) 934 UpdateCore.debug("Exception reading property file:" + propertyFile); } catch (MissingResourceException e) { 936 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_INSTALL) 937 UpdateCore.debug("Exception reading '" + identifier + "' from property file:" + propertyFile); } finally { 939 if (in == null) 940 try { 941 in.close(); 942 } catch (IOException e1) { 943 } 944 } 945 return result; 946 } 947 948 951 private static File getProductFile() { 952 953 String productInstallDirectory = ConfiguratorUtils.getInstallURL().getFile(); 954 if (productInstallDirectory != null) { 955 File productFile = new File (productInstallDirectory, PRODUCT_SITE_MARKER); 956 if (productFile.exists()) { 957 return productFile; 958 } else { 959 UpdateCore.warn("Product marker doesn't exist:" + productFile); } 961 } else { 962 UpdateCore.warn("Cannot retrieve install URL from BootLoader"); } 964 return null; 965 } 966 967 970 971 boolean createPrivateSiteMarker() { 972 URL siteURL = getSite().getURL(); 973 if (siteURL == null) { 974 UpdateCore.warn("Unable to create marker. The Site url is null."); return false; 976 } 977 978 if (!"file".equalsIgnoreCase(siteURL.getProtocol())) { UpdateCore.warn("Unable to create private marker. The Site is not on the local file system."); return false; 981 } 982 983 String siteLocation = siteURL.getFile(); 984 File productFile = getProductFile(); 985 boolean success = false; 986 if (productFile != null) { 987 String productId = getProductIdentifier("id", productFile); String productName = getProductIdentifier("name", productFile); String productVer = getProductIdentifier("version", productFile); if (productId != null) { 991 File file = new File (siteLocation, EXTENSION_SITE_MARKER); 992 if (!file.exists()) { 993 OutputStream out = null; 994 OutputStreamWriter outWriter = null; 995 try { 996 out = new FileOutputStream (file); 997 outWriter = new OutputStreamWriter (out, "UTF8"); outWriter.write("id=" + productId+"\n"); if (productName != null) 1000 outWriter.write("name=" + productName+"\n"); if (productVer != null) 1002 outWriter.write("version=" + productVer+"\n"); success = true; 1004 justCreated = true; 1005 } catch (Exception e) { 1006 UpdateCore.warn("Unable to create private Marker at:" + file, e); } finally { 1008 try { 1009 if (outWriter != null) 1010 outWriter.close(); 1011 } catch (IOException e1) { 1012 } 1013 try { 1014 if (out != null) 1015 out.close(); 1016 } catch (IOException e2) { 1017 } 1018 } 1019 } 1020 } 1021 } 1022 return success; 1023 } 1024 1025 1026 1030 public boolean isExtensionSite() { 1031 return containsMarker(EXTENSION_SITE_MARKER); 1032 } 1033 1034 1038 public boolean isProductSite() { 1039 return containsMarker(PRODUCT_SITE_MARKER); 1040 } 1041 1042 1046 public boolean isPrivateSite() { 1047 return isExtensionSite(); 1048 } 1049 1050 1053 private boolean containsMarker(String marker) { 1054 ISite site = getSite(); 1055 if (site == null) { 1056 UpdateCore.warn("Contains Markers:The site is null"); return false; 1058 } 1059 1060 URL url = site.getURL(); 1061 if (url == null) { 1062 UpdateCore.warn("Contains Markers:Site URL is null"); return false; 1064 } 1065 if (!"file".equalsIgnoreCase(url.getProtocol())) { UpdateCore.warn("Contains Markers:Non file protocol"); return false; 1068 } 1069 File file = new File (url.getFile()); 1070 if (!file.exists()) { 1071 UpdateCore.warn("Contains Markers:The site doesn't exist:" + file); return false; 1073 } 1074 File extension = new File (file, marker); 1075 if (!extension.exists()) { 1076 UpdateCore.warn("Contains Markers:The extensionfile does not exist:" + extension); return false; 1078 } 1079 return true; 1080 } 1081 1082 1085 public boolean isNativelyLinked() throws CoreException { 1086 String platformString = getPlatformURLString(); 1087 if (platformString == null) { 1088 UpdateCore.warn("Unable to retrieve platformString"); return false; 1090 } 1091 1092 URL siteURL = null; 1093 try { 1094 URL urlToCheck = new URL (platformString); 1097 IPlatformConfiguration runtimeConfig = ConfiguratorUtils.getCurrentPlatformConfiguration(); 1098 IPlatformConfiguration.ISiteEntry entry = runtimeConfig.findConfiguredSite(urlToCheck); 1099 if (entry != null) { 1100 return entry.isNativelyLinked(); 1101 } else { 1102 UpdateCore.warn("Unable to retrieve site:" + platformString + " from platform."); } 1104 1105 IPlatformConfiguration.ISiteEntry[] sites = runtimeConfig.getConfiguredSites(); 1107 for (int i = 0; i < sites.length; i++) { 1108 siteURL = sites[i].getURL(); 1109 URL resolvedURL = FileLocator.resolve(siteURL); 1110 if (UpdateManagerUtils.sameURL(resolvedURL, urlToCheck)) 1111 return true; 1112 } 1113 } catch (MalformedURLException e) { 1114 String msg = NLS.bind(Messages.ConfiguredSite_UnableResolveURL, (new String [] { platformString })); 1115 throw Utilities.newCoreException(msg, e); 1116 } catch (IOException e) { 1117 String msg = NLS.bind(Messages.ConfiguredSite_UnableToAccessSite, (new Object [] { siteURL })); 1118 throw Utilities.newCoreException(msg, e); 1119 } 1120 1121 return false; 1122 } 1123 1124 1127 private boolean validateNoConfiguredParents(IFeature feature) throws CoreException { 1128 if (feature == null) { 1129 UpdateCore.warn("ConfigurationPolicy: validate Feature is null"); return true; 1131 } 1132 1133 IFeatureReference[] parents = UpdateManagerUtils.getParentFeatures(feature, getConfiguredFeatures(), false); 1134 return (parents.length == 0); 1135 } 1136 1137} 1138 | Popular Tags |