KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.update.core.ContentReference;
22 import org.eclipse.update.core.IFeature;
23 import org.eclipse.update.core.IFeatureContentConsumer;
24 import org.eclipse.update.core.IFeatureFactory;
25 import org.eclipse.update.core.IFeatureReference;
26 import org.eclipse.update.core.IInstallHandler;
27 import org.eclipse.update.core.INonPluginEntry;
28 import org.eclipse.update.core.IPluginEntry;
29 import org.eclipse.update.core.ISite;
30 import org.eclipse.update.core.ISiteFeatureReference;
31 import org.eclipse.update.core.IVerificationListener;
32 import org.eclipse.update.core.IVerifier;
33 import org.eclipse.update.core.InstallMonitor;
34 import org.eclipse.update.core.Site;
35 import org.eclipse.update.core.Utilities;
36 import org.eclipse.update.core.model.ContentEntryModel;
37 import org.eclipse.update.core.model.FeatureModel;
38 import org.eclipse.update.core.model.FeatureReferenceModel;
39 import org.eclipse.update.core.model.InstallAbortedException;
40 import org.eclipse.update.internal.operations.UpdateUtils;
41
42
43 /**
44  * Site on the File System
45  */

46 public class SiteFile extends Site {
47
48     /**
49      * plugin entries
50      */

51     private List JavaDoc pluginEntries = new ArrayList JavaDoc(0);
52
53     /**
54      *
55      */

56     public ISiteContentConsumer createSiteContentConsumer(IFeature targetFeature) throws CoreException {
57         SiteFileContentConsumer consumer = new SiteFileContentConsumer(targetFeature);
58         consumer.setSite(this);
59         return consumer;
60     }
61
62     /**
63      */

64     public String JavaDoc getDefaultPackagedFeatureType() {
65         return DEFAULT_INSTALLED_FEATURE_TYPE;
66     }
67
68     /*
69      * @see ISite#install(IFeature, IVerifier, IProgressMonitor)
70      */

71     public IFeatureReference install(IFeature sourceFeature, IVerificationListener verificationListener, IProgressMonitor progress) throws CoreException {
72         return install(sourceFeature,null,verificationListener,progress);
73     }
74
75     /*
76      * @see ISite#install(IFeature, IVerifier, IProgressMonitor)
77      */

78     public IFeatureReference install(IFeature sourceFeature, IFeatureReference[] optionalfeatures, IVerificationListener verificationListener, IProgressMonitor progress) throws CoreException {
79
80         if (sourceFeature == null)
81             return null;
82
83         // make sure we have an InstallMonitor
84
InstallMonitor monitor;
85         if (progress == null)
86             monitor = null;
87         else if (progress instanceof InstallMonitor)
88             monitor = (InstallMonitor) progress;
89         else
90             monitor = new InstallMonitor(progress);
91
92         // create new executable feature and install source content into it
93
IFeature localFeature = createExecutableFeature(sourceFeature);
94
95         IFeatureReference localFeatureReference = null;
96         localFeatureReference = sourceFeature.install(localFeature, optionalfeatures, verificationListener, monitor);
97
98         return localFeatureReference;
99     }
100
101     /*
102      * @see ISite#install(IFeature,IFeatureContentConsumer, IVerifier,IVerificationLIstener, IProgressMonitor)
103      */

104     public IFeatureReference install(IFeature sourceFeature, IFeatureReference[] optionalfeatures, IFeatureContentConsumer parentContentConsumer, IVerifier parentVerifier, IVerificationListener verificationListener, IProgressMonitor progress)
105         throws InstallAbortedException, CoreException {
106
107         if (sourceFeature == null)
108             return null;
109
110         // make sure we have an InstallMonitor
111
InstallMonitor monitor;
112         if (progress == null)
113             monitor = null;
114         else if (progress instanceof InstallMonitor)
115             monitor = (InstallMonitor) progress;
116         else
117             monitor = new InstallMonitor(progress);
118
119         // create new executable feature and install source content into it
120
IFeature localFeature = createExecutableFeature(sourceFeature);
121         parentContentConsumer.addChild(localFeature);
122
123         // set the verifier
124
IVerifier vr = sourceFeature.getFeatureContentProvider().getVerifier();
125         if (vr != null)
126             vr.setParent(parentVerifier);
127
128         IFeatureReference localFeatureReference = null;
129         localFeatureReference = sourceFeature.install(localFeature, optionalfeatures, verificationListener, monitor);
130
131         return localFeatureReference;
132     }
133
134     /*
135      * @see ISite#remove(IFeature, IProgressMonitor)
136      */

137     public void remove(IFeature feature, IProgressMonitor progress) throws CoreException {
138
139         if (feature == null) {
140             UpdateCore.warn("Feature to remove is null"); //$NON-NLS-1$
141
return;
142         }
143         
144         ErrorRecoveryLog recoveryLog = ErrorRecoveryLog.getLog();
145
146         // make sure we have an InstallMonitor
147
InstallMonitor monitor;
148         if (progress == null)
149             monitor = null;
150         else if (progress instanceof InstallMonitor)
151             monitor = (InstallMonitor) progress;
152         else
153             monitor = new InstallMonitor(progress);
154
155         // Setup optional install handler
156
InstallHandlerProxy handler = new InstallHandlerProxy(IInstallHandler.HANDLER_ACTION_UNINSTALL, feature, feature.getInstallHandlerEntry(), monitor);
157         boolean success = false;
158         Throwable JavaDoc originalException = null;
159
160         try {
161
162             // start log
163
recoveryLog.open(ErrorRecoveryLog.START_REMOVE_LOG);
164
165             aboutToRemove(feature);
166
167             // log files have been downloaded
168
recoveryLog.append(ErrorRecoveryLog.END_ABOUT_REMOVE);
169
170             handler.uninstallInitiated();
171
172             // remove the feature and the plugins if they are not used and not activated
173
// get the plugins from the feature
174
IPluginEntry[] pluginsToRemove = getPluginEntriesOnlyReferencedBy(feature);
175
176             if (monitor != null) {
177                 monitor.beginTask(Messages.SiteFile_Removing + feature.getLabel(), pluginsToRemove.length + 1);
178             }
179
180             // remove feature reference from the site
181
ISiteFeatureReference[] featureReferences = getFeatureReferences();
182             if (featureReferences != null) {
183                 for (int indexRef = 0; indexRef < featureReferences.length; indexRef++) {
184                     IFeatureReference element = featureReferences[indexRef];
185                     if (element.getVersionedIdentifier().equals(feature.getVersionedIdentifier())) {
186                         removeFeatureReferenceModel((FeatureReferenceModel) element);
187                         break;
188                     }
189                 }
190             }
191
192             if (InstallRegistry.getInstance().get("feature_"+feature.getVersionedIdentifier()) == null) { //$NON-NLS-1$
193
UpdateCore.log(NLS.bind(Messages.SiteFile_featureNotRemoved, (new String JavaDoc[] { feature.getVersionedIdentifier().toString() })), null);
194             } else {
195                 // remove the feature content
196
ContentReference[] references = feature.getFeatureContentProvider().getFeatureEntryArchiveReferences(monitor);
197                 for (int i = 0; i < references.length; i++) {
198                     try {
199                         UpdateManagerUtils.removeFromFileSystem(references[i].asFile());
200                         if (monitor != null)
201                             monitor.worked(1);
202                     } catch (IOException JavaDoc e) {
203                         throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_CannotRemoveFeature, (new String JavaDoc[] { feature.getVersionedIdentifier().getIdentifier(), getURL().toExternalForm() })), e);
204                     }
205                 }
206                 InstallRegistry.unregisterFeature(feature);
207             }
208
209             //finds the contentReferences for an IPluginEntry
210
// and remove it
211
for (int i = 0; i < pluginsToRemove.length; i++) {
212                 remove(feature, pluginsToRemove[i], monitor);
213             }
214
215             // remove any children feature
216
IFeatureReference[] childrenRef = feature.getIncludedFeatureReferences();
217             for (int i = 0; i < childrenRef.length; i++) {
218                 IFeature childFeature = null;
219                 try {
220                     childFeature = childrenRef[i].getFeature(null);
221                 } catch (CoreException e) {
222                     UpdateCore.warn("Unable to retrieve feature to remove for:" + childrenRef[i]); //$NON-NLS-1$
223
}
224                 // do not remove nested feature if configured (i.e. used by another configured feature)
225
if (childFeature != null && !getCurrentConfiguredSite().isConfigured(childFeature))
226                     remove(childrenRef[i].getFeature(null), monitor);
227             }
228
229             // remove the feature from the site cache
230
removeFeatureFromCache(feature.getURL());
231             
232             handler.completeUninstall();
233
234             success = true;
235         } catch (Throwable JavaDoc t) {
236             originalException = t;
237         } finally {
238             Throwable JavaDoc newException = null;
239             try {
240                 if (success) {
241                     // close the log
242
recoveryLog.close(ErrorRecoveryLog.END_REMOVE_LOG);
243                     recoveryLog.delete();
244                 } else {
245                     recoveryLog.close(ErrorRecoveryLog.END_REMOVE_LOG);
246                 }
247                 handler.uninstallCompleted(success);
248             } catch (Throwable JavaDoc t) {
249                 newException = t;
250             }
251             if (originalException != null) // original exception wins
252
throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String JavaDoc[] { feature.getLabel() })), originalException);
253             if (newException != null)
254                 throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String JavaDoc[] { feature.getLabel() })), newException);
255         }
256     }
257
258     /**
259      * returns the download size
260      * of the feature to be installed on the site.
261      * If the site is <code>null</code> returns the maximum size
262      *
263      * If one plug-in entry has an unknown size.
264      * then the download size is unknown.
265      *
266      */

