Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/LocalProcess.java
40948 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.share;2425import nsk.share.*;2627import java.io.*;2829/**30* Wrapper for local process.31* <p>32* This class provides abilities to launch such process,33* redirect standard output streams, wait for process terminates34* or kill the process, and so on.35* <p>36* This object is finalized with <code>nsk.share.Finalizer</code>.37*38* @see nsk.share.FinalizableObject39* @see nsk.share.Finalizer40*/4142public class LocalProcess extends FinalizableObject {4344public final static int PROCESS_IS_ALIVE = 222;4546private Process process;4748protected Process getProcess() {49return process;50}5152public void launch (String[] args) throws IOException {53System.out.println("Launching process by array of args: ");54for (int mm=0; mm < args.length; mm++) {55System.out.println(" args[" + Integer.toString(mm) + "]: >" +56args[mm] + "<");5758}5960process = Runtime.getRuntime().exec(args);6162Finalizer finalizer = new Finalizer(this);63finalizer.activate();64}6566public void launch (String cmdLine) throws IOException {67System.out.println("Launching process by command line: " + cmdLine);6869process = Runtime.getRuntime().exec(cmdLine);7071Finalizer finalizer = new Finalizer(this);72finalizer.activate();73}7475/** Return exit status. */76public int getStatus () {77return process.exitValue();78}7980/** Check whether the process has been terminated. */81public boolean terminated() {82try {83int value = process.exitValue();84return true;85} catch (IllegalThreadStateException e) {86return false;87}88}8990/** Wait until the process shutdown or crash. */91public int waitFor () throws InterruptedException {92return process.waitFor();93}9495/**96* Wait until the process shutdown or crash for given timeout in milliseconds.97* Returns <code>LocalProcess.PROCESS_IS_ALIVE</code> if process is not terminated98* after timeout.99*/100101public int waitFor (long timeMillisec) throws InterruptedException {102final Object waitObject = new Object();103104class Watcher extends Thread {105int exitCode = LocalProcess.PROCESS_IS_ALIVE;106Process process;107108Watcher (Process process) {109this.process = process;110}111112public void run () {113try {114synchronized (this) {115exitCode = process.waitFor();116}117} catch (InterruptedException ie) {118}119synchronized (waitObject) {120waitObject.notifyAll();121}122}123124synchronized public int getExitCode() {125return exitCode;126}127}128129Watcher watcher;130// yield control to watcher for timeMillisec time.131synchronized (waitObject) {132watcher = new Watcher(process);133watcher.start();134135waitObject.wait(timeMillisec);136}137138if (watcher.isAlive()) {139watcher.interrupt();140}141142return watcher.getExitCode();143}144145// --------------------------------------------------- //146147/** Get a pipe to write to the process' stdin stream. */148public OutputStream getStdin () {149return process.getOutputStream();150}151152/** Get a pipe to read the process' stdout stream. */153public InputStream getStdout () {154return process.getInputStream();155}156157/** Get a pipe to read the process stderr stream. */158public InputStream getStderr () {159return process.getErrorStream();160}161162/** Kill the process. */163protected void kill() {164process.destroy();165}166167/**168* Finalize mirror by invoking <code>close()</code>.169*170* @throws Throwable if any throwable exception is thrown during finalization171*/172protected void finalize() throws Throwable {173kill();174super.finalize();175}176}177178179