01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc;
05:
06: import com.tc.admin.common.InputStreamDrainer;
07:
08: public class ProcessWaiter extends Thread {
09: private Process m_process;
10: private Runnable m_waiter;
11: private InputStreamDrainer m_outReader;
12: private InputStreamDrainer m_errReader;
13:
14: public ProcessWaiter(Process process, Runnable waiter) {
15: super ();
16:
17: if (process == null)
18: throw new IllegalArgumentException("process is null");
19:
20: m_process = process;
21: m_waiter = waiter;
22: }
23:
24: public ProcessWaiter(Process process) {
25: this (process, null);
26: }
27:
28: public void start(Runnable waiter) {
29: m_waiter = waiter;
30: start();
31: }
32:
33: public void run() {
34: m_outReader = new InputStreamDrainer(m_process.getInputStream());
35: m_errReader = new InputStreamDrainer(m_process.getErrorStream());
36:
37: m_outReader.start();
38: m_errReader.start();
39:
40: while (true) {
41: try {
42: if (m_outReader.isAlive()) {
43: m_outReader.join();
44: }
45: if (m_errReader.isAlive()) {
46: m_errReader.join();
47: }
48: break;
49: } catch (InterruptedException ie) {/**/
50: }
51:
52: try {
53: sleep(1000);
54: } catch (InterruptedException ie) {/**/
55: }
56: }
57:
58: if (m_waiter != null) {
59: m_waiter.run();
60: }
61: }
62:
63: public Process getProcess() {
64: return m_process;
65:
66: }
67:
68: public String getOutputBuffer() {
69: return m_outReader.getBufferContent();
70: }
71:
72: public String getErrorBuffer() {
73: return m_errReader != null ? m_errReader.getBufferContent()
74: : null;
75: }
76: }
|