01: /*
02: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.monitoring.utilities;
06:
07: import java.io.IOException;
08:
09: public class ProcessHelper {
10: public static int exec(String[] cmdArray, String[] envp)
11: throws IOException {
12: Process process = Runtime.getRuntime().exec(cmdArray, envp);
13: return getExitValue(process);
14: }
15:
16: public static int exec(String command) throws IOException {
17: Process process = Runtime.getRuntime().exec(command);
18: return getExitValue(process);
19: }
20:
21: /*
22: private static void waitForThread(Thread thread) {
23: while (true) {
24: try {
25: thread.join();
26: } catch (InterruptedException ie) {
27: continue;
28: }
29: }
30: }
31: */
32:
33: private static int getExitValue(Process process) {
34: int result = 0;
35:
36: ProcessStreamReader input = new ProcessStreamReader(process
37: .getInputStream(), Boolean.FALSE);
38: ProcessStreamReader error = new ProcessStreamReader(process
39: .getErrorStream(), Boolean.TRUE);
40: input.start();
41: error.start();
42:
43: while (true) {
44: try {
45: // System.out.println("Waiting for child process to finish...");
46: result = process.waitFor();
47: // System.out.println("Child process finished.");
48:
49: input.interrupt(); // Stop child output stream reader thread
50: error.interrupt(); // Stop child error stream reader thread
51: break;
52: } catch (InterruptedException ie) {
53: continue;
54: }
55: }
56:
57: /* There is no way to determine if input/error streams are done with!
58: System.out.println(ProcessHelper.class.getName() + ": Waiting for input stream reader to finish...");
59: waitForThread(input);
60: System.out.println(ProcessHelper.class.getName() + ": Input stream reader finished.");
61: System.out.println(ProcessHelper.class.getName() + ": Waiting for error stream reader to finish...");
62: waitForThread(error);
63: System.out.println(ProcessHelper.class.getName() + ": Error stream reader finished.");
64: */
65:
66: return result;
67: }
68: }
|