1 /*
   2  * Copyright (c) 2003, 2017, 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 /*
  25  * A Launcher to launch a java process with its standard input, output,
  26  * and error streams connected to a socket.
  27  */
  28 import java.io.IOException;
  29 import java.net.InetAddress;
  30 import java.net.InetSocketAddress;
  31 import java.nio.channels.DatagramChannel;
  32 import java.nio.channels.ServerSocketChannel;
  33 import java.nio.channels.SocketChannel;
  34 
  35 public class Launcher {
  36 
  37     static {
  38         System.loadLibrary("Launcher");
  39     }
  40 
  41     private static native void launch0(String cmdarray[], int fd) throws IOException;
  42 
  43     private static void launch(String className, String options[], String args[], int fd) throws IOException {
  44         // java [-options] class [args...]
  45         int optsLen = (options == null) ? 0 : options.length;
  46         int argsLen = (args == null) ? 0 : args.length;
  47         int len = 1 + optsLen + 1 + argsLen;
  48         String cmdarray[] = new String[len];
  49         int pos = 0;
  50         cmdarray[pos++] = Util.javaCommand();
  51         if (options != null) {
  52             for (String opt: options) {
  53                 cmdarray[pos++] = opt;
  54             }
  55         }
  56         cmdarray[pos++] = className;
  57         if (args != null) {
  58             for (String arg: args) {
  59                 cmdarray[pos++] = arg;
  60             }
  61         }
  62         launch0(cmdarray, fd);
  63     }
  64 
  65     /*
  66      * Launch 'java' with specified class with the specified arguments (may be null).
  67      * The launched process will inherit a connected TCP socket. The remote endpoint
  68      * will be the SocketChannel returned by this method.
  69      */
  70     public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
  71         ServerSocketChannel ssc = ServerSocketChannel.open();
  72         ssc.socket().bind(new InetSocketAddress(0));
  73         InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
  74                                                       ssc.socket().getLocalPort());
  75         SocketChannel sc1 = SocketChannel.open(isa);
  76         SocketChannel sc2 = ssc.accept();
  77         launch(className, options, args, Util.getFD(sc2));
  78         sc2.close();
  79         ssc.close();
  80         return sc1;
  81     }
  82 
  83     public static SocketChannel launchWithSocketChannel(String className, String args[]) throws IOException {
  84         return launchWithSocketChannel(className, null, args);
  85     }
  86 
  87     public static SocketChannel launchWithSocketChannel(String className) throws IOException {
  88         return launchWithSocketChannel(className, null);
  89     }
  90 
  91     /*
  92      * Launch 'java' with specified class with the specified arguments (may be null).
  93      * The launched process will inherited a TCP listener socket.
  94      * Once launched this method tries to connect to service. If a connection
  95      * can be established a SocketChannel, connected to the service, is returned.
  96      */
  97     public static SocketChannel launchWithServerSocketChannel(String className, String options[], String args[])
  98         throws IOException
  99     {
 100         ServerSocketChannel ssc = ServerSocketChannel.open();
 101         ssc.socket().bind(new InetSocketAddress(0));
 102         int port = ssc.socket().getLocalPort();
 103         launch(className, options, args, Util.getFD(ssc));
 104         ssc.close();
 105         InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
 106         return SocketChannel.open(isa);
 107     }
 108 
 109     public static SocketChannel launchWithServerSocketChannel(String className, String args[]) throws IOException {
 110         return launchWithServerSocketChannel(className, null, args);
 111     }
 112 
 113     public static SocketChannel launchWithServerSocketChannel(String className) throws IOException {
 114         return launchWithServerSocketChannel(className, null);
 115     }
 116 
 117     /*
 118      * Launch 'java' with specified class with the specified arguments (may be null).
 119      * The launch process will inherited a bound UDP socket.
 120      * Once launched this method creates a DatagramChannel and "connects
 121      * it to the service. The created DatagramChannel is then returned.
 122      * As it is connected any packets sent from the socket will be
 123      * sent to the service.
 124      */
 125     public static DatagramChannel launchWithDatagramChannel(String className, String options[], String args[])
 126         throws IOException
 127     {
 128         DatagramChannel dc = DatagramChannel.open();
 129         dc.socket().bind(new InetSocketAddress(0));
 130 
 131         int port = dc.socket().getLocalPort();
 132         launch(className, options, args, Util.getFD(dc));
 133         dc.close();
 134 
 135         dc = DatagramChannel.open();
 136         InetAddress address = InetAddress.getLocalHost();
 137         if (address.isLoopbackAddress()) {
 138             address = InetAddress.getLoopbackAddress();
 139         }
 140         InetSocketAddress isa = new InetSocketAddress(address, port);
 141 
 142         dc.connect(isa);
 143         return dc;
 144     }
 145 
 146     public static DatagramChannel launchWithDatagramChannel(String className, String args[]) throws IOException {
 147         return launchWithDatagramChannel(className, null, args);
 148     }
 149 
 150     public static DatagramChannel launchWithDatagramChannel(String className) throws IOException {
 151         return launchWithDatagramChannel(className, null);
 152     }
 153 }