KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > Feature


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.core;
12
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.Vector JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.core.runtime.SubProgressMonitor;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.update.core.model.ContentEntryModel;
27 import org.eclipse.update.core.model.FeatureModel;
28 import org.eclipse.update.core.model.FeatureReferenceModel;
29 import org.eclipse.update.core.model.ImportModel;
30 import org.eclipse.update.core.model.InstallAbortedException;
31 import org.eclipse.update.core.model.NonPluginEntryModel;
32 import org.eclipse.update.core.model.PluginEntryModel;
33 import org.eclipse.update.core.model.URLEntryModel;
34 import org.eclipse.update.internal.core.ErrorRecoveryLog;
35 import org.eclipse.update.internal.core.InstallHandlerProxy;
36 import org.eclipse.update.internal.core.InstallRegistry;
37 import org.eclipse.update.internal.core.Messages;
38 import org.eclipse.update.internal.core.TargetFeature;
39 import org.eclipse.update.internal.core.UpdateCore;
40 import org.eclipse.update.internal.core.UpdateManagerUtils;
41 import org.eclipse.update.internal.core.UpdateSiteIncludedFeatureReference;
42
43 /**
44  * Convenience implementation of a feature.
45  * <p>
46  * This class may be instantiated or subclassed by clients.
47  * </p>
48  * <p>
49  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
50  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
51  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
52  * (repeatedly) as the API evolves.
53  * </p>
54  * @see org.eclipse.update.core.IFeature
55  * @see org.eclipse.update.core.model.FeatureModel
56  * @since 2.0
57  */

