KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.*;
13 import java.net.*;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.runtime.*;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.update.configuration.*;
25 import org.eclipse.update.configurator.*;
26 import org.eclipse.update.core.*;
27 import org.eclipse.update.core.model.*;
28 import org.eclipse.update.internal.model.*;
29
30 /**
31  *
32  */

33 public class ConfigurationPolicy extends ConfigurationPolicyModel {
34
35     /**
36      * Constructor for ConfigurationPolicyModel.
37      */

38     public ConfigurationPolicy() {
39     }
40
41     /**
42      * Copy Constructor for ConfigurationPolicyModel.
43      */

44     public ConfigurationPolicy(ConfigurationPolicy configPolicy) {
45         super();
46         setPolicy(configPolicy.getPolicy());
47         setConfiguredFeatureReferences(configPolicy.getConfiguredFeatures());
48         setUnconfiguredFeatureReferences(configPolicy.getUnconfiguredFeatures());
49         setConfiguredSiteModel(configPolicy.getConfiguredSiteModel());
50     }
51
52     /**
53      * @since 2.0
54      */

55     private boolean isUnconfigured(IFeatureReference featureReference) {
56
57         if (featureReference == null)
58             return false;
59
60         // returns true if the feature is part of the configured list
61
IFeatureReference[] refs = getUnconfiguredFeatures();
62         for (int i = 0; i < refs.length; i++) {
63             if (featureReference.equals(refs[i])) {
64                 return true;
65             }
66         }
67         return false;
68     }
69
70     /**
71      * @since 2.0
72      */

73     public boolean isConfigured(IFeatureReference featureReference) {
74
75         if (featureReference == null)
76             return false;
77
78         // returns true if the feature is part of the configured list
79
IFeatureReference[] refs = getConfiguredFeatures();
80         for (int i = 0; i < refs.length; i++) {
81             if (featureReference.equals(refs[i])) {
82                 return true;
83             }
84         }
85         return false;
86     }
87
88     /**
89      * adds the feature to the list of features if the policy is USER_INCLUDE
90      */

91     public void configure(IFeatureReference featureReference, boolean callInstallHandler, boolean createActivity) throws CoreException {
92
93         if (isConfigured(featureReference)) // already configured
94
return;
95
96         if (featureReference == null) {
97             UpdateCore.warn("The feature reference to configure is null"); //$NON-NLS-1$
98
return;
99         }
100
101         IFeature feature = null;
102         try {
103             feature = featureReference.getFeature(null);
104         } catch (CoreException e) {
105             if (!UpdateManagerUtils.isOptional(featureReference)) {
106                 URL url = featureReference.getURL();
107                 String JavaDoc urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; //$NON-NLS-1$
108
UpdateCore.warn("Error retrieving feature:" + urlString, e); //$NON-NLS-1$
109
return;
110             }
111         }
112         if (feature == null) {
113             URL url = featureReference.getURL();
114             String JavaDoc urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; //$NON-NLS-1$
115
UpdateCore.warn("The feature to unconfigure is null: feature reference is:" + urlString); //$NON-NLS-1$
116
}
117
118         // Setup optional install handler
119
InstallHandlerProxy handler = null;
120         if (callInstallHandler && feature.getInstallHandlerEntry() != null)
121             handler = new InstallHandlerProxy(IInstallHandler.HANDLER_ACTION_CONFIGURE, feature, feature.getInstallHandlerEntry(), null);
122         boolean success = false;
123         Throwable JavaDoc originalException = null;
124
125         // do the configure action
126
try {
127             if (handler != null)
128                 handler.configureInitiated();
129
130             ConfigurationActivity activity = null;
131             if (createActivity) {
132                 activity = new ConfigurationActivity(IActivity.ACTION_CONFIGURE);
133                 activity.setLabel(feature.getVersionedIdentifier().toString());
134                 activity.setDate(new Date JavaDoc());
135             }
136
137             addConfiguredFeatureReference((FeatureReferenceModel) featureReference);
138
139             // everything done ok
140
if (activity != null) {
141                 InstallConfiguration installConfig = (InstallConfiguration) SiteManager.getLocalSite().getCurrentConfiguration();
142                 activity.setStatus(IActivity.STATUS_OK);
143                 installConfig.addActivity(activity);
144             }
145
146             if (handler != null)
147                 handler.completeConfigure();
148
149             success = true;
150         } catch (Throwable JavaDoc t) {
151             originalException = t;
152         } finally {
153             Throwable JavaDoc newException = null;
154             try {
155                 if (handler != null)
156                     handler.configureCompleted(success);
157             } catch (Throwable JavaDoc t) {
158                 newException = t;
159             }
160             if (originalException != null) // original exception wins
161
throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String JavaDoc[] { feature.getLabel() })), originalException);
162             if (newException != null)
163                 throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String JavaDoc[] { feature.getLabel() })), newException);
164         }
165     }
166
167     /**
168      * check if the plugins to unconfigure are required by other configured feature and
169      * adds the feature to the list of unconfigured features
170      */

