Path: blob/master/test/jdk/java/rmi/testlibrary/RegistryVM.java
41149 views
/*1* Copyright (c) 2017, 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.OutputStream;24import java.io.IOException;2526/**27* Class to run and control registry/rmiregistry in a sub-process.28* The behaviour changes when use different runner, currently29* there are 2 built-in runners, RegistryRunner and RMIRegistryRunner.30*31* We can't kill a registry if we have too-close control32* over it. We must make it in a subprocess, and then kill the33* subprocess when it has served our needs.34*/35public class RegistryVM extends JavaVM {3637private static final double START_TIMEOUT =3820_000 * TestLibrary.getTimeoutFactor();39private static final String DEFAULT_RUNNER = "RegistryRunner";4041private int port = -1;4243private RegistryVM(String runner, OutputStream out, OutputStream err,44String options, int port) {45super(runner, options, Integer.toString(port), out, err);46try {47Class runnerClass = Class.forName(runner);48if (!RegistryRunner.class.isAssignableFrom(runnerClass)) {49throw new RuntimeException("runner class must be RegistryRunner"50+ " or its sub class");51}52} catch (ClassNotFoundException ex) {53throw new RuntimeException(ex);54}55this.port = port;56}5758/**59* Create a RegistryVM instance on an ephemeral port.60*61* @return a RegistryVM instance62*/63public static RegistryVM createRegistryVM() {64return createRegistryVMWithRunner(DEFAULT_RUNNER, System.out, System.err, "", 0);65}6667/**68* Create a RegistryVM instance on an ephemeral port with additional69* command line options.70*71* @param options command line options72* @return a RegistryVM instance73*/74public static RegistryVM createRegistryVM(String options) {75return createRegistryVMWithRunner(76DEFAULT_RUNNER, System.out, System.err, options, 0);77}7879/**80* Create a RegistryVM instance on a specified port capturing stdout and81* stderr with additional command line options.82*83* @param out the OutputStream where the normal output of the84* registry subprocess goes85* @param err the OutputStream where the error output of the86* registry subprocess goes87* @param options the command line options88* @param port the port on which Registry accepts requests89* @return a RegistryVM instance90*/91public static RegistryVM createRegistryVM(OutputStream out, OutputStream err,92String options, int port) {93return createRegistryVMWithRunner(DEFAULT_RUNNER, out, err, options, port);94}9596/**97* Create a RegistryVM instance on an ephemeral port with additional98* command line options and a specified runner.99*100* @param runner the runner class name101* @param options command line options102* @return a RegistryVM instance103*/104public static RegistryVM createRegistryVMWithRunner(String runner, String options) {105return createRegistryVMWithRunner(runner, System.out, System.err, options, 0);106}107108/**109* Create a RegistryVM instance on a specified port capturing stdout and110* stderr with additional command line options and a specified runner.111*112* @param runner the runner class name113* @param out the OutputStream where the normal output of the114* registry subprocess goes115* @param err the OutputStream where the error output of the116* registry subprocess goes117* @param options the command line options118* @param port the port on which Registry accepts requests119* @return a RegistryVM instance120*/121public static RegistryVM createRegistryVMWithRunner(String runner, OutputStream out,122OutputStream err, String options, int port) {123options += " --add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED"124+ " --add-exports=java.rmi/sun.rmi.server=ALL-UNNAMED"125+ " --add-exports=java.rmi/sun.rmi.transport=ALL-UNNAMED"126+ " --add-exports=java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED";127RegistryVM reg = new RegistryVM(runner, out, err, options, port);128reg.setPolicyFile(TestParams.defaultRegistryPolicy);129return reg;130}131132/**133* Starts the registry in a sub-process and waits up to134* the given timeout period to confirm that it's running,135* and get the port where it's running.136*137* @throws IOException if fails to start subprocess138*/139public void start() throws IOException {140super.start();141long startTime = System.currentTimeMillis();142long deadline = TestLibrary.computeDeadline(startTime, (long)START_TIMEOUT);143while (true) {144try {145Thread.sleep(1000);146} catch (InterruptedException ignore) { }147148String output = outputStream.ba.toString();149port = RegistryRunner.getRegistryPort(output);150if (port != -1) {151break;152}153try {154int exit = vm.exitValue();155TestLibrary.bomb("[RegistryVM] registry sub-process exited with status "156+ exit + ".");157} catch (IllegalThreadStateException ignore) { }158159if (System.currentTimeMillis() > deadline) {160TestLibrary.bomb("Failed to start registry, giving up after " +161(System.currentTimeMillis() - startTime) + "ms.", null);162}163}164}165166/**167* Shuts down the registry.168*/169@Override170public void cleanup() {171RegistryRunner.requestExit(port);172super.destroy();173}174175/**176* Gets the port where the registry is serving.177*178* @return the port where the registry is serving179*/180public int getPort() {181return port;182}183}184185186