1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.exec;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.PipedOutputStream;
24 import java.time.Duration;
25 import java.time.Instant;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.ThreadFactory;
28
29 import org.apache.commons.exec.util.DebugUtils;
30
31
32
33
34
35 public class PumpStreamHandler implements ExecuteStreamHandler {
36
37 private static final Duration STOP_TIMEOUT_ADDITION = Duration.ofSeconds(2);
38
39 private Thread outputThread;
40
41 private Thread errorThread;
42
43 private Thread inputThread;
44
45 private final OutputStream outputStream;
46
47 private final OutputStream errorOutputStream;
48
49 private final InputStream inputStream;
50
51 private InputStreamPumper inputStreamPumper;
52
53
54 private Duration stopTimeout = Duration.ZERO;
55
56
57 private IOException caught;
58
59
60
61
62 private final ThreadFactory threadFactory;
63
64
65
66
67 public PumpStreamHandler() {
68 this(System.out, System.err);
69 }
70
71
72
73
74
75
76
77
78 public PumpStreamHandler(final OutputStream allOutputStream) {
79 this(allOutputStream, allOutputStream);
80 }
81
82
83
84
85
86
87
88
89
90
91 public PumpStreamHandler(final OutputStream outputStream, final OutputStream errorOutputStream) {
92 this(outputStream, errorOutputStream, null);
93 }
94
95
96
97
98
99
100
101
102
103
104
105 public PumpStreamHandler(final OutputStream outputStream, final OutputStream errorOutputStream, final InputStream inputStream) {
106 this(Executors.defaultThreadFactory(), outputStream, errorOutputStream, inputStream);
107 }
108
109
110
111
112
113
114
115
116
117
118
119 private PumpStreamHandler(final ThreadFactory threadFactory, final OutputStream outputStream, final OutputStream errorOutputStream,
120 final InputStream inputStream) {
121 this.threadFactory = threadFactory;
122 this.outputStream = outputStream;
123 this.errorOutputStream = errorOutputStream;
124 this.inputStream = inputStream;
125 }
126
127
128
129
130
131
132
133 protected void createProcessErrorPump(final InputStream is, final OutputStream os) {
134 errorThread = createPump(is, os);
135 }
136
137
138
139
140
141
142
143 protected void createProcessOutputPump(final InputStream is, final OutputStream os) {
144 outputThread = createPump(is, os);
145 }
146
147
148
149
150
151
152
153
154
155 protected Thread createPump(final InputStream is, final OutputStream os) {
156 return createPump(is, os, os instanceof PipedOutputStream);
157 }
158
159
160
161
162
163
164
165
166
167 protected Thread createPump(final InputStream is, final OutputStream os, final boolean closeWhenExhausted) {
168 return ThreadUtil.newThread(threadFactory, new StreamPumper(is, os, closeWhenExhausted), "CommonsExecStreamPumper-", true);
169 }
170
171
172
173
174
175
176
177
178 private Thread createSystemInPump(final InputStream is, final OutputStream os) {
179 inputStreamPumper = new InputStreamPumper(is, os);
180 return ThreadUtil.newThread(threadFactory, inputStreamPumper, "CommonsExecStreamPumper-", true);
181 }
182
183
184
185
186
187
188 protected OutputStream getErr() {
189 return errorOutputStream;
190 }
191
192
193
194
195
196
197 protected OutputStream getOut() {
198 return outputStream;
199 }
200
201 Duration getStopTimeout() {
202 return stopTimeout;
203 }
204
205
206
207
208
209
210 @Override
211 public void setProcessErrorStream(final InputStream is) {
212 if (errorOutputStream != null) {
213 createProcessErrorPump(is, errorOutputStream);
214 }
215 }
216
217
218
219
220
221
222 @Override
223 public void setProcessInputStream(final OutputStream os) {
224 if (inputStream != null) {
225 if (inputStream == System.in) {
226 inputThread = createSystemInPump(inputStream, os);
227 } else {
228 inputThread = createPump(inputStream, os, true);
229 }
230 } else {
231 try {
232 os.close();
233 } catch (final IOException e) {
234 final String msg = "Got exception while closing output stream";
235 DebugUtils.handleException(msg, e);
236 }
237 }
238 }
239
240
241
242
243
244
245 @Override
246 public void setProcessOutputStream(final InputStream is) {
247 if (outputStream != null) {
248 createProcessOutputPump(is, outputStream);
249 }
250 }
251
252
253
254
255
256
257
258 public void setStopTimeout(final Duration timeout) {
259 this.stopTimeout = timeout != null ? timeout : Duration.ZERO;
260 }
261
262
263
264
265
266
267
268 @Deprecated
269 public void setStopTimeout(final long timeout) {
270 this.stopTimeout = Duration.ofMillis(timeout);
271 }
272
273
274
275
276 @Override
277 public void start() {
278 start(outputThread);
279 start(errorThread);
280 start(inputThread);
281 }
282
283
284
285
286 private void start(final Thread thread) {
287 if (thread != null) {
288 thread.start();
289 }
290 }
291
292
293
294
295 @Override
296 public void stop() throws IOException {
297 if (inputStreamPumper != null) {
298 inputStreamPumper.stopProcessing();
299 }
300 stop(outputThread, stopTimeout);
301 stop(errorThread, stopTimeout);
302 stop(inputThread, stopTimeout);
303
304 if (errorOutputStream != null && errorOutputStream != outputStream) {
305 try {
306 errorOutputStream.flush();
307 } catch (final IOException e) {
308 final String msg = "Got exception while flushing the error stream : " + e.getMessage();
309 DebugUtils.handleException(msg, e);
310 }
311 }
312
313 if (outputStream != null) {
314 try {
315 outputStream.flush();
316 } catch (final IOException e) {
317 final String msg = "Got exception while flushing the output stream";
318 DebugUtils.handleException(msg, e);
319 }
320 }
321
322 if (caught != null) {
323 throw caught;
324 }
325 }
326
327
328
329
330
331
332
333
334 private void stop(final Thread thread, final Duration timeout) {
335 if (thread != null) {
336 try {
337 if (timeout.equals(Duration.ZERO)) {
338 thread.join();
339 } else {
340 final Duration timeToWait = timeout.plus(STOP_TIMEOUT_ADDITION);
341 final Instant startTime = Instant.now();
342 thread.join(timeToWait.toMillis());
343 if (Instant.now().isAfter(startTime.plus(timeToWait))) {
344 caught = new ExecuteException("The stop timeout of " + timeout + " ms was exceeded", Executor.INVALID_EXITVALUE);
345 }
346 }
347 } catch (final InterruptedException e) {
348 thread.interrupt();
349 }
350 }
351 }
352
353
354
355
356
357
358
359
360 protected void stopThread(final Thread thread, final long timeoutMillis) {
361 stop(thread, Duration.ofMillis(timeoutMillis));
362 }
363 }