171     public boolean unconfigure(IFeatureReference featureReference, boolean callInstallHandler, boolean createActivity) throws CoreException {
172
173         if (isUnconfigured(featureReference)) {
174             UpdateCore.warn("Feature already unconfigured"); //$NON-NLS-1$
175
return true;
176         }
177
178         if (featureReference == null) {
179             UpdateCore.warn("The feature reference to unconfigure is null"); //$NON-NLS-1$
180
return false;
181         }
182
183         IFeature feature = null;
184         try {
185             feature = featureReference.getFeature(null);
186         } catch (CoreException e) {
187             if (!UpdateManagerUtils.isOptional(featureReference)) {
188                 URL url = featureReference.getURL();
189                 String JavaDoc urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; //$NON-NLS-1$
190
UpdateCore.warn("Error retrieving feature:" + urlString, e); //$NON-NLS-1$
191
return false;
192             }
193         }
194
195         if (feature == null) {
196             URL url = featureReference.getURL();
197             String JavaDoc urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; //$NON-NLS-1$
198
UpdateCore.warn("The feature to unconfigure is null: feature reference is:" + urlString); //$NON-NLS-1$
199
return false;
200         }
201
202         // Setup optional install handler
203
InstallHandlerProxy handler = null;
204         if (callInstallHandler && feature.getInstallHandlerEntry() != null) {
205             handler = new InstallHandlerProxy(IInstallHandler.HANDLER_ACTION_UNCONFIGURE, feature, feature.getInstallHandlerEntry(), null);
206         }
207
208         boolean success = false;
209         Throwable JavaDoc originalException = null;
210
211         // do the unconfigure action
212
try {
213
214             ConfigurationActivity activity = null;
215             if (createActivity) {
216                 activity = new ConfigurationActivity(IActivity.ACTION_UNCONFIGURE);
217                 activity.setLabel(feature.getVersionedIdentifier().toString());
218                 activity.setDate(new Date JavaDoc());
219             }
220
221             InstallConfiguration installConfig = null;
222
223             // only ask for install config is activity created.
224
// prevents loops during reconciliation
225
if (activity != null)
226                 installConfig = ((InstallConfiguration) SiteManager.getLocalSite().getCurrentConfiguration());
227
228             // Allow unconfigure if the feature is optional from all the parents
229
// or if the feature is mandatory and non of its parent are configured
230
// removed, not a core issue (so deep down)
231
//if (validateNoConfiguredParents(feature)) {
232
if (handler != null)
233                 handler.unconfigureInitiated();
234             addUnconfiguredFeatureReference((FeatureReferenceModel) featureReference);
235             if (handler != null)
236                 handler.completeUnconfigure();
237
238             // everything done ok
239
if (activity != null) {
240                 activity.setStatus(IActivity.STATUS_OK);
241                 installConfig.addActivity(activity);
242             }
243             success = true;
244             //} else {
245
// if (activity != null) {
246
// activity.setStatus(IActivity.STATUS_NOK);
247
// installConfig.addActivityModel((ConfigurationActivityModel) activity);
248
// }
249
//}
250
} catch (Throwable JavaDoc t) {
251             originalException = t;
252         } finally {
253             Throwable JavaDoc newException = null;
254             try {
255                 if (handler != null)
256                     handler.unconfigureCompleted(success);
257             } catch (Throwable JavaDoc t) {
258                 newException = t;
259             }
260             if (originalException != null) // original exception wins
261
throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String JavaDoc[] { feature.getLabel() })), originalException);
262             if (newException != null)
263                 throw Utilities.newCoreException(NLS.bind(Messages.InstallHandler_error, (new String JavaDoc[] { feature.getLabel() })), newException);
264         }
265
266         if (!success) {
267             URL url = featureReference.getURL();
268             String JavaDoc urlString = (url != null) ? url.toExternalForm() : "<no feature reference url>"; //$NON-NLS-1$
269
UpdateCore.warn("Unable to unconfigure:" + urlString); //$NON-NLS-1$
270
}
271         return success;
272     }
273
274     /**
275      * Calculates the plugin list for the policy. For "INCLUDE" policy, this
276      * corresponds to the plugins for configured features. For "EXCLUDE"
277      * policy, this corresponds to the plugins for unconfigured features that
278      * are not referenced by any configured features.
279      */

