KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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.osgi.framework.*;
21 import org.osgi.service.packageadmin.*;
22
23 /**
24  * This class manages the configurations.
25  */

26
27 public class SiteStatusAnalyzer {
28
29     private static List JavaDoc allConfiguredFeatures; /*VersionedIdentifier */
30     private LocalSite siteLocal;
31
32     /**
33      *
34      */

35     public SiteStatusAnalyzer(LocalSite siteLocal) {
36         this.siteLocal = siteLocal;
37     }
38
39     /**
40      * manages the versionedIdentifier and location of parsed plugins
41      */

42     public class PluginIdentifier {
43         private VersionedIdentifier id;
44         private String JavaDoc label;
45         private boolean isFragment = false;
46
47         public PluginIdentifier(VersionedIdentifier id, String JavaDoc label, boolean fragment) {
48             this.id = id;
49             this.label = label;
50             this.isFragment = fragment;
51         }
52
53         public VersionedIdentifier getVersionedIdentifier() {
54             return id;
55         }
56
57         public boolean isFragment() {
58             return isFragment;
59         }
60
61         public String JavaDoc getLabel() {
62             return label;
63         }
64     }
65     /*
66      * check if the Plugins of the feature are on the plugin path
67      * If all the plugins are on the plugin path, and the version match and there is no other version -> HAPPY
68      * If all the plugins are on the plugin path, and the version match and there is other version -> AMBIGUOUS
69      * If some of the plugins are on the plugin path, but not all -> UNHAPPY
70      * Check on all ConfiguredSites
71      */

72     private IStatus getStatus(IFeature feature) {
73
74         // validate site
75
ISite featureSite = feature.getSite();
76         if (featureSite == null) {
77             if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION)
78                 UpdateCore.debug("Cannot determine status of feature:" + feature.getLabel() + ". Site is NULL."); //$NON-NLS-1$ //$NON-NLS-2$
79
String JavaDoc msg = NLS.bind(Messages.SiteLocal_UnableToDetermineFeatureStatusSiteNull, (new Object JavaDoc[] { feature.getURL()}));
80             return createStatus(IStatus.ERROR, IFeature.STATUS_AMBIGUOUS, msg, null);
81         }
82
83         // validate configured site
84
ConfiguredSite cSite = (ConfiguredSite) featureSite.getCurrentConfiguredSite();
85         if (cSite == null) {
86             if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION)
87                 UpdateCore.warn("Cannot determine status of feature: " + feature.getLabel() + ". Configured Site is NULL."); //$NON-NLS-1$ //$NON-NLS-2$
88
String JavaDoc msg = NLS.bind(Messages.SiteLocal_UnableToDetermineFeatureStatusConfiguredSiteNull, (new Object JavaDoc[] { feature.getURL()}));
89             return createStatus(IStatus.ERROR, IFeature.STATUS_AMBIGUOUS, msg, null);
90         }
91
92         // check if disable, if so return
93
IFeatureReference ref = cSite.getSite().getFeatureReference(feature);
94         if (ref != null) {
95             if (!cSite.getConfigurationPolicy().isConfigured(ref))
96                 return createStatus(IStatus.OK, IFeature.STATUS_DISABLED, "", null); //$NON-NLS-1$
97
} else {
98             if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION)
99                 UpdateCore.warn("Unable to find reference for feature " + feature + " in site " + cSite.getSite().getURL()); //$NON-NLS-1$ //$NON-NLS-2$
100
}
101
102         // check if broken
103
IStatus status = cSite.getBrokenStatus(feature);
104         if (status.getSeverity() != IStatus.OK) {
105             if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION)
106                 UpdateCore.debug("Feature broken:" + feature.getLabel() + ".Site:" + cSite.toString()); //$NON-NLS-1$ //$NON-NLS-2$
107
return status;
108         }
109
110         // check ambiguous against registry [17015]
111
IPluginEntry[] featuresEntries = feature.getPluginEntries();
112         return status( feature, featuresEntries);
113     }
114
115     /*
116      * check if the Plugins of the feature are on the plugin path
117      * If all the plugins are on the plugin path, and the version match and there is no other version -> HAPPY
118      * If all the plugins are on the plugin path, and the version match and there is other version -> AMBIGUOUS
119      * If some of the plugins are on the plugin path, but not all -> UNHAPPY
120      * Check on all ConfiguredSites
121      */