58 public class Feature extends FeatureModel implements IFeature {
59
60     /**
61      * Simple file name of the default feature manifest file
62      * @since 2.0
63      */

64     public static final String JavaDoc FEATURE_FILE = "feature"; //$NON-NLS-1$
65

66     /**
67      * File extension of the default feature manifest file
68      * @since 2.0
69      */

70     public static final String JavaDoc FEATURE_XML = FEATURE_FILE + ".xml"; //$NON-NLS-1$
71

72     private ISite site; // feature site
73
private IFeatureContentProvider featureContentProvider; // content provider
74
private List JavaDoc /*of IFeatureReference*/
75     includedFeatureReferences;
76
77     //PERF: new instance variable
78
private VersionedIdentifier versionId;
79
80     private InstallAbortedException abortedException = null;
81
82     /**
83      * Feature default constructor
84      *
85      * @since 2.0
86      */

87     public Feature() {
88     }
89
90     /**
91      * Compares two features for equality
92      *
93      * @param object feature object to compare with
94      * @return <code>true</code> if the two features are equal,
95      * <code>false</code> otherwise
96      * @since 2.0
97      */

98     public boolean equals(Object JavaDoc object) {
99         if (!(object instanceof IFeature))
100             return false;
101         IFeature f = (IFeature) object;
102         return getVersionedIdentifier().equals(f.getVersionedIdentifier());
103     }
104
105     /**
106      * Returns the feature identifier.
107      *
108      * @see IFeature#getVersionedIdentifier()
109      * @since 2.0
110      */

111     public VersionedIdentifier getVersionedIdentifier() {
112         if (versionId != null)
113             return versionId;
114
115         String JavaDoc id = getFeatureIdentifier();
116         String JavaDoc ver = getFeatureVersion();
117         if (id != null && ver != null) {
118             try {
119                 versionId = new VersionedIdentifier(id, ver);
120                 return versionId;
121             } catch (Exception JavaDoc e) {
122                 UpdateCore.warn(
123                     "Unable to create versioned identifier:" + id + ":" + ver); //$NON-NLS-1$ //$NON-NLS-2$
124
}
125         }
126
127         versionId = new VersionedIdentifier(getURL().toExternalForm(), null);
128         return versionId;
129     }
130
131     /**
132      * Returns the site this feature is associated with.
133      *
134      * @see IFeature#getSite()
135      * @since 2.0
136      */

137     public ISite getSite() {
138         return site;
139     }
140
141     /**
142      * Returns the feature URL.
143      *
144      * @see IFeature#getURL()
145      * @since 2.0
146      */

147     public URL JavaDoc getURL() {
148         IFeatureContentProvider contentProvider = null;
149         try {
150             contentProvider = getFeatureContentProvider();
151         } catch (CoreException e) {
152             UpdateCore.warn("No content Provider", e); //$NON-NLS-1$
153
}
154         return (contentProvider != null) ? contentProvider.getURL() : null;
155     }
156
157     /**
158      * Returns an information entry referencing the location of the
159      * feature update site.
160      *
161      * @see IFeature#getUpdateSiteEntry()
162      * @since 2.0
163      */

164     public IURLEntry getUpdateSiteEntry() {
165         return (IURLEntry) getUpdateSiteEntryModel();
166     }
167
168     /**
169      * Return an array of information entries referencing locations of other
170      * update sites.
171      *
172      * @see IFeature#getDiscoverySiteEntries()
173      * @since 2.0
174      */

175     public IURLEntry[] getDiscoverySiteEntries() {
176         URLEntryModel[] result = getDiscoverySiteEntryModels();
177         if (result.length == 0)
178             return new IURLEntry[0];
179         else
180             return (IURLEntry[]) result;
181     }
182
183     /**
184      * Returns and optional custom install handler entry.
185      *
186      * @see IFeature#getInstallHandlerEntry()
187      * @since 2.0
188      */

189     public IInstallHandlerEntry getInstallHandlerEntry() {
190         return (IInstallHandlerEntry) getInstallHandlerModel();
191     }
192
193     /**
194      * Returns the feature description.
195      *
196      * @see IFeature#getDescription()
197      * @since 2.0
198      */

199     public IURLEntry getDescription() {
200         return (IURLEntry) getDescriptionModel();
201     }
202
203     /**
204      * Returns the copyright information for the feature.
205      *
206      * @see IFeature#getCopyright()
207      * @since 2.0
208      */

209     public IURLEntry getCopyright() {
210         return (IURLEntry) getCopyrightModel();
211     }
212
213     /**
214      * Returns the license information for the feature.
215      *
216      * @see IFeature#getLicense()
217      * @since 2.0
218      */

219     public IURLEntry getLicense() {
220         return (IURLEntry) getLicenseModel();
221     }
222
223     /**
224      * Return optional image for the feature.
225      *
226      * @see IFeature#getImage()
227      * @since 2.0
228      */

229     public URL JavaDoc getImage() {
230         return getImageURL();
231     }
232
233     /**
234      * Return a list of plug-in dependencies for this feature.
235      *
236      * @see IFeature#getRawImports()
237      * @since 2.0
238      */

239     public IImport[] getRawImports() {
240         ImportModel[] result = getImportModels();
241         if (result.length == 0)
242             return new IImport[0];
243         else
244             return (IImport[]) result;
245     }
246
247     /**
248      * Install the contents of this feature into the specified target feature.
249      * This method is a reference implementation of the feature installation
250      * protocol. Other concrete feature implementation that override this
251      * method need to implement this protocol.
252      *
253      * @see IFeature#install(IFeature, IVerificationListener, IProgressMonitor)
254      * @since 2.0
255      */

256     public IFeatureReference install(
257         IFeature targetFeature,
258         IVerificationListener verificationListener,
259         IProgressMonitor progress)
260         throws InstallAbortedException, CoreException {
261         // call other API with all optional features, or setup variable meaning install all
262
return install(targetFeature, null, verificationListener, progress);
263     }
264
265     /**
266      * Install the contents of this feature into the specified target feature.
267      * This method is a reference implementation of the feature installation
268      * protocol. Other concrete feature implementation that override this
269      * method need to implement this protocol.
270      *
271      * @see IFeature#install(IFeature, IVerificationListener, IProgressMonitor)
272      * @since 2.0
273      */

274     public IFeatureReference install(
275         IFeature targetFeature,
276         IFeatureReference[] optionalfeatures,
277         IVerificationListener verificationListener,
278         IProgressMonitor progress)
279         throws InstallAbortedException, CoreException {
280
281         //DEBUG
282
debug("Installing...:" + getURL().toExternalForm()); //$NON-NLS-1$
283
ErrorRecoveryLog recoveryLog = ErrorRecoveryLog.getLog();
284
285         // make sure we have an InstallMonitor
286
InstallMonitor monitor;
287         if (progress == null)
288             monitor = new InstallMonitor(new NullProgressMonitor());
289         else if (progress instanceof InstallMonitor)
290             monitor = (InstallMonitor) progress;
291         else
292             monitor = new InstallMonitor(progress);
293
294         // Setup optional install handler
295
InstallHandlerProxy handler =
296             new InstallHandlerProxy(
297                 IInstallHandler.HANDLER_ACTION_INSTALL,
298                 this,
299                 this.getInstallHandlerEntry(),
300                 monitor);
301         boolean success = false;
302         Throwable JavaDoc originalException = null;
303         abortedException = null;
304
305         // Get source feature provider and verifier.
306
// Initialize target variables.
307
IFeatureContentProvider provider = getFeatureContentProvider();
308         IVerifier verifier = provider.getVerifier();
309         IFeatureReference result = null;
310         IFeatureReference alreadyInstalledFeature = null;
311         IFeatureContentConsumer consumer = null;
312         IPluginEntry[] targetSitePluginEntries = null;
313         ArrayList JavaDoc justInstalledPlugins = new ArrayList JavaDoc();
314
315         try {
316             // determine list of plugins to install
317
// find the intersection between the plugin entries already contained
318
// on the target site, and plugin entries packaged in source feature
319
IPluginEntry[] sourceFeaturePluginEntries = getPluginEntries();
320             ISite targetSite = targetFeature.getSite();
321             if (targetSite == null) {
322                 debug("The site to install in is null"); //$NON-NLS-1$
323
targetSitePluginEntries = new IPluginEntry[0];
324             } else {
325                 targetSitePluginEntries = targetSite.getPluginEntries();
326             }
327             IPluginEntry[] pluginsToInstall =
328                 UpdateManagerUtils.diff(
329                     sourceFeaturePluginEntries,
330                     targetSitePluginEntries);
331             INonPluginEntry[] nonPluginsToInstall = getNonPluginEntries();
332
333             IFeatureReference[] children = getIncludedFeatureReferences();
334             if (optionalfeatures != null) {
335                 children =
336                     UpdateManagerUtils.optionalChildrenToInstall(
337                         children,
338                         optionalfeatures);
339             }
340
341             // determine number of monitor tasks
342
// 2 tasks for the feature jar (download/verify + install)
343
// + 2*n tasks for plugin entries (download/verify + install for each)
344
// + 1*m tasks per non-plugin data entry (download for each)
345
// + 1 task for custom non-plugin entry handling (1 for all combined)
346
// + 5*x tasks for children features (5 subtasks per install)
347
int taskCount =
348                 2
349                     + 2 * pluginsToInstall.length
350                     + nonPluginsToInstall.length
351                     + 1
352                     + 5 * children.length;
353             monitor.beginTask("", taskCount); //$NON-NLS-1$
354
SubProgressMonitor subMonitor = null;
355
356             // start log
357
recoveryLog.open(ErrorRecoveryLog.START_INSTALL_LOG);
358
359             // Start the installation tasks
360
handler.installInitiated();
361
362             // Download and verify feature archive(s)
363
ContentReference[] references =
364                 provider.getFeatureEntryArchiveReferences(monitor);
365             verifyReferences(
366                 verifier,
367                 references,
368                 monitor,
369                 verificationListener,
370                 true);
371             monitorWork(monitor, 1);
372             
373             // Download and verify plugin archives
374
for (int i = 0; i < pluginsToInstall.length; i++) {
375                 references = provider.getPluginEntryArchiveReferences(
376                         pluginsToInstall[i], monitor);
377                 verifyReferences(verifier, references, monitor,
378                                 verificationListener, false);
379                 monitorWork(monitor, 1);
380             }
381             
382             handler.pluginsDownloaded(pluginsToInstall);
383
384             Vector JavaDoc filteredPlugins = new Vector JavaDoc();
385             // Download non-plugin archives. Verification handled by optional
386
// install handler
387
for (int i = 0; i < nonPluginsToInstall.length; i++) {
388                 if (handler.acceptNonPluginData(nonPluginsToInstall[i])) {
389                     references = provider.getNonPluginEntryArchiveReferences(
390                             nonPluginsToInstall[i], monitor);
391                     monitorWork(monitor, 1);
392                     filteredPlugins.add(nonPluginsToInstall[i]);
393                 }
394             }
395             nonPluginsToInstall = (INonPluginEntry[]) filteredPlugins
396                     .toArray(new INonPluginEntry[0]);
397             handler.nonPluginDataDownloaded(nonPluginsToInstall,
398                     verificationListener);
399
400             // All archives are downloaded and verified. Get ready to install
401
consumer = targetFeature.getFeatureContentConsumer();
402
403             // install the children feature
404
// check if they are optional, and if they should be installed [2.0.1]
405
for (int i = 0; i < children.length; i++) {
406                 IFeature childFeature = null;
407                 try {
408                     childFeature = children[i].getFeature(null);
409                 } catch (CoreException e) {
410                     UpdateCore.warn(null, e);
411                 }
412                 if (childFeature != null) {
413                     subMonitor = new SubProgressMonitor(monitor, 5);
414                     ((Site) targetSite).install(// need to cast
415
childFeature,
416                         optionalfeatures,
417                         consumer,
418                         verifier,
419                         verificationListener,
420                         subMonitor);
421                 }
422             }
423
424             // Install plugin files
425
for (int i = 0; i < pluginsToInstall.length; i++) {
426                 // if another feature has already installed this plugin, skip it
427
if (InstallRegistry.getInstance().isPluginJustInstalled(pluginsToInstall[i])) {
428                     monitor.worked(1);
429                     continue;
430                 }
431                 IContentConsumer pluginConsumer =
432                     consumer.open(pluginsToInstall[i]);
433                 // TODO consumer.open returns either
434
// SiteFilePackedPluginContentConsumer or SiteFilePluginContentConsumer
435
// and they are fed either
436
// PluginEntryArchiveReference or PluginEntryContentReferences
437
// it would be better to have one that is given PluginEntryArchiveReference
438
// but it would break external IContentConsumers
439

440                 if(pluginsToInstall[i] instanceof PluginEntryModel && !((PluginEntryModel)pluginsToInstall[i]).isUnpack()){
441                     // plugin can run from a jar
442
references = provider.getPluginEntryArchiveReferences(
443                             pluginsToInstall[i], monitor);
444                 } else{
445                     // plugin must be unpacked
446
references =
447                         provider.getPluginEntryContentReferences(
448                             pluginsToInstall[i],
449                             monitor);
450                 }
451
452                 String JavaDoc msg = ""; //$NON-NLS-1$
453
subMonitor = new SubProgressMonitor(monitor, 1);
454                 VersionedIdentifier pluginVerId =
455                     pluginsToInstall[i].getVersionedIdentifier();
456                 String JavaDoc pluginID =
457                     (pluginVerId == null) ? "" : pluginVerId.getIdentifier(); //$NON-NLS-1$
458
msg = NLS.bind(Messages.Feature_TaskInstallPluginFiles, (new String JavaDoc[] { pluginID }));
459
460                 for (int j = 0; j < references.length; j++) {
461                     setMonitorTaskName(
462                         subMonitor,
463                         msg + references[j].getIdentifier());
464                     pluginConsumer.store(references[j], subMonitor);
465                 }
466
467                 if (monitor.isCanceled())
468                     abort();
469                 else {
470                     justInstalledPlugins.add(pluginsToInstall[i]);
471                     InstallRegistry.registerPlugin(pluginsToInstall[i]);
472                 }
473             }
474
475             // check if we need to install feature files [16718]
476
// store will throw CoreException if another feature is already
477
// installed in the same place
478
alreadyInstalledFeature = featureAlreadyInstalled(targetSite);
479             // 18867
480
if (alreadyInstalledFeature == null) {
481                 //Install feature files
482
references = provider.getFeatureEntryContentReferences(monitor);
483
484                 String JavaDoc msg = ""; //$NON-NLS-1$
485
subMonitor = new SubProgressMonitor(monitor, 1);
486                 msg = Messages.Feature_TaskInstallFeatureFiles;
487
488                 for (int i = 0; i < references.length; i++) {
489                     setMonitorTaskName(
490                         subMonitor,
491                         msg + " " + references[i].getIdentifier()); //$NON-NLS-1$
492
consumer.store(references[i], subMonitor);
493                 }
494
495                 if (monitor.isCanceled())
496                     abort();
497                 else
498                     InstallRegistry.registerFeature(this);
499             } else {
500                 if (monitor.isCanceled())
501                     abort();
502                 else
503                     monitor.worked(1);
504             }
505
506             // call handler to complete installation (eg. handle non-plugin entries)
507
handler.completeInstall(consumer);
508             monitorWork(monitor, 1);
509
510             // indicate install success
511
success = true;
512
513         } catch (InstallAbortedException e) {
514             abortedException = e;
515         } catch (CoreException e) {
516             originalException = e;
517         } finally {
518             Exception JavaDoc newException = null;
519             try {
520                 if (consumer != null) {
521                     if (success) {
522                         result = consumer.close();
523                         if (result == null) {
524                             result = alreadyInstalledFeature; // 18867
525
if (result != null
526                                 && optionalfeatures != null
527                                 && optionalfeatures.length > 0) {
528                                 // reinitialize as new optional children may have been installed
529
reinitializeFeature(result);
530                             }
531                         }
532                         // close the log
533
recoveryLog.close(ErrorRecoveryLog.END_INSTALL_LOG);
534                     } else {
535                         // unregister the just installed plugins
536
for (int i=0; i<justInstalledPlugins.size(); i++)
537                             InstallRegistry.unregisterPlugin(((IPluginEntry)justInstalledPlugins.get(i)));
538                         consumer.abort();
539                     }
540                 }
541                 handler.installCompleted(success);
542                 // if abort is done, no need for the log to stay
543
recoveryLog.delete();
544             } catch (CoreException e) {
545                 newException = e;
546             }
547
548             // original exception wins unless it is InstallAbortedException
549
// and an error occured during abort
550
if (originalException != null) {
551                 throw Utilities.newCoreException(
552                     NLS.bind(Messages.InstallHandler_error, (new String JavaDoc[] { this.getLabel() })),
553                     originalException);
554             }
555
556             if (newException != null)
557                 throw Utilities.newCoreException(
558                     NLS.bind(Messages.InstallHandler_error, (new String JavaDoc[] { this.getLabel() })),
559                     newException);
560
561             if (abortedException != null) {
562                 throw abortedException;
563             }
564
565         }
566         return result;
567     }
568
569     /**
570      * Returns an array of plug-in entries referenced by this feature
571      *
572      * @see IFeature#getPluginEntries()
573      * @since 2.0
574      */

575     public IPluginEntry[] getRawPluginEntries() {
576         PluginEntryModel[] result = getPluginEntryModels();
577         if (result.length == 0)
578             return new IPluginEntry[0];
579         else
580             return (IPluginEntry[]) result;
581     }
582
583     /*
584      * Method filter.
585      * @param result
586      * @return IPluginEntry[]
587      */

588     private IPluginEntry[] filterPluginEntry(IPluginEntry[] all) {
589         List JavaDoc list = new ArrayList JavaDoc();
590         if (all != null) {
591             for (int i = 0; i < all.length; i++) {
592                 if (UpdateManagerUtils.isValidEnvironment(all[i]))
593                     list.add(all[i]);
594             }
595         }
596
597         IPluginEntry[] result = new IPluginEntry[list.size()];
598         if (!list.isEmpty()) {
599             list.toArray(result);
600         }
601
602         return result;
603     }
604
605     /**
606      * Returns the count of referenced plug-in entries.
607      *
608      * @see IFeature#getPluginEntryCount()
609      * @since 2.0
610      */

611     public int getPluginEntryCount() {
612         return getPluginEntries().length;
613     }
614
615     /**
616      * Returns an array of non-plug-in entries referenced by this feature
617      *
618      * @see IFeature#getNonPluginEntries()
619      * @since 2.0
620      */

621     public INonPluginEntry[] getRawNonPluginEntries() {
622         NonPluginEntryModel[] result = getNonPluginEntryModels();
623         if (result.length == 0)
624             return new INonPluginEntry[0];
625         else
626             return (INonPluginEntry[]) result;
627     }
628
629     /**
630      * Returns the count of referenced non-plug-in entries.
631      *
632      * @see IFeature#getNonPluginEntryCount()
633      * @since 2.0
634      */

635     public int getNonPluginEntryCount() {
636         return getNonPluginEntryModels().length;
637     }
638
639     /**
640      * Returns an array of feature references included by this feature
641      *
642      * @return an erray of feature references, or an empty array.
643      * @since 2.0
644      */

645     public IIncludedFeatureReference[] getRawIncludedFeatureReferences()
646         throws CoreException {
647         if (includedFeatureReferences == null)
648             initializeIncludedReferences();
649
650         if (includedFeatureReferences.size() == 0)
651             return new IncludedFeatureReference[0];
652
653         return (IIncludedFeatureReference[]) includedFeatureReferences.toArray(
654             new IIncludedFeatureReference[includedFeatureReferences.size()]);
655     }
656     /**
657      * Returns the download size of the feature, if it can be determined.
658      *
659      * @see IFeature#getDownloadSize()
660      * @since 2.0
661      */

662     public long getDownloadSize() {
663         try {
664             Set JavaDoc allPluginEntries = new HashSet JavaDoc();
665             Set JavaDoc allNonPluginEntries = new HashSet JavaDoc();
666
667             IPluginEntry[] plugins = getPluginEntries();
668             allPluginEntries.addAll(Arrays.asList(plugins));
669             INonPluginEntry[] nonPlugins = getNonPluginEntries();
670             allNonPluginEntries.addAll(Arrays.asList(nonPlugins));
671
672             IFeatureReference[] children = getIncludedFeatureReferences();
673             for (int i = 0; i < children.length; i++) {
674                 plugins = children[i].getFeature(null).getPluginEntries();
675                 allPluginEntries.addAll(Arrays.asList(plugins));
676                 nonPlugins = children[i].getFeature(null).getNonPluginEntries();
677                 allNonPluginEntries.addAll(Arrays.asList(nonPlugins));
678             }
679
680             IPluginEntry[] totalPlugins =
681                 new IPluginEntry[allPluginEntries.size()];
682             INonPluginEntry[] totalNonPlugins =
683                 new INonPluginEntry[allNonPluginEntries.size()];
684             if (allPluginEntries.size() != 0) {
685                 allPluginEntries.toArray(totalPlugins);
686             }
687             if (allNonPluginEntries.size() != 0) {
688                 allNonPluginEntries.toArray(totalNonPlugins);
689             }
690
691             return getFeatureContentProvider().getDownloadSizeFor(
692                 totalPlugins,
693                 totalNonPlugins);
694
695         } catch (CoreException e) {
696             UpdateCore.warn(null, e);
697             return ContentEntryModel.UNKNOWN_SIZE;
698         }
699     }
700
701     /**
702      * Returns the install size of the feature, if it can be determined.
703      *
704      * @see IFeature#getInstallSize()
705      * @since 2.0
706      */

707     public long getInstallSize() {
708         try {
709             Set JavaDoc allPluginEntries = new HashSet JavaDoc();
710             Set JavaDoc allNonPluginEntries = new HashSet JavaDoc();
711
712             IPluginEntry[] plugins = getPluginEntries();
713             allPluginEntries.addAll(Arrays.asList(plugins));
714             INonPluginEntry[] nonPlugins = getNonPluginEntries();
715             allNonPluginEntries.addAll(Arrays.asList(nonPlugins));
716
717             IFeatureReference[] children = getIncludedFeatureReferences();
718             for (int i = 0; i < children.length; i++) {
719                 plugins = children[i].getFeature(null).getPluginEntries();
720                 allPluginEntries.addAll(Arrays.asList(plugins));
721                 nonPlugins = children[i].getFeature(null).getNonPluginEntries();
722                 allNonPluginEntries.addAll(Arrays.asList(nonPlugins));
723             }
724
725             IPluginEntry[] totalPlugins =
726                 new IPluginEntry[allPluginEntries.size()];
727             INonPluginEntry[] totalNonPlugins =
728                 new INonPluginEntry[allNonPluginEntries.size()];
729             if (allPluginEntries.size() != 0) {
730                 allPluginEntries.toArray(totalPlugins);
731             }
732             if (allNonPluginEntries.size() != 0) {
733                 allNonPluginEntries.toArray(totalNonPlugins);
734             }
735
736             return getFeatureContentProvider().getInstallSizeFor(
737                 totalPlugins,
738                 totalNonPlugins);
739
740         } catch (CoreException e) {
741             UpdateCore.warn(null, e);
742             return ContentEntryModel.UNKNOWN_SIZE;
743         }
744     }
745
746     /**
747      * Returns the content provider for this feature.
748      *
749      * @see IFeature#getFeatureContentProvider()
750      * @since 2.0
751      */

752     public IFeatureContentProvider getFeatureContentProvider()
753         throws CoreException {
754         if (featureContentProvider == null) {
755             throw Utilities.newCoreException(
756                 NLS.bind(Messages.Feature_NoContentProvider, (new String JavaDoc[] { getVersionedIdentifier().toString() })),
757                 null);
758         }
759         return this.featureContentProvider;
760     }
761
762     /**
763      * Returns the content consumer for this feature.
764      *
765      * @see IFeature#getFeatureContentConsumer()
766      * @since 2.0
767      */

768     public IFeatureContentConsumer getFeatureContentConsumer()
769         throws CoreException {
770         throw new UnsupportedOperationException JavaDoc();
771     }
772
773     /**
774      * Sets the site for this feature.
775      *
776      * @see IFeature#setSite(ISite)
777      * @since 2.0
778      */

779     public void setSite(ISite site) throws CoreException {
780         if (this.site != null) {
781             String JavaDoc featureURLString =
782                 (getURL() != null) ? getURL().toExternalForm() : ""; //$NON-NLS-1$
783
throw Utilities.newCoreException(
784                 NLS.bind(Messages.Feature_SiteAlreadySet, (new String JavaDoc[] { featureURLString })),
785                 null);
786         }
787         this.site = site;
788     }
789
790     /**
791      * Sets the content provider for this feature.
792      *
793      * @see IFeature#setFeatureContentProvider(IFeatureContentProvider)
794      * @since 2.0
795      */

796     public void setFeatureContentProvider(IFeatureContentProvider featureContentProvider) {
797         this.featureContentProvider = featureContentProvider;
798         featureContentProvider.setFeature(this);
799     }
800
801     /**
802      * Return the string representation of this fetaure
803      *
804      * @return feature as string
805      * @since 2.0
806      */

807     public String JavaDoc toString() {
808         String JavaDoc URLString =
809             (getURL() == null)
810                 ? Messages.Feature_NoURL
811                 : getURL().toExternalForm();
812
813         String JavaDoc verString =
814             NLS.bind(Messages.Feature_FeatureVersionToString, (new String JavaDoc[] { URLString, getVersionedIdentifier().toString() }));
815         String JavaDoc label = getLabel() == null ? "" : getLabel(); //$NON-NLS-1$
816
return verString + " [" + label + "]"; //$NON-NLS-1$ //$NON-NLS-2$
817
}
818
819     /*
820      * Installation has been cancelled, abort and revert
821      */

822     private void abort() throws CoreException {
823         String JavaDoc msg = Messages.Feature_InstallationCancelled;
824         throw new InstallAbortedException(msg, null);
825     }
826
827     /*
828      * Initializes includes feature references
829      * If the included feature reference is found on the site, add it to the List
830      * Otherwise attempt to instanciate it using the same type as this feature and
831      * using the default location on the site.
832      */

833     private void initializeIncludedReferences() throws CoreException {
834         includedFeatureReferences = new ArrayList JavaDoc();
835
836         IIncludedFeatureReference[] nestedFeatures = getFeatureIncluded();
837         if (nestedFeatures.length == 0)
838             return;
839
840         ISite site = getSite();
841         if (site == null)
842             return;
843
844         for (int i = 0; i < nestedFeatures.length; i++) {
845             IIncludedFeatureReference include = nestedFeatures[i];
846             IIncludedFeatureReference newRef =
847                 getPerfectIncludeFeature(site, include);
848             includedFeatureReferences.add(newRef);
849         }
850     }
851
852     /*
853      *
854      */

855     private IIncludedFeatureReference getPerfectIncludeFeature(
856         ISite site,
857         IIncludedFeatureReference include)
858         throws CoreException {
859
860         // [20367] no site, cannot initialize nested references
861
ISiteFeatureReference[] refs = site.getFeatureReferences();
862         VersionedIdentifier identifier = include.getVersionedIdentifier();
863
864         // too long to compute if not a file system
865
// other solution would be to parse feature.xml
866
// when parsing file system to create archive features/FeatureId_Ver.jar
867
if ("file".equals(site.getURL().getProtocol())) { //$NON-NLS-1$
868
// check if declared on the Site
869
if (refs != null) {
870                 for (int ref = 0; ref < refs.length; ref++) {
871                     if (refs[ref] != null) {
872                         VersionedIdentifier id =
873                             refs[ref].getVersionedIdentifier();
874                         if (identifier.equals(id)) {
875                             // found a ISiteFeatureReference that matches our IIncludedFeatureReference
876
IncludedFeatureReference newRef =
877                                 new IncludedFeatureReference(refs[ref]);
878                             newRef.isOptional(include.isOptional());
879                             if (include instanceof FeatureReferenceModel)
880                                 newRef.setLabel(
881                                     ((FeatureReferenceModel) include)
882                                         .getLabel());
883                             newRef.setSearchLocation(
884                                 include.getSearchLocation());
885                             return newRef;
886                         }
887                     }
888                 }
889             }
890         }
891
892         // instantiate by mapping it based on the site.xml
893
// in future we may ask for a factory to create the feature ref
894
IncludedFeatureReference newRef = new UpdateSiteIncludedFeatureReference(include);
895         newRef.setSite(getSite());
896         IFeatureReference parentRef = getSite().getFeatureReference(this);
897         if (parentRef instanceof FeatureReference) {
898          newRef.setType(((FeatureReference) parentRef).getType());
899         }
900         String JavaDoc featureID = Site.DEFAULT_FEATURE_PATH + identifier.toString();
901         if(this instanceof TargetFeature)
902          featureID = featureID + "/"; //$NON-NLS-1$
903
else
904          featureID = featureID + ".jar"; //$NON-NLS-1$
905
URL JavaDoc featureURL =
906             getSite().getSiteContentProvider().getArchiveReference(featureID);
907         newRef.setURL(featureURL);
908         newRef.setFeatureIdentifier(identifier.getIdentifier());
909         newRef.setFeatureVersion(identifier.getVersion().toString());
910         try {
911             newRef.resolve(getSite().getURL(), null);
912             // no need to get the bundle
913
return newRef;
914         } catch (Exception JavaDoc e) {
915             throw Utilities.newCoreException(
916                 NLS.bind(Messages.Feature_UnableToInitializeFeatureReference, (new String JavaDoc[] { identifier.toString() })),
917                 e);
918         }
919     }
920
921     /*
922      *
923      */

924     private void debug(String JavaDoc trace) {
925         //DEBUG
926
if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_INSTALL) {
927             UpdateCore.debug(trace);
928         }
929     }
930
931     /*
932      *
933      */

