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 |
| package edu.rice.cs.util; |
38 |
| |
39 |
| import java.io.*; |
40 |
| |
41 |
| import edu.rice.cs.drjava.ui.DrJavaErrorHandler; |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| public class ProcessSequence extends Process { |
51 |
| |
52 |
| protected ProcessCreator[] _creators; |
53 |
| |
54 |
| |
55 |
| protected Process[] _processes; |
56 |
| |
57 |
| |
58 |
| protected volatile int _index = 0; |
59 |
| |
60 |
| |
61 |
| protected volatile boolean _aborted = false; |
62 |
| |
63 |
| |
64 |
| |
65 |
| protected StreamRedirectThread _stdOutRedirector; |
66 |
| |
67 |
| |
68 |
| |
69 |
| protected StreamRedirectThread _stdErrRedirector; |
70 |
| |
71 |
| |
72 |
| protected PipedInputStream _combinedInputStream; |
73 |
| |
74 |
| |
75 |
| protected PipedOutputStream _combinedStdOutStream; |
76 |
| |
77 |
| |
78 |
| protected JoinInputStream _combinedInputJoinedWithDebugStream; |
79 |
| |
80 |
| |
81 |
| protected PrintWriter _debugOutput; |
82 |
| |
83 |
| |
84 |
| protected PipedInputStream _debugInputStream; |
85 |
| protected PipedOutputStream _debugOutputStream; |
86 |
| |
87 |
| |
88 |
| protected PipedInputStream _combinedErrorStream; |
89 |
| |
90 |
| |
91 |
| protected PipedOutputStream _combinedStdErrStream; |
92 |
| |
93 |
| |
94 |
| protected volatile OutputStream _combinedOutputStream; |
95 |
| |
96 |
| |
97 |
| |
98 |
| protected Thread _deathThread; |
99 |
| |
100 |
| |
101 |
| |
102 |
0
| public ProcessSequence(ProcessCreator[] pcs) {
|
103 |
0
| _creators = pcs;
|
104 |
0
| _processes = new Process[_creators.length];
|
105 |
0
| for(int i = 0; i < _processes.length; ++i) { _processes[i] = null; }
|
106 |
0
| _combinedInputStream = new PipedInputStream();
|
107 |
0
| try {
|
108 |
0
| _combinedStdOutStream = new PipedOutputStream(_combinedInputStream);
|
109 |
0
| _combinedInputStream.connect(_combinedStdOutStream);
|
110 |
| } |
111 |
| catch(IOException e) { } |
112 |
| |
113 |
0
| _debugInputStream = new PipedInputStream();
|
114 |
0
| try {
|
115 |
0
| _debugOutputStream = new PipedOutputStream(_debugInputStream);
|
116 |
0
| _debugInputStream.connect(_debugOutputStream);
|
117 |
| } |
118 |
| catch(IOException e) { } |
119 |
0
| _combinedInputJoinedWithDebugStream = new JoinInputStream(_combinedInputStream, _debugInputStream);
|
120 |
0
| _debugOutput = new PrintWriter(new OutputStreamWriter(_debugOutputStream));
|
121 |
| |
122 |
0
| _combinedErrorStream = new PipedInputStream();
|
123 |
0
| try {
|
124 |
0
| _combinedStdErrStream = new PipedOutputStream(_combinedErrorStream);
|
125 |
0
| _combinedErrorStream.connect(_combinedStdErrStream);
|
126 |
| } |
127 |
| catch(IOException e) { } |
128 |
| |
129 |
0
| _deathThread = new Thread(new Runnable() {
|
130 |
0
| public void run() {
|
131 |
0
| GeneralProcessCreator.LOG.log("ProcessSequence._deathThread running");
|
132 |
0
| boolean interrupted = false;
|
133 |
| |
134 |
0
| while(_index < _processes.length) {
|
135 |
0
| GeneralProcessCreator.LOG.log("Waiting for process " + _index);
|
136 |
0
| do {
|
137 |
0
| interrupted = false;
|
138 |
0
| try {
|
139 |
0
| _processes[_index].waitFor();
|
140 |
| } |
141 |
0
| catch(InterruptedException e) { interrupted = true; }
|
142 |
0
| } while(interrupted);
|
143 |
0
| GeneralProcessCreator.LOG.log("Process " + _index + " terminated");
|
144 |
| |
145 |
0
| if (_index < _processes.length-1) {
|
146 |
| |
147 |
0
| ++_index;
|
148 |
0
| try {
|
149 |
0
| _processes[_index] = _creators[_index].start();
|
150 |
0
| GeneralProcessCreator.LOG.log("Process " + _index + " started");
|
151 |
0
| connectProcess(_processes[_index]);
|
152 |
| } |
153 |
| catch(IOException e) { |
154 |
0
| GeneralProcessCreator.LOG.log("\nIOException in external process: " + e.getMessage() + "\nCheck your command line.\n");
|
155 |
| |
156 |
0
| _debugOutput.println("\nIOException in external process: " + e.getMessage() + "\nCheck your command line.\n");
|
157 |
0
| _debugOutput.flush();
|
158 |
0
| _processes[_index] = DUMMY_PROCESS;
|
159 |
| } |
160 |
| } |
161 |
| else { |
162 |
0
| ++_index;
|
163 |
0
| GeneralProcessCreator.LOG.log("Closing StdOut and StdErr streams.");
|
164 |
0
| try {
|
165 |
0
| stopAllRedirectors();
|
166 |
0
| _combinedStdOutStream.flush();
|
167 |
0
| _combinedStdOutStream.close();
|
168 |
0
| _combinedStdErrStream.flush();
|
169 |
0
| _combinedStdErrStream.close();
|
170 |
| } |
171 |
| catch(IOException e) { } |
172 |
| } |
173 |
| } |
174 |
| } |
175 |
| },"Process Sequence Death Thread"); |
176 |
0
| _index = 0;
|
177 |
0
| try {
|
178 |
0
| _processes[_index] = _creators[_index].start();
|
179 |
| } |
180 |
| catch(IOException e) { |
181 |
0
| GeneralProcessCreator.LOG.log("\nIOException in external process: " + e.getMessage() + "\nCheck your command line.\n");
|
182 |
| |
183 |
0
| _processes[_index] = DUMMY_PROCESS;
|
184 |
0
| _debugOutput.println("\nIOException in external process: " + e.getMessage() + "\nCheck your command line.\n");
|
185 |
0
| _debugOutput.flush();
|
186 |
| } |
187 |
0
| connectProcess(_processes[_index]);
|
188 |
0
| _deathThread.start();
|
189 |
| |
190 |
| |
191 |
| } |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
0
| public OutputStream getOutputStream() {
|
200 |
0
| return new BufferedOutputStream(new OutputStream() {
|
201 |
0
| public void write(int b) throws IOException {
|
202 |
0
| _combinedOutputStream.write(b);
|
203 |
| } |
204 |
0
| public void flush() throws IOException {
|
205 |
0
| _combinedOutputStream.flush();
|
206 |
| } |
207 |
0
| public void close() throws IOException {
|
208 |
0
| _combinedOutputStream.close();
|
209 |
| } |
210 |
| }); |
211 |
| } |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
0
| public InputStream getErrorStream() {
|
220 |
0
| return _combinedErrorStream;
|
221 |
| } |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
0
| public InputStream getInputStream() {
|
230 |
0
| return _combinedInputJoinedWithDebugStream;
|
231 |
| } |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
0
| public int waitFor() throws InterruptedException {
|
249 |
0
| if (_aborted) { return -1; }
|
250 |
0
| int exitCode = 0;
|
251 |
0
| for(int i = 0; i < _processes.length; ++i) {
|
252 |
0
| while((!_aborted) && (_processes[i] == null)) {
|
253 |
0
| try {
|
254 |
| |
255 |
0
| Thread.sleep(100);
|
256 |
| } |
257 |
| catch(InterruptedException e) { } |
258 |
| } |
259 |
0
| if (_aborted) { return -1; }
|
260 |
0
| exitCode = _processes[i].waitFor();
|
261 |
| } |
262 |
0
| stopAllRedirectors();
|
263 |
0
| return exitCode;
|
264 |
| } |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
0
| public int exitValue() {
|
276 |
0
| if (_aborted) { return -1; }
|
277 |
0
| if ((_index < _processes.length-1) || (_processes[_processes.length-1] == null)) {
|
278 |
0
| throw new IllegalThreadStateException("Process sequence has not terminated yet, exit value not available.");
|
279 |
| } |
280 |
| |
281 |
| |
282 |
0
| return _processes[_processes.length-1].exitValue();
|
283 |
| } |
284 |
| |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
0
| public void destroy() {
|
290 |
0
| _aborted = true;
|
291 |
0
| for(int i = 0; i < _processes.length; ++i) {
|
292 |
0
| if (_processes[i] != null) { _processes[i].destroy(); }
|
293 |
| } |
294 |
0
| stopAllRedirectors();
|
295 |
| } |
296 |
| |
297 |
| |
298 |
0
| protected void stopAllRedirectors() {
|
299 |
0
| _stdOutRedirector.setStopFlag();
|
300 |
0
| _stdErrRedirector.setStopFlag();
|
301 |
| } |
302 |
| |
303 |
| |
304 |
0
| protected void connectProcess(Process p) {
|
305 |
| |
306 |
| |
307 |
| |
308 |
0
| if (_stdOutRedirector == null) {
|
309 |
0
| _stdOutRedirector = new StreamRedirectThread("stdout Redirector " + _index,
|
310 |
| p.getInputStream(), |
311 |
| _combinedStdOutStream, |
312 |
| false, |
313 |
| new ProcessSequenceThreadGroup(this), |
314 |
| true); |
315 |
0
| _stdOutRedirector.start();
|
316 |
| } |
317 |
| else { |
318 |
0
| _stdOutRedirector.setInputStream(p.getInputStream());
|
319 |
| } |
320 |
0
| if (_stdErrRedirector == null) {
|
321 |
0
| _stdErrRedirector = new StreamRedirectThread("stderr Redirector " + _index,
|
322 |
| p.getErrorStream(), |
323 |
| _combinedStdErrStream, |
324 |
| false, |
325 |
| new ProcessSequenceThreadGroup(this), |
326 |
| true); |
327 |
0
| _stdErrRedirector.start();
|
328 |
| } |
329 |
| else { |
330 |
0
| _stdErrRedirector.setInputStream(p.getErrorStream());
|
331 |
| } |
332 |
0
| _combinedOutputStream = p.getOutputStream();
|
333 |
| } |
334 |
| |
335 |
| |
336 |
| protected static class ProcessSequenceThreadGroup extends ThreadGroup { |
337 |
| private PrintWriter _debugOut; |
338 |
0
| public ProcessSequenceThreadGroup(ProcessSequence seq) {
|
339 |
0
| super("Process Sequence Thread Group");
|
340 |
0
| _debugOut = seq._debugOutput;
|
341 |
| } |
342 |
0
| public void uncaughtException(Thread t, Throwable e) {
|
343 |
0
| if ((e instanceof StreamRedirectException) &&
|
344 |
| (e.getCause() instanceof IOException)) { |
345 |
0
| _debugOut.println("\n\n\nAn exception occurred during the execution of the command line:\n" +
|
346 |
| e.toString() + "\n\n"); |
347 |
| } |
348 |
| else { |
349 |
0
| DrJavaErrorHandler.record(e);
|
350 |
| } |
351 |
| } |
352 |
| } |
353 |
| |
354 |
| |
355 |
| protected static final Process DUMMY_PROCESS = new Process() { |
356 |
0
| public void destroy() { }
|
357 |
0
| public int exitValue() { return -1; }
|
358 |
0
| public InputStream getErrorStream() { return new InputStream() {
|
359 |
0
| public int read() { return -1; }
|
360 |
| }; } |
361 |
0
| public InputStream getInputStream() { return new InputStream() {
|
362 |
0
| public int read() { return -1; }
|
363 |
| }; } |
364 |
0
| public OutputStream getOutputStream() { return new OutputStream() {
|
365 |
0
| public void write(int b) { }
|
366 |
| }; } |
367 |
0
| public int waitFor() { return -1; }
|
368 |
| }; |
369 |
| } |