duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
alanb | 0d05823 | 2012-11-02 15:50:11 +0000 | [diff] [blame] | 2 | * Copyright (c) 1998, 2012, 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; |
| 44 | |
| 45 | private static void mesg(Object mesg) { |
| 46 | System.err.println("JAVAVM: " + mesg.toString()); |
| 47 | } |
| 48 | |
| 49 | /** string name of the program execd by JavaVM */ |
| 50 | private static String javaProgram = "java"; |
| 51 | |
| 52 | static { |
| 53 | try { |
| 54 | javaProgram = TestLibrary.getProperty("java.home", "") + |
| 55 | File.separator + "bin" + File.separator + javaProgram; |
| 56 | } catch (SecurityException se) { |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public JavaVM(String classname) { |
| 61 | this.classname = classname; |
| 62 | } |
| 63 | public JavaVM(String classname, |
| 64 | String options, String args) { |
| 65 | this.classname = classname; |
| 66 | this.options = options; |
| 67 | this.args = args; |
| 68 | } |
| 69 | |
| 70 | public JavaVM(String classname, |
| 71 | String options, String args, |
| 72 | OutputStream out, OutputStream err) { |
| 73 | this(classname, options, args); |
| 74 | this.outputStream = out; |
| 75 | this.errorStream = err; |
| 76 | } |
| 77 | |
olagneau | b58eaf4 | 2012-05-11 14:13:29 -0700 | [diff] [blame] | 78 | // Prepends passed opts array to current options |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 79 | public void addOptions(String[] opts) { |
| 80 | String newOpts = ""; |
| 81 | for (int i = 0 ; i < opts.length ; i ++) { |
| 82 | newOpts += " " + opts[i]; |
| 83 | } |
| 84 | newOpts += " "; |
| 85 | options = newOpts + options; |
| 86 | } |
olagneau | b58eaf4 | 2012-05-11 14:13:29 -0700 | [diff] [blame] | 87 | |
| 88 | // Prepends passed arguments array to current args |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 89 | public void addArguments(String[] arguments) { |
| 90 | String newArgs = ""; |
| 91 | for (int i = 0 ; i < arguments.length ; i ++) { |
| 92 | newArgs += " " + arguments[i]; |
| 93 | } |
| 94 | newArgs += " "; |
| 95 | args = newArgs + args; |
| 96 | } |
| 97 | |
| 98 | public void setPolicyFile(String policyFileName) { |
| 99 | this.policyFileName = policyFileName; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * This method is used for setting VM options on spawned VMs. |
| 104 | * It returns the extra command line options required |
| 105 | * to turn on jcov code coverage analysis. |
| 106 | */ |
| 107 | protected static String getCodeCoverageOptions() { |
| 108 | return TestLibrary.getExtraProperty("jcov.options",""); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Exec the VM as specified in this object's constructor. |
| 114 | */ |
| 115 | public void start() throws IOException { |
| 116 | |
smarks | f34bec8 | 2012-12-20 20:11:45 -0800 | [diff] [blame^] | 117 | if (vm != null) |
| 118 | throw new IllegalStateException("JavaVM already started"); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * If specified, add option for policy file |
| 122 | */ |
| 123 | if (policyFileName != null) { |
| 124 | String option = "-Djava.security.policy=" + policyFileName; |
| 125 | addOptions(new String[] { option }); |
| 126 | } |
| 127 | |
| 128 | addOptions(new String[] { getCodeCoverageOptions() }); |
| 129 | |
| 130 | StringTokenizer optionsTokenizer = new StringTokenizer(options); |
| 131 | StringTokenizer argsTokenizer = new StringTokenizer(args); |
| 132 | int optionsCount = optionsTokenizer.countTokens(); |
| 133 | int argsCount = argsTokenizer.countTokens(); |
| 134 | |
| 135 | String javaCommand[] = new String[optionsCount + argsCount + 2]; |
| 136 | int count = 0; |
| 137 | |
| 138 | javaCommand[count++] = JavaVM.javaProgram; |
| 139 | while (optionsTokenizer.hasMoreTokens()) { |
| 140 | javaCommand[count++] = optionsTokenizer.nextToken(); |
| 141 | } |
| 142 | javaCommand[count++] = classname; |
| 143 | while (argsTokenizer.hasMoreTokens()) { |
| 144 | javaCommand[count++] = argsTokenizer.nextToken(); |
| 145 | } |
| 146 | |
| 147 | mesg("command = " + Arrays.asList(javaCommand).toString()); |
| 148 | System.err.println(""); |
| 149 | |
| 150 | vm = Runtime.getRuntime().exec(javaCommand); |
| 151 | |
| 152 | /* output from the execed process may optionally be captured. */ |
smarks | f34bec8 | 2012-12-20 20:11:45 -0800 | [diff] [blame^] | 153 | StreamPipe.plugTogether(vm.getInputStream(), this.outputStream); |
| 154 | StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public void destroy() { |
| 158 | if (vm != null) { |
| 159 | vm.destroy(); |
| 160 | } |
| 161 | vm = null; |
| 162 | } |
| 163 | |
| 164 | protected Process getVM() { |
| 165 | return vm; |
| 166 | } |
| 167 | } |