Skip to main content
summaryrefslogtreecommitdiffstats
blob: 34caa9ac7820750aba15aae5fd60cd540a415c2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package org.eclipse.cdt.internal.core;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;


/**
 * Bundled state of a launched process including the threads linking the process in/output
 * to console documents.
 */
public class ProcessClosure {
		
	/**
	 * Thread which continuously reads from a input stream and pushes the read data
	 * to an output stream which is immediately flushed afterwards.
	 */
	protected static class ReaderThread extends Thread {
		
		private InputStream fInputStream;
		private OutputStream fOutputStream;
		private boolean fFinished = false;
		private String lineSeparator;		
		/*
		 * outputStream can be null
		 */
		public ReaderThread(ThreadGroup group, String name, InputStream in, OutputStream out) {
			super(group, name);
			fOutputStream= out;
			fInputStream= in;
			setDaemon(true);
			lineSeparator =	(String) System.getProperty("line.separator");
		}
		
		public void run() {
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(fInputStream));
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fOutputStream));
				String line;
				while ((line = reader.readLine()) != null) {
					line += lineSeparator;
					char[] array = line.toCharArray();
					writer.write(array, 0, array.length);
					writer.flush();
				}
			} catch (IOException x) {
				// ignore
			} finally {
				try {
					fInputStream.close();
				} catch (IOException e) {
					// ignore
				}
				try {
					fOutputStream.close();
				} catch (IOException e) {
					// ignore
				}
				complete();				
			}
		}
		
		public synchronized boolean finished() {
			return fFinished;
		}
		
		public synchronized void waitFor() {
			while (!fFinished) {
				try {
					wait();
				} catch (InterruptedException e) {
				}
			}
		}
		
		public synchronized void complete() {
			fFinished = true;
			notify();
		}
	}
		
	protected static int fCounter= 0;
	
	protected Process fProcess;

	protected OutputStream fOutput;
	protected OutputStream fError;
	
	protected ReaderThread fOutputReader;
	protected ReaderThread fErrorReader;	
	
	/**
	 * Creates a process closure and connects the launched process with
	 * a console document.
	 * @param outputStream prcess stdout is written to this stream. Can be <code>null</code>, if
	 *        not interested in reading the output
	 * @param errorStream prcess stderr is written to this stream. Can be <code>null</code>, if
	 *        not interested in reading the output	 
	 */
	public ProcessClosure(Process process, OutputStream outputStream, OutputStream errorStream) {
		fProcess= process;		
		fOutput= outputStream;
		fError= errorStream;
	}
		
	/**
	 * Live links the launched process with the configured in/out streams using
	 * reader threads.
	 */
	public void runNonBlocking() {
		ThreadGroup group= new ThreadGroup("CBuilder" + fCounter++);
		
		InputStream stdin= fProcess.getInputStream();
		InputStream stderr= fProcess.getErrorStream();
		
		fOutputReader= new ReaderThread(group, "OutputReader", stdin, fOutput);
		fErrorReader= new ReaderThread(group, "ErrorReader", stderr, fError);
				
		fOutputReader.start();
		fErrorReader.start();
	}
	
	public void runBlocking() {
		runNonBlocking();
		
		boolean finished = false;
		while (!finished) {
			try {
				fProcess.waitFor();
			} catch (InterruptedException e) {
				//System.err.println("Closure exception " +e);
			}
			try {
				fProcess.exitValue();
				finished = true;
			} catch (IllegalThreadStateException e) {
				//System.err.println("Closure exception " +e);
			}
		}
		
		// @@@FIXME: Windows 2000 is screwed; double-check using output threads
		if (!fOutputReader.finished()) {
			fOutputReader.waitFor();
		}
		
		if (!fErrorReader.finished()) {
			fErrorReader.waitFor();
		}
		
		// it seems that thread termination and stream closing is working without
		// any help
		fProcess= null;
		fOutputReader= null;
		fErrorReader= null;
	}
	
	
	public boolean isAlive() {
		if (fProcess != null) {
			if (fOutputReader.isAlive() || fErrorReader.isAlive()) {
				return true;
			} else {
				fProcess= null;
				fOutputReader= null;
				fErrorReader= null;	
			}
		}
		return false;
	}
		
	/**
	 * Forces the termination the launched process
	 */
	public void terminate() {
		if (fProcess != null) {
			fProcess.destroy();
			fProcess= null;
		}
	}
}

Back to the top