Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/rmi/testlibrary/JavaVM.java
83402 views
/*1* Copyright (c) 1998, 2013, 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*/2223import java.io.File;24import java.io.IOException;25import java.io.OutputStream;26import java.util.Arrays;27import java.util.StringTokenizer;2829/**30* RMI regression test utility class that uses Runtime.exec to spawn a31* java process that will run a named java class.32*/33public class JavaVM {3435protected Process vm = null;3637private String classname = "";38private String args = "";39private String options = "";40private OutputStream outputStream = System.out;41private OutputStream errorStream = System.err;42private String policyFileName = null;43private StreamPipe outPipe;44private StreamPipe errPipe;4546private static void mesg(Object mesg) {47System.err.println("JAVAVM: " + mesg.toString());48}4950/** string name of the program execd by JavaVM */51private static String javaProgram = "java";5253static {54try {55javaProgram = TestLibrary.getProperty("java.home", "") +56File.separator + "bin" + File.separator + javaProgram;57} catch (SecurityException se) {58}59}6061public JavaVM(String classname) {62this.classname = classname;63}64public JavaVM(String classname,65String options, String args) {66this.classname = classname;67this.options = options;68this.args = args;69}7071public JavaVM(String classname,72String options, String args,73OutputStream out, OutputStream err) {74this(classname, options, args);75this.outputStream = out;76this.errorStream = err;77}7879// Prepends passed opts array to current options80public void addOptions(String[] opts) {81String newOpts = "";82for (int i = 0 ; i < opts.length ; i ++) {83newOpts += " " + opts[i];84}85newOpts += " ";86options = newOpts + options;87}8889// Prepends passed arguments array to current args90public void addArguments(String[] arguments) {91String newArgs = "";92for (int i = 0 ; i < arguments.length ; i ++) {93newArgs += " " + arguments[i];94}95newArgs += " ";96args = newArgs + args;97}9899public void setPolicyFile(String policyFileName) {100this.policyFileName = policyFileName;101}102103/**104* This method is used for setting VM options on spawned VMs.105* It returns the extra command line options required106* to turn on jcov code coverage analysis.107*/108protected static String getCodeCoverageOptions() {109return TestLibrary.getExtraProperty("jcov.options","");110}111112public void start(Runnable runnable) throws IOException {113if (runnable == null) {114throw new NullPointerException("Runnable cannot be null.");115}116117start();118new JavaVMCallbackHandler(runnable).start();119}120121/**122* Exec the VM as specified in this object's constructor.123*/124public void start() throws IOException {125126if (vm != null)127throw new IllegalStateException("JavaVM already started");128129/*130* If specified, add option for policy file131*/132if (policyFileName != null) {133String option = "-Djava.security.policy=" + policyFileName;134addOptions(new String[] { option });135}136137addOptions(new String[] { getCodeCoverageOptions() });138139StringTokenizer optionsTokenizer = new StringTokenizer(options);140StringTokenizer argsTokenizer = new StringTokenizer(args);141int optionsCount = optionsTokenizer.countTokens();142int argsCount = argsTokenizer.countTokens();143144String javaCommand[] = new String[optionsCount + argsCount + 2];145int count = 0;146147javaCommand[count++] = JavaVM.javaProgram;148while (optionsTokenizer.hasMoreTokens()) {149javaCommand[count++] = optionsTokenizer.nextToken();150}151javaCommand[count++] = classname;152while (argsTokenizer.hasMoreTokens()) {153javaCommand[count++] = argsTokenizer.nextToken();154}155156mesg("command = " + Arrays.asList(javaCommand).toString());157158vm = Runtime.getRuntime().exec(javaCommand);159160/* output from the execed process may optionally be captured. */161outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream);162errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream);163}164165public void destroy() {166if (vm != null) {167vm.destroy();168}169vm = null;170}171172/**173* Waits for the subprocess to exit, joins the pipe threads to ensure that174* all output is collected, and returns its exit status.175*/176public int waitFor() throws InterruptedException {177if (vm == null)178throw new IllegalStateException("can't wait for JavaVM that hasn't started");179180int status = vm.waitFor();181outPipe.join();182errPipe.join();183return status;184}185186/**187* Starts the subprocess, waits for it to exit, and returns its exit status.188*/189public int execute() throws IOException, InterruptedException {190start();191return waitFor();192}193194/**195* Handles calling the callback.196*/197private class JavaVMCallbackHandler extends Thread {198Runnable runnable;199200JavaVMCallbackHandler(Runnable runnable) {201this.runnable = runnable;202}203204205/**206* Wait for the Process to terminate and notify the callback.207*/208@Override209public void run() {210if (vm != null) {211try {212vm.waitFor();213} catch(InterruptedException ie) {214// Restore the interrupted status215Thread.currentThread().interrupt();216}217}218219if (runnable != null) {220runnable.run();221}222}223}224}225226227