jdk/test/java/rmi/testlibrary/REGISTRY.java
changeset 42775 a45fa7082c81
child 42918 038d77430a46
equal deleted inserted replaced
42774:74bcf37d15d8 42775:a45fa7082c81
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     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  *
       
    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.
       
    22  */
       
    23 
       
    24 import java.io.OutputStream;
       
    25 import java.io.IOException;
       
    26 
       
    27 /**
       
    28  * Class to run and control rmiregistry in a sub-process.
       
    29  *
       
    30  * We can't kill a registry if we have too-close control
       
    31  * over it.  We must make it in a subprocess, and then kill the
       
    32  * subprocess when it has served our needs.
       
    33  */
       
    34 public class REGISTRY extends JavaVM {
       
    35 
       
    36     private static double startTimeout = 20_000 * TestLibrary.getTimeoutFactor();
       
    37 
       
    38     private int port = -1;
       
    39 
       
    40     private REGISTRY(OutputStream out, OutputStream err,
       
    41                     String options, int port) {
       
    42         super("RegistryRunner", options, Integer.toString(port), out, err);
       
    43         this.port = port;
       
    44     }
       
    45 
       
    46     public static REGISTRY createREGISTRY() {
       
    47         return createREGISTRY(System.out, System.err, "", 0);
       
    48     }
       
    49 
       
    50     public static REGISTRY createREGISTRY(OutputStream out, OutputStream err,
       
    51                                     String options, int port) {
       
    52         options += " --add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED"
       
    53                  + " --add-exports=java.rmi/sun.rmi.server=ALL-UNNAMED"
       
    54                  + " --add-exports=java.rmi/sun.rmi.transport=ALL-UNNAMED"
       
    55                  + " --add-exports=java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED";
       
    56         REGISTRY reg = new REGISTRY(out, err, options, port);
       
    57         return reg;
       
    58     }
       
    59 
       
    60     /**
       
    61      * Starts the registry in a sub-process and waits up to
       
    62      * the given timeout period to confirm that it's running,
       
    63      * and get the port where it's running.
       
    64      */
       
    65     public void start() throws IOException {
       
    66         super.start();
       
    67         long startTime = System.currentTimeMillis();
       
    68         long deadline = TestLibrary.computeDeadline(startTime, (long)startTimeout);
       
    69         while (true) {
       
    70             try {
       
    71                 Thread.sleep(1000);
       
    72             } catch (InterruptedException ignore) { }
       
    73 
       
    74             String output = outputStream.ba.toString();
       
    75             port = RegistryRunner.getRegistryPort(output);
       
    76             if (port != -1) {
       
    77                 break;
       
    78             }
       
    79             if (System.currentTimeMillis() > deadline) {
       
    80                 TestLibrary.bomb("Failed to start registry, giving up after " +
       
    81                     (System.currentTimeMillis() - startTime) + "ms.", null);
       
    82             }
       
    83         }
       
    84     }
       
    85 
       
    86     /**
       
    87      * Shuts down the registry.
       
    88      */
       
    89     public void shutdown() {
       
    90         RegistryRunner.requestExit(port);
       
    91     }
       
    92 
       
    93     /**
       
    94      * Gets the port where the registry is serving.
       
    95      */
       
    96     public int getPort() {
       
    97         return port;
       
    98     }
       
    99 }