-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathDefaultExecutor.java
More file actions
536 lines (476 loc) · 17.5 KB
/
DefaultExecutor.java
File metadata and controls
536 lines (476 loc) · 17.5 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.exec;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.function.Supplier;
import org.apache.commons.exec.launcher.CommandLauncher;
import org.apache.commons.exec.launcher.CommandLauncherFactory;
/**
* The default class to start a subprocess. The implementation allows to
* <ul>
* <li>set a current working directory for the subprocess</li>
* <li>provide a set of environment variables passed to the subprocess</li>
* <li>capture the subprocess output of stdout and stderr using an ExecuteStreamHandler</li>
* <li>kill long-running processes using an ExecuteWatchdog</li>
* <li>define a set of expected exit values</li>
* <li>terminate any started processes when the main process is terminating using a ProcessDestroyer</li>
* </ul>
*
* The following example shows the basic usage:
*
* <pre>
* Executor exec = DefaultExecutor.builder().get();
* CommandLine cl = new CommandLine("ls -l");
* int exitvalue = exec.execute(cl);
* </pre>
*/
public class DefaultExecutor implements Executor {
/**
* Constructs a new builder.
*
* @param <T> The builder type.
* @since 1.4.0
*/
public static class Builder<T extends Builder<T>> implements Supplier<DefaultExecutor> {
private ThreadFactory threadFactory;
private ExecuteStreamHandler executeStreamHandler;
private File workingDirectory;
@SuppressWarnings("unchecked")
T asThis() {
return (T) this;
}
/**
* Creates a new configured DefaultExecutor.
*
* @return a new configured DefaultExecutor.
*/
@Override
public DefaultExecutor get() {
return new DefaultExecutor(threadFactory, executeStreamHandler, workingDirectory);
}
ExecuteStreamHandler getExecuteStreamHandler() {
return executeStreamHandler;
}
ThreadFactory getThreadFactory() {
return threadFactory;
}
File getWorkingDirectory() {
return workingDirectory;
}
/**
* Sets the PumpStreamHandler.
*
* @param executeStreamHandler the ExecuteStreamHandler, null resets to the default.
* @return this.
*/
public T setExecuteStreamHandler(final ExecuteStreamHandler executeStreamHandler) {
this.executeStreamHandler = executeStreamHandler;
return asThis();
}
/**
* Sets the ThreadFactory.
*
* @param threadFactory the ThreadFactory, null resets to the default.
* @return this.
*/
public T setThreadFactory(final ThreadFactory threadFactory) {
this.threadFactory = threadFactory;
return asThis();
}
/**
* Sets the working directory.
*
* @param workingDirectory the working directory., null resets to the default.
* @return this.
*/
public T setWorkingDirectory(final File workingDirectory) {
this.workingDirectory = workingDirectory;
return asThis();
}
}
/**
* Creates a new builder.
*
* @return a new builder.
* @since 1.4.0
*/
public static Builder<?> builder() {
return new Builder<>();
}
/** Taking care of output and error stream. */
private ExecuteStreamHandler executeStreamHandler;
/** The working directory of the process. */
private File workingDirectory;
/** Monitoring of long running processes. */
private ExecuteWatchdog watchdog;
/** The exit values considered to be successful. */
private int[] exitValues;
/** Launches the command in a new process. */
private final CommandLauncher launcher;
/** Optional cleanup of started processes. */
private ProcessDestroyer processDestroyer;
/** Worker thread for asynchronous execution. */
private Thread executorThread;
/** The first exception being caught to be thrown to the caller. */
private IOException exceptionCaught;
/**
* The thread factory.
*/
private final ThreadFactory threadFactory;
/**
* Constructs a default {@code PumpStreamHandler} and sets the working directory of the subprocess to the current working directory.
*
* The {@code PumpStreamHandler} pumps the output of the subprocess into our {@code System.out} and {@code System.err} to avoid into our {@code System.out}
* and {@code System.err} to avoid a blocked or deadlocked subprocess (see {@link Process Process}).
*
* @deprecated Use {@link Builder#get()}.
*/
@Deprecated
public DefaultExecutor() {
this(Executors.defaultThreadFactory(), new PumpStreamHandler(), new File("."));
}
DefaultExecutor(final ThreadFactory threadFactory, final ExecuteStreamHandler executeStreamHandler, final File workingDirectory) {
this.threadFactory = threadFactory != null ? threadFactory : Executors.defaultThreadFactory();
this.executeStreamHandler = executeStreamHandler != null ? executeStreamHandler : new PumpStreamHandler();
this.workingDirectory = workingDirectory != null ? workingDirectory : new File(".");
this.launcher = CommandLauncherFactory.createVMLauncher();
this.exitValues = new int[0];
}
private void checkWorkingDirectory() throws IOException {
checkWorkingDirectory(workingDirectory);
}
private void checkWorkingDirectory(final File directory) throws IOException {
if (directory != null && !directory.exists()) {
throw new IOException(directory + " doesn't exist.");
}
}
/**
* Closes the Closeable, remembering any exception.
*
* @param closeable the {@link Closeable} to close.
*/
private void closeCatch(final Closeable closeable) {
try {
closeable.close();
} catch (final IOException e) {
setExceptionCaught(e);
}
}
/**
* Closes the streams belonging to the given Process.
*
* @param process the {@link Process}.
*/
@SuppressWarnings("resource")
private void closeProcessStreams(final Process process) {
closeCatch(process.getInputStream());
closeCatch(process.getOutputStream());
closeCatch(process.getErrorStream());
}
/**
* Creates a thread waiting for the result of an asynchronous execution.
*
* @param runnable the runnable passed to the thread.
* @param name the name of the thread.
* @return the thread
*/
protected Thread createThread(final Runnable runnable, final String name) {
return ThreadUtil.newThread(threadFactory, runnable, name, false);
}
/**
* @see org.apache.commons.exec.Executor#execute(CommandLine)
*/
@Override
public int execute(final CommandLine command) throws ExecuteException, IOException {
return execute(command, (Map<String, String>) null);
}
/**
* @see org.apache.commons.exec.Executor#execute(CommandLine, org.apache.commons.exec.ExecuteResultHandler)
*/
@Override
public void execute(final CommandLine command, final ExecuteResultHandler handler) throws ExecuteException, IOException {
execute(command, null, handler);
}
/**
* @see org.apache.commons.exec.Executor#execute(CommandLine, java.util.Map)
*/
@Override
public int execute(final CommandLine command, final Map<String, String> environment) throws ExecuteException, IOException {
checkWorkingDirectory();
final Process process = startProcess(command, environment);
return waitForProcessExit(process);
}
/**
* @see org.apache.commons.exec.Executor#execute(CommandLine, java.util.Map, org.apache.commons.exec.ExecuteResultHandler)
*/
@Override
public void execute(final CommandLine command, final Map<String, String> environment, final ExecuteResultHandler handler)
throws ExecuteException, IOException {
checkWorkingDirectory();
if (watchdog != null) {
watchdog.setProcessNotStarted();
}
// Start process from calling thread, to fail fast and to make sure process destroyer (if any) is registered
// and there is no race condition where JVM exits before process destroyer has been registered
final Process process = startProcess(command, environment);
executorThread = createThread(() -> {
int exitValue = Executor.INVALID_EXITVALUE;
try {
exitValue = waitForProcessExit(process);
handler.onProcessComplete(exitValue);
} catch (final ExecuteException e) {
handler.onProcessFailed(e);
} catch (final Exception e) {
handler.onProcessFailed(new ExecuteException("Execution failed", exitValue, e));
}
}, "CommonsExecDefaultExecutor");
getExecutorThread().start();
}
private Process startProcess(final CommandLine command, final Map<String, String> environment) throws IOException {
final Process process;
try {
process = launch(command, environment, workingDirectory);
} catch (final IOException e) {
if (watchdog != null) {
watchdog.failedToStart(e);
}
throw e;
}
// add the process to the list of those to destroy if the VM exits
if (getProcessDestroyer() != null) {
getProcessDestroyer().add(process);
}
return process;
}
/**
* Sets up the handling of the process streams and waits until the process exits. If the executing thread
* is interrupted while waiting for the child process to return, the child process will be killed.
*
* @param process the process to wait for.
* @return the exit code of the process.
* @throws IOException executing the process failed.
*/
private int waitForProcessExit(final Process process) throws IOException {
final ExecuteStreamHandler streams = executeStreamHandler;
exceptionCaught = null;
try {
try {
setStreams(streams, process);
} catch (final IOException e) {
process.destroy();
if (watchdog != null) {
watchdog.failedToStart(e);
}
throw e;
}
streams.start();
// associate the watchdog with the newly created process
if (watchdog != null) {
watchdog.start(process);
}
int exitValue = Executor.INVALID_EXITVALUE;
try {
exitValue = process.waitFor();
} catch (final InterruptedException e) {
process.destroy();
} finally {
// see http://bugs.sun.com/view_bug.do?bug_id=6420270
// see https://issues.apache.org/jira/browse/EXEC-46
// Process.waitFor should clear interrupt status when throwing InterruptedException
// but we have to do that manually
Thread.interrupted();
}
if (watchdog != null) {
watchdog.stop();
}
try {
streams.stop();
} catch (final IOException e) {
setExceptionCaught(e);
}
closeProcessStreams(process);
if (getExceptionCaught() != null) {
throw getExceptionCaught();
}
if (watchdog != null) {
try {
watchdog.checkException();
} catch (final IOException e) {
throw e;
} catch (final Exception e) {
throw new IOException(e);
}
}
if (isFailure(exitValue)) {
throw new ExecuteException("Process exited with an error: " + exitValue, exitValue);
}
return exitValue;
} finally {
// remove the process from the list of those to destroy if the VM exits
if (getProcessDestroyer() != null) {
getProcessDestroyer().remove(process);
}
}
}
/**
* Gets the first IOException being thrown.
*
* @return the first IOException being caught.
*/
private IOException getExceptionCaught() {
return exceptionCaught;
}
/**
* Gets the worker thread being used for asynchronous execution.
*
* @return the worker thread.
*/
protected Thread getExecutorThread() {
return executorThread;
}
/**
* @see org.apache.commons.exec.Executor#getProcessDestroyer()
*/
@Override
public ProcessDestroyer getProcessDestroyer() {
return processDestroyer;
}
/**
* @see org.apache.commons.exec.Executor#getStreamHandler()
*/
@Override
public ExecuteStreamHandler getStreamHandler() {
return executeStreamHandler;
}
/**
* Gets the thread factory. Z
*
* @return the thread factory.
*/
ThreadFactory getThreadFactory() {
return threadFactory;
}
/**
* @see org.apache.commons.exec.Executor#getWatchdog()
*/
@Override
public ExecuteWatchdog getWatchdog() {
return watchdog;
}
/**
* @see org.apache.commons.exec.Executor#getWorkingDirectory()
*/
@Override
public File getWorkingDirectory() {
return workingDirectory;
}
/** @see org.apache.commons.exec.Executor#isFailure(int) */
@Override
public boolean isFailure(final int exitValue) {
if (exitValues == null) {
return false;
}
if (exitValues.length == 0) {
return launcher.isFailure(exitValue);
}
for (final int exitValue2 : exitValues) {
if (exitValue2 == exitValue) {
return false;
}
}
return true;
}
/**
* Creates a process that runs a command.
*
* @param command the command to run.
* @param env the environment for the command.
* @param workingDirectory the working directory for the command.
* @return the process started.
* @throws IOException forwarded from the particular launcher used.
*/
protected Process launch(final CommandLine command, final Map<String, String> env, final File workingDirectory) throws IOException {
if (launcher == null) {
throw new IllegalStateException("CommandLauncher can not be null");
}
checkWorkingDirectory(workingDirectory);
return launcher.exec(command, env, workingDirectory);
}
/**
* Sets the first IOException thrown.
*
* @param e the IOException.
*/
private void setExceptionCaught(final IOException e) {
if (exceptionCaught == null) {
exceptionCaught = e;
}
}
/** @see org.apache.commons.exec.Executor#setExitValue(int) */
@Override
public void setExitValue(final int value) {
setExitValues(new int[] { value });
}
/** @see org.apache.commons.exec.Executor#setExitValues(int[]) */
@Override
public void setExitValues(final int[] values) {
exitValues = values == null ? null : (int[]) values.clone();
}
/**
* @see org.apache.commons.exec.Executor#setProcessDestroyer(ProcessDestroyer)
*/
@Override
public void setProcessDestroyer(final ProcessDestroyer processDestroyer) {
this.processDestroyer = processDestroyer;
}
/**
* @see org.apache.commons.exec.Executor#setStreamHandler(org.apache.commons.exec.ExecuteStreamHandler)
*/
@Override
public void setStreamHandler(final ExecuteStreamHandler streamHandler) {
this.executeStreamHandler = streamHandler;
}
@SuppressWarnings("resource")
private void setStreams(final ExecuteStreamHandler streams, final Process process) throws IOException {
streams.setProcessInputStream(process.getOutputStream());
streams.setProcessOutputStream(process.getInputStream());
streams.setProcessErrorStream(process.getErrorStream());
}
/**
* @see org.apache.commons.exec.Executor#setWatchdog(org.apache.commons.exec.ExecuteWatchdog)
*/
@Override
public void setWatchdog(final ExecuteWatchdog watchdog) {
this.watchdog = watchdog;
}
/**
* Sets the working directory.
*
* @see org.apache.commons.exec.Executor#setWorkingDirectory(java.io.File)
* @deprecated Use {@link Builder#setWorkingDirectory(File)}.
*/
@Deprecated
@Override
public void setWorkingDirectory(final File workingDirectory) {
this.workingDirectory = workingDirectory;
}
}