122     public IStatus getFeatureStatus(IFeature feature) throws CoreException {
123
124         IFeature childFeature = null;
125         IStatus childStatus;
126
127         IFeatureReference[] children = feature.getIncludedFeatureReferences();
128
129         // consider disable
130
// check the current feature
131
String JavaDoc msg = Messages.SiteLocal_FeatureDisable;
132         int code = IFeature.STATUS_DISABLED;
133         IStatus featureStatus = getStatus(feature);
134         MultiStatus multiTemp = new MultiStatus(featureStatus.getPlugin(), code, msg, null);
135         if (featureStatus.getSeverity() == IStatus.ERROR) {
136             if (featureStatus.isMultiStatus()) {
137                 multiTemp.addAll(featureStatus);
138             } else {
139                 multiTemp.add(featureStatus);
140             }
141         }
142         // preserve the worse code through the method (self assesment + children assessment)
143
if (featureStatus.getCode() > code)
144             code = featureStatus.getCode();
145
146         // do not check children if feature is disable
147
if (!(code == IFeature.STATUS_DISABLED)) {
148             for (int i = 0; i < children.length; i++) {
149                 if (!UpdateManagerUtils.isOptional(children[i])) {
150                     try {
151                         childFeature = children[i].getFeature(null);
152                     } catch (CoreException e) {
153                         childFeature = null;
154                         if (!UpdateManagerUtils.isOptional(children[i]))
155                             UpdateCore.warn("Error retrieving feature:" + children[i]); //$NON-NLS-1$
156
}
157
158                     if (childFeature == null) {
159                         UpdateCore.warn("getFeatureStatus: Feature is null for:" + children[i]); //$NON-NLS-1$
160
// Unable to find children feature, broken
161
Object JavaDoc featureAsPrintableObject = children[i].getURL();
162                         featureAsPrintableObject = children[i].getVersionedIdentifier();
163                         String JavaDoc msg1 = NLS.bind(Messages.SiteLocal_NestedFeatureUnavailable, (new Object JavaDoc[] { featureAsPrintableObject }));
164                         multiTemp.add(createStatus(IStatus.ERROR, IFeature.STATUS_UNHAPPY, msg1, null));
165                         if (IFeature.STATUS_UNHAPPY > code)
166                             code = IFeature.STATUS_UNHAPPY;
167                     } else {
168                         childStatus = getFeatureStatus(childFeature);
169                         // do not add the status, add the children status as getFeatureStatus
170
// returns a multiStatus
171
if (childStatus.getCode() == IFeature.STATUS_DISABLED) {
172                             VersionedIdentifier versionID = childFeature.getVersionedIdentifier();
173                             String JavaDoc featureVer = (versionID == null) ? "" : versionID.getVersion().toString(); //$NON-NLS-1$
174
String JavaDoc msg1 = NLS.bind(Messages.SiteLocal_NestedFeatureDisable, (new String JavaDoc[] { childFeature.getLabel(), featureVer }));
175                             multiTemp.add(createStatus(IStatus.ERROR, childStatus.getCode(), msg1, null));
176                             if (IFeature.STATUS_UNHAPPY > code)
177                                 code = IFeature.STATUS_UNHAPPY;
178                         }
179                         if (childStatus.getSeverity() != IStatus.OK) {
180                             VersionedIdentifier versionID = childFeature.getVersionedIdentifier();
181                             String JavaDoc featureVer = (versionID == null) ? "" : versionID.getVersion().toString(); //$NON-NLS-1$
182
String JavaDoc msg1 = NLS.bind(Messages.SiteLocal_NestedFeatureUnHappy, (new String JavaDoc[] { childFeature.getLabel(), featureVer }));
183                             multiTemp.add(createStatus(IStatus.ERROR, childStatus.getCode(), msg1, null));
184                             if (childStatus.getCode() > code)
185                                 code = childStatus.getCode();
186                         }
187                     }
188                 }
189             }
190         }
191
192         // set message
193
switch (code) {
194             case IFeature.STATUS_HAPPY :
195                 msg = Messages.SiteLocal_FeatureHappy;
196                 break;
197             case IFeature.STATUS_UNHAPPY :
198                 msg = Messages.SiteLocal_FeatureUnHappy;
199                 break;
200             case IFeature.STATUS_AMBIGUOUS :
201                 msg = Messages.SiteLocal_FeatureAmbiguous;
202                 break;
203             case IFeature.STATUS_DISABLED :
204                 msg = Messages.SiteLocal_FeatureDisable;
205                 break;
206             default :
207                 msg = Messages.SiteLocal_FeatureStatusUnknown;
208                 break;
209         }
210         MultiStatus multi = new MultiStatus(featureStatus.getPlugin(), code, msg, null);
211         multi.addAll(multiTemp);
212         return multi;
213     }
214
215     /*
216      * compute the status based on getStatus() rules
217      */

