2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2021-05-27 15:19:45 +00:00
|
|
|
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 22:58:33 +00:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-25 09:31:49 +00:00
|
|
|
import java.io.BufferedReader;
|
2016-12-20 01:09:10 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2016-10-25 09:31:49 +00:00
|
|
|
import java.io.DataInputStream;
|
2012-12-21 04:11:45 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2016-10-25 09:31:49 +00:00
|
|
|
import java.io.InputStreamReader;
|
2012-12-21 04:11:45 +00:00
|
|
|
import java.io.OutputStream;
|
2007-12-01 00:00:00 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.StringTokenizer;
|
2014-02-15 02:23:07 +00:00
|
|
|
import java.util.concurrent.TimeoutException;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RMI regression test utility class that uses Runtime.exec to spawn a
|
|
|
|
* java process that will run a named java class.
|
|
|
|
*/
|
|
|
|
public class JavaVM {
|
|
|
|
|
2016-12-20 01:09:10 +00:00
|
|
|
static class CachedOutputStream extends OutputStream {
|
|
|
|
ByteArrayOutputStream ba;
|
|
|
|
OutputStream os;
|
|
|
|
|
|
|
|
public CachedOutputStream(OutputStream os) {
|
|
|
|
this.os = os;
|
|
|
|
this.ba = new ByteArrayOutputStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(int b) throws IOException {
|
|
|
|
ba.write(b);
|
|
|
|
os.write(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset() throws IOException {
|
|
|
|
os.flush();
|
|
|
|
ba.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 02:54:37 +00:00
|
|
|
public static final long POLLTIME_MS = 100L;
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
protected Process vm = null;
|
|
|
|
|
|
|
|
private String classname = "";
|
2016-10-25 09:31:49 +00:00
|
|
|
protected String args = "";
|
|
|
|
protected String options = "";
|
2016-12-20 01:09:10 +00:00
|
|
|
protected CachedOutputStream outputStream = new CachedOutputStream(System.out);
|
|
|
|
protected CachedOutputStream errorStream = new CachedOutputStream(System.err);
|
2007-12-01 00:00:00 +00:00
|
|
|
private String policyFileName = null;
|
2013-01-08 02:09:07 +00:00
|
|
|
private StreamPipe outPipe;
|
|
|
|
private StreamPipe errPipe;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
private static void mesg(Object mesg) {
|
|
|
|
System.err.println("JAVAVM: " + mesg.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** string name of the program execd by JavaVM */
|
|
|
|
private static String javaProgram = "java";
|
|
|
|
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
javaProgram = TestLibrary.getProperty("java.home", "") +
|
|
|
|
File.separator + "bin" + File.separator + javaProgram;
|
|
|
|
} catch (SecurityException se) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public JavaVM(String classname,
|
|
|
|
String options, String args) {
|
|
|
|
this.classname = classname;
|
|
|
|
this.options = options;
|
|
|
|
this.args = args;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JavaVM(String classname,
|
|
|
|
String options, String args,
|
|
|
|
OutputStream out, OutputStream err) {
|
|
|
|
this(classname, options, args);
|
2016-12-20 01:09:10 +00:00
|
|
|
this.outputStream = new CachedOutputStream(out);
|
|
|
|
this.errorStream = new CachedOutputStream(err);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2012-05-11 21:13:29 +00:00
|
|
|
// Prepends passed opts array to current options
|
8142968: Module System implementation
Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282
Co-authored-by: Alex Buckley <alex.buckley@oracle.com>
Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com>
Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com>
Co-authored-by: Mandy Chung <mandy.chung@oracle.com>
Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com>
Co-authored-by: Chris Hegarty <chris.hegarty@oracle.com>
Co-authored-by: Alexandr Scherbatiy <alexandr.scherbatiy@oracle.com>
Co-authored-by: Amy Lu <amy.lu@oracle.com>
Co-authored-by: Calvin Cheung <calvin.cheung@oracle.com>
Co-authored-by: Daniel Fuchs <daniel.fuchs@oracle.com>
Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com>
Co-authored-by: Harold Seigel <harold.seigel@oracle.com>
Co-authored-by: Jaroslav Bachorik <jaroslav.bachorik@oracle.com>
Co-authored-by: Jean-Francois Denise <jean-francois.denise@oracle.com>
Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com>
Co-authored-by: James Laskey <james.laskey@oracle.com>
Co-authored-by: Lois Foltan <lois.foltan@oracle.com>
Co-authored-by: Miroslav Kos <miroslav.kos@oracle.com>
Co-authored-by: Huaming Li <huaming.li@oracle.com>
Co-authored-by: Sean Mullan <sean.mullan@oracle.com>
Co-authored-by: Naoto Sato <naoto.sato@oracle.com>
Co-authored-by: Masayoshi Okutsu <masayoshi.okutsu@oracle.com>
Co-authored-by: Peter Levart <peter.levart@gmail.com>
Co-authored-by: Philip Race <philip.race@oracle.com>
Co-authored-by: Claes Redestad <claes.redestad@oracle.com>
Co-authored-by: Sergey Bylokhov <sergey.bylokhov@oracle.com>
Co-authored-by: Alexandre Iline <alexandre.iline@oracle.com>
Co-authored-by: Volker Simonis <volker.simonis@gmail.com>
Co-authored-by: Staffan Larsen <staffan.larsen@oracle.com>
Co-authored-by: Stuart Marks <stuart.marks@oracle.com>
Co-authored-by: Semyon Sadetsky <semyon.sadetsky@oracle.com>
Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com>
Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com>
Co-authored-by: Valerie Peng <valerie.peng@oracle.com>
Co-authored-by: Vincent Ryan <vincent.x.ryan@oracle.com>
Co-authored-by: Weijun Wang <weijun.wang@oracle.com>
Co-authored-by: Yuri Nesterenko <yuri.nesterenko@oracle.com>
Co-authored-by: Yekaterina Kantserova <yekaterina.kantserova@oracle.com>
Co-authored-by: Alexander Kulyakthin <alexander.kulyakhtin@oracle.com>
Co-authored-by: Felix Yang <felix.yang@oracle.com>
Co-authored-by: Andrei Eremeev <andrei.eremeev@oracle.com>
Co-authored-by: Frank Yuan <frank.yuan@oracle.com>
Co-authored-by: Sergei Pikalev <sergei.pikalev@oracle.com>
Co-authored-by: Sibabrata Sahoo <sibabrata.sahoo@oracle.com>
Co-authored-by: Tiantian Du <tiantian.du@oracle.com>
Co-authored-by: Sha Jiang <sha.jiang@oracle.com>
Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt
2016-03-17 19:04:16 +00:00
|
|
|
public void addOptions(String... opts) {
|
2007-12-01 00:00:00 +00:00
|
|
|
String newOpts = "";
|
|
|
|
for (int i = 0 ; i < opts.length ; i ++) {
|
|
|
|
newOpts += " " + opts[i];
|
|
|
|
}
|
|
|
|
newOpts += " ";
|
|
|
|
options = newOpts + options;
|
|
|
|
}
|
2012-05-11 21:13:29 +00:00
|
|
|
|
|
|
|
// Prepends passed arguments array to current args
|
8142968: Module System implementation
Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282
Co-authored-by: Alex Buckley <alex.buckley@oracle.com>
Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com>
Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com>
Co-authored-by: Mandy Chung <mandy.chung@oracle.com>
Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com>
Co-authored-by: Chris Hegarty <chris.hegarty@oracle.com>
Co-authored-by: Alexandr Scherbatiy <alexandr.scherbatiy@oracle.com>
Co-authored-by: Amy Lu <amy.lu@oracle.com>
Co-authored-by: Calvin Cheung <calvin.cheung@oracle.com>
Co-authored-by: Daniel Fuchs <daniel.fuchs@oracle.com>
Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com>
Co-authored-by: Harold Seigel <harold.seigel@oracle.com>
Co-authored-by: Jaroslav Bachorik <jaroslav.bachorik@oracle.com>
Co-authored-by: Jean-Francois Denise <jean-francois.denise@oracle.com>
Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com>
Co-authored-by: James Laskey <james.laskey@oracle.com>
Co-authored-by: Lois Foltan <lois.foltan@oracle.com>
Co-authored-by: Miroslav Kos <miroslav.kos@oracle.com>
Co-authored-by: Huaming Li <huaming.li@oracle.com>
Co-authored-by: Sean Mullan <sean.mullan@oracle.com>
Co-authored-by: Naoto Sato <naoto.sato@oracle.com>
Co-authored-by: Masayoshi Okutsu <masayoshi.okutsu@oracle.com>
Co-authored-by: Peter Levart <peter.levart@gmail.com>
Co-authored-by: Philip Race <philip.race@oracle.com>
Co-authored-by: Claes Redestad <claes.redestad@oracle.com>
Co-authored-by: Sergey Bylokhov <sergey.bylokhov@oracle.com>
Co-authored-by: Alexandre Iline <alexandre.iline@oracle.com>
Co-authored-by: Volker Simonis <volker.simonis@gmail.com>
Co-authored-by: Staffan Larsen <staffan.larsen@oracle.com>
Co-authored-by: Stuart Marks <stuart.marks@oracle.com>
Co-authored-by: Semyon Sadetsky <semyon.sadetsky@oracle.com>
Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com>
Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com>
Co-authored-by: Valerie Peng <valerie.peng@oracle.com>
Co-authored-by: Vincent Ryan <vincent.x.ryan@oracle.com>
Co-authored-by: Weijun Wang <weijun.wang@oracle.com>
Co-authored-by: Yuri Nesterenko <yuri.nesterenko@oracle.com>
Co-authored-by: Yekaterina Kantserova <yekaterina.kantserova@oracle.com>
Co-authored-by: Alexander Kulyakthin <alexander.kulyakhtin@oracle.com>
Co-authored-by: Felix Yang <felix.yang@oracle.com>
Co-authored-by: Andrei Eremeev <andrei.eremeev@oracle.com>
Co-authored-by: Frank Yuan <frank.yuan@oracle.com>
Co-authored-by: Sergei Pikalev <sergei.pikalev@oracle.com>
Co-authored-by: Sibabrata Sahoo <sibabrata.sahoo@oracle.com>
Co-authored-by: Tiantian Du <tiantian.du@oracle.com>
Co-authored-by: Sha Jiang <sha.jiang@oracle.com>
Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt
2016-03-17 19:04:16 +00:00
|
|
|
public void addArguments(String... arguments) {
|
2007-12-01 00:00:00 +00:00
|
|
|
String newArgs = "";
|
|
|
|
for (int i = 0 ; i < arguments.length ; i ++) {
|
|
|
|
newArgs += " " + arguments[i];
|
|
|
|
}
|
|
|
|
newArgs += " ";
|
|
|
|
args = newArgs + args;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPolicyFile(String policyFileName) {
|
|
|
|
this.policyFileName = policyFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is used for setting VM options on spawned VMs.
|
|
|
|
* It returns the extra command line options required
|
|
|
|
* to turn on jcov code coverage analysis.
|
|
|
|
*/
|
|
|
|
protected static String getCodeCoverageOptions() {
|
|
|
|
return TestLibrary.getExtraProperty("jcov.options","");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exec the VM as specified in this object's constructor.
|
|
|
|
*/
|
2016-10-25 09:31:49 +00:00
|
|
|
private void start0() throws IOException {
|
2016-12-20 01:09:10 +00:00
|
|
|
outputStream.reset();
|
|
|
|
errorStream.reset();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2012-12-21 04:11:45 +00:00
|
|
|
if (vm != null)
|
|
|
|
throw new IllegalStateException("JavaVM already started");
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If specified, add option for policy file
|
|
|
|
*/
|
|
|
|
if (policyFileName != null) {
|
|
|
|
String option = "-Djava.security.policy=" + policyFileName;
|
|
|
|
addOptions(new String[] { option });
|
|
|
|
}
|
|
|
|
|
2014-03-06 22:26:26 +00:00
|
|
|
addOptions(new String[] {
|
|
|
|
getCodeCoverageOptions(),
|
|
|
|
TestParams.testJavaOpts,
|
|
|
|
TestParams.testVmOpts
|
|
|
|
});
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
StringTokenizer optionsTokenizer = new StringTokenizer(options);
|
|
|
|
StringTokenizer argsTokenizer = new StringTokenizer(args);
|
|
|
|
int optionsCount = optionsTokenizer.countTokens();
|
|
|
|
int argsCount = argsTokenizer.countTokens();
|
|
|
|
|
|
|
|
String javaCommand[] = new String[optionsCount + argsCount + 2];
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
javaCommand[count++] = JavaVM.javaProgram;
|
|
|
|
while (optionsTokenizer.hasMoreTokens()) {
|
|
|
|
javaCommand[count++] = optionsTokenizer.nextToken();
|
|
|
|
}
|
|
|
|
javaCommand[count++] = classname;
|
|
|
|
while (argsTokenizer.hasMoreTokens()) {
|
|
|
|
javaCommand[count++] = argsTokenizer.nextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
mesg("command = " + Arrays.asList(javaCommand).toString());
|
|
|
|
|
|
|
|
vm = Runtime.getRuntime().exec(javaCommand);
|
2016-10-25 09:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void start() throws IOException {
|
|
|
|
start0();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2016-10-25 09:31:49 +00:00
|
|
|
/* output from the exec'ed process may optionally be captured. */
|
2013-01-08 02:09:07 +00:00
|
|
|
outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream);
|
|
|
|
errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void destroy() {
|
|
|
|
if (vm != null) {
|
2017-01-24 08:48:51 +00:00
|
|
|
vm.destroyForcibly();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
vm = null;
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:36:11 +00:00
|
|
|
/**
|
|
|
|
* Return exit value for vm process.
|
|
|
|
* @return exit value for vm process
|
|
|
|
* @throws IllegalThreadStateException if the vm process has not yet terminated
|
|
|
|
*/
|
|
|
|
public int exitValue() {
|
|
|
|
return vm.exitValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the vm process, and do necessary cleanup.
|
|
|
|
*/
|
|
|
|
public void cleanup() {
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
2016-06-01 00:54:41 +00:00
|
|
|
/**
|
|
|
|
* Destroys the VM, waits for it to terminate, and returns
|
|
|
|
* its exit status.
|
|
|
|
*
|
|
|
|
* @throws IllegalStateException if the VM has already been destroyed
|
|
|
|
* @throws InterruptedException if the caller is interrupted while waiting
|
|
|
|
*/
|
|
|
|
public int terminate() throws InterruptedException {
|
|
|
|
if (vm == null) {
|
|
|
|
throw new IllegalStateException("JavaVM already destroyed");
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.destroy();
|
|
|
|
int status = waitFor();
|
|
|
|
vm = null;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-08 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Waits for the subprocess to exit, joins the pipe threads to ensure that
|
|
|
|
* all output is collected, and returns its exit status.
|
|
|
|
*/
|
|
|
|
public int waitFor() throws InterruptedException {
|
|
|
|
if (vm == null)
|
2013-12-25 00:43:19 +00:00
|
|
|
throw new IllegalStateException("can't wait for JavaVM that isn't running");
|
2013-01-08 02:09:07 +00:00
|
|
|
|
|
|
|
int status = vm.waitFor();
|
|
|
|
outPipe.join();
|
|
|
|
errPipe.join();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-02-15 02:23:07 +00:00
|
|
|
/**
|
|
|
|
* Causes the current thread to wait the vm process to exit, if necessary,
|
|
|
|
* wait until the vm process has terminated, or the specified waiting time
|
|
|
|
* elapses. Release allocated input/output after vm process has terminated.
|
|
|
|
* @param timeout the maximum milliseconds to wait.
|
|
|
|
* @return exit value for vm process.
|
|
|
|
* @throws InterruptedException if the current thread is interrupted
|
|
|
|
* while waiting.
|
|
|
|
* @throws TimeoutException if subprocess does not end after timeout
|
|
|
|
* milliseconds passed
|
|
|
|
*/
|
|
|
|
public int waitFor(long timeout)
|
|
|
|
throws InterruptedException, TimeoutException {
|
|
|
|
if (vm == null)
|
|
|
|
throw new IllegalStateException("can't wait for JavaVM that isn't running");
|
2014-12-24 00:58:06 +00:00
|
|
|
long deadline = TestLibrary.computeDeadline(System.currentTimeMillis(), timeout);
|
2014-02-15 02:23:07 +00:00
|
|
|
|
2014-12-05 02:54:37 +00:00
|
|
|
while (true) {
|
2014-02-15 02:23:07 +00:00
|
|
|
try {
|
|
|
|
int status = vm.exitValue();
|
|
|
|
outPipe.join();
|
|
|
|
errPipe.join();
|
|
|
|
return status;
|
2014-12-05 02:54:37 +00:00
|
|
|
} catch (IllegalThreadStateException ignore) { }
|
|
|
|
|
|
|
|
if (System.currentTimeMillis() > deadline)
|
|
|
|
throw new TimeoutException();
|
|
|
|
|
|
|
|
Thread.sleep(POLLTIME_MS);
|
|
|
|
}
|
2014-02-15 02:23:07 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Starts the subprocess, waits for it to exit, and returns its exit status.
|
|
|
|
*/
|
|
|
|
public int execute() throws IOException, InterruptedException {
|
|
|
|
start();
|
|
|
|
return waitFor();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|