267     public long getDownloadSizeFor(IFeature feature) {
268         long result = 0;
269         //[132029]
270
//IPluginEntry[] entriesToInstall = feature.getPluginEntries();
271
//IPluginEntry[] siteEntries = this.getPluginEntries();
272
//entriesToInstall = UpdateManagerUtils.diff(entriesToInstall, siteEntries);
273
//[18355]
274
//INonPluginEntry[] nonPluginEntriesToInstall = feature.getNonPluginEntries();
275

276         try {
277             //[132029]
278
//result = feature.getFeatureContentProvider().getDownloadSizeFor(entriesToInstall, nonPluginEntriesToInstall);
279
IFeatureReference[] children = feature.getIncludedFeatureReferences();
280             IFeature currentFeature = null;
281             for (int i = 0; i < children.length; i++) {
282                 currentFeature = UpdateUtils.getIncludedFeature(feature, children[i]);
283                 if (currentFeature != null) {
284                     result += getDownloadSizeFor(currentFeature);
285                     if(result == ContentEntryModel.UNKNOWN_SIZE)
286                         return result;
287                 }
288             }
289
290             IPluginEntry[] entriesToInstall = feature.getPluginEntries();
291             IPluginEntry[] siteEntries = this.getPluginEntries();
292             entriesToInstall = UpdateManagerUtils.diff(entriesToInstall, siteEntries);
293             //[18355]
294
INonPluginEntry[] nonPluginEntriesToInstall = feature.getNonPluginEntries();
295
296              result += feature.getFeatureContentProvider().getDownloadSizeFor(entriesToInstall, nonPluginEntriesToInstall);
297         } catch (CoreException e) {
298             UpdateCore.warn(null, e);
299             result = ContentEntryModel.UNKNOWN_SIZE;
300         }
301         return result;
302     }
303
304     /**
305      * returns the download size
306      * of the feature to be installed on the site.
307      * If the site is <code>null</code> returns the maximum size
308      *
309      * If one plug-in entry has an unknown size.
310      * then the download size is unknown.
311      *
312      * @see ISite#getDownloadSizeFor(IFeature)
313      *
314      */

