jdk/test/java/rmi/testlibrary/REGISTRY.java
author mli
Tue, 20 Dec 2016 17:34:11 -0800
changeset 42918 038d77430a46
parent 42775 a45fa7082c81
permissions -rw-r--r--
8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42775
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
     1
/*
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
     4
 *
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
     8
 *
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    13
 * accompanied this code).
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    14
 *
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    18
 *
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    21
 * questions.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    22
 */
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    23
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    24
import java.io.OutputStream;
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    25
import java.io.IOException;
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    26
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    27
/**
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    28
 * Class to run and control rmiregistry in a sub-process.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    29
 *
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    30
 * We can't kill a registry if we have too-close control
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    31
 * over it.  We must make it in a subprocess, and then kill the
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    32
 * subprocess when it has served our needs.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    33
 */
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    34
public class REGISTRY extends JavaVM {
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    35
42918
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    36
    private static final double START_TIMEOUT =
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    37
            20_000 * TestLibrary.getTimeoutFactor();
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    38
    private static final String DEFAULT_RUNNER = "RegistryRunner";
42775
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    39
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    40
    private int port = -1;
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    41
42918
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    42
    private REGISTRY(String runner, OutputStream out, OutputStream err,
42775
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    43
                    String options, int port) {
42918
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    44
        super(runner, options, Integer.toString(port), out, err);
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    45
        try {
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    46
            Class runnerClass = Class.forName(runner);
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    47
            if (!RegistryRunner.class.isAssignableFrom(runnerClass)) {
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    48
                throw new RuntimeException("runner class must be RegistryRunner"
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    49
                        + " or its sub class");
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    50
            }
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    51
        } catch (ClassNotFoundException ex) {
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    52
            throw new RuntimeException(ex);
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    53
        }
42775
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    54
        this.port = port;
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    55
    }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    56
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    57
    public static REGISTRY createREGISTRY() {
42918
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    58
        return createREGISTRYWithRunner(DEFAULT_RUNNER, System.out, System.err, "", 0);
42775
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    59
    }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    60
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    61
    public static REGISTRY createREGISTRY(OutputStream out, OutputStream err,
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    62
                                    String options, int port) {
42918
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    63
        return createREGISTRYWithRunner(DEFAULT_RUNNER, out, err, options, port);
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    64
    }
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    65
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    66
    public static REGISTRY createREGISTRYWithRunner(String runner, String options) {
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    67
        return createREGISTRYWithRunner(runner, System.out, System.err, options, 0);
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    68
    }
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    69
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    70
    public static REGISTRY createREGISTRYWithRunner(String runner, OutputStream out,
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    71
                                        OutputStream err, String options, int port) {
42775
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    72
        options += " --add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED"
42918
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    73
                + " --add-exports=java.rmi/sun.rmi.server=ALL-UNNAMED"
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    74
                + " --add-exports=java.rmi/sun.rmi.transport=ALL-UNNAMED"
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    75
                + " --add-exports=java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED";
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    76
       REGISTRY reg = new REGISTRY(runner, out, err, options, port);
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    77
       return reg;
42775
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    78
    }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    79
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    80
    /**
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    81
     * Starts the registry in a sub-process and waits up to
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    82
     * the given timeout period to confirm that it's running,
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    83
     * and get the port where it's running.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    84
     */
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    85
    public void start() throws IOException {
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    86
        super.start();
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    87
        long startTime = System.currentTimeMillis();
42918
038d77430a46 8029360: java/rmi/transport/dgcDeadLock/DGCDeadLock.java failing intermittently
mli
parents: 42775
diff changeset
    88
        long deadline = TestLibrary.computeDeadline(startTime, (long)START_TIMEOUT);
42775
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    89
        while (true) {
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    90
            try {
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    91
                Thread.sleep(1000);
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    92
            } catch (InterruptedException ignore) { }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    93
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    94
            String output = outputStream.ba.toString();
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    95
            port = RegistryRunner.getRegistryPort(output);
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    96
            if (port != -1) {
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    97
                break;
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    98
            }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
    99
            if (System.currentTimeMillis() > deadline) {
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   100
                TestLibrary.bomb("Failed to start registry, giving up after " +
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   101
                    (System.currentTimeMillis() - startTime) + "ms.", null);
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   102
            }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   103
        }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   104
    }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   105
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   106
    /**
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   107
     * Shuts down the registry.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   108
     */
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   109
    public void shutdown() {
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   110
        RegistryRunner.requestExit(port);
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   111
    }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   112
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   113
    /**
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   114
     * Gets the port where the registry is serving.
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   115
     */
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   116
    public int getPort() {
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   117
        return port;
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   118
    }
a45fa7082c81 8025199: java/rmi/registry/reexport/Reexport.java failed with: Port already in use
mli
parents:
diff changeset
   119
}