KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > standalone > UpdateCommand


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.standalone;
12
13 import java.net.*;
14 import java.util.ArrayList 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.eclipse.update.internal.core.*;
21 import org.eclipse.update.internal.operations.*;
22 import org.eclipse.update.operations.*;
23 import org.eclipse.update.search.*;
24
25 /**
26  * Command to update and existing feature.
27  * <p>
28  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
29  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
30  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
31  * (repeatedly) as the API evolves.
32  * </p>
33  * @since 3.0
34  */

35 public class UpdateCommand extends ScriptedCommand {
36
37     private IConfiguredSite targetSite;
38     private UpdateSearchRequest searchRequest;
39     private UpdateSearchResultCollector collector;
40     private String JavaDoc featureId;
41     //private String version;
42
private IFeature currentFeature;
43
44     /**
45      * Update command for updating a feature to a newer version.
46      * @param featureId
47      * @param verifyOnly
48      * @throws Exception
49      */

50     public UpdateCommand(String JavaDoc featureId, String JavaDoc verifyOnly)
51     throws Exception JavaDoc {
52         this(featureId, null, verifyOnly);
53     }
54     
55     /**
56      * Update command for updating a feature to a specified newer version.
57      * @param featureId
58      * @param version
59      * @param verifyOnly
60      * @throws Exception
61      */

62     public UpdateCommand(String JavaDoc featureId, String JavaDoc version, String JavaDoc verifyOnly)
63         throws Exception JavaDoc {
64
65         super(verifyOnly);
66
67         try {
68             this.featureId = featureId;
69             //this.version = version;
70
if (featureId != null) {
71                 this.targetSite =
72                     UpdateUtils.getSiteWithFeature(
73                         getConfiguration(),
74                         featureId);
75                 if (targetSite == null) {
76                     throw new Exception JavaDoc(NLS.bind(Messages.Standalone_noConfigSiteForFeature, (new String JavaDoc[] { featureId })));
77                 }
78                 IFeature[] currentFeatures =
79                     UpdateUtils.searchSite(featureId, targetSite, true);
80                 if (currentFeatures == null || currentFeatures.length == 0) {
81                     throw new Exception JavaDoc(NLS.bind(Messages.Standalone_noFeatures3, (new String JavaDoc[] { featureId })));
82                 }
83                 this.currentFeature = currentFeatures[0];
84             } else {
85                 // Get site to install to
86
IConfiguredSite[] sites =
87                     getConfiguration().getConfiguredSites();
88                 for (int i = 0; i < sites.length; i++) {
89                     if (sites[i].isProductSite()) {
90                         targetSite = sites[i];
91                         break;
92                     }
93                 }
94             }
95             if (currentFeature == null)
96                 searchRequest = UpdateUtils.createNewUpdatesRequest(null);
97             else {
98                 searchRequest =
99                     UpdateUtils.createNewUpdatesRequest(
100                         new IFeature[] { currentFeature });
101                 if (version != null)
102                     searchRequest.addFilter(
103                         new VersionedIdentifiersFilter(
104                             new VersionedIdentifier[] { new VersionedIdentifier(featureId, version) }));
105             }
106
107             collector = new UpdateSearchResultCollector();
108
109         } catch (MalformedURLException e) {
110             StandaloneUpdateApplication.exceptionLogged();
111             UpdateCore.log(e);
112         } catch (CoreException e) {
113             StandaloneUpdateApplication.exceptionLogged();
114             UpdateCore.log(e);
115         }
116     }
117
118     /**
119      */

120     public boolean run(IProgressMonitor monitor) {
121         // check if the config file has been modifed while we were running
122
IStatus status = OperationsManager.getValidator().validatePlatformConfigValid();
123         if (status != null) {
124             UpdateCore.log(status);
125             return false;
126         }
127         try {
128             monitor.beginTask(Messages.Standalone_updating, 4);
129             searchRequest.performSearch(collector, new SubProgressMonitor(monitor,1));
130             IInstallFeatureOperation[] operations = collector.getOperations();
131             if (operations == null || operations.length == 0) {
132                 StandaloneUpdateApplication.exceptionLogged();
133                 UpdateCore.log(Utilities.newCoreException(NLS.bind(Messages.Standalone_noUpdate, (new String JavaDoc[] { featureId })), null));
134                 return false;
135             }
136
137             // Check for duplication conflicts
138
ArrayList JavaDoc conflicts =
139                 DuplicateConflictsValidator.computeDuplicateConflicts(
140                     operations,
141                     getConfiguration());
142             if (conflicts != null) {
143                 StandaloneUpdateApplication.exceptionLogged();
144                 UpdateCore.log(Utilities.newCoreException(Messages.Standalone_duplicate, null));
145                 return false;
146             }
147             
148             if (isVerifyOnly()) {
149                 status = OperationsManager.getValidator().validatePendingChanges(operations);
150                 if (status != null && status.getCode() == IStatus.ERROR)
151                     throw new CoreException(status);
152                 else
153                     return true;
154             }
155
156             IBatchOperation installOperation =
157                 OperationsManager
158                     .getOperationFactory()
159                     .createBatchInstallOperation(
160                     operations);
161             try {
162                 installOperation.execute(new SubProgressMonitor(monitor,3), this);
163                 System.out.println(
164                         Messages.Standalone_feature
165                         + featureId
166                         + " " //$NON-NLS-1$
167
+ Messages.Standalone_updated);
168                 return true;
169             } catch (Exception JavaDoc e) {
170                 StandaloneUpdateApplication.exceptionLogged();
171                 UpdateCore.log(
172                     Utilities.newCoreException(
173                             NLS.bind(Messages.Standalone_noUpdate, (new String JavaDoc[] { featureId })),
174                         e));
175                 return false;
176             }
177         } catch (CoreException ce) {
178             status = ce.getStatus();
179             if (status != null
180                 && status.getCode() == ISite.SITE_ACCESS_EXCEPTION) {
181                 // Just show this but do not throw exception
182
// because there may be results anyway.
183
System.out.println(Messages.Standalone_connection);
184             } else {
185                 StandaloneUpdateApplication.exceptionLogged();
186                 UpdateCore.log(ce);
187             }
188             return false;
189         } catch (OperationCanceledException ce) {
190             return true;
191         } finally {
192             monitor.done();
193         }
194     }
195
196
197     class UpdateSearchResultCollector implements IUpdateSearchResultCollector {
198         private ArrayList JavaDoc operations = new ArrayList JavaDoc();
199
200         public void accept(IFeature feature) {
201
202             IInstallFeatureOperation op =
203                 OperationsManager.getOperationFactory().createInstallOperation(
204                     null,
205                     feature,
206                     null,
207                     null,
208                     null);
209
210             IConfiguredSite site =
211                 UpdateUtils.getDefaultTargetSite(getConfiguration(), op);
212             if (site == null)
213                 site = UpdateUtils.getAffinitySite(getConfiguration(), feature);
214             if (site == null)
215                 site = targetSite;
216
217             op.setTargetSite(site);
218             operations.add(op);
219         }
220         public IInstallFeatureOperation[] getOperations() {
221             IInstallFeatureOperation[] opsArray =
222                 new IInstallFeatureOperation[operations.size()];
223             operations.toArray(opsArray);
224             return opsArray;
225         }
226     }
227 }
228
Popular Tags