280     public String JavaDoc[] getPluginPath(ISite site) throws CoreException {
281         // TODO we may need to exclude patched plugins here, but this should be good enough for now
282
if (getPolicy() == IPlatformConfiguration.ISitePolicy.MANAGED_ONLY)
283             return new String JavaDoc[0];
284             
285         String JavaDoc[] pluginPaths;
286         // Note: Since 3.0M7 we leave patched features configured,
287
// and take this into account when computing configured plugins
288
// all unconfigured features. Note that patched features are still
289
// configured
290
IFeatureReference[] unconfiguredFeatures = getUnconfiguredFeatures();
291         // all configured features, including patches and patched features
292
IFeatureReference[] configuredFeatures = getConfiguredFeatures();
293         if (!isEnabled()) {
294             if (getPolicy() == IPlatformConfiguration.ISitePolicy.USER_INCLUDE) {
295                 // disabled site, INCLUDE policy
296
pluginPaths = new String JavaDoc[0];
297             } else {
298                 // disabled site, EXCLUDE policy
299
pluginPaths = getAllKnownPluginStrings(site,
300                         configuredFeatures, unconfiguredFeatures);
301             }
302         } else {
303             // PatchedFeatures (may have no patches) with corresponding patches
304
PatchedFeature[] patchedFeatures = buildPatchedFeatures(configuredFeatures);
305             if (getPolicy() == IPlatformConfiguration.ISitePolicy.USER_INCLUDE) {
306                 // enabled site, INCLUDE policy
307
pluginPaths = getConfiguredPluginStrings(site, patchedFeatures);
308             } else {
309                 // enabled site, EXCLUDE policy - the usual scenario for local
310
// site.
311
// return all known MINUS configured plugins
312
pluginPaths = subtract(getAllKnownPluginStrings(site,
313                         configuredFeatures, unconfiguredFeatures),
314                         getConfiguredPluginStrings(site, patchedFeatures));
315             }
316         }
317         //TRACE
318
if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_RECONCILER) {
319             UpdateCore
320                     .debug("GetPluginPath for: " //$NON-NLS-1$
321
+ ((site == null) ? "<No site>" : site.getURL() //$NON-NLS-1$
322
.toString()));
323             for (int i = 0; i < pluginPaths.length; i++) {
324                 UpdateCore.debug("To write:" + pluginPaths[i]); //$NON-NLS-1$
325
}
326         }
327         return pluginPaths;
328     }
329     
330     /**
331      * Obtains PatchedFeatures - non patch features with corresponding patches if any
332      *
333      * @param features
334      * array of features to operate with
335      * @return Patches
336      */

