author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57564 | 0a8436eda2fa |
permissions | -rw-r--r-- |
34597
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
1 |
/* |
55133
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
34597
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
4 |
* |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
8 |
* |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
13 |
* accompanied this code). |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
14 |
* |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
18 |
* |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
21 |
* questions. |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
22 |
*/ |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
23 |
|
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
24 |
package jdk.test.lib.process; |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
25 |
|
51675 | 26 |
import java.io.ByteArrayOutputStream; |
27 |
import java.io.InputStream; |
|
57564
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
28 |
import java.nio.charset.Charset; |
55133
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
29 |
import java.time.Instant; |
51675 | 30 |
import java.util.concurrent.CancellationException; |
31 |
import java.util.concurrent.ExecutionException; |
|
32 |
import java.util.concurrent.Future; |
|
34597
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
33 |
|
51675 | 34 |
public interface OutputBuffer { |
35 |
public static class OutputBufferException extends RuntimeException { |
|
36 |
private static final long serialVersionUID = 8528687792643129571L; |
|
37 |
||
38 |
public OutputBufferException(Throwable cause) { |
|
39 |
super(cause); |
|
40 |
} |
|
34597
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
41 |
} |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
42 |
|
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
43 |
/** |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
44 |
* Returns the stdout result |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
45 |
* |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
46 |
* @return stdout result |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
47 |
*/ |
51675 | 48 |
public String getStdout(); |
34597
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
49 |
/** |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
50 |
* Returns the stderr result |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
51 |
* |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
52 |
* @return stderr result |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
53 |
*/ |
51675 | 54 |
public String getStderr(); |
55 |
public int getExitValue(); |
|
56 |
||
57564
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
57 |
public static OutputBuffer of(Process p, Charset cs) { |
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
58 |
return new LazyOutputBuffer(p, cs); |
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
59 |
} |
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
60 |
|
51675 | 61 |
public static OutputBuffer of(Process p) { |
57564
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
62 |
return new LazyOutputBuffer(p, null); |
51675 | 63 |
} |
64 |
||
65 |
public static OutputBuffer of(String stdout, String stderr, int exitValue) { |
|
66 |
return new EagerOutputBuffer(stdout, stderr, exitValue); |
|
67 |
} |
|
68 |
||
69 |
public static OutputBuffer of(String stdout, String stderr) { |
|
70 |
return of(stdout, stderr, -1); |
|
71 |
} |
|
72 |
||
73 |
class LazyOutputBuffer implements OutputBuffer { |
|
74 |
private static class StreamTask { |
|
75 |
private final ByteArrayOutputStream buffer; |
|
76 |
private final Future<Void> future; |
|
57564
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
77 |
private final Charset cs; |
51675 | 78 |
|
57564
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
79 |
private StreamTask(InputStream stream, Charset cs) { |
51675 | 80 |
this.buffer = new ByteArrayOutputStream(); |
57564
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
81 |
this.cs = cs; |
51675 | 82 |
this.future = new StreamPumper(stream, buffer).process(); |
83 |
} |
|
84 |
||
85 |
public String get() { |
|
86 |
try { |
|
87 |
future.get(); |
|
57564
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
88 |
return cs == null ? buffer.toString() : buffer.toString(cs); |
51675 | 89 |
} catch (InterruptedException e) { |
90 |
Thread.currentThread().interrupt(); |
|
91 |
throw new OutputBufferException(e); |
|
92 |
} catch (ExecutionException | CancellationException e) { |
|
93 |
throw new OutputBufferException(e); |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
private final StreamTask outTask; |
|
99 |
private final StreamTask errTask; |
|
100 |
private final Process p; |
|
101 |
||
55133
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
102 |
private final void logProgress(String state) { |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
103 |
System.out.println("[" + Instant.now().toString() + "] " + state |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
104 |
+ " for process " + p.pid()); |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
105 |
System.out.flush(); |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
106 |
} |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
107 |
|
57564
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
108 |
private LazyOutputBuffer(Process p, Charset cs) { |
51675 | 109 |
this.p = p; |
55133
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
110 |
logProgress("Gathering output"); |
57564
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
111 |
outTask = new StreamTask(p.getInputStream(), cs); |
0a8436eda2fa
8228589: BasicJStackTest.java and JcmdOutputEncodingTest.java failing after JDK-8227868
rschmelter
parents:
55133
diff
changeset
|
112 |
errTask = new StreamTask(p.getErrorStream(), cs); |
51675 | 113 |
} |
114 |
||
115 |
@Override |
|
116 |
public String getStdout() { |
|
117 |
return outTask.get(); |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public String getStderr() { |
|
122 |
return errTask.get(); |
|
123 |
} |
|
124 |
||
125 |
@Override |
|
126 |
public int getExitValue() { |
|
127 |
try { |
|
55133
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
128 |
logProgress("Waiting for completion"); |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
129 |
boolean aborted = true; |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
130 |
try { |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
131 |
int result = p.waitFor(); |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
132 |
logProgress("Waiting for completion finished"); |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
133 |
aborted = false; |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
134 |
return result; |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
135 |
} finally { |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
136 |
if (aborted) { |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
137 |
logProgress("Waiting for completion FAILED"); |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
138 |
} |
3f2f89737be5
8219149: ProcessTools.ProcessBuilder should print timing info for subprocesses
kbarrett
parents:
51675
diff
changeset
|
139 |
} |
51675 | 140 |
} catch (InterruptedException e) { |
141 |
Thread.currentThread().interrupt(); |
|
142 |
throw new OutputBufferException(e); |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
class EagerOutputBuffer implements OutputBuffer { |
|
148 |
private final String stdout; |
|
149 |
private final String stderr; |
|
150 |
private final int exitValue; |
|
151 |
||
152 |
private EagerOutputBuffer(String stdout, String stderr, int exitValue) { |
|
153 |
this.stdout = stdout; |
|
154 |
this.stderr = stderr; |
|
155 |
this.exitValue = exitValue; |
|
156 |
} |
|
157 |
||
158 |
@Override |
|
159 |
public String getStdout() { |
|
160 |
return stdout; |
|
161 |
} |
|
162 |
||
163 |
@Override |
|
164 |
public String getStderr() { |
|
165 |
return stderr; |
|
166 |
} |
|
167 |
||
168 |
@Override |
|
169 |
public int getExitValue() { |
|
170 |
return exitValue; |
|
171 |
} |
|
34597
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
172 |
} |
e5f2344c6fab
8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff
changeset
|
173 |
} |