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