337     private PatchedFeature[] buildPatchedFeatures(IFeatureReference[] features) {
338         // PatchedFeatures by VersionedIdentifier
339
Map JavaDoc map = new HashMap JavaDoc();
340         // Create a map of features (not patches)
341
for (int f = 0; f < features.length; f++) {
342             IFeatureReference featureRef = features[f];
343             try {
344                 if(featureRef.isPatch()){
345                     continue;
346                 }
347                 VersionedIdentifier vi = featureRef.getVersionedIdentifier();
348                 map.put(vi, new PatchedFeature(features[f]));
349             } catch (CoreException e) {
350                 UpdateCore.warn(null, e);
351             }
352         }
353         // attach patches to features
354
for (int f = 0; f < features.length; f++) {
355             IFeatureReference patchCandidate = features[f];
356             try {
357                 IFeature feature = patchCandidate.getFeature(null);
358                 IImport[] imports = feature.getImports();
359                 for (int i = 0; i < imports.length; i++) {
360                     IImport oneImport = imports[i];
361                     if (!oneImport.isPatch())
362                         continue;
363                     // it is a patch for
364
VersionedIdentifier patchedIdentifier =
365                         oneImport.getVersionedIdentifier();
366                     PatchedFeature pf=(PatchedFeature) map.get(patchedIdentifier);
367                     if (pf!=null) {
368                         pf.addPatch(patchCandidate);
369                     } else {
370                         // patched feature not enabled
371
}
372                 }
373             } catch (CoreException e) {
374                 UpdateCore.warn(null, e);
375             }
376         }
377         Collection JavaDoc patchedFeatures=map.values();
378         return (PatchedFeature[])patchedFeatures.toArray(new PatchedFeature[patchedFeatures.size()]);
379     }
380     
381     /**
382      * @since 2.0
383      */

384     public IFeatureReference[] getConfiguredFeatures() {
385         FeatureReferenceModel[] result = getConfiguredFeaturesModel();
386         if (result.length == 0)
387             return new IFeatureReference[0];
388         else
389             return (IFeatureReference[]) result;
390     }
391
392     /**
393      * @since 2.0
394      */

395     public IFeatureReference[] getUnconfiguredFeatures() {
396         FeatureReferenceModel[] result = getUnconfiguredFeaturesModel();
397         if (result.length == 0)
398             return new IFeatureReference[0];
399         else
400             return (IFeatureReference[]) result;
401     }
402
403     /**
404      * Gets the configuredSite.
405      * @return Returns a IConfiguredSite
406      */

407     public IConfiguredSite getConfiguredSite() {
408         return (IConfiguredSite) getConfiguredSiteModel();
409     }
410
411     /**
412      * removes a feature reference
413      */

414     public void removeFeatureReference(IFeatureReference featureRef) {
415         if (featureRef instanceof FeatureReferenceModel) {
416             removeFeatureReference((FeatureReferenceModel) featureRef);
417         }
418     }
419
420     /**
421      * @return an array of plugin path for the array of feature reference. For
422      * features that have patches, plugin path will
423      * point to plugin with the same ID provided by the patch if it
424      * exists. Each plugin path only appears once [bug 21750]
425      */

426     private String JavaDoc[] getConfiguredPluginStrings(ISite site, PatchedFeature[] features) throws CoreException {
427         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION){
428             UpdateCore.warn("CONFIGURED PLUGINS"); //$NON-NLS-1$
429
}
430     
431         // Use set to eliminate plugins with same ID and version.
432
// Different versions of plugins with same ID are allowed if coming from different features
433
Set JavaDoc featurePlugins = new HashSet JavaDoc();
434         for (int i = 0; i < features.length; i++) {
435             FeaturePlugin[] plugins = features[i].getPlugins();
436             featurePlugins.addAll(Arrays.asList(plugins));
437         }
438         Set JavaDoc pluginStrings = getPluginStrings(site, (FeaturePlugin[]) featurePlugins.toArray(new FeaturePlugin[featurePlugins.size()]));
439         return (String JavaDoc[]) pluginStrings.toArray(new String JavaDoc[pluginStrings.size()]);
440     }
441     /**
442      * @return an array of plugin path for every plugin in known features
443      */

