KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.OperationCanceledException;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.update.configuration.IConfiguredSite;
25 import org.eclipse.update.core.IFeature;
26 import org.eclipse.update.core.ISite;
27 import org.eclipse.update.core.SiteManager;
28 import org.eclipse.update.core.Utilities;
29 import org.eclipse.update.core.VersionedIdentifier;
30 import org.eclipse.update.internal.configurator.UpdateURLDecoder;
31 import org.eclipse.update.internal.core.Messages;
32 import org.eclipse.update.internal.core.UpdateCore;
33 import org.eclipse.update.internal.core.UpdateManagerUtils;
34 import org.eclipse.update.internal.operations.DuplicateConflictsValidator;
35 import org.eclipse.update.internal.operations.UpdateUtils;
36 import org.eclipse.update.internal.search.SiteSearchCategory;
37 import org.eclipse.update.operations.IBatchOperation;
38 import org.eclipse.update.operations.IInstallFeatureOperation;
39 import org.eclipse.update.operations.OperationsManager;
40 import org.eclipse.update.search.BackLevelFilter;
41 import org.eclipse.update.search.EnvironmentFilter;
42 import org.eclipse.update.search.IUpdateSearchResultCollector;
43 import org.eclipse.update.search.UpdateSearchRequest;
44 import org.eclipse.update.search.UpdateSearchScope;
45 import org.eclipse.update.search.VersionedIdentifiersFilter;
46
47 /**
48  * Command to install a feature.
49  * <p>
50  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
51  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
52  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
53  * (repeatedly) as the API evolves.
54  * </p>
55  * @since 3.0
56  */