218     private IStatus status(IFeature pluginsOriginatorFeature, IPluginEntry[] featurePlugins) {
219         VersionedIdentifier featurePluginID;
220
221         String JavaDoc happyMSG = Messages.SiteLocal_FeatureHappy;
222         String JavaDoc ambiguousMSG = Messages.SiteLocal_FeatureAmbiguous;
223         IStatus featureStatus = createStatus(IStatus.OK, IFeature.STATUS_HAPPY, "", null); //$NON-NLS-1$
224
MultiStatus multi = new MultiStatus(featureStatus.getPlugin(), IFeature.STATUS_AMBIGUOUS, ambiguousMSG, null);
225         PackageAdmin pkgAdmin = UpdateCore.getPlugin().getPackageAdmin();
226         
227         // is Ambigous if we find a plugin from the feature
228
// with a different version and not the one we are looking
229
for (int i = 0; i < featurePlugins.length; i++) {
230             MultiStatus tempmulti = new MultiStatus(featureStatus.getPlugin(), IFeature.STATUS_AMBIGUOUS, ambiguousMSG, null);
231             featurePluginID = featurePlugins[i].getVersionedIdentifier();
232             boolean found = false;
233             
234             String JavaDoc singleVersionRange = '[' + featurePluginID.getVersion().toString() + ',' + featurePluginID.getVersion().toString() + ']';
235             Bundle[] bundles = pkgAdmin.getBundles(featurePluginID.getIdentifier(), singleVersionRange);
236             if (bundles != null && bundles.length == 1) {
237                 found = true;
238                 continue;
239             }
240             
241             // Check if there is another feature with this plugin (but different version)
242
// log it
243
bundles = pkgAdmin.getBundles(featurePluginID.getIdentifier(), null);
244             for (int j = 0; bundles != null && j < bundles.length && !found; j++ ) {
245                 String JavaDoc bundleVersion = (String JavaDoc)bundles[j].getHeaders().get(Constants.BUNDLE_VERSION);
246                 IFeature feature = getFeatureForId(new VersionedIdentifier(bundles[j].getSymbolicName(), bundleVersion ));
247                 if ((feature != null) && (!isFeaturePatchOfThisFeature(pluginsOriginatorFeature, feature))) {
248                     String JavaDoc msg = null;
249                     if (feature == null) {
250                         Object JavaDoc[] values = new Object JavaDoc[] {bundles[j].getSymbolicName(), featurePluginID.getVersion(), bundleVersion};
251                         msg = NLS.bind(Messages.SiteLocal_TwoVersionSamePlugin1, values);
252                     } else {
253                         String JavaDoc label = feature.getLabel();
254                         String JavaDoc featureVersion = feature.getVersionedIdentifier().getVersion().toString();
255                         Object JavaDoc[] values = new Object JavaDoc[] { bundles[j].getSymbolicName(), featurePluginID.getVersion(), bundleVersion, label, featureVersion };
256                         msg = NLS.bind(Messages.SiteLocal_TwoVersionSamePlugin2, values);
257                     }
258     
259                     UpdateCore.warn("Found another version of the same plugin on the path:" + bundles[j].getSymbolicName() + " " + bundleVersion); //$NON-NLS-1$ //$NON-NLS-2$
260
tempmulti.add(createStatus(IStatus.ERROR, IFeature.STATUS_AMBIGUOUS, msg, null));
261                 } else {
262                     found = true;
263                 }
264             
265             }
266     
267
268             // if we haven't found the exact plugin, add the children
269
// of tempMulti (i,e the other we found)
270
// if we have no children, we have a problem as a required plugin is not there at all
271
if (!found) {
272                 if (tempmulti.getChildren().length > 0) {
273                     multi.addAll(tempmulti);
274                 } else {
275                     if (multi.getCode() != IFeature.STATUS_UNHAPPY) {
276                         String JavaDoc unhappyMSG = Messages.SiteLocal_FeatureUnHappy;
277                         MultiStatus newMulti = new MultiStatus(featureStatus.getPlugin(), IFeature.STATUS_UNHAPPY, unhappyMSG, null);
278                         newMulti.addAll(multi);
279                         multi = newMulti;
280                     }
281                     String JavaDoc msg = NLS.bind(Messages.SiteLocal_NoPluginVersion, (new String JavaDoc[] { featurePluginID.getIdentifier() }));
282                     multi.add(createStatus(IStatus.ERROR, IFeature.STATUS_UNHAPPY, msg, null));
283                 }
284             }
285         }
286
287         if (!multi.isOK())
288             return multi;
289
290         // we return happy as we consider the isBroken verification has been done
291
return createStatus(IStatus.OK, IFeature.STATUS_HAPPY, happyMSG, null);
292     }
293     private boolean isFeaturePatchOfThisFeature(IFeature pluginsOriginatorFeature, IFeature feature) {
294         
295         if (!feature.isPatch())
296             return false;
297         
298         IImport[] featureImports = feature.getImports();
299
300         if (featureImports == null) {
301             return false;
302         }
303         
304         for(int i = 0; i < featureImports.length; i++) {
305             if (featureImports[i].isPatch() && featureImports[i].getVersionedIdentifier().equals(pluginsOriginatorFeature.getVersionedIdentifier())) {
306                 return true;
307             }
308         }
309         return false;
310     }
311
312     /*
313      * creates a Status
314      */

