test/java/rmi/testlibrary/JavaVM.java

Print this page
rev 15773 : 8085192: java/rmi/activation/Activatable tests fail intermittently due to "Port already in use"
Reviewed-by:

*** 19,30 **** --- 19,33 ---- * 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. */ + import java.io.BufferedReader; + import java.io.DataInputStream; import java.io.File; import java.io.IOException; + import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Arrays; import java.util.StringTokenizer; import java.util.concurrent.TimeoutException;
*** 37,48 **** public static final long POLLTIME_MS = 100L; protected Process vm = null; private String classname = ""; ! private String args = ""; ! private String options = ""; private OutputStream outputStream = System.out; private OutputStream errorStream = System.err; private String policyFileName = null; private StreamPipe outPipe; private StreamPipe errPipe; --- 40,51 ---- public static final long POLLTIME_MS = 100L; protected Process vm = null; private String classname = ""; ! protected String args = ""; ! protected String options = ""; private OutputStream outputStream = System.out; private OutputStream errorStream = System.err; private String policyFileName = null; private StreamPipe outPipe; private StreamPipe errPipe;
*** 111,121 **** } /** * Exec the VM as specified in this object's constructor. */ ! public void start() throws IOException { if (vm != null) throw new IllegalStateException("JavaVM already started"); /* --- 114,124 ---- } /** * Exec the VM as specified in this object's constructor. */ ! private void start0() throws IOException { if (vm != null) throw new IllegalStateException("JavaVM already started"); /*
*** 150,165 **** } mesg("command = " + Arrays.asList(javaCommand).toString()); vm = Runtime.getRuntime().exec(javaCommand); ! /* output from the execed process may optionally be captured. */ outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream); errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream); } public void destroy() { if (vm != null) { vm.destroy(); } vm = null; --- 153,206 ---- } mesg("command = " + Arrays.asList(javaCommand).toString()); vm = Runtime.getRuntime().exec(javaCommand); + } + + public void start() throws IOException { + start0(); ! /* output from the exec'ed process may optionally be captured. */ outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream); errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream); } + public int startAndGetPort() throws IOException { + start0(); + + int port = -1; + if (options.contains("java.nio.channels.spi.SelectorProvider=RMIDSelectorProvider")) { + // Obtain the server socket channel's ephemeral port number of the + // child rmid process. + BufferedReader reader = new BufferedReader( + new InputStreamReader(vm.getInputStream())); + String s; + while ((s = reader.readLine()) != null) { + System.out.println(s); + int i = s.indexOf(RMID.EPHEMERAL_MSG); + if (i != -1) { + String v = s.substring(RMID.EPHEMERAL_MSG.length()); + port = Integer.valueOf(v); + break; + } + } + if (port == -1) { + // something failed + reader = new BufferedReader(new InputStreamReader(vm.getErrorStream())); + while ((s = reader.readLine()) != null) + System.err.println(s); + } + } + + /* output from the exec'ed process may optionally be captured. */ + outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream); + errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream); + + return port; + } + public void destroy() { if (vm != null) { vm.destroy(); } vm = null;