934     private void setMonitorTaskName(
935         IProgressMonitor monitor,
936         String JavaDoc taskName) {
937         if (monitor != null)
938             monitor.setTaskName(taskName);
939     }
940
941     /*
942      *
943      */

944     private void monitorWork(IProgressMonitor monitor, int tick)
945         throws CoreException {
946         if (monitor != null) {
947             monitor.worked(tick);
948             if (monitor.isCanceled()) {
949                 abort();
950             }
951         }
952     }
953
954     /*
955      *
956      */

957     private void verifyReferences(
958         IVerifier verifier,
959         ContentReference[] references,
960         InstallMonitor monitor,
961         IVerificationListener verificationListener,
962         boolean isFeature)
963         throws CoreException {
964         IVerificationResult vr = null;
965         if (verifier != null) {
966             for (int j = 0; j < references.length; j++) {
967                 vr = verifier.verify(this, references[j], isFeature, monitor);
968                 if (vr != null) {
969                     if (verificationListener == null)
970                         return;
971
972                     int result = verificationListener.prompt(vr);
973
974                     if (result == IVerificationListener.CHOICE_ABORT) {
975                         String JavaDoc msg = Messages.JarVerificationService_CancelInstall;
976                         Exception JavaDoc e = vr.getVerificationException();
977                         throw new InstallAbortedException(msg, e);
978                     }
979                     if (result == IVerificationListener.CHOICE_ERROR) {
980                         throw Utilities
981                             .newCoreException(
982                                 Messages.JarVerificationService_UnsucessfulVerification,
983                         vr.getVerificationException());
984                     }
985                 }
986             }
987         }
988     }
989
990     /*
991      * returns reference if the same feature is installed on the site
992      * [18867]
993      */

