KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.update.core.*;
20 /**
21  * Feature and corresponding patch features
22  */

23 public class PatchedFeature {
24     private IFeatureReference feature;
25     private Collection JavaDoc patches = new HashSet JavaDoc();
26     /**
27      *
28      */

29     public PatchedFeature(IFeatureReference feature) {
30         super();
31         this.feature = feature;
32     }
33     public void addPatch(IFeatureReference patch) {
34         patches.add(patch);
35     }
36     /**
37      * @return Returns the feature.
38      */

39     public IFeatureReference getFeature() {
40         return feature;
41     }
42     /**
43      * @return Returns the patches.
44      */

45     public IFeatureReference[] getPatches() {
46         return (IFeatureReference[]) patches.toArray(new IFeatureReference[patches.size()]);
47     }
48     /**
49      * @return Returns the feature and the patches.
50      */

51     public IFeatureReference[] getFeatureAndPatches() {
52         IFeatureReference[] features = new IFeatureReference[patches.size() + 1];
53         features[0] = feature;
54         System.arraycopy(getPatches(), 0, features, 1, patches.size());
55         return features;
56     }
57     /**
58      * Obtains all plugins from the feature and its patches. Each plugin will
59      * have unique ID.
60      * If there are multiple version of plugin with same ID among the feature
61      * and its patches, highest version plugins are chosen.
62      *
63      * @return FeaturePlugin[]
64      */

65     public FeaturePlugin[] getPlugins() {
66         // Use a map of PatchedPluigns by plugin ID
67
// to collect one version of each plugin
68
Map JavaDoc plugins = new HashMap JavaDoc();
69         IFeatureReference[] featureRefs = getFeatureAndPatches();
70         // for each (feature or any patch)
71
for (int i = 0; i < featureRefs.length; i++) {
72             try {
73                 IFeature feature = featureRefs[i].getFeature(null);
74                 if (feature == null) {
75                     UpdateCore.warn("Null Feature", new Exception JavaDoc()); //$NON-NLS-1$
76
continue;
77                 }
78                 // get plugin entries
79
IPluginEntry[] entries = feature.getPluginEntries();
80                 for (int entr = 0; entr < entries.length; entr++) {
81                     String JavaDoc pluginId = entries[entr].getVersionedIdentifier().getIdentifier();
82                     PluginVersionIdentifier pluginVersion = entries[entr].getVersionedIdentifier().getVersion();
83                     // check if map contains >= version of same plugin
84
FeaturePlugin existingPlugin = (FeaturePlugin) plugins.get(pluginId);
85                     if (existingPlugin != null && existingPlugin.getEntry().getVersionedIdentifier().getVersion().isGreaterOrEqualTo(pluginVersion)) {
86                         // same or newer plugin already collected
87
continue;
88                     } else {
89                         plugins.put(pluginId, new FeaturePlugin(entries[entr], feature));
90                     }
91                 }
92             } catch (CoreException e) {
93                 UpdateCore.warn(null, e);
94             }
95         }
96         return (FeaturePlugin[]) plugins.values().toArray(new FeaturePlugin[plugins.size()]);
97     }
98     public String JavaDoc toString() {
99         StringBuffer JavaDoc str = new StringBuffer JavaDoc(feature.toString());
100         IFeatureReference[] patches = getFeatureAndPatches();
101         for (int i = 0; i < patches.length; i++) {
102             str.append(" +patch=" + patches[i].toString() + " "); //$NON-NLS-1$ //$NON-NLS-2$
103
}
104         return str.toString();
105     }
106 }
107
Popular Tags