Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/process/ProcessExecutor.java
41159 views
1
/*
2
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
package vm.share.process;
24
25
import nsk.share.TestBug;
26
import nsk.share.TestFailure;
27
import nsk.share.log.Log;
28
import vm.share.ProcessUtils;
29
30
import java.io.InputStream;
31
import java.io.InputStreamReader;
32
import java.io.BufferedReader;
33
import java.io.OutputStream;
34
import java.io.PrintStream;
35
import java.io.PipedInputStream;
36
import java.io.PipedOutputStream;
37
import java.io.IOException;
38
import java.util.*;
39
import java.lang.reflect.Field;
40
41
public class ProcessExecutor {
42
private static long CLEANUP_TIMEOUT = 60000;
43
private Process process;
44
private StreamReader stdoutReader = new StreamReader("stdout");
45
private StreamReader stderrReader = new StreamReader("stderr");
46
private Waiter waiter = new Waiter();
47
private OutputStream stdin;
48
private volatile boolean running;
49
private volatile int result = -1;
50
private List<String> args = new ArrayList<String>();
51
52
public int getResult() {
53
return result;
54
}
55
56
public void clearArgs() {
57
this.args.clear();
58
}
59
60
public void addArg(String arg) {
61
this.args.add(arg);
62
}
63
64
public void addArgs(String[] args) {
65
for (String arg : args) {
66
this.args.add(arg);
67
}
68
}
69
70
public void addArgs(Collection<String> args) {
71
this.args.addAll(args);
72
}
73
74
private void printCommandLine() {
75
for (String arg : args) {
76
System.out.println(arg);
77
}
78
}
79
80
/*
81
* Start process.
82
*/
83
public void start() {
84
if (process != null) {
85
throw new TestBug("Process is already started");
86
}
87
printCommandLine();
88
try {
89
process = createProcess();
90
stdoutReader.setDescription("stdout: " + toString());
91
stdoutReader.setStream(process.getInputStream());
92
stderrReader.setDescription("stderr: " + toString());
93
stderrReader.setStream(process.getErrorStream());
94
stdin = process.getOutputStream();
95
stdoutReader.start();
96
stderrReader.start();
97
waiter.start();
98
} catch (IOException e) {
99
throw new TestFailure("Error running process: " + toString(), e);
100
}
101
}
102
103
protected Process createProcess() throws IOException {
104
String[] commandLine = args.toArray(new String[args.size()]);
105
return Runtime.getRuntime().exec(commandLine);
106
}
107
108
/**
109
* Run and wait for completion.
110
*/
111
public int execute(long timeout) {
112
if (timeout <= 0) {
113
throw new TestBug("Positive timeout is required");
114
}
115
start();
116
return waitFor(timeout);
117
}
118
119
public int waitFor(long timeout) {
120
if (process == null) {
121
throw new TestBug("Process is not yet started");
122
}
123
if (timeout <= 0) {
124
throw new TestBug("Positive timeout is required");
125
}
126
long timeleft = timeout;
127
long startTime = 0;
128
while (timeleft > 0) {
129
try {
130
startTime = System.currentTimeMillis();
131
waiter.join(timeout);
132
return result;
133
} catch (InterruptedException e) {
134
e.printStackTrace();
135
}
136
timeleft -= (System.currentTimeMillis() - startTime);
137
}
138
return -1;
139
}
140
141
public int getPid() {
142
return ProcessUtils.getPid(process);
143
}
144
145
public OutputStream getStdIn() {
146
if (process == null) {
147
throw new TestBug(
148
"Process is not running; prepare writers after it is started");
149
}
150
return stdin;
151
}
152
153
public PrintStream getStdInPrint() {
154
return new PrintStream(getStdIn(),
155
true); // Autoflush is important here.
156
}
157
158
/*
159
public InputStream getStdOut() {
160
if (process != null)
161
throw new TestBug("Process is already running; prepare readers before it is started or some output may be missed");
162
return stdoutReader.getInputStream();
163
}
164
165
public BufferedReader getStdOutReader() {
166
return new BufferedReader(new InputStreamReader(getStdOut()));
167
}
168
169
public InputStream getStdErr() {
170
if (process != null)
171
throw new TestBug("Process is already running; prepare readers before it is started or some output may be missed");
172
return stderrReader.getInputStream();
173
}
174
175
public BufferedReader getStdErrReader() {
176
return new BufferedReader(new InputStreamReader(getStdOut()));
177
}
178
179
public String getStdoutOutput() {
180
if (stdoutReader == null)
181
throw new TestBug("Process is not running");
182
return stdoutReader.getOutput();
183
}
184
185
public String getStderrOutput() {
186
if (stderrReader == null)
187
throw new TestBug("Process is not running");
188
return stderrReader.getOutput();
189
}
190
*/
191
192
public void addStdOutListener(StreamListener l) {
193
stdoutReader.addListener(l);
194
}
195
196
public void addStdErrListener(StreamListener l) {
197
stderrReader.addListener(l);
198
}
199
200
public void logStdOut(String prefix, Log log) {
201
stdoutReader.addListener(new StreamLogger(prefix, log));
202
}
203
204
public void logStdErr(String prefix, Log log) {
205
stderrReader.addListener(new StreamLogger(prefix, log));
206
}
207
208
public void logStdOutErr(String prefix, Log log) {
209
logStdOut(prefix, log);
210
logStdErr("(stderr)" + prefix, log);
211
}
212
213
public boolean isStarted() {
214
return (process != null);
215
}
216
217
public void kill() {
218
if (process == null) {
219
throw new TestBug("Process is not running");
220
}
221
process.destroy();
222
if (stdoutReader != null) {
223
stdoutReader.kill();
224
}
225
if (stderrReader != null) {
226
stderrReader.kill();
227
}
228
if (waiter != null && waiter.isAlive()) {
229
waiter.kill();
230
}
231
process = null;
232
}
233
234
public void finish() {
235
if (stdoutReader != null) {
236
try {
237
stdoutReader.join(CLEANUP_TIMEOUT);
238
} catch (InterruptedException e) {
239
e.printStackTrace();
240
}
241
stdoutReader = null;
242
}
243
if (stderrReader != null) {
244
try {
245
stderrReader.join(CLEANUP_TIMEOUT);
246
} catch (InterruptedException e) {
247
e.printStackTrace();
248
}
249
stderrReader = null;
250
}
251
process = null;
252
}
253
254
public String toString() {
255
String ts = "";
256
if (args != null) {
257
for (String s : args) {
258
ts += s;
259
ts += " ";
260
}
261
}
262
return ts;
263
}
264
265
private class Waiter extends Thread {
266
public Waiter() {
267
super("Process Waiter: (not setup)");
268
setDaemon(true);
269
}
270
271
public void run() {
272
setName("Process Waiter: " + ProcessExecutor.this);
273
try {
274
result = process.waitFor();
275
} catch (InterruptedException e) {
276
// Ignore
277
}
278
}
279
280
public void kill() {
281
this.interrupt();
282
long timeleft = CLEANUP_TIMEOUT;
283
long startTime = 0;
284
while (timeleft > 0) {
285
try {
286
startTime = System.currentTimeMillis();
287
this.join(timeleft);
288
return;
289
} catch (InterruptedException e) {
290
e.printStackTrace();
291
}
292
timeleft -= (System.currentTimeMillis() - startTime);
293
}
294
}
295
}
296
}
297
298