994     private IFeatureReference featureAlreadyInstalled(ISite targetSite) {
995
996         ISiteFeatureReference[] references = targetSite.getFeatureReferences();
997         IFeatureReference currentReference = null;
998         for (int i = 0; i < references.length; i++) {
999             currentReference = references[i];
1000            // do not compare URL
1001
try {
1002                if (this.equals(currentReference.getFeature(null)))
1003                    return currentReference; // 18867
1004
} catch (CoreException e) {
1005                UpdateCore.warn(null, e);
1006            }
1007        }
1008
1009        UpdateCore.warn(
1010            "ValidateAlreadyInstalled:Feature " //$NON-NLS-1$
1011
+ this
1012                + " not found on site:" //$NON-NLS-1$
1013
+ this.getURL());
1014        return null;
1015    }
1016
1017    /*
1018     * re initialize children of the feature, invalidate the cache
1019     * @param result FeatureReference to reinitialize.
1020     */

1021    private void reinitializeFeature(IFeatureReference referenceToReinitialize) {
1022
1023        if (referenceToReinitialize == null)
1024            return;
1025
1026        if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION)
1027            UpdateCore.debug(
1028                "Re initialize feature reference:" + referenceToReinitialize); //$NON-NLS-1$
1029

1030        IFeature feature = null;
1031        try {
1032            feature = referenceToReinitialize.getFeature(null);
1033            if (feature != null && feature instanceof Feature) {
1034                ((Feature) feature).initializeIncludedReferences();
1035            }
1036            // bug 24981 - recursively go into hierarchy
1037
// only if site if file
1038
ISite site = referenceToReinitialize.getSite();
1039            if (site == null)
1040                return;
1041            URL JavaDoc url = site.getURL();
1042            if (url == null)
1043                return;
1044            if ("file".equals(url.getProtocol())) { //$NON-NLS-1$
1045
IFeatureReference[] included =
1046                    feature.getIncludedFeatureReferences();
1047                for (int i = 0; i < included.length; i++) {
1048                    reinitializeFeature(included[i]);
1049                }
1050            }
1051        } catch (CoreException e) {
1052            UpdateCore.warn("", e); //$NON-NLS-1$
1053
}
1054    }
1055
1056    /**
1057     * @see org.eclipse.update.core.IFeature#getRawIncludedFeatureReferences()
1058     */

