1 11 package org.eclipse.update.internal.core; 12 import java.io.*; 13 import java.net.*; 14 import java.util.*; 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.eclipse.update.configurator.*; 21 import org.eclipse.update.internal.model.*; 22 23 26 27 public class SiteLocal extends SiteLocalModel implements ILocalSite{ 28 29 private ListenersList listeners = new ListenersList(); 30 private SiteStatusAnalyzer siteStatusAnalyzer; 31 private boolean isTransient = false; 32 33 36 public static boolean newFeaturesFound = false; 37 38 42 public static ILocalSite getLocalSite() throws CoreException { 43 return internalGetLocalSite(); 44 } 45 46 49 public static ILocalSite internalGetLocalSite() throws CoreException { 50 51 SiteLocal localSite = new SiteLocal(); 52 53 IPlatformConfiguration currentPlatformConfiguration = ConfiguratorUtils.getCurrentPlatformConfiguration(); 55 localSite.isTransient(currentPlatformConfiguration.isTransient()); 56 57 try { 58 URL configXML = currentPlatformConfiguration.getConfigurationLocation(); 59 localSite.setLocationURLString(configXML.toExternalForm()); 60 localSite.resolve(configXML, null); 61 62 parseLocalSiteFile(currentPlatformConfiguration, localSite); 64 65 } catch (MalformedURLException exception) { 66 throw Utilities.newCoreException(NLS.bind(Messages.SiteLocal_UnableToCreateURLFor, (new String [] { localSite.getLocationURLString() + " & " + CONFIG_FILE })), exception); } 68 69 return localSite; 70 } 71 72 75 private static boolean parseLocalSiteFile(IPlatformConfiguration platformConfig, SiteLocal localSite ) throws CoreException, MalformedURLException { 76 77 try { 80 new SiteLocalParser(platformConfig, localSite); 82 return true; 83 } catch (Exception exception) { 84 return false; 85 } 86 } 87 88 91 protected SiteLocal() { 92 } 93 94 98 public void addConfiguration(IInstallConfiguration config) { 99 if (config != null) { 100 addConfigurationModel((InstallConfigurationModel) config); 101 102 trimHistoryToCapacity(); 103 104 if (getCurrentConfigurationModel() != null) 106 getCurrentConfigurationModel().setCurrent(false); 107 if (config instanceof InstallConfiguration) 108 ((InstallConfiguration) config).setCurrent(true); 109 110 setCurrentConfigurationModel((InstallConfigurationModel) config); 111 ((InstallConfigurationModel) config).markReadOnly(); 112 113 Object [] siteLocalListeners = listeners.getListeners(); 115 for (int i = 0; i < siteLocalListeners.length; i++) { 116 ((ILocalSiteChangedListener) siteLocalListeners[i]).currentInstallConfigurationChanged(config); 117 } 118 } 119 120 } 121 122 125 private void trimHistoryToCapacity() { 126 while (getConfigurationHistory().length > getMaximumHistoryCount() && 129 getConfigurationHistory().length > 1) { 130 InstallConfigurationModel removedConfig = getConfigurationHistoryModel()[1]; 132 if (removeConfigurationModel(removedConfig)) { 133 134 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION) { 136 UpdateCore.debug("Removed configuration :" + removedConfig.getLabel()); } 138 139 Object [] siteLocalListeners = listeners.getListeners(); 141 for (int i = 0; i < siteLocalListeners.length; i++) { 142 ((ILocalSiteChangedListener) siteLocalListeners[i]).installConfigurationRemoved((IInstallConfiguration) removedConfig); 143 } 144 145 URL url = removedConfig.getURL(); 147 UpdateManagerUtils.removeFromFileSystem(new File(url.getFile())); 148 } 149 } 150 } 151 154 public void addLocalSiteChangedListener(ILocalSiteChangedListener listener) { 155 synchronized (listeners) { 156 listeners.add(listener); 157 } 158 } 159 160 163 public void removeLocalSiteChangedListener(ILocalSiteChangedListener listener) { 164 synchronized (listeners) { 165 listeners.add(listener); 166 } 167 } 168 169 173 public boolean save() throws CoreException { 174 175 return ((InstallConfiguration) getCurrentConfiguration()).save(); 179 } 180 181 191 192 196 public IInstallConfiguration cloneCurrentConfiguration() throws CoreException { 197 try { 198 return new InstallConfiguration(getCurrentConfiguration()); 199 } catch (MalformedURLException e) { 200 throw Utilities.newCoreException(Messages.SiteLocal_cloneConfig, e); 201 } 202 } 203 204 207 public void revertTo(IInstallConfiguration configuration, IProgressMonitor monitor, IProblemHandler handler) throws CoreException { 208 209 ConfigurationActivity activity = new ConfigurationActivity(IActivity.ACTION_REVERT); 212 activity.setLabel(configuration.getLabel()); 213 activity.setDate(new Date()); 214 IInstallConfiguration newConfiguration = null; 215 216 try { 217 newConfiguration = cloneCurrentConfiguration(); 219 newConfiguration.setLabel(configuration.getLabel()); 220 221 addConfiguration(newConfiguration); 223 224 ((InstallConfiguration) newConfiguration).revertTo(configuration, monitor, handler); 228 229 activity.setStatus(IActivity.STATUS_OK); 231 } catch (CoreException e) { 232 activity.setStatus(IActivity.STATUS_NOK); 234 throw e; 235 } catch (InterruptedException e) { 236 } finally { 239 if (newConfiguration != null) 240 ((InstallConfiguration) newConfiguration).addActivity(activity); 241 } 242 243 } 244 245 249 public IInstallConfiguration addToPreservedConfigurations(IInstallConfiguration configuration) throws CoreException { 250 return null; 251 } 252 253 256 public IInstallConfiguration findPreservedConfigurationFor(IInstallConfiguration configuration) { 257 258 InstallConfigurationModel preservedConfig = null; 260 if (configuration != null) { 261 InstallConfigurationModel[] preservedConfigurations = getPreservedConfigurationsModel(); 262 if (preservedConfigurations != null) { 263 for (int indexPreserved = 0; indexPreserved < preservedConfigurations.length; indexPreserved++) { 264 if (configuration.getCreationDate().equals(preservedConfigurations[indexPreserved].getCreationDate())) { 265 preservedConfig = preservedConfigurations[indexPreserved]; 266 break; 267 } 268 } 269 } 270 } 271 272 return (IInstallConfiguration) preservedConfig; 273 } 274 275 280 public IInstallConfiguration getCurrentConfiguration() { 281 if (getCurrentConfigurationModel() == null) { 282 int index = 0; 283 if ((index = getConfigurationHistoryModel().length) == 0) { 284 return null; 285 } else { 286 InstallConfigurationModel config = getConfigurationHistoryModel()[index - 1]; 287 config.setCurrent(true); 288 setCurrentConfigurationModel(config); 289 } 290 } 291 return (IInstallConfiguration) getCurrentConfigurationModel(); 292 } 293 294 297 public IInstallConfiguration[] getPreservedConfigurations() { 298 if (getPreservedConfigurationsModel().length == 0) 299 return new IInstallConfiguration[0]; 300 return (IInstallConfiguration[]) getPreservedConfigurationsModel(); 301 } 302 303 306 public void removeFromPreservedConfigurations(IInstallConfiguration configuration) { 307 if (removePreservedConfigurationModel((InstallConfigurationModel) configuration)) 308 ((InstallConfiguration) configuration).remove(); 309 } 310 311 314 public IInstallConfiguration[] getConfigurationHistory() { 315 if (getConfigurationHistoryModel().length == 0) 316 return new IInstallConfiguration[0]; 317 return (IInstallConfiguration[]) getConfigurationHistoryModel(); 318 } 319 320 321 325 public boolean isTransient() { 326 return isTransient; 327 } 328 329 333 private void isTransient(boolean isTransient) { 334 this.isTransient = isTransient; 335 } 336 337 340 private SiteStatusAnalyzer getSiteStatusAnalyzer() { 341 if (siteStatusAnalyzer == null) 342 siteStatusAnalyzer = new SiteStatusAnalyzer(this); 343 return siteStatusAnalyzer; 344 } 345 346 353 public IStatus getFeatureStatus(IFeature feature) throws CoreException { 354 return getSiteStatusAnalyzer().getFeatureStatus(feature); 355 } 356 359 public void setMaximumHistoryCount(int history) { 360 super.setMaximumHistoryCount(history); 361 trimHistoryToCapacity(); 362 } 363 364 } 365 | Popular Tags |