KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > LocalSite


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Date JavaDoc;
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 /**
36  * This class manages the configurations.
37  */

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     /*
46      * Have new features been found during reconciliation
47      */

48     public static boolean newFeaturesFound = false;
49
50     /*
51      * initialize the configurations from the persistent model.
52      * Set the reconciliation as non optimistic
53      */

54     public static ILocalSite getLocalSite() throws CoreException {
55         return internalGetLocalSite();
56     }
57
58     /*
59      *Internal call is reconciliation needs to be optimistic
60      */

61     public static ILocalSite internalGetLocalSite() throws CoreException {
62     
63         LocalSite localSite = new LocalSite();
64     
65         // obtain platform configuration
66
IPlatformConfiguration currentPlatformConfiguration = ConfiguratorUtils.getCurrentPlatformConfiguration();
67         localSite.isTransient(currentPlatformConfiguration.isTransient());
68     
69         try {
70             URL JavaDoc configXML = currentPlatformConfiguration.getConfigurationLocation();
71             localSite.setLocationURLString(configXML.toExternalForm());
72             localSite.resolve(configXML, null);
73     
74             // Attempt to read previous state
75
parseLocalSiteFile(currentPlatformConfiguration, localSite);
76
77         } catch (MalformedURLException JavaDoc exception) {
78             throw Utilities.newCoreException(NLS.bind(Messages.SiteLocal_UnableToCreateURLFor, (new String JavaDoc[] { localSite.getLocationURLString() + " & " + CONFIG_FILE })), exception); //$NON-NLS-1$
79
}
80     
81         return localSite;
82     }
83
84     /**
85      * Create the localSite object
86      */

87     private static boolean parseLocalSiteFile(IPlatformConfiguration platformConfig, LocalSite localSite ) throws CoreException, MalformedURLException JavaDoc {
88
89         //attempt to parse the LocalSite.xml
90
// URL resolvedURL = URLEncoder.encode(configXML);
91
try {
92 // InputStream in = UpdateCore.getPlugin().get(resolvedURL).getInputStream();
93
new SiteLocalParser(platformConfig, localSite);
94             return true;
95         } catch (Exception JavaDoc exception) {
96             return false;
97         }
98     }
99
100     /**
101      *
102      */

103     protected LocalSite() {
104     }
105
106     /**
107      * adds a new configuration to the LocalSite
108      * the newly added configuration is teh current one
109      */

110     public void addConfiguration(IInstallConfiguration config) {
111         if (config != null) {
112             addConfigurationModel((InstallConfigurationModel) config);
113
114             trimHistoryToCapacity();
115
116             // set configuration as current
117
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             // notify listeners
126
Object JavaDoc[] siteLocalListeners = listeners.getListeners();
127             for (int i = 0; i < siteLocalListeners.length; i++) {
128                 ((ILocalSiteChangedListener) siteLocalListeners[i]).currentInstallConfigurationChanged(config);
129             }
130         }
131
132     }
133
134     /*
135      *
136      */

137     private void trimHistoryToCapacity() {
138         // check if we have to remove a configuration
139
// the first added is #0
140
while (getConfigurationHistory().length > getMaximumHistoryCount() &&
141                 getConfigurationHistory().length > 1) {
142             // do not remove the first element in history, this is the original config
143
InstallConfigurationModel removedConfig = getConfigurationHistoryModel()[1];
144             if (removeConfigurationModel(removedConfig)) {
145
146                 // DEBUG:
147
if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION) {
148                     UpdateCore.debug("Removed configuration :" + removedConfig.getLabel()); //$NON-NLS-1$
149
}
150
151                 // notify listeners
152
Object JavaDoc[] siteLocalListeners = listeners.getListeners();
153                 for (int i = 0; i < siteLocalListeners.length; i++) {
154                     ((ILocalSiteChangedListener) siteLocalListeners[i]).installConfigurationRemoved((IInstallConfiguration) removedConfig);
155                 }
156
157                 //remove files
158
URL JavaDoc url = removedConfig.getURL();
159                 UpdateManagerUtils.removeFromFileSystem(new File JavaDoc(url.getFile()));
160             }
161         }
162     }
163     /*
164      * @see ILocalSite#addLocalSiteChangedListener(ILocalSiteChangedListener)
165      */

166     public void addLocalSiteChangedListener(ILocalSiteChangedListener listener) {
167         synchronized (listeners) {
168             listeners.add(listener);
169         }
170     }
171
172     /*
173      * @see ILocalSite#removeLocalSiteChangedListener(ILocalSiteChangedListener)
174      */

175     public void removeLocalSiteChangedListener(ILocalSiteChangedListener listener) {
176         synchronized (listeners) {
177             listeners.add(listener);
178         }
179     }
180
181     /**
182      * Saves the site into the config file.
183      * @return true if changes restart is needed
184      */

185     public boolean save() throws CoreException {
186
187         // Save the current configuration as
188
// the other are already saved
189
// and set runtim info for next startup
190
return ((InstallConfiguration) getCurrentConfiguration()).save();
191     }
192     
193 // /**
194
// * Method createNewInstallConfiguration.
195
// * @return IInstallConfiguration
196
// */
197
// private IInstallConfiguration createNewInstallConfiguration() throws CoreException {
198
// InstallConfiguration newInstallConfig = createConfigurationSite(null);
199
// newInstallConfig.setTimeline(newInstallConfig.getCreationDate().getTime());
200
// return newInstallConfig;
201
// }
202