1059    public IIncludedFeatureReference[] getIncludedFeatureReferences()
1060        throws CoreException {
1061        return filterFeatures(getRawIncludedFeatureReferences());
1062    }
1063
1064    /*
1065     * Method filterFeatures.
1066     * Also implemented in Site
1067     *
1068     * @param list
1069     * @return List
1070     */

1071    private IIncludedFeatureReference[] filterFeatures(IIncludedFeatureReference[] allIncluded) {
1072        List JavaDoc list = new ArrayList JavaDoc();
1073        if (allIncluded != null) {
1074            for (int i = 0; i < allIncluded.length; i++) {
1075                IIncludedFeatureReference included = allIncluded[i];
1076                if (UpdateManagerUtils.isValidEnvironment(included))
1077                    list.add(included);
1078                else {
1079                    if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_WARNINGS) {
1080                        UpdateCore.warn(
1081                            "Filtered out feature reference:" + included); //$NON-NLS-1$
1082
}
1083                }
1084            }
1085        }
1086
1087        IIncludedFeatureReference[] result =
1088            new IIncludedFeatureReference[list.size()];
1089        if (!list.isEmpty()) {
1090            list.toArray(result);
1091        }
1092
1093        return result;
1094    }
1095
1096    /**
1097     * @see org.eclipse.update.core.IFeature#getRawNonPluginEntries()
1098     */

