KickJava   Java API By Example, From Geeks To Geeks.

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


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.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.HashMap JavaDoc;
16
17 import org.eclipse.update.core.Utilities;
18 import org.eclipse.update.internal.core.Messages;
19 import org.eclipse.update.internal.core.UpdateCore;
20 import org.eclipse.update.internal.mirror.MirrorCommand;
21
22 /**
23  * This class parses the command line arguments for update standalone commands
24  * <p>
25  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
26  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
27  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
28  * (repeatedly) as the API evolves.
29  * </p>
30  * @since 3.0
31  */

32 public class CmdLineArgs {
33     private HashMap JavaDoc options = new HashMap JavaDoc();
34     public CmdLineArgs(String JavaDoc[] args) {
35 // // default command
36
// options.put("-command", "install");
37

38         for (int i = 0; i < args.length - 1; i++) {
39             if ("-command".equals(args[i])) { //$NON-NLS-1$
40
if (isValidCommand(args[i + 1])) {
41                     options.put("-command", args[i + 1]); //$NON-NLS-1$
42
i++;
43                 } else {
44                     StandaloneUpdateApplication.exceptionLogged();
45                     UpdateCore.log(
46                         Utilities.newCoreException(
47                             Messages.Standalone_invalidCmd + args[i + 1],
48                             null));
49                     return;
50                 }
51             }
52
53             if (isValidParam(args[i])) {
54                 options.put(args[i], args[i + 1]);
55                 i++;
56
57             }
58             // -to should specify a directory
59
// if -to specifies file URL, change it to a directory
60
String JavaDoc to = (String JavaDoc) options.get("-to"); //$NON-NLS-1$
61
if (to != null && to.startsWith("file:")) { //$NON-NLS-1$
62
try {
63                     URL JavaDoc url = new URL JavaDoc(to);
64                     options.put("-to", url.getFile()); //$NON-NLS-1$
65
} catch (MalformedURLException JavaDoc mue) {
66                 }
67             }
68         }
69     }
70
71     private boolean isValidParam(String JavaDoc param) {
72         return param.equals("-command") //$NON-NLS-1$
73
|| param.equals("-version") //$NON-NLS-1$
74
|| param.equals("-to") //$NON-NLS-1$
75
|| param.equals("-from") //$NON-NLS-1$
76
|| param.equals("-featureId") //$NON-NLS-1$
77
|| param.equals("-verifyOnly") //$NON-NLS-1$
78
|| param.equals("-mirrorURL") //$NON-NLS-1$
79
|| param.equals("-ignoreMissingPlugins"); //$NON-NLS-1$
80
}
81
82     private boolean isValidCommand(String JavaDoc cmd) {
83         if (cmd == null)
84             return false;
85         else
86             return cmd.equals("install") //$NON-NLS-1$
87
|| cmd.equals("enable") //$NON-NLS-1$
88
|| cmd.equals("disable") //$NON-NLS-1$
89
|| cmd.equals("search") //$NON-NLS-1$
90
|| cmd.equals("update") //$NON-NLS-1$
91
|| cmd.equals("mirror") //$NON-NLS-1$
92
|| cmd.equals("uninstall") //$NON-NLS-1$
93
|| cmd.equals("listFeatures") //$NON-NLS-1$
94
|| cmd.equals("addSite") //$NON-NLS-1$
95
|| cmd.equals("removeSite"); //$NON-NLS-1$
96
}
97
98     public ScriptedCommand getCommand() {
99         try {
100             String JavaDoc cmd = (String JavaDoc) options.get("-command"); //$NON-NLS-1$
101
if (cmd == null)
102                 return null;
103             if (cmd.equals("install")) //$NON-NLS-1$
104
return new InstallCommand(
105                     (String JavaDoc) options.get("-featureId"), //$NON-NLS-1$
106
(String JavaDoc) options.get("-version"), //$NON-NLS-1$
107
(String JavaDoc) options.get("-from"), //$NON-NLS-1$
108
(String JavaDoc) options.get("-to"), //$NON-NLS-1$
109
(String JavaDoc) options.get("-verifyOnly")); //$NON-NLS-1$
110
else if (cmd.equals("enable")) //$NON-NLS-1$
111
return new EnableCommand(
112                     (String JavaDoc) options.get("-featureId"), //$NON-NLS-1$
113
(String JavaDoc) options.get("-version"), //$NON-NLS-1$
114
(String JavaDoc) options.get("-to"), //$NON-NLS-1$
115
(String JavaDoc) options.get("-verifyOnly")); //$NON-NLS-1$
116
else if (cmd.equals("disable")) //$NON-NLS-1$
117
return new DisableCommand(
118                     (String JavaDoc) options.get("-featureId"), //$NON-NLS-1$
119
(String JavaDoc) options.get("-version"), //$NON-NLS-1$
120
(String JavaDoc) options.get("-to"), //$NON-NLS-1$
121
(String JavaDoc) options.get("-verifyOnly")); //$NON-NLS-1$
122
else if (cmd.equals("search")) //$NON-NLS-1$
123
return new SearchCommand((String JavaDoc) options.get("-from")); //$NON-NLS-1$
124
else if (cmd.equals("update")) //$NON-NLS-1$
125
return new UpdateCommand(
126                     (String JavaDoc) options.get("-featureId"), //$NON-NLS-1$
127
(String JavaDoc) options.get("-version"), //$NON-NLS-1$
128
(String JavaDoc) options.get("-verifyOnly")); //$NON-NLS-1$
129
else if (cmd.equals("mirror")) //$NON-NLS-1$
130
return new MirrorCommand(
131                     (String JavaDoc) options.get("-featureId"), //$NON-NLS-1$
132
(String JavaDoc) options.get("-version"), //$NON-NLS-1$
133
(String JavaDoc) options.get("-from"), //$NON-NLS-1$
134
(String JavaDoc) options.get("-to"), //$NON-NLS-1$
135
(String JavaDoc) options.get("-mirrorURL"), //$NON-NLS-1$
136
(String JavaDoc) options.get("-ignoreMissingPlugins")); //$NON-NLS-1$
137
else if (cmd.equals("uninstall")) //$NON-NLS-1$
138
return new UninstallCommand(
139                     (String JavaDoc) options.get("-featureId"), //$NON-NLS-1$
140
(String JavaDoc) options.get("-version"), //$NON-NLS-1$
141
(String JavaDoc) options.get("-to"), //$NON-NLS-1$
142
(String JavaDoc) options.get("-verifyOnly")); //$NON-NLS-1$
143
else if (cmd.equals("listFeatures")) //$NON-NLS-1$
144
return new ListFeaturesCommand((String JavaDoc) options.get("-from")); //$NON-NLS-1$
145
else if (cmd.equals("addSite")) //$NON-NLS-1$
146
return new AddSiteCommand((String JavaDoc) options.get("-from")); //$NON-NLS-1$
147
else if (cmd.equals("removeSite")) //$NON-NLS-1$
148
return new RemoveSiteCommand((String JavaDoc) options.get("-to")); //$NON-NLS-1$
149
else
150                 return null;
151         } catch (Exception JavaDoc e) {
152             StandaloneUpdateApplication.exceptionLogged();
153             UpdateCore.log(e);
154             return null;
155         }
156     }
157
158 }
159
Popular Tags