KickJava   Java API By Example, From Geeks To Geeks.

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


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 disable (unconfigure) a feature
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 DisableCommand extends ScriptedCommand {
35
36     private IConfiguredSite targetSite;
37     private IFeature feature;
38
39     public DisableCommand(
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 containing the feature to disable
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, true);
73             if (features == null || features.length == 0) {
74                 throw new Exception JavaDoc(NLS.bind(Messages.Standalone_noFeatures3, (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                         feature = features[i];
86                         break;
87                     }
88                 }
89             if (feature == null) {
90                 throw new Exception JavaDoc(NLS.bind(Messages.Standalone_noFeatures4, (new String JavaDoc[] { featureId, version })));
91             }
92
93         } catch (MalformedURLException e) {
94             throw e;
95         } catch (CoreException e) {
96             throw e;
97         }
98     }
99
100     /**
101      */

102     public boolean run(IProgressMonitor monitor) {
103         // check if the config file has been modifed while we were running
104
IStatus status = OperationsManager.getValidator().validatePlatformConfigValid();
105         if (status != null) {
106             UpdateCore.log(status);
107             return false;
108         }
109         if (isVerifyOnly()) {
110             status =
111                 OperationsManager.getValidator().validatePendingUnconfig(
112                     feature);
113             if (status != null && status.getCode() == IStatus.WARNING)
114                 UpdateCore.log(status);
115             return status == null || status.getCode() == IStatus.WARNING;
116         }
117
118         final IUnconfigFeatureOperation configOperation =
119             OperationsManager.getOperationFactory().createUnconfigOperation(
120                 targetSite,
121                 feature);
122
123         try {
124             configOperation.execute(monitor, this);
125             return true;
126         } catch (CoreException e) {
127             StandaloneUpdateApplication.exceptionLogged();
128             UpdateCore.log(e);
129             return false;
130         } catch (InvocationTargetException e) {
131             StandaloneUpdateApplication.exceptionLogged();
132             UpdateCore.log(e);
133             return false;
134         }
135     }
136
137 }
138
Popular Tags