test/hotspot/jtreg/vmTestbase/nsk/share/LocalProcess.java
changeset 49934 44839fbb20db
equal deleted inserted replaced
49933:c63bdf53a1a7 49934:44839fbb20db
       
     1 /*
       
     2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package nsk.share;
       
    25 
       
    26 import nsk.share.*;
       
    27 
       
    28 import java.io.*;
       
    29 
       
    30 /**
       
    31  * Wrapper for local process.
       
    32  * <p>
       
    33  * This class provides abilities to launch such process,
       
    34  * redirect standard output streams, wait for process terminates
       
    35  * or kill the process, and so on.
       
    36  * <p>
       
    37  * This object is finalized with <code>nsk.share.Finalizer</code>.
       
    38  *
       
    39  * @see nsk.share.FinalizableObject
       
    40  * @see nsk.share.Finalizer
       
    41  */
       
    42 
       
    43 public class LocalProcess extends FinalizableObject {
       
    44 
       
    45     public final static int PROCESS_IS_ALIVE = 222;
       
    46 
       
    47     private Process process;
       
    48 
       
    49     protected Process getProcess() {
       
    50         return process;
       
    51     }
       
    52 
       
    53     public void launch (String[] args) throws IOException {
       
    54         System.out.println("Launching process by array of args: ");
       
    55         for (int mm=0; mm < args.length; mm++) {
       
    56             System.out.println("    args[" + Integer.toString(mm) + "]: >" +
       
    57                                args[mm] + "<");
       
    58 
       
    59         }
       
    60 
       
    61         process = Runtime.getRuntime().exec(args);
       
    62 
       
    63         Finalizer finalizer = new Finalizer(this);
       
    64         finalizer.activate();
       
    65     }
       
    66 
       
    67     public void launch (String cmdLine) throws IOException {
       
    68         System.out.println("Launching process by command line: " + cmdLine);
       
    69 
       
    70         process = Runtime.getRuntime().exec(cmdLine);
       
    71 
       
    72         Finalizer finalizer = new Finalizer(this);
       
    73         finalizer.activate();
       
    74     }
       
    75 
       
    76     /** Return exit status. */
       
    77     public int getStatus () {
       
    78         return process.exitValue();
       
    79     }
       
    80 
       
    81     /** Check whether the process has been terminated. */
       
    82     public boolean terminated() {
       
    83         try {
       
    84             int value = process.exitValue();
       
    85             return true;
       
    86         } catch (IllegalThreadStateException e) {
       
    87             return false;
       
    88         }
       
    89     }
       
    90 
       
    91     /** Wait until the process shutdown or crash. */
       
    92     public int waitFor () throws InterruptedException {
       
    93         return process.waitFor();
       
    94     }
       
    95 
       
    96     /**
       
    97      * Wait until the process shutdown or crash for given timeout in milliseconds.
       
    98      * Returns <code>LocalProcess.PROCESS_IS_ALIVE</code> if process is not terminated
       
    99      * after timeout.
       
   100      */
       
   101 
       
   102     public int waitFor (long timeMillisec) throws InterruptedException {
       
   103         final Object waitObject = new Object();
       
   104 
       
   105         class Watcher extends Thread {
       
   106             int exitCode = LocalProcess.PROCESS_IS_ALIVE;
       
   107             Process process;
       
   108 
       
   109             Watcher (Process process) {
       
   110                this.process = process;
       
   111             }
       
   112 
       
   113             public void run () {
       
   114                 try {
       
   115                     synchronized (this) {
       
   116                        exitCode = process.waitFor();
       
   117                     }
       
   118                 } catch (InterruptedException ie) {
       
   119                 }
       
   120                 synchronized (waitObject) {
       
   121                     waitObject.notifyAll();
       
   122                 }
       
   123             }
       
   124 
       
   125             synchronized public int getExitCode() {
       
   126                 return exitCode;
       
   127             }
       
   128         }
       
   129 
       
   130         Watcher watcher;
       
   131         // yield control to watcher for timeMillisec time.
       
   132         synchronized (waitObject) {
       
   133             watcher = new Watcher(process);
       
   134             watcher.start();
       
   135 
       
   136             waitObject.wait(timeMillisec);
       
   137         }
       
   138 
       
   139         if (watcher.isAlive()) {
       
   140             watcher.interrupt();
       
   141         }
       
   142 
       
   143         return watcher.getExitCode();
       
   144     }
       
   145 
       
   146     // --------------------------------------------------- //
       
   147 
       
   148     /** Get a pipe to write to the process' stdin stream. */
       
   149     public OutputStream getStdin () {
       
   150         return process.getOutputStream();
       
   151     }
       
   152 
       
   153     /** Get a pipe to read the process' stdout stream. */
       
   154     public InputStream getStdout () {
       
   155         return process.getInputStream();
       
   156     }
       
   157 
       
   158     /** Get a pipe to read the process stderr stream. */
       
   159     public InputStream getStderr () {
       
   160         return process.getErrorStream();
       
   161     }
       
   162 
       
   163     /** Kill the process. */
       
   164     protected void kill() {
       
   165         process.destroy();
       
   166     }
       
   167 
       
   168     /**
       
   169      * Finalize mirror by invoking <code>close()</code>.
       
   170      *
       
   171      * @throws Throwable if any throwable exception is thrown during finalization
       
   172      */
       
   173     protected void finalize() throws Throwable {
       
   174         kill();
       
   175         super.finalize();
       
   176     }
       
   177 }