1099    public INonPluginEntry[] getNonPluginEntries() {
1100        return filterNonPluginEntry(getRawNonPluginEntries());
1101    }
1102
1103    /**
1104     * Method filterPluginEntry.
1105     * @param all
1106     * @return INonPluginEntry[]
1107     */

1108    private INonPluginEntry[] filterNonPluginEntry(INonPluginEntry[] all) {
1109        List JavaDoc list = new ArrayList JavaDoc();
1110        if (all != null) {
1111            for (int i = 0; i < all.length; i++) {
1112                if (UpdateManagerUtils.isValidEnvironment(all[i]))
1113                    list.add(all[i]);
1114            }
1115        }
1116
1117        INonPluginEntry[] result = new INonPluginEntry[list.size()];
1118        if (!list.isEmpty()) {
1119            list.toArray(result);
1120        }
1121
1122        return result;
1123    }
1124
1125    /**
1126     * @see org.eclipse.update.core.IFeature#getRawPluginEntries()
1127     */

1128    public IPluginEntry[] getPluginEntries() {
1129        return filterPluginEntry(getRawPluginEntries());
1130    }
1131
1132    /**
1133     * @see org.eclipse.update.core.IFeature#getImports()
1134     */

1135    public IImport[] getImports() {
1136        return filterImports(getRawImports());
1137    }
1138
1139    /**
1140     * Method filterImports.
1141     * @param all
1142     * @return IImport[]
1143     */

1144    private IImport[] filterImports(IImport[] all) {
1145        List JavaDoc list = new ArrayList JavaDoc();
1146        if (all != null) {
1147            for (int i = 0; i < all.length; i++) {
1148                if (UpdateManagerUtils.isValidEnvironment(all[i]))
1149                    list.add(all[i]);
1150            }
1151        }
1152
1153        IImport[] result = new IImport[list.size()];
1154        if (!list.isEmpty()) {
1155            list.toArray(result);
1156        }
1157
1158        return result;
1159    }
1160
1161}
Popular Tags