66package org .wildfly .test .scripts ;
77
88import java .io .IOException ;
9- import java .io .InputStream ;
10- import java .io .OutputStream ;
119import java .io .UncheckedIOException ;
1210import java .nio .charset .StandardCharsets ;
1311import java .nio .file .Files ;
2018import java .util .Iterator ;
2119import java .util .List ;
2220import java .util .Map ;
21+ import java .util .Optional ;
2322import java .util .concurrent .Callable ;
23+ import java .util .concurrent .CompletableFuture ;
2424import java .util .concurrent .ExecutionException ;
2525import java .util .concurrent .ExecutorService ;
2626import java .util .concurrent .Executors ;
2727import java .util .concurrent .Future ;
2828import java .util .concurrent .TimeUnit ;
2929import java .util .concurrent .TimeoutException ;
3030import java .util .function .Function ;
31+ import java .util .stream .Stream ;
3132
3233import org .jboss .as .controller .client .ModelControllerClient ;
3334import org .jboss .as .test .shared .TestSuiteEnvironment ;
3839 *
3940 * @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
4041 */
41- public class ScriptProcess extends Process implements AutoCloseable {
42+ public class ScriptProcess implements AutoCloseable , ProcessHandle {
4243 private static final Logger LOGGER = Logger .getLogger (ScriptProcess .class );
4344
4445 private static final Path PROC_DIR ;
@@ -59,6 +60,7 @@ public class ScriptProcess extends Process implements AutoCloseable {
5960 private final long timeout ;
6061 private final Collection <String > prefixCmds ;
6162 private Process delegate ;
63+ private ProcessHandle handleDelegate ;
6264 private Path stdoutLog ;
6365 private String lastExecutedCmd ;
6466
@@ -107,6 +109,7 @@ void start(final Function<ModelControllerClient, Boolean> check, final Map<Strin
107109 waitFor (process , check );
108110 }
109111 this .delegate = process ;
112+ this .handleDelegate = process .toHandle ();
110113 }
111114
112115 @ SuppressWarnings ("unused" )
@@ -170,64 +173,89 @@ public void close() {
170173 }
171174
172175 @ Override
173- public OutputStream getOutputStream () {
176+ public long pid () {
174177 checkStatus ();
175- return delegate . getOutputStream ();
178+ return handleDelegate . pid ();
176179 }
177180
178181 @ Override
179- public InputStream getInputStream () {
182+ public Optional < ProcessHandle > parent () {
180183 checkStatus ();
181- return delegate . getInputStream ();
184+ return handleDelegate . parent ();
182185 }
183186
184187 @ Override
185- public InputStream getErrorStream () {
188+ public Stream < ProcessHandle > children () {
186189 checkStatus ();
187- return delegate . getErrorStream ();
190+ return handleDelegate . children ();
188191 }
189192
190193 @ Override
191- public int waitFor () throws InterruptedException {
194+ public Stream < ProcessHandle > descendants () {
192195 checkStatus ();
193- return delegate . waitFor ();
196+ return handleDelegate . children ();
194197 }
195198
196199 @ Override
197- public boolean waitFor ( final long timeout , final TimeUnit unit ) throws InterruptedException {
200+ public Info info () {
198201 checkStatus ();
199- return delegate . waitFor ( timeout , unit );
202+ return handleDelegate . info ( );
200203 }
201204
202205 @ Override
203- public int exitValue () {
206+ public CompletableFuture < ProcessHandle > onExit () {
204207 checkStatus ();
205- return delegate . exitValue ();
208+ return handleDelegate . onExit ();
206209 }
207210
208211 @ Override
209- public void destroy () {
212+ public boolean supportsNormalTermination () {
210213 checkStatus ();
211- delegate .destroy ();
214+ return handleDelegate .supportsNormalTermination ();
215+ }
216+
217+ @ Override
218+ public boolean destroy () {
219+ if (handleDelegate != null ) {
220+ return handleDelegate .destroy ();
221+ }
222+ return false ;
223+ }
224+
225+ @ Override
226+ public boolean destroyForcibly () {
227+ if (handleDelegate != null ) {
228+ return handleDelegate .destroyForcibly ();
229+ }
230+ return false ;
212231 }
213232
214233 @ Override
215- public Process destroyForcibly ( ) {
234+ public int compareTo ( final ProcessHandle other ) {
216235 checkStatus ();
217- return delegate . destroyForcibly ( );
236+ return handleDelegate . compareTo ( other );
218237 }
219238
220239 @ Override
221240 public boolean isAlive () {
222- checkStatus ();
223- return delegate .isAlive ();
241+ return delegate != null && delegate .isAlive ();
224242 }
225243
226244 @ Override
227245 public String toString () {
228246 return getCommandString (Collections .emptyList ());
229247 }
230248
249+ boolean waitFor (final long timeout , final TimeUnit unit ) throws InterruptedException {
250+ checkStatus ();
251+ return delegate .waitFor (timeout , unit );
252+ }
253+
254+ int exitValue () {
255+ checkStatus ();
256+ return delegate .exitValue ();
257+ }
258+
231259 private String getCommandString (final Collection <String > arguments ) {
232260 final List <String > cmd = getCommand (arguments );
233261 final StringBuilder result = new StringBuilder ();
@@ -242,7 +270,7 @@ private String getCommandString(final Collection<String> arguments) {
242270 }
243271
244272 private void checkStatus () {
245- if (delegate == null ) {
273+ if (delegate == null || handleDelegate == null ) {
246274 throw new IllegalStateException ("The script has not yet been started." );
247275 }
248276 }
0 commit comments