forked from alcestes/scribble-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecUtil.java
More file actions
161 lines (152 loc) · 4.42 KB
/
Copy pathExecUtil.java
File metadata and controls
161 lines (152 loc) · 4.42 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
package scribtest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.commons.io.IOUtils;
public class ExecUtil {
/**
* The summary of a process.
* @author Tiago Cogumbreiro (cogumbreiro@users.sf.net)
*
*/
public static class ProcessSummary {
/**
* The standard output.
*/
public final String stdout;
/**
* The standard error output.
*/
public final String stderr;
/**
* The exit value of the process.
*/
public final int exitValue;
public ProcessSummary(String stdout, String stderr, int exitValue) {
this.stdout = stdout;
this.stderr = stderr;
this.exitValue = exitValue;
}
}
public static String joinPath(String ...parts) {
return join(File.separator, parts);
}
public static String joinPath(Iterable<String> parts) {
return join(File.separator, parts);
}
public static String join(String separator, String ...parts) {
int count = 0;
StringBuilder builder = new StringBuilder();
for (String part : parts) {
if (count > 0) {
builder.append(separator);
}
builder.append(part);
count++;
}
return builder.toString();
}
public static String join(String separator, Iterable<String> parts) {
int count = 0;
StringBuilder builder = new StringBuilder();
for (String part : parts) {
if (count > 0) {
builder.append(separator);
}
builder.append(part);
count++;
}
return builder.toString();
}
/**
* Runs a process, up to a certain timeout,
*
* @throws IOException
* @throws TimeoutException
* @throws ExecutionException
* @throws InterruptedException
*/
public static ProcessSummary execUntil(long timeout, String... command)
throws IOException, InterruptedException, ExecutionException {
return execUntil(new TreeMap<String, String>(), timeout, command);
}
/**
* Runs a process, up to a certain timeout,
*
* @throws IOException
* @throws TimeoutException
* @throws ExecutionException
* @throws InterruptedException
*/
public static ProcessSummary execUntil(Map<String, String> env, long timeout, String... command)
throws IOException, InterruptedException, ExecutionException {
ProcessBuilder builder = new ProcessBuilder(command);
builder.environment().putAll(env);
Process process = builder.start();
ExecutorService executorService = Executors.newFixedThreadPool(3);
CyclicBarrier onStart = new CyclicBarrier(3);
Future<String> stderr = executorService.submit(toString(process.getErrorStream(),
onStart));
Future<String> stdout = executorService.submit(toString(process.getInputStream(),
onStart));
Future<Integer> waitFor = executorService.submit(waitFor(process,
onStart));
ProcessSummary result = null;
try {
waitFor.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
// timeouts are ok
} finally {
process.destroy();
waitFor.get();
result = new ProcessSummary(stdout.get(), stderr.get(), process.exitValue());
executorService.shutdown();
}
return result;
}
/**
* Waits for a process to terminate.
*
* @param process The process it should wait for.
* @param onStart Notifies others upon starting.
* @return The return code.
*/
public static Callable<Integer> waitFor(final Process process,
final CyclicBarrier onStart) {
return new Callable<Integer>() {
public Integer call() throws InterruptedException, BrokenBarrierException {
onStart.await();
return process.waitFor();
}
};
}
/**
* Converts an input stream to a string as much as possible. It stops
* at IOExceptions, but returns what it read thus far.
* @param in The input stream.
* @param onStart Notify others that it started.
*/
private static Callable<String> toString(final InputStream input, final CyclicBarrier onStart) {
return new Callable<String>() {
public String call() throws InterruptedException, BrokenBarrierException, IOException {
onStart.await();
try {
return IOUtils.toString(input);
} finally {
IOUtils.closeQuietly(input);
}
}
};
}
}