1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.experiment;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.util.Timer;
26 import java.util.TimerTask;
27
28
29
30
31
32
33 public final class ShellCommand
34 {
35
36
37
38
39
40 private static final int WAIT_FOR_TIMEOUT = 10000;
41
42
43
44
45 private volatile boolean stdoutChanged = false;
46
47
48
49
50 private volatile boolean stderrChanged = false;
51
52
53
54
55 private volatile Process process;
56
57
58
59
60
61
62
63 private volatile boolean timeout = false;
64
65
66
67
68
69 private volatile boolean suppressCmdOutput = false;
70
71
72
73
74
75 private static boolean waitForTimeout = false;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public ShellCommand(final String cmd, ThreadGroup threadGroup)
94 {
95 this(cmd, threadGroup, false);
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public ShellCommand(final String cmd, final ThreadGroup threadGroup, boolean suppressCmdOutput)
113 {
114 this.suppressCmdOutput = suppressCmdOutput;
115 if (threadGroup != null) {
116 Thread newThread = new Thread(threadGroup, cmd) {
117 public void run() {
118 try {
119 createProcess(cmd, threadGroup);
120 } catch (IOException e) {
121 e.printStackTrace();
122 System.out.println("FAILED: " + cmd);
123 }
124 }
125 };
126 newThread.start();
127 } else {
128 try {
129 createProcess(cmd, null);
130 } catch (IOException e) {
131 e.printStackTrace();
132 System.out.println("FAILED: " + cmd);
133 }
134 }
135 }
136
137
138
139
140
141 public boolean waitForProcess()
142 {
143 TimerTask tt = new TimerTask() {
144 public void run() {
145 timeout = true;
146 }
147 };
148 Timer timer = new Timer("WaitForProcess", true);
149 timer.schedule(tt, 5000);
150
151 while (process == null && !timeout) { }
152 if (process == null)
153 return false;
154 try {
155 process.waitFor();
156 } catch (InterruptedException e) {
157 process.destroy();
158 }
159 timer.cancel();
160 return true;
161 }
162
163 public boolean stdoutModified()
164 {
165 return stdoutChanged;
166 }
167
168 public boolean stderrModified()
169 {
170 return stderrChanged;
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 public static void exec(final String cmd, ThreadGroup threadGroup)
193 throws IOException
194 {
195 new ShellCommand(cmd, threadGroup);
196 }
197
198
199
200
201
202
203
204 public static void exec(final String cmd)
205 throws IOException
206 {
207 exec(cmd, null);
208 }
209
210
211
212
213
214
215
216
217
218
219 public static void waitFor(ThreadGroup threadGroup, boolean debug)
220 {
221 int count = threadGroup.activeCount();
222 final Thread curThread = Thread.currentThread();
223 Timer timer = new Timer("WaitForThreadGroup", true);
224 timer.schedule(new TimerTask() {
225 public void run() {
226 waitForTimeout = true;
227 curThread.interrupt();
228 }
229 }, WAIT_FOR_TIMEOUT*count);
230
231
232 while (count > 0 && !waitForTimeout) {
233 try {
234 Thread.sleep(2000);
235 } catch (InterruptedException e) {
236
237
238
239
240
241 }
242 count = threadGroup.activeCount();
243 }
244 if (count > 0) {
245
246 threadGroup.interrupt();
247 if (debug) {
248 System.out.println("Remaining threads: " + count);
249 threadGroup.list();
250 }
251 }
252 timer.cancel();
253 waitForTimeout = false;
254 }
255
256 public static void waitFor(ThreadGroup threadGroup)
257 {
258 waitFor(threadGroup, false);
259 }
260
261
262
263
264
265
266 private void createProcess(final String cmd, ThreadGroup threadGroup)
267 throws IOException
268 {
269 stdoutChanged = false;
270 stderrChanged = false;
271 process = Runtime.getRuntime().exec(cmd);
272 if (!suppressCmdOutput)
273 System.out.println(cmd);
274 final String scmd = cmd.split(" ")[0];
275
276 Thread outThread = new Thread(scmd + "-out") {
277 public void run() {
278 InputStream processOut = process.getInputStream();
279 BufferedReader out = new BufferedReader(new InputStreamReader(processOut));
280 String stdout;
281
282 try {
283 while ((stdout = out.readLine()) != null) {
284 synchronized(this) {
285 stdoutChanged = true;
286 System.out.println(scmd + "-out: " + stdout);
287 }
288 }
289 out.close();
290 } catch (java.io.IOException e) {
291 e.printStackTrace();
292 System.out.println(cmd + " failed");
293 }
294 }
295 };
296
297 Thread errorThread = new Thread(scmd + "-err") {
298 public void run() {
299 InputStream processError = process.getErrorStream();
300 BufferedReader err = new BufferedReader(new InputStreamReader(processError));
301 String stderr;
302
303 try {
304 while ((stderr = err.readLine()) != null) {
305 synchronized(this) {
306 stderrChanged = true;
307 System.out.println(cmd);
308 System.out.println(scmd + "-err: " + stderr);
309 }
310 }
311 err.close();
312 } catch (java.io.IOException e) {
313 System.out.println(cmd + " failed");
314 }
315 }
316 };
317
318 outThread.start();
319 errorThread.start();
320 }
321 }