203
204     /**
205      * @since 2.0
206      * @deprecated This method should not be used. The current install configuration is to be used.
207      */

208     public IInstallConfiguration cloneCurrentConfiguration() throws CoreException {
209         try {
210             return new InstallConfiguration(getCurrentConfiguration());
211         } catch (MalformedURLException JavaDoc e) {
212             throw Utilities.newCoreException(Messages.SiteLocal_cloneConfig, e);
213         }
214     }
215
216     /**
217      * @since 2.0
218      */

219     public void revertTo(IInstallConfiguration configuration, IProgressMonitor monitor, IProblemHandler handler) throws CoreException {
220
221         // create the activity
222
//Start UOW ?
223
ConfigurationActivity activity = new ConfigurationActivity(IActivity.ACTION_REVERT);
224         activity.setLabel(configuration.getLabel());
225         activity.setDate(new Date JavaDoc());
226         IInstallConfiguration newConfiguration = null;
227
228         try {
229             // create a configuration
230
newConfiguration = cloneCurrentConfiguration();
231             newConfiguration.setLabel(configuration.getLabel());
232
233             // add to the stack which will set up as current
234
addConfiguration(newConfiguration);
235
236             // process delta
237
// the Configured featuresConfigured are the same as the old configuration
238
// the unconfigured featuresConfigured are the rest...
239
((InstallConfiguration) newConfiguration).revertTo(configuration, monitor, handler);
240
241             // everything done ok
242
activity.setStatus(IActivity.STATUS_OK);
243         } catch (CoreException e) {
244             // error
245
activity.setStatus(IActivity.STATUS_NOK);
246             throw e;
247         } catch (InterruptedException JavaDoc e) {
248             //user decided not to revert, do nothing
249
// because we didn't add the configuration to the history
250
} finally {
251             if (newConfiguration != null)
252                  ((InstallConfiguration) newConfiguration).addActivity(activity);
253         }
254
255     }
256
257     /**
258      * @since 2.0
259      * @deprecated
260      */

261     public IInstallConfiguration addToPreservedConfigurations(IInstallConfiguration configuration) throws CoreException {
262         return null;
263     }
264
265     /*
266      * @see ILocalSite#getPreservedConfigurationFor(IInstallConfiguration)
267      */

268     public IInstallConfiguration findPreservedConfigurationFor(IInstallConfiguration configuration) {
269
270         // based on time stamp for now
271
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     /*
288      * @see ILocalSite#getCurrentConfiguration()
289      * LocalSiteModel#getCurrentConfigurationModel() may return null if
290      * we just parsed LocalSite.xml
291      */

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     /*
307      * @see ILocalSite#getPreservedConfigurations()
308      */

309     public IInstallConfiguration[] getPreservedConfigurations() {
310         if (getPreservedConfigurationsModel().length == 0)
311             return new IInstallConfiguration[0];
312         return (IInstallConfiguration[]) getPreservedConfigurationsModel();
313     }
314
315     /*
316      * @see ILocalSite#removeFromPreservedConfigurations(IInstallConfiguration)
317      */

318     public void removeFromPreservedConfigurations(IInstallConfiguration configuration) {
319         if (removePreservedConfigurationModel((InstallConfigurationModel) configuration))
320              ((InstallConfiguration) configuration).remove();
321     }
322
323     /*
324      * @see ILocalSite#getConfigurationHistory()
325      */

326     public IInstallConfiguration[] getConfigurationHistory() {
327         if (getConfigurationHistoryModel().length == 0)
328             return new IInstallConfiguration[0];
329         return (IInstallConfiguration[]) getConfigurationHistoryModel();
330     }
331
332
333     /**
334      * Gets the isTransient.
335      * @return Returns a boolean
336      */

337     public boolean isTransient() {
338         return isTransient;
339     }
340
341     /**
342      * Sets the isTransient.
343      * @param isTransient The isTransient to set
344      */

345     private void isTransient(boolean isTransient) {
346         this.isTransient = isTransient;
347     }
348
349     /*
350      *
351      */

352     private SiteStatusAnalyzer getSiteStatusAnalyzer() {
353         if (siteStatusAnalyzer == null)
354             siteStatusAnalyzer = new SiteStatusAnalyzer(this);
355         return siteStatusAnalyzer;
356     }
357
358     /*
359      * check if the Plugins of the feature are on the plugin path
360      * If all the plugins are on the plugin path, and the version match and there is no other version -> HAPPY
361      * If all the plugins are on the plugin path, and the version match and there is other version -> AMBIGUOUS
362      * If some of the plugins are on the plugin path, but not all -> UNHAPPY
363      * Check on all ConfiguredSites
364      */

365     public IStatus getFeatureStatus(IFeature feature) throws CoreException {
366         return getSiteStatusAnalyzer().getFeatureStatus(feature);
367     }
368     /**
369      * @see org.eclipse.update.internal.model.SiteLocalModel#setMaximumHistoryCount(int)
370      */

371     public void setMaximumHistoryCount(int history) {
372         super.setMaximumHistoryCount(history);
373         trimHistoryToCapacity();
374     }
375
376 }
377
Popular Tags