1 /*
   2  * Copyright (c) 1998, 2016, 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 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.OutputStream;
  27 import java.util.Arrays;
  28 import java.util.StringTokenizer;
  29 import java.util.concurrent.TimeoutException;
  30 
  31 /**
  32  * RMI regression test utility class that uses Runtime.exec to spawn a
  33  * java process that will run a named java class.
  34  */
  35 public class JavaVM {
  36 
  37     public static final long POLLTIME_MS = 100L;
  38 
  39     protected Process vm = null;
  40 
  41     private String classname = "";
  42     private String args = "";
  43     private String options = "";
  44     private OutputStream outputStream = System.out;
  45     private OutputStream errorStream = System.err;
  46     private String policyFileName = null;
  47     private StreamPipe outPipe;
  48     private StreamPipe errPipe;
  49 
  50     private static void mesg(Object mesg) {
  51         System.err.println("JAVAVM: " + mesg.toString());
  52     }
  53 
  54     /** string name of the program execd by JavaVM */
  55     private static String javaProgram = "java";
  56 
  57     static {
  58         try {
  59             javaProgram = TestLibrary.getProperty("java.home", "") +
  60                 File.separator + "bin" + File.separator + javaProgram;
  61         } catch (SecurityException se) {
  62         }
  63     }
  64 
  65     public JavaVM(String classname,
  66                   String options, String args) {
  67         this.classname = classname;
  68         this.options = options;
  69         this.args = args;
  70     }
  71 
  72     public JavaVM(String classname,
  73                   String options, String args,
  74                   OutputStream out, OutputStream err) {
  75         this(classname, options, args);
  76         this.outputStream = out;
  77         this.errorStream = err;
  78     }
  79 
  80     // Prepends passed opts array to current options
  81     public void addOptions(String... opts) {
  82         String newOpts = "";
  83         for (int i = 0 ; i < opts.length ; i ++) {
  84             newOpts += " " + opts[i];
  85         }
  86         newOpts += " ";
  87         options = newOpts + options;
  88     }
  89 
  90     // Prepends passed arguments array to current args
  91     public void addArguments(String... arguments) {
  92         String newArgs = "";
  93         for (int i = 0 ; i < arguments.length ; i ++) {
  94             newArgs += " " + arguments[i];
  95         }
  96         newArgs += " ";
  97         args = newArgs + args;
  98     }
  99 
 100     public void setPolicyFile(String policyFileName) {
 101         this.policyFileName = policyFileName;
 102     }
 103 
 104     /**
 105      * This method is used for setting VM options on spawned VMs.
 106      * It returns the extra command line options required
 107      * to turn on jcov code coverage analysis.
 108      */
 109     protected static String getCodeCoverageOptions() {
 110         return TestLibrary.getExtraProperty("jcov.options","");
 111     }
 112 
 113     /**
 114      * Exec the VM as specified in this object's constructor.
 115      */
 116     public void start() throws IOException {
 117 
 118         if (vm != null)
 119             throw new IllegalStateException("JavaVM already started");
 120 
 121         /*
 122          * If specified, add option for policy file
 123          */
 124         if (policyFileName != null) {
 125             String option = "-Djava.security.policy=" + policyFileName;
 126             addOptions(new String[] { option });
 127         }
 128 
 129         addOptions(new String[] {
 130             getCodeCoverageOptions(),
 131             TestParams.testJavaOpts,
 132             TestParams.testVmOpts
 133         });
 134 
 135         StringTokenizer optionsTokenizer = new StringTokenizer(options);
 136         StringTokenizer argsTokenizer = new StringTokenizer(args);
 137         int optionsCount = optionsTokenizer.countTokens();
 138         int argsCount = argsTokenizer.countTokens();
 139 
 140         String javaCommand[] = new String[optionsCount + argsCount + 2];
 141         int count = 0;
 142 
 143         javaCommand[count++] = JavaVM.javaProgram;
 144         while (optionsTokenizer.hasMoreTokens()) {
 145             javaCommand[count++] = optionsTokenizer.nextToken();
 146         }
 147         javaCommand[count++] = classname;
 148         while (argsTokenizer.hasMoreTokens()) {
 149             javaCommand[count++] = argsTokenizer.nextToken();
 150         }
 151 
 152         mesg("command = " + Arrays.asList(javaCommand).toString());
 153 
 154         vm = Runtime.getRuntime().exec(javaCommand);
 155 
 156         /* output from the execed process may optionally be captured. */
 157         outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream);
 158         errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream);
 159     }
 160 
 161     public void destroy() {
 162         if (vm != null) {
 163             vm.destroy();
 164         }
 165         vm = null;
 166     }
 167 
 168     /**
 169      * Destroys the VM, waits for it to terminate, and returns
 170      * its exit status.
 171      *
 172      * @throws IllegalStateException if the VM has already been destroyed
 173      * @throws InterruptedException if the caller is interrupted while waiting
 174      */
 175     public int terminate() throws InterruptedException {
 176         if (vm == null) {
 177             throw new IllegalStateException("JavaVM already destroyed");
 178         }
 179 
 180         vm.destroy();
 181         int status = waitFor();
 182         vm = null;
 183         return status;
 184     }
 185 
 186 
 187     /**
 188      * Waits for the subprocess to exit, joins the pipe threads to ensure that
 189      * all output is collected, and returns its exit status.
 190      */
 191     public int waitFor() throws InterruptedException {
 192         if (vm == null)
 193             throw new IllegalStateException("can't wait for JavaVM that isn't running");
 194 
 195         int status = vm.waitFor();
 196         outPipe.join();
 197         errPipe.join();
 198         return status;
 199     }
 200 
 201     /**
 202      * Causes the current thread to wait the vm process to exit, if necessary,
 203      * wait until the vm process has terminated, or the specified waiting time
 204      * elapses. Release allocated input/output after vm process has terminated.
 205      * @param timeout the maximum milliseconds to wait.
 206      * @return exit value for vm process.
 207      * @throws InterruptedException if the current thread is interrupted
 208      *         while waiting.
 209      * @throws TimeoutException if subprocess does not end after timeout
 210      *         milliseconds passed
 211      */
 212     public int waitFor(long timeout)
 213             throws InterruptedException, TimeoutException {
 214         if (vm == null)
 215             throw new IllegalStateException("can't wait for JavaVM that isn't running");
 216         long deadline = TestLibrary.computeDeadline(System.currentTimeMillis(), timeout);
 217 
 218         while (true) {
 219             try {
 220                 int status = vm.exitValue();
 221                 outPipe.join();
 222                 errPipe.join();
 223                 return status;
 224             } catch (IllegalThreadStateException ignore) { }
 225 
 226             if (System.currentTimeMillis() > deadline)
 227                 throw new TimeoutException();
 228 
 229             Thread.sleep(POLLTIME_MS);
 230         }
 231     }
 232 
 233     /**
 234      * Starts the subprocess, waits for it to exit, and returns its exit status.
 235      */
 236     public int execute() throws IOException, InterruptedException {
 237         start();
 238         return waitFor();
 239     }
 240 }