KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > ProcessWaiter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc;
5
6 import com.tc.admin.common.InputStreamDrainer;
7
8 public class ProcessWaiter extends Thread JavaDoc {
9   private Process JavaDoc m_process;
10   private Runnable JavaDoc m_waiter;
11   private InputStreamDrainer m_outReader;
12   private InputStreamDrainer m_errReader;
13
14   public ProcessWaiter(Process JavaDoc process, Runnable JavaDoc waiter) {
15     super();
16     
17     if(process == null)
18       throw new IllegalArgumentException JavaDoc("process is null");
19     
20     m_process = process;
21     m_waiter = waiter;
22   }
23
24   public ProcessWaiter(Process JavaDoc process) {
25     this(process, null);
26   }
27   
28   public void start(Runnable JavaDoc 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 JavaDoc ie) {/**/}
50       
51       try {sleep(1000);} catch(InterruptedException JavaDoc ie) {/**/}
52     }
53    
54     if(m_waiter != null) {
55       m_waiter.run();
56     }
57   }
58   
59   public Process JavaDoc getProcess() {
60     return m_process;
61     
62   }
63   
64   public String JavaDoc getOutputBuffer() {
65     return m_outReader.getBufferContent();
66   }
67
68   public String JavaDoc getErrorBuffer() {
69     return m_errReader != null ? m_errReader.getBufferContent() : null;
70   }
71 }
72
Popular Tags