blob: 9c5e3e01923c3cc3bc776f0ba7bc8a79adb8db0d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 *
26 */
27
28import java.io.*;
29import java.util.Properties;
30
31public class JavaProcess {
32
33 protected Process process = null;
34
35 private String classname;
36 private StringBuilder classArgs;
37 private StringBuilder javaOptions;
38
39 private static String java = System.getProperty("java.home")
40 + File.separator + "bin"
41 + File.separator + "java";
42
43 public JavaProcess(String classname) {
44 this(classname, "", "");
45 }
46
47 public JavaProcess(String classname, String classArgs) {
48 this(classname, "", classArgs);
49 }
50
51 public JavaProcess(String classname, String javaOptions, String classArgs) {
52 this.classname = classname;
53 this.javaOptions = new StringBuilder(javaOptions);
54 this.classArgs = new StringBuilder(classArgs);
55 }
56
57 /**
58 * add java options to the java command
59 */
60 public void addOptions(String[] opts) {
61 if (javaOptions != null && javaOptions.length() > 0) {
62 javaOptions.append(" ");
63 }
64
65 for (int i = 0; i < opts.length; i++) {
66 if (i != 0) {
67 javaOptions.append(" ");
68 }
69 javaOptions.append(opts[i]);
70 }
71 }
72
73 /**
74 * add arguments to the class arguments
75 */
76 public void addArguments(String[] args) {
77 if (classArgs != null && classArgs.length() > 0) {
78 classArgs.append(" ");
79 }
80
81 for (int i = 0; i < args.length; i++) {
82 if (i != 0) {
83 classArgs.append(" ");
84 }
85 classArgs.append(args[i]);
86 }
87 }
88
89 /**
90 * start the java process
91 */
92 public void start() throws IOException {
93 if (process != null) {
94 return;
95 }
96
97 String javaCommand = java + " " + javaOptions + " "
98 + classname + " " + classArgs;
99
100 System.out.println("exec'ing: " + javaCommand);
101
102 process = Runtime.getRuntime().exec(javaCommand);
103 }
104
105 /**
106 * destroy the java process
107 */
108 public void destroy() {
109 if (process != null) {
110 process.destroy();
111 }
112 process = null;
113 }
114
115 public int exitValue() {
116 if (process != null) {
117 return process.exitValue();
118 }
119 throw new RuntimeException("exitValue called with process == null");
120 }
121
122 public InputStream getErrorStream() {
123 if (process != null) {
124 return process.getErrorStream();
125 }
126 throw new RuntimeException(
127 "getErrorStream() called with process == null");
128 }
129
130 public InputStream getInputStream() {
131 if (process != null) {
132 return process.getInputStream();
133 }
134 throw new RuntimeException(
135 "getInputStream() called with process == null");
136 }
137
138 public OutputStream getOutputStream() {
139 if (process != null) {
140 return process.getOutputStream();
141 }
142 throw new RuntimeException(
143 "getOutputStream() called with process == null");
144 }
145
146 public int waitFor() throws InterruptedException {
147 if (process != null) {
148 return process.waitFor();
149 }
150 throw new RuntimeException("waitFor() called with process == null");
151 }
152}