KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 import java.io.*;
13 import java.lang.reflect.*;
14 import java.net.*;
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
24 /**
25  * Command to uninstall a feature. The feature must be disabled before uninstalling it.
26  * <p>
27  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
28  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
29  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
30  * (repeatedly) as the API evolves.
31  * </p>
32  * @since 3.0
33  */

34 public class UninstallCommand extends ScriptedCommand {
35
36     private IConfiguredSite targetSite;
37     private IFeature feature;
38
39     public UninstallCommand(
40         String JavaDoc featureId,
41         String JavaDoc version,
42         String JavaDoc toSite,
43         String JavaDoc verifyOnly)
44         throws Exception JavaDoc {
45
46         super(verifyOnly);
47
48         try {
49             IConfiguredSite[] sites = getConfiguration().getConfiguredSites();
50
51             // Get site that contains the feature to uninstall
52
if (toSite != null) {
53                 URL toSiteURL = new File(toSite).toURL();
54                 if (SiteManager.getSite(toSiteURL, null) == null) {
55                     throw new Exception JavaDoc(Messages.Standalone_noSite + toSite);
56                 }
57                 targetSite =
58                     SiteManager
59                         .getSite(toSiteURL, null)
60                         .getCurrentConfiguredSite();
61             }
62             if (targetSite == null) {
63                 for (int i = 0; i < sites.length; i++) {
64                     if (sites[i].isProductSite()) {
65                         targetSite = sites[i];
66                         break;
67                     }
68                 }
69             }
70
71             IFeature[] features =
72                 UpdateUtils.searchSite(featureId, targetSite, false);
73             if (features == null || features.length == 0) {
74                 throw new Exception JavaDoc(NLS.bind(Messages.Standalone_noFeatures1, (new String JavaDoc[] { featureId })));
75             }
76             if (version == null || version.trim().length() == 0)
77                 feature = features[0]; // pick the first feature
78
else
79                 for (int i = 0; features != null && i < features.length; i++) {
80                     if (features[i]
81                         .getVersionedIdentifier()
82                         .getVersion()
83                         .toString()
84                         .equals(version)
85                         && !targetSite.isConfigured(features[i])) {
86                         feature = features[i];
87                         break;
88                     }
89                 }
90             if (feature == null) {
91                 throw new Exception JavaDoc(NLS.bind(Messages.Standalone_noFeatures2, (new String JavaDoc[] { featureId, version })));
92             }
93
94         } catch (MalformedURLException e) {
95             throw e;
96         } catch (CoreException e) {
97             throw e;
98         }
99     }
100
101     /**
102      */

103     public boolean run(IProgressMonitor monitor) {
104         // check if the config file has been modifed while we were running
105
IStatus status = OperationsManager.getValidator().validatePlatformConfigValid();
106         if (status != null) {
107             UpdateCore.log(status);
108             return false;
109         }
110         if (InstallRegistry.getInstance().get("feature_"+ feature.getVersionedIdentifier()) == null) { //$NON-NLS-1$
111
StandaloneUpdateApplication.exceptionLogged();
112             UpdateCore.log(Utilities.newCoreException(NLS.bind(Messages.UninstallCommand_featureNotInstalledByUM, (new String JavaDoc[] { feature.toString() })),null));
113             return false;
114         }
115                             
116         if (isVerifyOnly()) {
117             // if reached this point, it is safe to uninstall
118
return true;
119         }
120
121         final IUninstallFeatureOperation uninstallOperation =
122             OperationsManager.getOperationFactory().createUninstallOperation(
123                 targetSite,
124                 feature);
125
126         try {
127             uninstallOperation.execute(monitor, this);
128             return true;
129         } catch (CoreException e) {
130             StandaloneUpdateApplication.exceptionLogged();
131             UpdateCore.log(e);
132             return false;
133         } catch (InvocationTargetException e) {
134             StandaloneUpdateApplication.exceptionLogged();
135             UpdateCore.log(e);
136             return false;
137         }
138     }
139
140 }
141
Popular Tags