315     public long getInstallSizeFor(IFeature feature) {
316         long result = 0;
317
318         try {
319             List JavaDoc pluginsToInstall = new ArrayList JavaDoc();
320
321             // get all the plugins [17304]
322
pluginsToInstall.addAll(Arrays.asList(feature.getPluginEntries()));
323             IFeatureReference[] children = feature.getIncludedFeatureReferences();
324             IFeature currentFeature = null;
325             for (int i = 0; i < children.length; i++) {
326                 currentFeature = UpdateUtils.getIncludedFeature(feature, children[i]);
327                 if (currentFeature != null) {
328                     //[132029]
329
//pluginsToInstall.addAll(Arrays.asList(currentFeature.getPluginEntries()));
330
result += getInstallSizeFor(currentFeature);
331                     if (result == ContentEntryModel.UNKNOWN_SIZE)
332                         return result;
333                 }
334             }
335
336             IPluginEntry[] entriesToInstall = new IPluginEntry[0];
337             if (pluginsToInstall.size() > 0) {
338                 entriesToInstall = new IPluginEntry[pluginsToInstall.size()];
339                 pluginsToInstall.toArray(entriesToInstall);
340             }
341
342             IPluginEntry[] siteEntries = this.getPluginEntries();
343             entriesToInstall = UpdateManagerUtils.diff(entriesToInstall, siteEntries);
344
345             //[18355]
346
INonPluginEntry[] nonPluginEntriesToInstall = feature.getNonPluginEntries();
347             
348             //[132029]
349
//result = feature.getFeatureContentProvider().getInstallSizeFor(entriesToInstall, nonPluginEntriesToInstall);
350
result += feature.getFeatureContentProvider().getInstallSizeFor(entriesToInstall, nonPluginEntriesToInstall);
351         } catch (CoreException e) {
352             UpdateCore.warn(null, e);
353             result = ContentEntryModel.UNKNOWN_SIZE;
354         }
355
356         return result;
357     }
358
359     /**
360      * Adds a plugin entry
361      * Either from parsing the file system or
362      * installing a feature
363      *
364      * We cannot figure out the list of plugins by reading the Site.xml as
365      * the archives tag are optionals
366      */

