KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > operations > InstallOperation


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.operations;
12
13 import java.util.HashSet JavaDoc;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.update.configuration.*;
18 import org.eclipse.update.core.*;
19 import org.eclipse.update.internal.core.Messages;
20 import org.eclipse.update.operations.*;
21
22 /**
23  * Configure a feature.
24  * ConfigOperation
25  */

26 public class InstallOperation
27     extends FeatureOperation
28     implements IInstallFeatureOperation {
29     
30     public boolean equals(Object JavaDoc arg) {
31         if (arg == null) {
32             return false;
33         }
34         
35         if (!(arg instanceof InstallOperation)) {
36             return false;
37         }
38         
39         InstallOperation io = (InstallOperation)arg;
40         
41         return io.getFeature().getVersionedIdentifier().equals(this.getFeature().getVersionedIdentifier());
42     }
43
44     public int hashCode() {
45         // TODO Auto-generated method stub
46
return super.getFeature().hashCode();
47     }
48
49     private IFeatureReference[] optionalFeatures;
50     private IFeature[] unconfiguredOptionalFeatures;
51     private IVerificationListener verifier;
52
53     /**
54      * Constructor
55      * @param site
56      * @param feature
57      * @param optionalFeatures optional features to install. If null, the operation will install them all (if any)
58      * @param unconfiguredOptionalElements optional features unconfigured before the operation. They should remain unconfigured after the install.
59      * @param verifier
60      */

61     public InstallOperation(
62         IConfiguredSite site,
63         IFeature feature,
64         IFeatureReference[] optionalFeatures,
65         IFeature[] unconfiguredOptionalElements,
66         IVerificationListener verifier) {
67         super(site, feature);
68         IFeature[] installed = UpdateUtils.getInstalledFeatures(feature);
69         if (installed.length > 0)
70             this.oldFeature = installed[0];
71         this.unconfiguredOptionalFeatures = unconfiguredOptionalElements;
72         this.optionalFeatures = optionalFeatures;
73         this.verifier = verifier;
74     }
75
76     public IFeatureReference[] getOptionalFeatures() {
77         return optionalFeatures;
78     }
79
80     public boolean execute(IProgressMonitor pm, IOperationListener listener)
81         throws CoreException {
82
83         boolean reinstall = false;
84         if (oldFeature != null
85             && feature.getVersionedIdentifier().equals(
86                 oldFeature.getVersionedIdentifier()))
87             reinstall = true;
88
89         setOptionalFeatures();
90
91         if (optionalFeatures == null)
92             targetSite.install(feature, verifier, pm);
93         else
94             targetSite.install(feature, optionalFeatures, verifier, pm);
95
96         if (!reinstall) {
97
98             if (oldFeature != null) { //&& isOptionalDelta()) {
99
preserveOptionalState();
100
101                 boolean oldSuccess = unconfigure(oldFeature, null); // pick any site containing old feature
102
if (!oldSuccess) {
103                     IInstallConfiguration config = SiteManager.getLocalSite().getCurrentConfiguration();
104                     if (!UpdateUtils.isNestedChild(config, oldFeature)) {
105                         // "eat" the error if nested child
106
String JavaDoc message =
107                             NLS.bind(Messages.OperationsManager_error_old, (new String JavaDoc[] { oldFeature.getLabel() }));
108                         IStatus status =
109                             new Status(
110                                 IStatus.ERROR,
111                                 UpdateUtils.getPluginId(),
112                                 IStatus.OK,
113                                 message,
114                                 null);
115                         throw new CoreException(status);
116                     }
117                 }
118             }
119
120 // if (oldFeature == null) {
121
// ensureUnique();
122
// }
123
}
124         return true;
125     }
126
127     private void preserveOptionalState() {
128         if (unconfiguredOptionalFeatures == null)
129             return;
130
131         for (int i = 0; i < unconfiguredOptionalFeatures.length; i++) {
132             try {
133                 // Get the feature that matches the original unconfigured ones.
134
IFeature localFeature =
135                     UpdateUtils.getLocalFeature(
136                         targetSite,
137                         unconfiguredOptionalFeatures[i]);
138                 if (localFeature != null)
139                     targetSite.unconfigure(localFeature);
140
141             } catch (CoreException e) {
142                 // Ignore this - we will leave with it
143
}
144         }
145     }
146     
147     private void setOptionalFeatures() {
148         try {
149             // Ensure optional features are correctly set
150
if (optionalFeatures == null && UpdateUtils.hasOptionalFeatures(feature) ) {
151                 JobRoot jobRoot = new JobRoot(this);
152                 IInstallConfiguration config = SiteManager.getLocalSite().getCurrentConfiguration();
153                 HashSet JavaDoc set = new HashSet JavaDoc();
154                 boolean update = oldFeature != null;
155                 boolean patch = UpdateUtils.isPatch(feature);
156                 FeatureHierarchyElement[] elements = jobRoot.getElements();
157                 for (int i = 0; i < elements.length; i++) {
158                     elements[i].addCheckedOptionalFeatures(update, patch, config, set);
159                 }
160                 optionalFeatures = new IFeatureReference[set.size()];
161                 set.toArray(optionalFeatures);
162                 unconfiguredOptionalFeatures = jobRoot.getUnconfiguredOptionalFeatures(config, targetSite);
163             }
164         } catch (CoreException e) {
165             UpdateUtils.logException(e);
166         }
167     }
168 }
169
Popular Tags