444     private String JavaDoc[] getAllKnownPluginStrings(ISite site, IFeatureReference[] configured,IFeatureReference[] unconfigured) throws CoreException {
445         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION){
446             UpdateCore.warn("ALL PLUGINS"); //$NON-NLS-1$
447
}
448         // Add features, patched features, or patches
449
IFeatureReference[] all=new IFeatureReference[configured.length+unconfigured.length];
450         System.arraycopy(configured, 0, all, 0, configured.length);
451         System.arraycopy(unconfigured, 0, all, configured.length, unconfigured.length);
452         //
453
Set JavaDoc patchedPlugins = new HashSet JavaDoc();
454         for (int i=0; i< all.length; i++) {
455             try {
456                 IFeature feature = all[i].getFeature(null);
457                 if (feature == null) {
458                     UpdateCore.warn("Null Feature", new Exception JavaDoc()); //$NON-NLS-1$
459
continue;
460                 }
461
462                 IPluginEntry[] entries = feature.getPluginEntries();
463                 // add every plugin to the map
464
for (int entr = 0; entr < entries.length; entr++) {
465                     patchedPlugins.add(new FeaturePlugin(entries[entr], feature));
466                 }
467
468             } catch (CoreException e) {
469                 UpdateCore.warn(null, e);
470             }
471         }
472         Set JavaDoc pluginStrings = getPluginStrings(site, (FeaturePlugin[])patchedPlugins.toArray(new FeaturePlugin[patchedPlugins.size()]));
473         return (String JavaDoc[]) pluginStrings.toArray(new String JavaDoc[pluginStrings.size()]);
474     }
475     /**
476      * @param site
477      * @param plugins[]
478      * @return valid string pointing to plugins in given features
479      * @throws CoreException
480      */

481     private Set JavaDoc getPluginStrings(ISite site, FeaturePlugin[] plugins) throws CoreException {
482         Set JavaDoc pluginStrings=new HashSet JavaDoc();
483         for (int i=0; i< plugins.length; i++) {
484             IPluginEntry entry = plugins[i].getEntry();
485             IFeature feature=plugins[i].getFeature();
486
487             // obtain the path of the plugin directories on the site
488
ContentReference[] featureContentReference = null;
489             try {
490                 featureContentReference = feature.getFeatureContentProvider().getPluginEntryArchiveReferences(entry, null /*IProgressMonitor*/
491                 );
492             } catch (CoreException e) {
493                 UpdateCore.warn(null, e);
494             }
495
496             // transform into a valid String
497
if (featureContentReference != null) {
498                 for (int j = 0; j < featureContentReference.length; j++) {
499                     URL url = site.getSiteContentProvider().getArchiveReference(featureContentReference[j].getIdentifier());
500                     if (url != null) {
501                         // make it relative to the site
502
String JavaDoc path = UpdateManagerUtils.getURLAsString(site.getURL(), url);
503                         // add end "/"
504
if(!path.endsWith(".jar")) //$NON-NLS-1$
505
path += (path.endsWith(File.separator) || path.endsWith("/")) ? "" : "/"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
506
pluginStrings.add(path);
507                         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION)
508                             UpdateCore.warn("Add plugin: " + path + " to the list"); //$NON-NLS-1$ //$NON-NLS-2$
509
}
510                 }
511             }
512         }
513         return pluginStrings;
514     }
515
516     /**
517      * Obtains strings existing in the allStrings array, but not in the stringsToRemove
518      */

519     private String JavaDoc[] subtract(String JavaDoc[] allStrings, String JavaDoc[] stringsToRemove) {
520         HashSet JavaDoc resultList = new HashSet JavaDoc(Arrays.asList(allStrings));
521         resultList.removeAll(Arrays.asList(stringsToRemove));
522         return (String JavaDoc[])resultList.toArray(new String JavaDoc[resultList.size()]);
523     }
524 }
525
Popular Tags