1 11 package org.eclipse.update.internal.core; 12 13 import java.io.File ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.Date ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.update.configuration.IActivity; 23 import org.eclipse.update.configuration.IInstallConfiguration; 24 import org.eclipse.update.configuration.ILocalSite; 25 import org.eclipse.update.configuration.ILocalSiteChangedListener; 26 import org.eclipse.update.configuration.IProblemHandler; 27 import org.eclipse.update.configurator.ConfiguratorUtils; 28 import org.eclipse.update.configurator.IPlatformConfiguration; 29 import org.eclipse.update.core.IFeature; 30 import org.eclipse.update.core.Utilities; 31 import org.eclipse.update.internal.model.InstallConfigurationModel; 32 import org.eclipse.update.internal.model.SiteLocalModel; 33 import org.eclipse.update.internal.model.SiteLocalParser; 34 35 38 39 public class LocalSite extends SiteLocalModel implements ILocalSite{ 40 41 private ListenersList listeners = new ListenersList(); 42 private SiteStatusAnalyzer siteStatusAnalyzer; 43 private boolean isTransient = false; 44 45 48 public static boolean newFeaturesFound = false; 49 50 54 public static ILocalSite getLocalSite() throws CoreException { 55 return internalGetLocalSite(); 56 } 57 58 61 public static ILocalSite internalGetLocalSite() throws CoreException { 62 63 LocalSite localSite = new LocalSite(); 64 65 IPlatformConfiguration currentPlatformConfiguration = ConfiguratorUtils.getCurrentPlatformConfiguration(); 67 localSite.isTransient(currentPlatformConfiguration.isTransient()); 68 69 try { 70 URL configXML = currentPlatformConfiguration.getConfigurationLocation(); 71 localSite.setLocationURLString(configXML.toExternalForm()); 72 localSite.resolve(configXML, null); 73 74 parseLocalSiteFile(currentPlatformConfiguration, localSite); 76 77 } catch (MalformedURLException exception) { 78 throw Utilities.newCoreException(NLS.bind(Messages.SiteLocal_UnableToCreateURLFor, (new String [] { localSite.getLocationURLString() + " & " + CONFIG_FILE })), exception); } 80 81 return localSite; 82 } 83 84 87 private static boolean parseLocalSiteFile(IPlatformConfiguration platformConfig, LocalSite localSite ) throws CoreException, MalformedURLException { 88 89 try { 92 new SiteLocalParser(platformConfig, localSite); 94 return true; 95 } catch (Exception exception) { 96 return false; 97 } 98 } 99 100 103 protected LocalSite() { 104 } 105 106 110 public void addConfiguration(IInstallConfiguration config) { 111 if (config != null) { 112 addConfigurationModel((InstallConfigurationModel) config); 113 114 trimHistoryToCapacity(); 115 116 if (getCurrentConfigurationModel() != null) 118 getCurrentConfigurationModel().setCurrent(false); 119 if (config instanceof InstallConfiguration) 120 ((InstallConfiguration) config).setCurrent(true); 121 122 setCurrentConfigurationModel((InstallConfigurationModel) config); 123 ((InstallConfigurationModel) config).markReadOnly(); 124 125 Object [] siteLocalListeners = listeners.getListeners(); 127 for (int i = 0; i < siteLocalListeners.length; i++) { 128 ((ILocalSiteChangedListener) siteLocalListeners[i]).currentInstallConfigurationChanged(config); 129 } 130 } 131 132 } 133 134 137 private void trimHistoryToCapacity() { 138 while (getConfigurationHistory().length > getMaximumHistoryCount() && 141 getConfigurationHistory().length > 1) { 142 InstallConfigurationModel removedConfig = getConfigurationHistoryModel()[1]; 144 if (removeConfigurationModel(removedConfig)) { 145 146 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION) { 148 UpdateCore.debug("Removed configuration :" + removedConfig.getLabel()); } 150 151 Object [] siteLocalListeners = listeners.getListeners(); 153 for (int i = 0; i < siteLocalListeners.length; i++) { 154 ((ILocalSiteChangedListener) siteLocalListeners[i]).installConfigurationRemoved((IInstallConfiguration) removedConfig); 155 } 156 157 URL url = removedConfig.getURL(); 159 UpdateManagerUtils.removeFromFileSystem(new File (url.getFile())); 160 } 161 } 162 } 163 166 public void addLocalSiteChangedListener(ILocalSiteChangedListener listener) { 167 synchronized (listeners) { 168 listeners.add(listener); 169 } 170 } 171 172 175 public void removeLocalSiteChangedListener(ILocalSiteChangedListener listener) { 176 synchronized (listeners) { 177 listeners.add(listener); 178 } 179 } 180 181 185 public boolean save() throws CoreException { 186 187 return ((InstallConfiguration) getCurrentConfiguration()).save(); 191 } 192 193 203 204 208 public IInstallConfiguration cloneCurrentConfiguration() throws CoreException { 209 try { 210 return new InstallConfiguration(getCurrentConfiguration()); 211 } catch (MalformedURLException e) { 212 throw Utilities.newCoreException(Messages.SiteLocal_cloneConfig, e); 213 } 214 } 215 216 219 public void revertTo(IInstallConfiguration configuration, IProgressMonitor monitor, IProblemHandler handler) throws CoreException { 220 221 ConfigurationActivity activity = new ConfigurationActivity(IActivity.ACTION_REVERT); 224 activity.setLabel(configuration.getLabel()); 225 activity.setDate(new Date ()); 226 IInstallConfiguration newConfiguration = null; 227 228 try { 229 newConfiguration = cloneCurrentConfiguration(); 231 newConfiguration.setLabel(configuration.getLabel()); 232 233 addConfiguration(newConfiguration); 235 236 ((InstallConfiguration) newConfiguration).revertTo(configuration, monitor, handler); 240 241 activity.setStatus(IActivity.STATUS_OK); 243 } catch (CoreException e) { 244 activity.setStatus(IActivity.STATUS_NOK); 246 throw e; 247 } catch (InterruptedException e) { 248 } finally { 251 if (newConfiguration != null) 252 ((InstallConfiguration) newConfiguration).addActivity(activity); 253 } 254 255 } 256 257 261 public IInstallConfiguration addToPreservedConfigurations(IInstallConfiguration configuration) throws CoreException { 262 return null; 263 } 264 265 268 public IInstallConfiguration findPreservedConfigurationFor(IInstallConfiguration configuration) { 269 270 InstallConfigurationModel preservedConfig = null; 272 if (configuration != null) { 273 InstallConfigurationModel[] preservedConfigurations = getPreservedConfigurationsModel(); 274 if (preservedConfigurations != null) { 275 for (int indexPreserved = 0; indexPreserved < preservedConfigurations.length; indexPreserved++) { 276 if (configuration.getCreationDate().equals(preservedConfigurations[indexPreserved].getCreationDate())) { 277 preservedConfig = preservedConfigurations[indexPreserved]; 278 break; 279 } 280 } 281 } 282 } 283 284 return (IInstallConfiguration) preservedConfig; 285 } 286 287 292 public IInstallConfiguration getCurrentConfiguration() { 293 if (getCurrentConfigurationModel() == null) { 294 int index = 0; 295 if ((index = getConfigurationHistoryModel().length) == 0) { 296 return null; 297 } else { 298 InstallConfigurationModel config = getConfigurationHistoryModel()[index - 1]; 299 config.setCurrent(true); 300 setCurrentConfigurationModel(config); 301 } 302 } 303 return (IInstallConfiguration) getCurrentConfigurationModel(); 304 } 305 306 309 public IInstallConfiguration[] getPreservedConfigurations() { 310 if (getPreservedConfigurationsModel().length == 0) 311 return new IInstallConfiguration[0]; 312 return (IInstallConfiguration[]) getPreservedConfigurationsModel(); 313 } 314 315 318 public void removeFromPreservedConfigurations(IInstallConfiguration configuration) { 319 if (removePreservedConfigurationModel((InstallConfigurationModel) configuration)) 320 ((InstallConfiguration) configuration).remove(); 321 } 322 323 326 public IInstallConfiguration[] getConfigurationHistory() { 327 if (getConfigurationHistoryModel().length == 0) 328 return new IInstallConfiguration[0]; 329 return (IInstallConfiguration[]) getConfigurationHistoryModel(); 330 } 331 332 333 337 public boolean isTransient() { 338 return isTransient; 339 } 340 341 345 private void isTransient(boolean isTransient) { 346 this.isTransient = isTransient; 347 } 348 349 352 private SiteStatusAnalyzer getSiteStatusAnalyzer() { 353 if (siteStatusAnalyzer == null) 354 siteStatusAnalyzer = new SiteStatusAnalyzer(this); 355 return siteStatusAnalyzer; 356 } 357 358 365 public IStatus getFeatureStatus(IFeature feature) throws CoreException { 366 return getSiteStatusAnalyzer().getFeatureStatus(feature); 367 } 368 371 public void setMaximumHistoryCount(int history) { 372 super.setMaximumHistoryCount(history); 373 trimHistoryToCapacity(); 374 } 375 376 } 377 | Popular Tags |