KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 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 /**
24  * This class manages the configurations.
25  */

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

36     public static boolean newFeaturesFound = false;
37
38     /*
39      * initialize the configurations from the persistent model.
40      * Set the reconciliation as non optimistic
41      */

42     public static ILocalSite getLocalSite() throws CoreException {
43         return internalGetLocalSite();
44     }
45
46     /*
47      *Internal call is reconciliation needs to be optimistic
48      */

49     public static ILocalSite internalGetLocalSite() throws CoreException {
50     
51         SiteLocal localSite = new SiteLocal();
52     
53         // obtain platform configuration
54
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             // Attempt to read previous state
63
parseLocalSiteFile(currentPlatformConfiguration, localSite);
64
65         } catch (MalformedURLException exception) {
66             throw Utilities.newCoreException(NLS.bind(Messages.SiteLocal_UnableToCreateURLFor, (new String JavaDoc[] { localSite.getLocationURLString() + " & " + CONFIG_FILE })), exception); //$NON-NLS-1$
67
}
68     
69         return localSite;
70     }
71
72     /**
73      * Create the localSite object
74      */

75     private static boolean parseLocalSiteFile(IPlatformConfiguration platformConfig, SiteLocal localSite ) throws CoreException, MalformedURLException {
76
77         //attempt to parse the LocalSite.xml
78
// URL resolvedURL = URLEncoder.encode(configXML);
79
try {
80 // InputStream in = UpdateCore.getPlugin().get(resolvedURL).getInputStream();
81
new SiteLocalParser(platformConfig, localSite);
82             return true;
83         } catch (Exception JavaDoc exception) {
84             return false;
85         }
86     }
87
88     /**
89      *
90      */

91     protected SiteLocal() {
92     }
93
94     /**
95      * adds a new configuration to the LocalSite
96      * the newly added configuration is teh current one
97      */

98     public void addConfiguration(IInstallConfiguration config) {
99         if (config != null) {
100             addConfigurationModel((InstallConfigurationModel) config);
101
102             trimHistoryToCapacity();
103
104             // set configuration as current
105
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             // notify listeners
114
Object JavaDoc[] siteLocalListeners = listeners.getListeners();
115             for (int i = 0; i < siteLocalListeners.length; i++) {
116                 ((ILocalSiteChangedListener) siteLocalListeners[i]).currentInstallConfigurationChanged(config);
117             }
118         }
119
120     }
121
122     /*
123      *
124      */

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

154     public void addLocalSiteChangedListener(ILocalSiteChangedListener listener) {
155         synchronized (listeners) {
156             listeners.add(listener);
157         }
158     }
159
160     /*
161      * @see ILocalSite#removeLocalSiteChangedListener(ILocalSiteChangedListener)
162      */

163     public void removeLocalSiteChangedListener(ILocalSiteChangedListener listener) {
164         synchronized (listeners) {
165             listeners.add(listener);
166         }
167     }
168
169     /**
170      * Saves the site into the config file.
171      * @return true if changes restart is needed
172      */

173     public boolean save() throws CoreException {
174
175         // Save the current configuration as
176
// the other are already saved
177
// and set runtim info for next startup
178
return ((InstallConfiguration) getCurrentConfiguration()).save();
179     }
180     
181 // /**
182
// * Method createNewInstallConfiguration.
183
// * @return IInstallConfiguration
184
// */
185
// private IInstallConfiguration createNewInstallConfiguration() throws CoreException {
186
// InstallConfiguration newInstallConfig = createConfigurationSite(null);
187
// newInstallConfig.setTimeline(newInstallConfig.getCreationDate().getTime());
188
// return newInstallConfig;
189
// }
190

191
192     /**
193      * @since 2.0
194      * @deprecated This method should not be used. The current install configuration is to be used.
195      */

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     /**
205      * @since 2.0
206      */

