jdk/test/java/nio/channels/spi/SelectorProvider/inheritedChannel/Launcher.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * A Launcher to launch a java process with its standard input, output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * and error streams connected to a socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class Launcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        System.loadLibrary("Launcher");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static native void launch0(String cmdarray[], int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static void launch(String className, String options[], String args[], int fd) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        String[] javacmd = Util.javaCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        int options_len = (options == null) ? 0 : options.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        int args_len = (args == null) ? 0 : args.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        // java [-options] class [args...]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        int len = javacmd.length + options_len + 1 + args_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        String cmdarray[] = new String[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        for (int i=0; i<javacmd.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            cmdarray[pos++] = javacmd[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        for (int i=0; i<options_len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            cmdarray[pos++] = options[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        cmdarray[pos++] = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for (int i=0; i<args_len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            cmdarray[pos++] = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        launch0(cmdarray, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Launch 'java' with specified class with the specified arguments (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * The launched process will inherit a connected TCP socket. The remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * will be the SocketChannel returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                                      ssc.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        SocketChannel sc1 = SocketChannel.open(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        SocketChannel sc2 = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        launch(className, options, args, Util.getFD(sc2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        sc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return sc1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static SocketChannel launchWithSocketChannel(String className, String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return launchWithSocketChannel(className, null, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static SocketChannel launchWithSocketChannel(String className) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return launchWithSocketChannel(className, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Launch 'java' with specified class with the specified arguments (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The launched process will inherited a TCP listener socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Once launched this method tries to connect to service. If a connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * can be established a SocketChannel, connected to the service, is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static SocketChannel launchWithServerSocketChannel(String className, String options[], String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int port = ssc.socket().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        launch(className, options, args, Util.getFD(ssc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return SocketChannel.open(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static SocketChannel launchWithServerSocketChannel(String className, String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return launchWithServerSocketChannel(className, null, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static SocketChannel launchWithServerSocketChannel(String className) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return launchWithServerSocketChannel(className, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Launch 'java' with specified class with the specified arguments (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The launch process will inherited a bound UDP socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Once launched this method creates a DatagramChannel and "connects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * it to the service. The created DatagramChannel is then returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * As it is connected any packets sent from the socket will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * sent to the service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static DatagramChannel launchWithDatagramChannel(String className, String options[], String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        DatagramChannel dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        dc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int port = dc.socket().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        launch(className, options, args, Util.getFD(dc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
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
}