duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
smarks | f351c0a | 2013-01-07 18:09:07 -0800 | [diff] [blame] | 2 | * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
smarks | f34bec8 | 2012-12-20 20:11:45 -0800 | [diff] [blame] | 24 | import java.io.File; |
| 25 | import java.io.IOException; |
| 26 | import java.io.OutputStream; |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 27 | import java.util.Arrays; |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 28 | import java.util.StringTokenizer; |
| 29 | |
| 30 | /** |
| 31 | * RMI regression test utility class that uses Runtime.exec to spawn a |
| 32 | * java process that will run a named java class. |
| 33 | */ |
| 34 | public class JavaVM { |
| 35 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 36 | protected Process vm = null; |
| 37 | |
| 38 | private String classname = ""; |
| 39 | private String args = ""; |
| 40 | private String options = ""; |
| 41 | private OutputStream outputStream = System.out; |
| 42 | private OutputStream errorStream = System.err; |
| 43 | private String policyFileName = null; |
smarks | f351c0a | 2013-01-07 18:09:07 -0800 | [diff] [blame] | 44 | private StreamPipe outPipe; |
| 45 | private StreamPipe errPipe; |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 46 | |
| 47 | private static void mesg(Object mesg) { |
| 48 | System.err.println("JAVAVM: " + mesg.toString()); |
| 49 | } |
| 50 | |
| 51 | /** string name of the program execd by JavaVM */ |
| 52 | private static String javaProgram = "java"; |
| 53 | |
| 54 | static { |
| 55 | try { |
| 56 | javaProgram = TestLibrary.getProperty("java.home", "") + |
| 57 | File.separator + "bin" + File.separator + javaProgram; |
| 58 | } catch (SecurityException se) { |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public JavaVM(String classname) { |
| 63 | this.classname = classname; |
| 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 | |
olagneau | b58eaf4 | 2012-05-11 14:13:29 -0700 | [diff] [blame] | 80 | // Prepends passed opts array to current options |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | } |
olagneau | b58eaf4 | 2012-05-11 14:13:29 -0700 | [diff] [blame] | 89 | |
| 90 | // Prepends passed arguments array to current args |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | |
dmocek | 77716a4 | 2012-11-19 13:54:12 -0800 | [diff] [blame] | 113 | public void start(Runnable runnable) throws IOException { |
| 114 | if (runnable == null) { |
| 115 | throw new NullPointerException("Runnable cannot be null."); |
| 116 | } |
| 117 | |
| 118 | start(); |
| 119 | new JavaVMCallbackHandler(runnable).start(); |
| 120 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Exec the VM as specified in this object's constructor. |
| 124 | */ |
| 125 | public void start() throws IOException { |
| 126 | |
smarks | f34bec8 | 2012-12-20 20:11:45 -0800 | [diff] [blame] | 127 | if (vm != null) |
| 128 | throw new IllegalStateException("JavaVM already started"); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * If specified, add option for policy file |
| 132 | */ |
| 133 | if (policyFileName != null) { |
| 134 | String option = "-Djava.security.policy=" + policyFileName; |
| 135 | addOptions(new String[] { option }); |
| 136 | } |
| 137 | |
| 138 | addOptions(new String[] { getCodeCoverageOptions() }); |
| 139 | |
| 140 | StringTokenizer optionsTokenizer = new StringTokenizer(options); |
| 141 | StringTokenizer argsTokenizer = new StringTokenizer(args); |
| 142 | int optionsCount = optionsTokenizer.countTokens(); |
| 143 | int argsCount = argsTokenizer.countTokens(); |
| 144 | |
| 145 | String javaCommand[] = new String[optionsCount + argsCount + 2]; |
| 146 | int count = 0; |
| 147 | |
| 148 | javaCommand[count++] = JavaVM.javaProgram; |
| 149 | while (optionsTokenizer.hasMoreTokens()) { |
| 150 | javaCommand[count++] = optionsTokenizer.nextToken(); |
| 151 | } |
| 152 | javaCommand[count++] = classname; |
| 153 | while (argsTokenizer.hasMoreTokens()) { |
| 154 | javaCommand[count++] = argsTokenizer.nextToken(); |
| 155 | } |
| 156 | |
| 157 | mesg("command = " + Arrays.asList(javaCommand).toString()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 158 | |
| 159 | vm = Runtime.getRuntime().exec(javaCommand); |
| 160 | |
| 161 | /* output from the execed process may optionally be captured. */ |
smarks | f351c0a | 2013-01-07 18:09:07 -0800 | [diff] [blame] | 162 | outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream); |
| 163 | errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | public void destroy() { |
| 167 | if (vm != null) { |
| 168 | vm.destroy(); |
| 169 | } |
| 170 | vm = null; |
| 171 | } |
| 172 | |
smarks | f351c0a | 2013-01-07 18:09:07 -0800 | [diff] [blame] | 173 | /** |
| 174 | * Waits for the subprocess to exit, joins the pipe threads to ensure that |
| 175 | * all output is collected, and returns its exit status. |
| 176 | */ |
| 177 | public int waitFor() throws InterruptedException { |
| 178 | if (vm == null) |
| 179 | throw new IllegalStateException("can't wait for JavaVM that hasn't started"); |
| 180 | |
| 181 | int status = vm.waitFor(); |
| 182 | outPipe.join(); |
| 183 | errPipe.join(); |
| 184 | return status; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Starts the subprocess, waits for it to exit, and returns its exit status. |
| 189 | */ |
| 190 | public int execute() throws IOException, InterruptedException { |
| 191 | start(); |
| 192 | return waitFor(); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 193 | } |
dmocek | 77716a4 | 2012-11-19 13:54:12 -0800 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Handles calling the callback. |
| 197 | */ |
| 198 | private class JavaVMCallbackHandler extends Thread { |
| 199 | Runnable runnable; |
| 200 | |
| 201 | JavaVMCallbackHandler(Runnable runnable) { |
| 202 | this.runnable = runnable; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /** |
| 207 | * Wait for the Process to terminate and notify the callback. |
| 208 | */ |
| 209 | @Override |
| 210 | public void run() { |
| 211 | if (vm != null) { |
| 212 | try { |
| 213 | vm.waitFor(); |
| 214 | } catch(InterruptedException ie) { |
| 215 | // Restore the interrupted status |
| 216 | Thread.currentThread().interrupt(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (runnable != null) { |
| 221 | runnable.run(); |
| 222 | } |
| 223 | } |
| 224 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 225 | } |