View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.launcher;
19  
20  import java.io.File;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  
24  /**
25   * A class that represents the holds the various argument types that are used
26   * in a Java command. In addition, it holds many of the flags that are used
27   * by the {@link LaunchTask} class when executing a JVM process.
28   *
29   * @author Patrick Luby
30   */
31  public class LaunchCommand {
32  
33      //------------------------------------------------------------------ Fields
34  
35      /**
36       * Cached appendOutput flag.
37       */
38      private boolean appendOutput = false;
39  
40      /**
41       * Cached classpath.
42       */
43      private String classpath = null;
44  
45      /**
46       * Cached debug flag.
47       */
48      private boolean debug = false;
49  
50      /**
51       * Cached displayMinimizedWindow flag.
52       */
53      private boolean displayMinimizedWindow = false;
54  
55      /**
56       * Cached disposeMinimizedWindow flag.
57       */
58      private boolean disposeMinimizedWindow = true;
59  
60      /**
61       * Cached failOnError flag.
62       */
63      private boolean failOnError = true;
64  
65      /**
66       * Cached main class name.
67       */
68      private String mainClassName = null;
69  
70      /**
71       * Cached minimizedWindowIcon.
72       */
73      private File minimizedWindowIcon = null;
74  
75      /**
76       * Cached minimizedWindowTitle.
77       */
78      private String minimizedWindowTitle = null;
79  
80      /**
81       * Cached output file.
82       */
83      private File outputFile = null;
84  
85      /**
86       * Cached print flag.
87       */
88      private boolean print = false;
89  
90      /**
91       * Cached requireTools flag.
92       */
93      private boolean requireTools = false;
94  
95      /**
96       * Cached redirect flag.
97       */
98      private boolean redirect = false;
99  
100     /**
101      * Cached arg elements
102      */
103     private ArrayList args = null;
104 
105     /**
106      * Cached jvmarg elements
107      */
108     private ArrayList jvmArgs = null;
109 
110     /**
111      * Cached sysproperty elements
112      */
113     private HashMap sysProperties = null;
114 
115     /**
116      * Cached useSystemIn flag.
117      */
118     private boolean useSystemIn = true;
119 
120     /**
121      * Cached waitForChild flag.
122      */
123     private boolean waitForChild = true;
124 
125     //----------------------------------------------------------------- Methods
126 
127     /**
128      * Get the class name.
129      *
130      * @return the class to execute <code>main(String[])</code>
131      */
132     public String getClassname() {
133 
134         return mainClassName;
135 
136     }
137 
138     /**
139      * Get the classpath.
140      *
141      * @return the classpath
142      */
143     public String getClasspath() {
144 
145         return classpath;
146 
147     }
148 
149     /**
150      * Get the debug flag.
151      *
152      * @return the debug flag
153      */
154     public boolean getDebug() {
155 
156         return debug;
157 
158     }
159 
160     /**
161      * Get the displayMinimizedWindow flag.
162      *
163      * @return the displayMinimizedWindow flag
164      */
165     public boolean getDisplayminimizedwindow() {
166 
167         return displayMinimizedWindow;
168 
169     }
170 
171     /**
172      * Get the disposeMinimizedWindow flag.
173      *
174      * @return the disposeMinimizedWindow flag
175      */
176     public boolean getDisposeminimizedwindow() {
177 
178         return disposeMinimizedWindow;
179 
180     }
181 
182     /**
183      * Get the failOnError flag.
184      *
185      * @return the failOnError flag
186      */
187     public boolean getFailonerror() {
188 
189         return failOnError;
190 
191     }
192 
193     /**
194      * Get the title for the minimized window that will be displayed in the
195      * Windows taskbar.
196      *
197      * @return the title to set for any minimized window that is displayed
198      *  in the Windows taskbar
199      */
200     public String getMinimizedwindowtitle() {
201 
202         return minimizedWindowTitle;
203 
204     }
205 
206     /**
207      * Get the icon file for the minimized window that will be displayed in the
208      * Windows taskbar.
209      *
210      * @return the icon file to use for any minimized window that is displayed
211      *  in the Windows taskbar
212      */
213     public File getMinimizedwindowicon() {
214 
215         return minimizedWindowIcon;
216 
217     }
218 
219     /**
220      * Get the file that the child JVM's System.out and System.err will be
221      * redirected to.
222      *
223      * @return the File to redirect System.out and System.err to
224      */
225     public File getOutput() {
226 
227         return outputFile;
228 
229     }
230 
231     /**
232      * Get the appendOutput flag.
233      *
234      * @return the appendOutput flag
235      */
236     public boolean getAppendoutput() {
237 
238         return appendOutput;
239 
240     }
241 
242     /**
243      * Get the redirect flag.
244      *
245      * @return the redirect flag
246      */
247     public boolean getRedirectoutput() {
248 
249         return redirect;
250 
251     }
252 
253     /**
254      * Get the list of nested arg elements.
255      *
256      * @return the list of {@link String} objects
257      */
258     public ArrayList getArgs() {
259 
260         return args;
261 
262     }
263 
264     /**
265      * Get the list of nested jvmarg elements.
266      *
267      * @return the list of {@link String} objects
268      */
269     public ArrayList getJvmargs() {
270 
271         return jvmArgs;
272 
273     }
274 
275     /**
276      * Get the print flag.
277      *
278      * @return the print flag
279      */
280     public boolean getPrint() {
281 
282         return print;
283 
284     }
285 
286     /**
287      * Get the requireTools flag.
288      *
289      * @return the requireTools flag
290      */
291     public boolean getRequiretools() {
292 
293         return requireTools;
294 
295     }
296 
297     /**
298      * Get the list of nested sysproperty elements.
299      *
300      * @return the {@link String} objects
301      */
302     public HashMap getSysproperties() {
303 
304         return sysProperties;
305 
306     }
307 
308     /**
309      * Get the useSystemIn flag.
310      *
311      * @return the useSystemIn flag
312      */
313     public boolean getUsesystemin() {
314 
315         return useSystemIn;
316 
317     }
318 
319     /**
320      * Get the waitForChild flag.
321      *
322      * @return the waitForChild flag
323      */
324     public boolean getWaitforchild() {
325 
326         return waitForChild;
327 
328     }
329 
330     /**
331      * Set the print flag.
332      *
333      * @param print the print flag
334      */
335     public void setPrint(boolean print) {
336 
337         this.print = print;
338 
339     }
340 
341     /**
342      * Set the requireTools flag.
343      *
344      * @param requireTools the requireTools flag
345      */
346     public void setRequiretools(boolean requireTools) {
347 
348         this.requireTools = requireTools;
349 
350     }
351 
352     /**
353      * Set the useSystemIn flag. Setting this flag to false will cause this
354      * task to not read System.in. This will cause the child JVM to never
355      * receive any bytes when it reads System.in. Setting this flag to false
356      * is useful in some Unix environments where processes cannot be put in
357      * the background when they read System.in.
358      *
359      * @param useSystemIn the useSystemIn flag
360      */
361     public void setUsesystemin(boolean useSystemIn) {
362 
363         this.useSystemIn = useSystemIn;
364 
365     }
366 
367     /**
368      * Set the waitForChild flag. Setting this flag to true will cause this
369      * task to wait for the child JVM to finish executing before the task
370      * completes. Setting this flag to false will cause this task to complete
371      * immediately after it starts the execution of the child JVM. Setting it
372      * false emulates the "&" background operator in most Unix shells and is
373      * most of set to false when launching server or GUI applications.
374      *
375      * @param waitForChild the waitForChild flag
376      */
377     public void setWaitforchild(boolean waitForChild) {
378 
379         this.waitForChild = waitForChild;
380 
381     }
382 
383     /**
384      * Set the class name.
385      *
386      * @param mainClassName the class to execute <code>main(String[])</code>
387      */
388     public void setClassname(String mainClassName) {
389 
390         this.mainClassName = mainClassName;
391 
392     }
393 
394     /**
395      * Set the classpath.
396      *
397      * @param classpath the classpath
398      */
399     public void setClasspath(String classpath) {
400 
401         this.classpath = classpath;
402 
403     }
404 
405     /**
406      * Set the debug flag.
407      *
408      * @param debug the debug flag 
409      */
410     public void setDebug(boolean debug) {
411 
412         this.debug = debug;
413 
414     }
415 
416     /**
417      * Set the displayMinimizedWindow flag. Note that this flag has no effect
418      * on non-Windows platforms. On Windows platform, setting this flag to true
419      * will cause a minimized window to be displayed in the Windows task bar
420      * while the child process is executing. This flag is usually set to true
421      * for server applications that also have their "waitForChild" attribute
422      * set to false via the {@link #setWaitforchild(boolean)} method.
423      *
424      * @param displayMinimizedWindow true if a minimized window should be
425      *  displayed in the Windows task bar while the child process is executing 
426      */
427     public void setDisplayminimizedwindow(boolean displayMinimizedWindow) {
428 
429         this.displayMinimizedWindow = displayMinimizedWindow;
430 
431     }
432 
433     /**
434      * Set the disposeMinimizedWindow flag. Note that this flag has no effect
435      * on non-Windows platforms. On Windows platform, setting this flag to true
436      * will cause any minimized window that is display by setting the
437      * "displayMinimizedWindow" attribute to true via the
438      * {@link #setDisplayminimizedwindow(boolean)} to be automatically
439      * disposed of when the child JVM's <code>main(String[])</code> returns.
440      * This flag is normally used for applications that don't explicitly call
441      * {@link System#exit(int)}. If an application does not explicitly call
442      * {@link System#exit(int)}, an minimized windows need to be disposed of
443      * for the child JVM to exit.
444      *
445      * @param disposeMinimizedWindow true if a minimized window in the Windows
446      *  taskbar should be automatically disposed of after the child JVM's
447      *  <code>main(String[])</code> returns
448      */
449     public void setDisposeminimizedwindow(boolean disposeMinimizedWindow) {
450 
451         this.disposeMinimizedWindow = disposeMinimizedWindow;
452 
453     }
454 
455     /**
456      * Set the failOnError flag.
457      *
458      * @param failOnError the failOnError flag 
459      */
460     public void setFailonerror(boolean failOnError) {
461 
462         this.failOnError = failOnError;
463 
464     }
465 
466     /**
467      * Set the title for the minimized window that will be displayed in the
468      * Windows taskbar. Note that this property has no effect on non-Windows
469      * platforms.
470      *
471      * @param minimizedWindowTitle the title to set for any minimized window
472      *  that is displayed in the Windows taskbar
473      */
474     public void setMinimizedwindowtitle(String minimizedWindowTitle) {
475 
476         this.minimizedWindowTitle = minimizedWindowTitle;
477 
478     }
479 
480     /**
481      * Set the icon file for the minimized window that will be displayed in the
482      * Windows taskbar. Note that this property has no effect on non-Windows
483      * platforms.
484      *
485      * @param minimizedWindowIcon the icon file to use for any minimized window
486      *  that is displayed in the Windows taskbar
487      */
488     public void setMinimizedwindowicon(File minimizedWindowIcon) {
489 
490         this.minimizedWindowIcon = minimizedWindowIcon;
491 
492     }
493 
494     /**
495      * Set the file that the child JVM's System.out and System.err will be
496      * redirected to. Output will only be redirected if the redirect flag
497      * is set to true via the {@link #setRedirectoutput(boolean)} method.
498      *
499      * @param outputFile a File to redirect System.out and System.err to
500      */
501     public void setOutput(File outputFile) {
502 
503         this.outputFile = outputFile;
504 
505     }
506 
507     /**
508      * Set the appendOutput flag. Setting this flag to true will cause the child
509      * JVM to append System.out and System.err to the file specified by the
510      * {@link #setOutput(File)} method. Setting this flag to false will cause
511      * the child to overwrite the file.
512      *
513      * @param appendOutput true if output should be appended to the output file
514      */
515     public void setAppendoutput(boolean appendOutput) {
516 
517         this.appendOutput = appendOutput;
518 
519     }
520 
521     /**
522      * Set the list of nested arg elements.
523      *
524      * @param args a list of {@link String} objects
525      */
526     public void setArgs(ArrayList args) {
527 
528         this.args = args;
529 
530     }
531 
532     /**
533      * Set the list of nested jvmarg elements.
534      *
535      * @param jvmArgs a list of {@link String} objects
536      */
537     public void setJvmargs(ArrayList jvmArgs) {
538 
539         this.jvmArgs = jvmArgs;
540 
541     }
542 
543     /**
544      * Set the list of nested sysproperty elements.
545      *
546      * @param sysProperties a map of {@link String} objects
547      */
548     public void setSysproperties(HashMap sysProperties) {
549 
550         this.sysProperties = sysProperties;
551 
552     }
553 
554     /**
555      * Set the redirect flag. Setting this flag to true will cause the child
556      * JVM's System.out and System.err to be redirected to file set using the
557      * {@link #setOutput(File)} method. Setting this flag to false will
558      * cause no redirection.
559      *
560      * @param redirect true if System.out and System.err should be redirected
561      */
562     public void setRedirectoutput(boolean redirect) {
563 
564         this.redirect = redirect;
565 
566     }
567 
568 }