315     private IStatus createStatus(int statusSeverity, int statusCode, String JavaDoc msg, Exception JavaDoc e) {
316         String JavaDoc id = UpdateCore.getPlugin().getBundle().getSymbolicName();
317
318         StringBuffer JavaDoc completeString = new StringBuffer JavaDoc(""); //$NON-NLS-1$
319
if (msg != null)
320             completeString.append(msg);
321         if (e != null) {
322             completeString.append("\r\n["); //$NON-NLS-1$
323
completeString.append(e.toString());
324             completeString.append("]\r\n"); //$NON-NLS-1$
325
}
326         return new Status(statusSeverity, id, statusCode, completeString.toString(), e);
327     }
328
329
330     /*
331      * returns all the configured fetaures
332      */

333     private IFeature[] getAllConfiguredFeatures() {
334         if (allConfiguredFeatures == null) {
335
336             allConfiguredFeatures = new ArrayList JavaDoc();
337             IConfiguredSite[] allConfiguredSites = siteLocal.getCurrentConfiguration().getConfiguredSites();
338
339             for (int i = 0; i < allConfiguredSites.length; i++) {
340                 IFeatureReference[] refs = allConfiguredSites[i].getConfiguredFeatures();
341                 IFeature feature = null;
342                 for (int j = 0; j < refs.length; j++) {
343                     feature = null;
344                     try {
345                         feature = refs[j].getFeature(null);
346                     } catch (CoreException e) {
347                     }
348                     if (feature != null) {
349                         allConfiguredFeatures.add(feature);
350                     }
351                 }
352             }
353         }
354
355         IFeature[] features = new IFeature[allConfiguredFeatures.size()];
356         if (allConfiguredFeatures.size() > 0) {
357             allConfiguredFeatures.toArray(features);
358         }
359         return features;
360     }
361
362     /*
363      * returns the Feature that declares this versionedIdentifier or null if none found
364      */

365     private IFeature getFeatureForId(VersionedIdentifier id) {
366
367         if (id == null)
368             return null;
369
370         IFeature[] allFeatures = getAllConfiguredFeatures();
371         IFeature currentFeature = null;
372         IPluginEntry[] allPlugins = null;
373         IPluginEntry currentPlugin = null;
374         for (int i = 0; i < allFeatures.length; i++) {
375             currentFeature = allFeatures[i];
376             allPlugins = currentFeature.getPluginEntries();
377             for (int j = 0; j < allPlugins.length; j++) {
378                 currentPlugin = allPlugins[j];
379                 if (id.equals(currentPlugin.getVersionedIdentifier()))
380                     return currentFeature;
381             }
382         }
383         return null;
384     }
385 }
386
Popular Tags