367     public void addPluginEntry(IPluginEntry pluginEntry) {
368         pluginEntries.add(pluginEntry);
369     }
370
371     public IPluginEntry[] getPluginEntries() {
372         IPluginEntry[] result = new IPluginEntry[0];
373         if (!(pluginEntries == null || pluginEntries.isEmpty())) {
374             result = new IPluginEntry[pluginEntries.size()];
375             pluginEntries.toArray(result);
376         }
377         return result;
378     }
379
380
381     public int getPluginEntryCount() {
382         return getPluginEntries().length;
383     }
384
385     /**
386      *
387      */

388     private IFeature createExecutableFeature(IFeature sourceFeature) throws CoreException {
389         IFeature result = null;
390         IFeatureFactory factory = FeatureTypeFactory.getInstance().getFactory(DEFAULT_INSTALLED_FEATURE_TYPE);
391         result = factory.createFeature(/*URL*/null, this, null);
392
393         // at least set the version identifier to be the same
394
((FeatureModel) result).setFeatureIdentifier(sourceFeature.getVersionedIdentifier().getIdentifier());
395         ((FeatureModel) result).setFeatureVersion(sourceFeature.getVersionedIdentifier().getVersion().toString());
396         return result;
397     }
398
399     /**
400      *
401      */

402     private void remove(IFeature feature, IPluginEntry pluginEntry, InstallMonitor monitor) throws CoreException {
403
404         if (pluginEntry == null)
405             return;
406             
407         if (InstallRegistry.getInstance().get("plugin_"+pluginEntry.getVersionedIdentifier()) == null) { //$NON-NLS-1$
408
UpdateCore.log(NLS.bind(Messages.SiteFile_pluginNotRemoved, (new String JavaDoc[] { pluginEntry.getVersionedIdentifier().toString() })), null);
409             return;
410         }
411
412         ContentReference[] references = feature.getFeatureContentProvider().getPluginEntryArchiveReferences(pluginEntry, monitor);
413         for (int i = 0; i < references.length; i++) {
414             try {
415                 UpdateManagerUtils.removeFromFileSystem(references[i].asFile());
416                 if (monitor != null)
417                     monitor.worked(1);
418             } catch (IOException JavaDoc e) {
419                 throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_CannotRemovePlugin, (new String JavaDoc[] { pluginEntry.getVersionedIdentifier().toString(), getURL().toExternalForm() })), e);
420             }
421         }
422         pluginEntries.remove(pluginEntry);
423         InstallRegistry.unregisterPlugin(pluginEntry);
424     }
425
426     /*
427      *
428      */

429     private void aboutToRemove(IFeature feature) throws CoreException {
430
431         ErrorRecoveryLog recoveryLog = ErrorRecoveryLog.getLog();
432         // if the recovery is not turned on
433
if (!ErrorRecoveryLog.RECOVERY_ON)
434             return;
435
436         //logFeature
437
if (feature != null) {
438                     
439             // log feature URL
440
ContentReference[] references = feature.getFeatureContentProvider().getFeatureEntryArchiveReferences(null);
441             for (int i = 0; i < references.length; i++) {
442                 try {
443                     recoveryLog.appendPath(ErrorRecoveryLog.FEATURE_ENTRY, references[i].asFile().getAbsolutePath());
444                 } catch (IOException JavaDoc e) {
445                     String JavaDoc id = UpdateCore.getPlugin().getBundle().getSymbolicName();
446                     throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_CannotRemoveFeature, (new String JavaDoc[] { feature.getVersionedIdentifier().getIdentifier(), getURL().toExternalForm() })), e);
447                 }
448             }
449             // log pluginEntry URL
450
IPluginEntry[] pluginsToRemove = getPluginEntriesOnlyReferencedBy(feature);
451             for (int i = 0; i < pluginsToRemove.length; i++) {
452                 references = feature.getFeatureContentProvider().getPluginEntryArchiveReferences(pluginsToRemove[i], null);
453                 for (int j = 0; j < references.length; j++) {
454                     try {
455                         recoveryLog.appendPath(ErrorRecoveryLog.BUNDLE_JAR_ENTRY, references[j].asFile().getAbsolutePath());
456                     } catch (IOException JavaDoc e) {
457                         throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_CannotRemovePlugin, (new String JavaDoc[] { pluginsToRemove[i].getVersionedIdentifier().toString(), getURL().toExternalForm() })), e);
458                     }
459                 }
460             }
461         }
462
463         // call recursively for each children
464
IFeatureReference[] childrenRef = feature.getIncludedFeatureReferences();
465         IFeature childFeature = null;
466         for (int i = 0; i < childrenRef.length; i++) {
467             try {
468                 childFeature = childrenRef[i].getFeature(null);
469             } catch (CoreException e) {
470                 UpdateCore.warn("Unable to retrieve feature to remove for:" + childrenRef[i]); //$NON-NLS-1$
471
}
472             aboutToRemove(childFeature);
473         }
474     }
475
476 }
477
Popular Tags