test/lib/jdk/test/lib/process/ProcessTools.java
author iignatyev
Tue, 04 Sep 2018 14:35:59 -0700
changeset 51637 e9177e7749e7
parent 46221 d5f0bab7d85c
child 51675 b487c1e914d0
permissions -rw-r--r--
8210039: move OSInfo to top level testlibrary Reviewed-by: serb, chegar, alanb, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
     1
/*
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
     2
 * Copyright (c) 2013, 2018, 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
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    26
import java.io.ByteArrayOutputStream;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    27
import java.io.IOException;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    28
import java.io.InputStream;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    29
import java.io.OutputStream;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    30
import java.io.PrintStream;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    31
import java.util.ArrayList;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    32
import java.util.Arrays;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    33
import java.util.Collections;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    34
import java.util.concurrent.CountDownLatch;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    35
import java.util.Map;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    36
import java.util.concurrent.ExecutionException;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    37
import java.util.concurrent.Future;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    38
import java.util.concurrent.TimeUnit;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    39
import java.util.concurrent.TimeoutException;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    40
import java.util.function.Predicate;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    41
import java.util.function.Consumer;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    42
import java.util.stream.Collectors;
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
    43
import java.security.AccessController;
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
    44
import java.security.PrivilegedActionException;
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
    45
import java.security.PrivilegedExceptionAction;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    46
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    47
import jdk.test.lib.JDKToolFinder;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    48
import jdk.test.lib.Utils;
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
public final class ProcessTools {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    51
    private static final class LineForwarder extends StreamPumper.LinePump {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    52
        private final PrintStream ps;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    53
        private final String prefix;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    54
        LineForwarder(String prefix, PrintStream os) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    55
            this.ps = os;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    56
            this.prefix = prefix;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    57
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    58
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    59
        protected void processLine(String line) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    60
            ps.println("[" + prefix + "] " + line);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    61
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    62
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    63
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    64
    private ProcessTools() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    65
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    66
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    67
    /**
44581
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    68
     * Pumps stdout and stderr from running the process into a String.
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    69
     *
46216
46de64cb5e2d 8180183: Confusing javadoc comment to the getOutput(ProcessBuilder processBuilder) method of jdk.test.lib.process.ProcessTools
dfazunen
parents: 44697
diff changeset
    70
     * @param processBuilder ProcessBuilder to run.
44581
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    71
     * @return Output from process.
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    72
     * @throws IOException If an I/O error occurs.
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    73
     */
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    74
    public static OutputBuffer getOutput(ProcessBuilder processBuilder) throws IOException {
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
    75
        return getOutput(privilegedStart(processBuilder));
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    76
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    77
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    78
    /**
44581
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    79
     * Pumps stdout and stderr the running process into a String.
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    80
     *
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    81
     * @param process Process to pump.
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    82
     * @return Output from process.
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    83
     * @throws IOException If an I/O error occurs.
37ae1c4c89c3 8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents: 44580
diff changeset
    84
     */
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    85
    public static OutputBuffer getOutput(Process process) throws IOException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    86
        ByteArrayOutputStream stderrBuffer = new ByteArrayOutputStream();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    87
        ByteArrayOutputStream stdoutBuffer = new ByteArrayOutputStream();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    88
        StreamPumper outPumper = new StreamPumper(process.getInputStream(), stdoutBuffer);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    89
        StreamPumper errPumper = new StreamPumper(process.getErrorStream(), stderrBuffer);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    90
        Thread outPumperThread = new Thread(outPumper);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    91
        Thread errPumperThread = new Thread(errPumper);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    92
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    93
        outPumperThread.setDaemon(true);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    94
        errPumperThread.setDaemon(true);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    95
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    96
        outPumperThread.start();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    97
        errPumperThread.start();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    98
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    99
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   100
            process.waitFor();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   101
            outPumperThread.join();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   102
            errPumperThread.join();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   103
        } catch (InterruptedException e) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   104
            Thread.currentThread().interrupt();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   105
            return null;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   106
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   107
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   108
        return new OutputBuffer(stdoutBuffer.toString(), stderrBuffer.toString());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   109
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   110
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   111
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   112
     * <p>Starts a process from its builder.</p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   113
     * <span>The default redirects of STDOUT and STDERR are started</span>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   114
     * @param name The process name
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   115
     * @param processBuilder The process builder
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   116
     * @return Returns the initialized process
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   117
     * @throws IOException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   118
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   119
    public static Process startProcess(String name,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   120
                                       ProcessBuilder processBuilder)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   121
    throws IOException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   122
        return startProcess(name, processBuilder, (Consumer<String>)null);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   123
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   124
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   125
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   126
     * <p>Starts a process from its builder.</p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   127
     * <span>The default redirects of STDOUT and STDERR are started</span>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   128
     * <p>It is possible to monitor the in-streams via the provided {@code consumer}
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   129
     * @param name The process name
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   130
     * @param consumer {@linkplain Consumer} instance to process the in-streams
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   131
     * @param processBuilder The process builder
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   132
     * @return Returns the initialized process
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   133
     * @throws IOException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   134
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   135
    @SuppressWarnings("overloads")
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   136
    public static Process startProcess(String name,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   137
                                       ProcessBuilder processBuilder,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   138
                                       Consumer<String> consumer)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   139
    throws IOException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   140
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   141
            return startProcess(name, processBuilder, consumer, null, -1, TimeUnit.NANOSECONDS);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   142
        } catch (InterruptedException | TimeoutException e) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   143
            // will never happen
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   144
            throw new RuntimeException(e);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   145
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   146
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   147
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   148
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   149
     * <p>Starts a process from its builder.</p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   150
     * <span>The default redirects of STDOUT and STDERR are started</span>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   151
     * <p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   152
     * It is possible to wait for the process to get to a warmed-up state
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   153
     * via {@linkplain Predicate} condition on the STDOUT
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   154
     * </p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   155
     * @param name The process name
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   156
     * @param processBuilder The process builder
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   157
     * @param linePredicate The {@linkplain Predicate} to use on the STDOUT
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   158
     *                      Used to determine the moment the target app is
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   159
     *                      properly warmed-up.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   160
     *                      It can be null - in that case the warmup is skipped.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   161
     * @param timeout The timeout for the warmup waiting; -1 = no wait; 0 = wait forever
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   162
     * @param unit The timeout {@linkplain TimeUnit}
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   163
     * @return Returns the initialized {@linkplain Process}
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   164
     * @throws IOException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   165
     * @throws InterruptedException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   166
     * @throws TimeoutException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   167
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   168
    public static Process startProcess(String name,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   169
                                       ProcessBuilder processBuilder,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   170
                                       final Predicate<String> linePredicate,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   171
                                       long timeout,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   172
                                       TimeUnit unit)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   173
    throws IOException, InterruptedException, TimeoutException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   174
        return startProcess(name, processBuilder, null, linePredicate, timeout, unit);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   175
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   176
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   177
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   178
     * <p>Starts a process from its builder.</p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   179
     * <span>The default redirects of STDOUT and STDERR are started</span>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   180
     * <p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   181
     * It is possible to wait for the process to get to a warmed-up state
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   182
     * via {@linkplain Predicate} condition on the STDOUT and monitor the
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   183
     * in-streams via the provided {@linkplain Consumer}
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   184
     * </p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   185
     * @param name The process name
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   186
     * @param processBuilder The process builder
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   187
     * @param lineConsumer  The {@linkplain Consumer} the lines will be forwarded to
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   188
     * @param linePredicate The {@linkplain Predicate} to use on the STDOUT
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   189
     *                      Used to determine the moment the target app is
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   190
     *                      properly warmed-up.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   191
     *                      It can be null - in that case the warmup is skipped.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   192
     * @param timeout The timeout for the warmup waiting; -1 = no wait; 0 = wait forever
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   193
     * @param unit The timeout {@linkplain TimeUnit}
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   194
     * @return Returns the initialized {@linkplain Process}
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   195
     * @throws IOException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   196
     * @throws InterruptedException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   197
     * @throws TimeoutException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   198
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   199
    public static Process startProcess(String name,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   200
                                       ProcessBuilder processBuilder,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   201
                                       final Consumer<String> lineConsumer,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   202
                                       final Predicate<String> linePredicate,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   203
                                       long timeout,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   204
                                       TimeUnit unit)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   205
    throws IOException, InterruptedException, TimeoutException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   206
        System.out.println("["+name+"]:" + processBuilder.command().stream().collect(Collectors.joining(" ")));
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   207
        Process p = privilegedStart(processBuilder);
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   208
        StreamPumper stdout = new StreamPumper(p.getInputStream());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   209
        StreamPumper stderr = new StreamPumper(p.getErrorStream());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   210
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   211
        stdout.addPump(new LineForwarder(name, System.out));
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   212
        stderr.addPump(new LineForwarder(name, System.err));
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   213
        if (lineConsumer != null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   214
            StreamPumper.LinePump pump = new StreamPumper.LinePump() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   215
                @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   216
                protected void processLine(String line) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   217
                    lineConsumer.accept(line);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   218
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   219
            };
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   220
            stdout.addPump(pump);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   221
            stderr.addPump(pump);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   222
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   223
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   224
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   225
        CountDownLatch latch = new CountDownLatch(1);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   226
        if (linePredicate != null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   227
            StreamPumper.LinePump pump = new StreamPumper.LinePump() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   228
                @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   229
                protected void processLine(String line) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   230
                    if (latch.getCount() > 0 && linePredicate.test(line)) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   231
                        latch.countDown();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   232
                    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   233
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   234
            };
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   235
            stdout.addPump(pump);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   236
            stderr.addPump(pump);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   237
        } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   238
            latch.countDown();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   239
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   240
        final Future<Void> stdoutTask = stdout.process();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   241
        final Future<Void> stderrTask = stderr.process();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   242
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   243
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   244
            if (timeout > -1) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   245
                if (timeout == 0) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   246
                    latch.await();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   247
                } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   248
                    if (!latch.await(Utils.adjustTimeout(timeout), unit)) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   249
                        throw new TimeoutException();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   250
                    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   251
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   252
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   253
        } catch (TimeoutException | InterruptedException e) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   254
            System.err.println("Failed to start a process (thread dump follows)");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   255
            for(Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   256
                printStack(s.getKey(), s.getValue());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   257
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   258
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   259
            if (p.isAlive()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   260
                p.destroyForcibly();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   261
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   262
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   263
            stdoutTask.cancel(true);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   264
            stderrTask.cancel(true);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   265
            throw e;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   266
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   267
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   268
        return new ProcessImpl(p, stdoutTask, stderrTask);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   269
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   270
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   271
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   272
     * <p>Starts a process from its builder.</p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   273
     * <span>The default redirects of STDOUT and STDERR are started</span>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   274
     * <p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   275
     * It is possible to wait for the process to get to a warmed-up state
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   276
     * via {@linkplain Predicate} condition on the STDOUT. The warm-up will
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   277
     * wait indefinitely.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   278
     * </p>
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   279
     * @param name The process name
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   280
     * @param processBuilder The process builder
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   281
     * @param linePredicate The {@linkplain Predicate} to use on the STDOUT
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   282
     *                      Used to determine the moment the target app is
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   283
     *                      properly warmed-up.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   284
     *                      It can be null - in that case the warmup is skipped.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   285
     * @return Returns the initialized {@linkplain Process}
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   286
     * @throws IOException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   287
     * @throws InterruptedException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   288
     * @throws TimeoutException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   289
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   290
    @SuppressWarnings("overloads")
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   291
    public static Process startProcess(String name,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   292
                                       ProcessBuilder processBuilder,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   293
                                       final Predicate<String> linePredicate)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   294
    throws IOException, InterruptedException, TimeoutException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   295
        return startProcess(name, processBuilder, linePredicate, 0, TimeUnit.SECONDS);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   296
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   297
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   298
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   299
     * Get the process id of the current running Java process
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   300
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   301
     * @return Process id
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   302
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   303
    public static long getProcessId() throws Exception {
44624
45c9b7419c6f 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 40614
diff changeset
   304
        return ProcessHandle.current().pid();
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   305
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   306
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   307
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   308
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   309
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   310
     * Create ProcessBuilder using the java launcher from the jdk to be tested and
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   311
     * with any platform specific arguments prepended
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   312
     */
44579
0f99d2c0df52 8178340: remove unneeded "throws" from ProcessTools::createJavaProcessBuilder
iignatyev
parents: 40614
diff changeset
   313
    public static ProcessBuilder createJavaProcessBuilder(String... command) {
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   314
        return createJavaProcessBuilder(false, command);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   315
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   316
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   317
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   318
     * Create ProcessBuilder using the java launcher from the jdk to be tested,
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   319
     * and with any platform specific arguments prepended.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   320
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   321
     * @param addTestVmAndJavaOptions If true, adds test.vm.opts and test.java.opts
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   322
     *        to the java arguments.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   323
     * @param command Arguments to pass to the java command.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   324
     * @return The ProcessBuilder instance representing the java command.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   325
     */
44579
0f99d2c0df52 8178340: remove unneeded "throws" from ProcessTools::createJavaProcessBuilder
iignatyev
parents: 40614
diff changeset
   326
    public static ProcessBuilder createJavaProcessBuilder(boolean addTestVmAndJavaOptions, String... command) {
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   327
        String javapath = JDKToolFinder.getJDKTool("java");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   328
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   329
        ArrayList<String> args = new ArrayList<>();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   330
        args.add(javapath);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   331
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 34597
diff changeset
   332
        args.add("-cp");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 34597
diff changeset
   333
        args.add(System.getProperty("java.class.path"));
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 34597
diff changeset
   334
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   335
        if (addTestVmAndJavaOptions) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   336
            Collections.addAll(args, Utils.getTestJavaOpts());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   337
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   338
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   339
        Collections.addAll(args, command);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   340
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   341
        // Reporting
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   342
        StringBuilder cmdLine = new StringBuilder();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   343
        for (String cmd : args)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   344
            cmdLine.append(cmd).append(' ');
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   345
        System.out.println("Command line: [" + cmdLine.toString() + "]");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   346
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   347
        return new ProcessBuilder(args.toArray(new String[args.size()]));
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   348
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   349
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   350
    private static void printStack(Thread t, StackTraceElement[] stack) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   351
        System.out.println("\t" +  t +
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   352
                           " stack: (length = " + stack.length + ")");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   353
        if (t != null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   354
            for (StackTraceElement stack1 : stack) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   355
                System.out.println("\t" + stack1);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   356
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   357
            System.out.println();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   358
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   359
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   360
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   361
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   362
     * Executes a test jvm process, waits for it to finish and returns the process output.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   363
     * The default jvm options from jtreg, test.vm.opts and test.java.opts, are added.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   364
     * The java from the test.jdk is used to execute the command.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   365
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   366
     * The command line will be like:
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   367
     * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   368
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   369
     * The jvm process will have exited before this method returns.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   370
     *
45052
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   371
     * @param cmds User specified arguments.
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   372
     * @return The output from the process.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   373
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   374
    public static OutputAnalyzer executeTestJvm(String... cmds) throws Exception {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   375
        ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   376
        return executeProcess(pb);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   377
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   378
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   379
    /**
45052
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   380
     * @see #executeTestJvm(String...)
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   381
     * @param cmds User specified arguments.
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   382
     * @return The output from the process.
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   383
     */
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   384
    public static OutputAnalyzer executeTestJava(String... cmds) throws Exception {
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   385
        return executeTestJvm(cmds);
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   386
    }
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   387
d7deaa5e4f97 8180195: remove jaxp testlibrary
iignatyev
parents: 44697
diff changeset
   388
    /**
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   389
     * Executes a process, waits for it to finish and returns the process output.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   390
     * The process will have exited before this method returns.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   391
     * @param pb The ProcessBuilder to execute.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   392
     * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   393
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   394
    public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   395
        OutputAnalyzer output = null;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   396
        Process p = null;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   397
        boolean failed = false;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   398
        try {
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   399
            p = privilegedStart(pb);
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   400
            output = new OutputAnalyzer(p);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   401
            p.waitFor();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   402
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   403
            return output;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   404
        } catch (Throwable t) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   405
            if (p != null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   406
                p.destroyForcibly().waitFor();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   407
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   408
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   409
            failed = true;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   410
            System.out.println("executeProcess() failed: " + t);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   411
            throw t;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   412
        } finally {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   413
            if (failed) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   414
                System.err.println(getProcessLog(pb, output));
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   415
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   416
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   417
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   418
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   419
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   420
     * Executes a process, waits for it to finish and returns the process output.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   421
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   422
     * The process will have exited before this method returns.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   423
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   424
     * @param cmds The command line to execute.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   425
     * @return The output from the process.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   426
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   427
    public static OutputAnalyzer executeProcess(String... cmds) throws Throwable {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   428
        return executeProcess(new ProcessBuilder(cmds));
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   429
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   430
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   431
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   432
     * Used to log command line, stdout, stderr and exit code from an executed process.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   433
     * @param pb The executed process.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   434
     * @param output The output from the process.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   435
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   436
    public static String getProcessLog(ProcessBuilder pb, OutputAnalyzer output) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   437
        String stderr = output == null ? "null" : output.getStderr();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   438
        String stdout = output == null ? "null" : output.getStdout();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   439
        String exitValue = output == null ? "null": Integer.toString(output.getExitValue());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   440
        StringBuilder logMsg = new StringBuilder();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   441
        final String nl = System.getProperty("line.separator");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   442
        logMsg.append("--- ProcessLog ---" + nl);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   443
        logMsg.append("cmd: " + getCommandLine(pb) + nl);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   444
        logMsg.append("exitvalue: " + exitValue + nl);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   445
        logMsg.append("stderr: " + stderr + nl);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   446
        logMsg.append("stdout: " + stdout + nl);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   447
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   448
        return logMsg.toString();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   449
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   450
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   451
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   452
     * @return The full command line for the ProcessBuilder.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   453
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   454
    public static String getCommandLine(ProcessBuilder pb) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   455
        if (pb == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   456
            return "null";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   457
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   458
        StringBuilder cmd = new StringBuilder();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   459
        for (String s : pb.command()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   460
            cmd.append(s).append(" ");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   461
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   462
        return cmd.toString().trim();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   463
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   464
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   465
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   466
     * Executes a process, waits for it to finish, prints the process output
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   467
     * to stdout, and returns the process output.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   468
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   469
     * The process will have exited before this method returns.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   470
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   471
     * @param cmds The command line to execute.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   472
     * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   473
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   474
    public static OutputAnalyzer executeCommand(String... cmds)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   475
            throws Throwable {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   476
        String cmdLine = Arrays.stream(cmds).collect(Collectors.joining(" "));
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   477
        System.out.println("Command line: [" + cmdLine + "]");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   478
        OutputAnalyzer analyzer = ProcessTools.executeProcess(cmds);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   479
        System.out.println(analyzer.getOutput());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   480
        return analyzer;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   481
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   482
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   483
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   484
     * Executes a process, waits for it to finish, prints the process output
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   485
     * to stdout and returns the process output.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   486
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   487
     * The process will have exited before this method returns.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   488
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   489
     * @param pb The ProcessBuilder to execute.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   490
     * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   491
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   492
    public static OutputAnalyzer executeCommand(ProcessBuilder pb)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   493
            throws Throwable {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   494
        String cmdLine = pb.command().stream().collect(Collectors.joining(" "));
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   495
        System.out.println("Command line: [" + cmdLine + "]");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   496
        OutputAnalyzer analyzer = ProcessTools.executeProcess(pb);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   497
        System.out.println(analyzer.getOutput());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   498
        return analyzer;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   499
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   500
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   501
    private static Process privilegedStart(ProcessBuilder pb) throws IOException {
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   502
        try {
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   503
            return AccessController.doPrivileged(
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   504
                (PrivilegedExceptionAction<Process>) () -> pb.start());
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   505
        } catch (PrivilegedActionException e) {
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   506
            @SuppressWarnings("unchecked")
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   507
            IOException t = (IOException) e.getException();
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   508
            throw t;
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   509
        }
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   510
    }
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 46221
diff changeset
   511
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   512
    private static class ProcessImpl extends Process {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   513
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   514
        private final Process p;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   515
        private final Future<Void> stdoutTask;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   516
        private final Future<Void> stderrTask;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   517
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   518
        public ProcessImpl(Process p, Future<Void> stdoutTask, Future<Void> stderrTask) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   519
            this.p = p;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   520
            this.stdoutTask = stdoutTask;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   521
            this.stderrTask = stderrTask;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   522
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   523
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   524
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   525
        public OutputStream getOutputStream() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   526
            return p.getOutputStream();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   527
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   528
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   529
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   530
        public InputStream getInputStream() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   531
            return p.getInputStream();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   532
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   533
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   534
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   535
        public InputStream getErrorStream() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   536
            return p.getErrorStream();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   537
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   538
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   539
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   540
        public int waitFor() throws InterruptedException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   541
            int rslt = p.waitFor();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   542
            waitForStreams();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   543
            return rslt;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   544
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   545
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   546
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   547
        public int exitValue() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   548
            return p.exitValue();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   549
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   550
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   551
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   552
        public void destroy() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   553
            p.destroy();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   554
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   555
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   556
        @Override
44624
45c9b7419c6f 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 40614
diff changeset
   557
        public long pid() {
45c9b7419c6f 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 40614
diff changeset
   558
            return p.pid();
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   559
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   560
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   561
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   562
        public boolean isAlive() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   563
            return p.isAlive();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   564
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   565
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   566
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   567
        public Process destroyForcibly() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   568
            return p.destroyForcibly();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   569
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   570
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   571
        @Override
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   572
        public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   573
            boolean rslt = p.waitFor(timeout, unit);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   574
            if (rslt) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   575
                waitForStreams();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   576
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   577
            return rslt;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   578
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   579
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   580
        private void waitForStreams() throws InterruptedException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   581
            try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   582
                stdoutTask.get();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   583
            } catch (ExecutionException e) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   584
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   585
            try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   586
                stderrTask.get();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   587
            } catch (ExecutionException e) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   588
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   589
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   590
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   591
}