jdk/test/java/nio/channels/spi/SelectorProvider/inheritedChannel/Launcher.java
author alanb
Sun, 02 Mar 2014 15:56:07 +0000
changeset 23041 1aa7ce9cd0f1
parent 23010 6dadb192ad81
child 45578 cd8f28842351
permissions -rw-r--r--
8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol) Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 16480
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * A Launcher to launch a java process with its standard input, output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * and error streams connected to a socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class Launcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        System.loadLibrary("Launcher");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static native void launch0(String cmdarray[], int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static void launch(String className, String options[], String args[], int fd) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        // java [-options] class [args...]
23041
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    42
        int optsLen = (options == null) ? 0 : options.length;
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    43
        int argsLen = (args == null) ? 0 : args.length;
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    44
        int len = 1 + optsLen + 1 + argsLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        String cmdarray[] = new String[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        int pos = 0;
23041
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    47
        cmdarray[pos++] = Util.javaCommand();
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    48
        if (options != null) {
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    49
            for (String opt: options) {
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    50
                cmdarray[pos++] = opt;
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    51
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        cmdarray[pos++] = className;
23041
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    54
        if (args != null) {
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    55
            for (String arg: args) {
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    56
                cmdarray[pos++] = arg;
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    57
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        launch0(cmdarray, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Launch 'java' with specified class with the specified arguments (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The launched process will inherit a connected TCP socket. The remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * will be the SocketChannel returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                                      ssc.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        SocketChannel sc1 = SocketChannel.open(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        SocketChannel sc2 = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        launch(className, options, args, Util.getFD(sc2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        sc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return sc1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static SocketChannel launchWithSocketChannel(String className, String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return launchWithSocketChannel(className, null, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static SocketChannel launchWithSocketChannel(String className) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return launchWithSocketChannel(className, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Launch 'java' with specified class with the specified arguments (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The launched process will inherited a TCP listener socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Once launched this method tries to connect to service. If a connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * can be established a SocketChannel, connected to the service, is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static SocketChannel launchWithServerSocketChannel(String className, String options[], String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int port = ssc.socket().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        launch(className, options, args, Util.getFD(ssc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return SocketChannel.open(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static SocketChannel launchWithServerSocketChannel(String className, String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return launchWithServerSocketChannel(className, null, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static SocketChannel launchWithServerSocketChannel(String className) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return launchWithServerSocketChannel(className, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Launch 'java' with specified class with the specified arguments (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * The launch process will inherited a bound UDP socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Once launched this method creates a DatagramChannel and "connects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * it to the service. The created DatagramChannel is then returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * As it is connected any packets sent from the socket will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * sent to the service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static DatagramChannel launchWithDatagramChannel(String className, String options[], String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        DatagramChannel dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        dc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int port = dc.socket().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        launch(className, options, args, Util.getFD(dc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        dc = DatagramChannel.open();
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   133
        InetAddress address = InetAddress.getLocalHost();
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   134
        if (address.isLoopbackAddress()) {
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   135
            address = InetAddress.getLoopbackAddress();
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   136
        }
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   137
        InetSocketAddress isa = new InetSocketAddress(address, port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        dc.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static DatagramChannel launchWithDatagramChannel(String className, String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return launchWithDatagramChannel(className, null, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static DatagramChannel launchWithDatagramChannel(String className) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return launchWithDatagramChannel(className, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}