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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.system.execution;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
* Configurable system command executor that captures stdout and stderr.
*
* @author gjoranv
* @author bjorncs
*/
public class ProcessExecutor {
public static class Builder {
private final int timeoutSeconds;
private int[] successExitCodes;
public Builder(int timeoutSeconds) {
this.timeoutSeconds = timeoutSeconds;
}
public Builder successExitCodes(int... successExitCodes) {
this.successExitCodes = successExitCodes;
return this;
}
public ProcessExecutor build() {
return new ProcessExecutor(timeoutSeconds, successExitCodes);
}
}
private ProcessExecutor(int timeoutSeconds, int[] successExitCodes) {
this.timeoutSeconds = timeoutSeconds;
this.successExitCodes = successExitCodes;
}
public final int timeoutSeconds;
private final int[] successExitCodes;
/**
* Convenience method to execute a process with no input data. See {@link #execute(String, String)} for details.
*/
public Optional<ProcessResult> execute(String command) throws IOException {
return execute(command, null);
}
/**
* Executes the given command synchronously.
*
* @param command The command to execute.
* @param processInput Input provided to the process.
* @return The result of the execution, or empty if the process does not terminate within the timeout set for this executor.
* @throws IOException if the process execution failed.
*/
public Optional<ProcessResult> execute(String command, String processInput) throws IOException {
ByteArrayOutputStream processErr = new ByteArrayOutputStream();
ByteArrayOutputStream processOut = new ByteArrayOutputStream();
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(createStreamHandler(processOut, processErr, processInput));
ExecuteWatchdog watchDog = new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(timeoutSeconds));
executor.setWatchdog(watchDog);
executor.setExitValues(successExitCodes);
int exitCode;
try {
exitCode = executor.execute(CommandLine.parse(command));
} catch (ExecuteException e) {
exitCode = e.getExitValue();
}
return (watchDog.killedProcess()) ?
Optional.empty() : Optional.of(new ProcessResult(exitCode, processOut.toString(), processErr.toString()));
}
private static PumpStreamHandler createStreamHandler(ByteArrayOutputStream processOut,
ByteArrayOutputStream processErr,
String input) {
if (input != null) {
InputStream processInput = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
return new PumpStreamHandler(processOut, processErr, processInput);
} else {
return new PumpStreamHandler(processOut, processErr);
}
}
}
|