207     public void revertTo(IInstallConfiguration configuration, IProgressMonitor monitor, IProblemHandler handler) throws CoreException {
208
209         // create the activity
210
//Start UOW ?
211
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             // create a configuration
218
newConfiguration = cloneCurrentConfiguration();
219             newConfiguration.setLabel(configuration.getLabel());
220
221             // add to the stack which will set up as current
222
addConfiguration(newConfiguration);
223
224             // process delta
225
// the Configured featuresConfigured are the same as the old configuration
226
// the unconfigured featuresConfigured are the rest...
227
((InstallConfiguration) newConfiguration).revertTo(configuration, monitor, handler);
228
229             // everything done ok
230
activity.setStatus(IActivity.STATUS_OK);
231         } catch (CoreException e) {
232             // error
233
activity.setStatus(IActivity.STATUS_NOK);
234             throw e;
235         } catch (InterruptedException JavaDoc e) {
236             //user decided not to revert, do nothing
237
// because we didn't add the configuration to the history
238
} finally {
239             if (newConfiguration != null)
240                  ((InstallConfiguration) newConfiguration).addActivity(activity);
241         }
242
243     }
244
245     /**
246      * @since 2.0
247      * @deprecated
248      */

249     public IInstallConfiguration addToPreservedConfigurations(IInstallConfiguration configuration) throws CoreException {
250         return null;
251     }
252
253     /*
254      * @see ILocalSite#getPreservedConfigurationFor(IInstallConfiguration)
255      */

256     public IInstallConfiguration findPreservedConfigurationFor(IInstallConfiguration configuration) {
257
258         // based on time stamp for now
259
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     /*
276      * @see ILocalSite#getCurrentConfiguration()
277      * LocalSiteModel#getCurrentConfigurationModel() may return null if
278      * we just parsed LocalSite.xml
279      */

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

297     public IInstallConfiguration[] getPreservedConfigurations() {
298         if (getPreservedConfigurationsModel().length == 0)
299             return new IInstallConfiguration[0];
300         return (IInstallConfiguration[]) getPreservedConfigurationsModel();
301     }
302
303     /*
304      * @see ILocalSite#removeFromPreservedConfigurations(IInstallConfiguration)
305      */

306     public void removeFromPreservedConfigurations(IInstallConfiguration configuration) {
307         if (removePreservedConfigurationModel((InstallConfigurationModel) configuration))
308              ((InstallConfiguration) configuration).remove();
309     }
310
311     /*
312      * @see ILocalSite#getConfigurationHistory()
313      */

314     public IInstallConfiguration[] getConfigurationHistory() {
315         if (getConfigurationHistoryModel().length == 0)
316             return new IInstallConfiguration[0];
317         return (IInstallConfiguration[]) getConfigurationHistoryModel();
318     }
319
320
321     /**
322      * Gets the isTransient.
323      * @return Returns a boolean
324      */

325     public boolean isTransient() {
326         return isTransient;
327     }
328
329     /**
330      * Sets the isTransient.
331      * @param isTransient The isTransient to set
332      */

333     private void isTransient(boolean isTransient) {
334         this.isTransient = isTransient;
335     }
336
337     /*
338      *
339      */

340     private SiteStatusAnalyzer getSiteStatusAnalyzer() {
341         if (siteStatusAnalyzer == null)
342             siteStatusAnalyzer = new SiteStatusAnalyzer(this);
343         return siteStatusAnalyzer;
344     }
345
346     /*
347      * check if the Plugins of the feature are on the plugin path
348      * If all the plugins are on the plugin path, and the version match and there is no other version -> HAPPY
349      * If all the plugins are on the plugin path, and the version match and there is other version -> AMBIGUOUS
350      * If some of the plugins are on the plugin path, but not all -> UNHAPPY
351      * Check on all ConfiguredSites
352      */

353     public IStatus getFeatureStatus(IFeature feature) throws CoreException {
354         return getSiteStatusAnalyzer().getFeatureStatus(feature);
355     }
356     /**
357      * @see org.eclipse.update.internal.model.SiteLocalModel#setMaximumHistoryCount(int)
358      */

359     public void setMaximumHistoryCount(int history) {
360         super.setMaximumHistoryCount(history);
361         trimHistoryToCapacity();
362     }
363
364 }
365
Popular Tags