57 public class InstallCommand extends ScriptedCommand {
58
59     private IConfiguredSite targetSite;
60     private UpdateSearchRequest searchRequest;
61     private UpdateSearchResultCollector collector;
62     private URL JavaDoc remoteSiteURL;
63     private String JavaDoc featureId;
64     private String JavaDoc version;
65
66     public InstallCommand(
67         String JavaDoc featureId,
68         String JavaDoc version,
69         String JavaDoc fromSite,
70         String JavaDoc toSite,
71         String JavaDoc verifyOnly)
72         throws Exception JavaDoc {
73
74         super(verifyOnly);
75
76         try {
77             this.featureId = featureId;
78             this.version = version;
79
80             //PAL foundation
81
this.remoteSiteURL = new URL JavaDoc(UpdateURLDecoder.decode(fromSite, "UTF-8")); //$NON-NLS-1$
82

83             // Get site to install to
84
targetSite = getTargetSite(toSite);
85             // if no site, try selecting the site that already has the old feature
86
if (targetSite == null)
87                 targetSite = UpdateUtils.getSiteWithFeature(getConfiguration(), featureId);
88             // if still no site, pick the product site, if writeable
89
if (targetSite == null) {
90                 IConfiguredSite[] sites = getConfiguration().getConfiguredSites();
91                 for (int i = 0; i < sites.length; i++) {
92                     if (sites[i].isProductSite() && sites[i].isUpdatable()) {
93                         targetSite = sites[i];
94                         break;
95                     }
96                 }
97             }
98             // if all else fails, pick the first updateable site
99
if (targetSite == null) {
100                 IConfiguredSite[] sites = getConfiguration().getConfiguredSites();
101                 for (int i = 0; i < sites.length; i++) {
102                     if (sites[i].isUpdatable()) {
103                         targetSite = sites[i];
104                         break;
105                     }
106                 }
107             }
108             // are we still checking for sites? forget about it
109
if (targetSite == null)
110                 throw Utilities.newCoreException(
111                         Messages.Standalone_cannotInstall + featureId + " " + version, //$NON-NLS-1$
112
null);
113             
114             UpdateSearchScope searchScope = new UpdateSearchScope();
115             searchScope.addSearchSite(
116                 NLS.bind( Messages.InstallCommand_site, remoteSiteURL.toExternalForm()),
117                 remoteSiteURL,
118                 new String JavaDoc[0]);
119
120             searchRequest =
121                 new UpdateSearchRequest(new SiteSearchCategory(), searchScope);
122             VersionedIdentifier vid =
123                 new VersionedIdentifier(featureId, version);
124             searchRequest.addFilter(
125                 new VersionedIdentifiersFilter(
126                     new VersionedIdentifier[] { vid }));
127             searchRequest.addFilter(new EnvironmentFilter());
128             searchRequest.addFilter(new BackLevelFilter());
129
130             collector = new UpdateSearchResultCollector();
131
132         } catch (MalformedURLException JavaDoc e) {
133             throw e;
134         } catch (CoreException e) {
135             throw e;
136         }
137     }
138
139     /**
140      */

141     public boolean run(IProgressMonitor monitor) {
142         try {
143             monitor.beginTask(Messages.Standalone_installing, 4);
144             searchRequest.performSearch(collector, new SubProgressMonitor(monitor,1));
145             IInstallFeatureOperation[] operations = collector.getOperations();
146             if (operations == null || operations.length == 0) {
147                 throw Utilities.newCoreException(
148                     Messages.Standalone_feature
149                         + featureId
150                         + " " //$NON-NLS-1$
151
+ version
152                         + Messages.Standalone_notFound
153                         + remoteSiteURL
154                         + Messages.Standalone_newerInstalled,
155                     null);
156             }
157
158             // Check for duplication conflicts
159
ArrayList JavaDoc conflicts =
160                 DuplicateConflictsValidator.computeDuplicateConflicts(
161                     operations,
162                     getConfiguration());
163             if (conflicts != null) {
164                 throw Utilities.newCoreException(Messages.Standalone_duplicate, null);
165             }
166
167             if (isVerifyOnly()) {
168                 if (operations == null || operations.length == 0)
169                     return false;
170                 IStatus status = OperationsManager.getValidator().validatePendingChanges(operations);
171                 if (status != null && status.getCode() == IStatus.ERROR)
172                     throw new CoreException(status);
173                 else
174                     return true;
175             }
176
177             IBatchOperation installOperation =
178                 OperationsManager
179                     .getOperationFactory()
180                     .createBatchInstallOperation(
181                     operations);
182             try {
183                 installOperation.execute(new SubProgressMonitor(monitor,3), this);
184                 System.out.println(
185                     Messages.Standalone_feature
186                         + featureId
187                         + " " //$NON-NLS-1$
188
+ version
189                         + Messages.Standalone_installed);
190                 return true;
191             } catch (Exception JavaDoc e) {
192                 throw Utilities.newCoreException(
193                     Messages.Standalone_cannotInstall + featureId + " " + version, //$NON-NLS-1$
194
e);
195             }
196         } catch (CoreException ce) {
197             StandaloneUpdateApplication.exceptionLogged();
198             UpdateCore.log(ce);
199             return false;
200         } catch (OperationCanceledException ce) {
201             return true;
202         } finally {
203             monitor.done();
204         }
205     }
206
207     private IConfiguredSite getTargetSite(String JavaDoc toSite) throws Exception JavaDoc {
208         if (toSite == null)
209             return null;
210             
211         IConfiguredSite[] configuredSites = getConfiguration().getConfiguredSites();
212         File JavaDoc sitePath = new File JavaDoc(toSite);
213         File JavaDoc secondaryPath = sitePath.getName().equals("eclipse") ? //$NON-NLS-1$
214
null : new File JavaDoc(sitePath, "eclipse"); //$NON-NLS-1$
215

216         for (int i = 0; i < configuredSites.length; i++) {
217             IConfiguredSite csite = configuredSites[i];
218             if (UpdateManagerUtils.sameURL(csite.getSite().getURL(),sitePath.toURL()))
219                 return csite;
220             else if (secondaryPath != null && UpdateManagerUtils.sameURL(csite.getSite().getURL(),secondaryPath.toURL()))
221                 return csite;
222         }
223
224         // extension site not found, need to create one
225
if (!sitePath.exists())
226             sitePath.mkdirs();
227         URL JavaDoc toSiteURL = sitePath.toURL();
228         ISite site = SiteManager.getSite(toSiteURL, null);
229         if (site == null) {
230             throw new Exception JavaDoc(Messages.Standalone_noSite + toSite);
231         }
232         IConfiguredSite csite = site.getCurrentConfiguredSite();
233         if (csite == null) {
234             csite = getConfiguration().createConfiguredSite(sitePath);
235             IStatus status = csite.verifyUpdatableStatus();
236             if (status.isOK())
237                 getConfiguration().addConfiguredSite(csite);
238             else
239                 throw new CoreException(status);
240
241             return csite;
242         }
243         return csite;
244     }
245     class UpdateSearchResultCollector implements IUpdateSearchResultCollector {
246         private ArrayList JavaDoc operations = new ArrayList JavaDoc();
247
248         public void accept(IFeature feature) {
249             if (feature
250                 .getVersionedIdentifier()
251                 .getIdentifier()
252                 .equals(featureId)
253                 && feature
254                     .getVersionedIdentifier()
255                     .getVersion()
256                     .toString()
257                     .equals(
258                     version)) {
259                 operations.add(
260                     OperationsManager
261                         .getOperationFactory()
262                         .createInstallOperation(
263                         targetSite,
264                         feature,
265                         null,
266                         null,
267                         null));
268             }
269         }
270         public IInstallFeatureOperation[] getOperations() {
271             IInstallFeatureOperation[] opsArray =
272                 new IInstallFeatureOperation[operations.size()];
273             operations.toArray(opsArray);
274             return opsArray;
275         }
276     }
277 }
278
Popular Tags