KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > standalone > Options


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.help.internal.standalone;
12
13 import java.io.*;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Options for starting stand alone help and infocenter.
19  */

20 public class Options {
21     // Update command parameters' keys
22
public static final String JavaDoc PARAM_FEATUREID = "featureId"; //$NON-NLS-1$
23

24     public static final String JavaDoc PARAM_VERSION = "version"; //$NON-NLS-1$
25

26     public static final String JavaDoc PARAM_FROM = "from"; //$NON-NLS-1$
27

28     public static final String JavaDoc PARAM_TO = "to"; //$NON-NLS-1$
29

30     public static final String JavaDoc PARAM_VERIFYONLY = "verifyOnly"; //$NON-NLS-1$
31

32     // debugging
33
private static boolean debug = false;
34
35     // use eclipse.exe
36
private static boolean useExe = true;
37
38     // Eclipse installation directory
39
private static File eclipseHome;
40
41     // workspace directory to be used by Eclipse
42
private static File workspace;
43
44     // Eclipse .lock file
45
private static File lockFile;
46
47     // .hostport file to obtain help server host and port from Eclipse help
48
// application
49
private static File hostPortFile;
50
51     // vm to use
52
private static String JavaDoc vm;
53
54     // arguments to pass to Eclipse
55
private static List JavaDoc vmArgs;
56
57     // arguments to pass to VM
58
private static List JavaDoc eclipseArgs;
59
60     // help command to execute
61
private static List JavaDoc helpCommand;
62
63     // host to override appserver preferences
64
private static String JavaDoc host;
65
66     // port to override appserver preferences
67
private static String JavaDoc port;
68
69     // User ID of the administrator
70
private static String JavaDoc adminId = null;
71     
72     // Password of the administrator
73
private static String JavaDoc adminPassword = null;
74     
75     // location of the trustStore to use if SSL
76
// connection must be established
77
private static String JavaDoc trustStoreLocation = null;
78     
79     // password of the trustStore to use if SSL
80
// connection must be established
81
private static String JavaDoc trustStorePassword = null;
82     
83     // update parameters, ex: "version=1.0.0", "from=file:///c:/site"
84
private static String JavaDoc[] updateParameters;
85
86     /**
87      * Initializes options.
88      *
89      * @param appId
90      * eclipse application id
91      * @param args
92      * array of String options and their values Option
93      * <code>-eclipseHome dir</code> specifies Eclipse installation
94      * directory. It must be provided, when current directory is not
95      * the same as Eclipse installation directory. Additionally, most
96      * options accepted by Eclipse execuable are supported.
97      */

98     public static void init(String JavaDoc appId, String JavaDoc[] args) {
99         // convert array of arguments to a list
100
List JavaDoc list = new ArrayList JavaDoc();
101         for (int i = 0; i < args.length; i++) {
102             list.add(args[i]);
103         }
104
105         init(appId, list);
106     }
107
108     /**
109      * Initializes options.
110      *
111      * @param appId
112      * eclipse application id
113      * @param options
114      * list of options and their values Option
115      * <code>-eclipseHome dir</code> specifies Eclipse installation
116      * directory. It must be provided, when current directory is not
117      * the same as Eclipse installation directory. Additionally, most
118      * options accepted by Eclipse execuable are supported.
119      */

120     public static void init(String JavaDoc appId, List JavaDoc options) {
121         // Initialize eclipseArgs with all passed options
122
eclipseArgs = new ArrayList JavaDoc();
123         eclipseArgs.addAll(options);
124
125         // consume -command option
126
helpCommand = extractOption(eclipseArgs, "-command"); //$NON-NLS-1$
127
if (helpCommand == null) {
128             helpCommand = new ArrayList JavaDoc(0);
129         }
130         // consume update commands' parameters
131
List JavaDoc parameters = new ArrayList JavaDoc();
132         List JavaDoc param = extractOption(eclipseArgs, "-" + PARAM_FEATUREID); //$NON-NLS-1$
133
if (param != null) {
134             parameters.add(PARAM_FEATUREID + "=" + (String JavaDoc) param.get(0)); //$NON-NLS-1$
135
}
136         param = extractOption(eclipseArgs, "-" + PARAM_VERSION); //$NON-NLS-1$
137
if (param != null) {
138             parameters.add(PARAM_VERSION + "=" + (String JavaDoc) param.get(0)); //$NON-NLS-1$
139
}
140         param = extractOption(eclipseArgs, "-" + PARAM_FROM); //$NON-NLS-1$
141
if (param != null) {
142             parameters.add(PARAM_FROM + "=" + (String JavaDoc) param.get(0)); //$NON-NLS-1$
143
}
144         param = extractOption(eclipseArgs, "-" + PARAM_TO); //$NON-NLS-1$
145
if (param != null) {
146             parameters.add(PARAM_TO + "=" + (String JavaDoc) param.get(0)); //$NON-NLS-1$
147
}
148         param = extractOption(eclipseArgs, "-" + PARAM_VERIFYONLY); //$NON-NLS-1$
149
if (param != null) {
150             parameters.add(PARAM_VERIFYONLY + "=" + (String JavaDoc) param.get(0)); //$NON-NLS-1$
151
}
152         updateParameters = (String JavaDoc[]) parameters.toArray(new String JavaDoc[parameters
153                 .size()]);
154
155         // read -debug option
156
if (getOption(eclipseArgs, "-debug") != null) { //$NON-NLS-1$
157
debug = true;
158             System.out.println("Debugging is on."); //$NON-NLS-1$
159
}
160         // consume -noexec option
161
if (extractOption(eclipseArgs, "-noexec") != null) { //$NON-NLS-1$
162
useExe = false;
163         }
164         // consume -eclipsehome (accept eclipse_home too) option
165
List JavaDoc homes = extractOption(eclipseArgs, "-eclipseHome"); //$NON-NLS-1$
166
if (homes == null || homes.isEmpty()) {
167             homes = extractOption(eclipseArgs, "-eclipse_Home"); //$NON-NLS-1$
168
}
169         if (homes != null && !homes.isEmpty()) {
170             eclipseHome = new File((String JavaDoc) homes.get(0));
171         } else {
172             eclipseHome = new File(System.getProperty("user.dir")); //$NON-NLS-1$
173
}
174
175         // read -data option
176
List JavaDoc workspaces = extractOption(eclipseArgs, "-data"); //$NON-NLS-1$
177
if (workspaces != null && !workspaces.isEmpty()) {
178             String JavaDoc workspacePath = (String JavaDoc) workspaces.get(0);
179             workspace = new File(workspacePath);
180             if (!workspace.isAbsolute()) {
181                 workspace = new File(eclipseHome, workspacePath);
182             }
183         } else {
184             workspace = new File(eclipseHome, "workspace"); //$NON-NLS-1$
185
}
186         lockFile = new File(workspace, "/.metadata/.helplock"); //$NON-NLS-1$
187
hostPortFile = new File(workspace, "/.metadata/.connection"); //$NON-NLS-1$
188

189         // consume -host option
190
List JavaDoc hosts = extractOption(eclipseArgs, "-host"); //$NON-NLS-1$
191
if (hosts != null && hosts.size() > 0) {
192             host = (String JavaDoc) hosts.get(0);
193         }
194
195         // consume -port option
196
List JavaDoc ports = extractOption(eclipseArgs, "-port"); //$NON-NLS-1$
197
if (ports != null && ports.size() > 0) {
198             port = (String JavaDoc) ports.get(0);
199         }
200         
201         // consume - adminId option
202
List JavaDoc adminIds = extractOption(eclipseArgs, "-adminId"); //$NON-NLS-1$
203
if (adminIds != null && adminIds.size() > 0) {
204             adminId = (String JavaDoc) adminIds.get(0);
205         }
206         
207         // consume - admin option
208
List JavaDoc adminPasswords = extractOption(eclipseArgs, "-adminPassword"); //$NON-NLS-1$
209
if (adminPasswords != null && adminPasswords.size() > 0) {
210             adminPassword = (String JavaDoc) adminPasswords.get(0);
211         }
212         
213         // consume - trustStoreLocation option
214
List JavaDoc trustStoreLocations = extractOption(eclipseArgs, "-trustStoreLocation"); //$NON-NLS-1$
215
if (trustStoreLocations != null && trustStoreLocations.size() > 0) {
216             trustStoreLocation = (String JavaDoc) trustStoreLocations.get(0);
217         }
218         
219         // consume - trustStoreLocation option
220
List JavaDoc trustStorePasswords = extractOption(eclipseArgs, "-trustStorePassword"); //$NON-NLS-1$
221
if (trustStorePasswords != null && trustStorePasswords.size() > 0) {
222             trustStorePassword = (String JavaDoc) trustStorePasswords.get(0);
223         }
224
225         // consume -vm option
226
List JavaDoc vms = extractOption(eclipseArgs, "-vm"); //$NON-NLS-1$
227
if (vms != null && !vms.isEmpty()) {
228             vm = (String JavaDoc) vms.get(0);
229         } else {
230             String JavaDoc vmName = System.getProperty("java.vm.name"); //$NON-NLS-1$
231
String JavaDoc executable = "J9".equals(vmName) ? "j9" : "java"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
232
if (System.getProperty("os.name").startsWith("Win")) { //$NON-NLS-1$ //$NON-NLS-2$
233
if (!debug) {
234                     executable += "w.exe"; //$NON-NLS-1$
235
} else {
236                     executable += ".exe"; //$NON-NLS-1$
237
}
238             }
239             vm = System.getProperty("java.home") //$NON-NLS-1$
240
+ File.separator + "bin" //$NON-NLS-1$
241
+ File.separator + executable;
242         }
243
244         // consume -vmargs option
245
vmArgs = new ArrayList JavaDoc(0);
246         List JavaDoc passedVmArgs = extractOption(eclipseArgs, "-vmargs"); //$NON-NLS-1$
247
if (passedVmArgs != null && passedVmArgs.size() > 0) {
248             vmArgs = passedVmArgs;
249         }
250
251         // modify the options for passing them to eclipse
252
// add -application option
253
eclipseArgs.add(0, "-data"); //$NON-NLS-1$
254
eclipseArgs.add(1, getWorkspace().getAbsolutePath());
255
256         extractOption(eclipseArgs, "-application"); //$NON-NLS-1$
257
eclipseArgs.add(0, "-application"); //$NON-NLS-1$
258
eclipseArgs.add(1, appId);
259
260         // add -nosplash option (prevent splash)
261
extractOption(eclipseArgs, "-showsplash"); //$NON-NLS-1$
262
extractOption(eclipseArgs, "-endsplash"); //$NON-NLS-1$
263
extractOption(eclipseArgs, "-nosplash"); //$NON-NLS-1$
264
eclipseArgs.add(0, "-nosplash"); //$NON-NLS-1$
265

266         // add server_host and/or port to -vmargs option
267
if (host != null || port != null) {
268             if (host != null) {
269                 vmArgs.add("-Dserver_host=" + host); //$NON-NLS-1$
270
}
271             if (port != null) {
272                 vmArgs.add("-Dserver_port=" + port); //$NON-NLS-1$
273
}
274         }
275     }
276
277     /**
278      * Returns true if debugging is enabled
279      */

280     public static boolean isDebug() {
281         return debug;
282     }
283
284     public static String JavaDoc getAdminId() {
285         return adminId;
286     }
287     
288     public static String JavaDoc getAdminPassword() {
289         return adminPassword;
290     }
291     
292     public static String JavaDoc getTrustStoreLocation() {
293         return trustStoreLocation;
294     }
295     
296     public static String JavaDoc getTrustStorePassword() {
297         return trustStorePassword;
298     }
299     
300     public static File getConnectionFile() {
301         return hostPortFile;
302     }
303
304     public static File getLockFile() {
305         return lockFile;
306     }
307
308     public static File getEclipseHome() {
309         return eclipseHome;
310     }
311
312     public static File getWorkspace() {
313         return workspace;
314     }
315
316     public static List JavaDoc getHelpCommand() {
317         return helpCommand;
318     }
319
320     public static String JavaDoc[] getUpdateParameters() {
321         return updateParameters;
322     }
323
324     public static List JavaDoc getEclipseArgs() {
325         return eclipseArgs;
326     }
327
328     /**
329      * Removes specified option and its list of values from a list of options
330      *
331      * @param optionName
332      * name of the option e.g. -data
333      * @return List of String values of the specified option, or null if option
334      * is not present
335      */

336     private static List JavaDoc extractOption(List JavaDoc options, String JavaDoc optionName) {
337         List JavaDoc values = null;
338         for (int i = 0; i < options.size();) {
339             if (optionName.equalsIgnoreCase((String JavaDoc) options.get(i))) {
340                 if (values == null) {
341                     values = new ArrayList JavaDoc(1);
342                 }
343                 // found the option, remove option
344
options.remove(i);
345                 // remove option parameters
346
while (i < options.size()) {
347                     if (((String JavaDoc) options.get(i)).startsWith("-") //$NON-NLS-1$
348
&& !optionName.equals("-vmargs")) { //$NON-NLS-1$
349
// start of next option
350
break;
351                     }
352                     // note, and remove option value
353
values.add(options.get(i));
354                     options.remove(i);
355                 }
356             } else {
357                 i++;
358             }
359         }
360         return values;
361     }
362
363     /**
364      * Obtains specified option and its list of values from a list of options
365      *
366      * @param optionName
367      * name of the option e.g. -data
368      * @param options
369      * List of Eclipse options
370      * @return List of String values of the specified option, or null if option
371      * is not present
372      */

373     private static List JavaDoc getOption(List JavaDoc options, String JavaDoc optionName) {
374         List JavaDoc values = null;
375         for (int i = 0; i < options.size(); i++) {
376             if (optionName.equalsIgnoreCase((String JavaDoc) options.get(i))) {
377                 if (values == null) {
378                     values = new ArrayList JavaDoc(1);
379                 }
380                 // read option parameters
381
for (int j = i + 1; j < options.size(); j++) {
382                     if (((String JavaDoc) options.get(j)).startsWith("-") //$NON-NLS-1$
383
&& !optionName.equals("-vmargs")) { //$NON-NLS-1$
384
// start of next option
385
i = j;
386                         break;
387                     }
388                     values.add(options.get(j));
389                 }
390             }
391         }
392         return values;
393     }
394
395     public static String JavaDoc getVm() {
396         return vm;
397     }
398
399     public static List JavaDoc getVmArgs() {
400         return vmArgs;
401     }
402
403     /**
404      * Returns the useExe.
405      *
406      * @return boolean
407      */

408     public static boolean useExe() {
409         return useExe;
410     }
411
412 }
413
Popular Tags