2
|
1 |
/*
|
|
2 |
* Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
*
|
|
26 |
*
|
|
27 |
* A Launcher to launch a java process with its standard input, output,
|
|
28 |
* and error streams connected to a socket.
|
|
29 |
*/
|
|
30 |
import java.net.*;
|
|
31 |
import java.nio.channels.*;
|
|
32 |
import java.io.IOException;
|
|
33 |
|
|
34 |
public class Launcher {
|
|
35 |
|
|
36 |
static {
|
|
37 |
System.loadLibrary("Launcher");
|
|
38 |
}
|
|
39 |
|
|
40 |
private static native void launch0(String cmdarray[], int fd) throws IOException;
|
|
41 |
|
|
42 |
private static void launch(String className, String options[], String args[], int fd) throws IOException {
|
|
43 |
String[] javacmd = Util.javaCommand();
|
|
44 |
int options_len = (options == null) ? 0 : options.length;
|
|
45 |
int args_len = (args == null) ? 0 : args.length;
|
|
46 |
|
|
47 |
// java [-options] class [args...]
|
|
48 |
int len = javacmd.length + options_len + 1 + args_len;
|
|
49 |
|
|
50 |
String cmdarray[] = new String[len];
|
|
51 |
int pos = 0;
|
|
52 |
for (int i=0; i<javacmd.length; i++) {
|
|
53 |
cmdarray[pos++] = javacmd[i];
|
|
54 |
}
|
|
55 |
for (int i=0; i<options_len; i++) {
|
|
56 |
cmdarray[pos++] = options[i];
|
|
57 |
}
|
|
58 |
cmdarray[pos++] = className;
|
|
59 |
for (int i=0; i<args_len; i++) {
|
|
60 |
cmdarray[pos++] = args[i];
|
|
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 |
|
|
137 |
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
|
|
138 |
|
|
139 |
dc.connect(isa);
|
|
140 |
return dc;
|
|
141 |
}
|
|
142 |
|
|
143 |
public static DatagramChannel launchWithDatagramChannel(String className, String args[]) throws IOException {
|
|
144 |
return launchWithDatagramChannel(className, null, args);
|
|
145 |
}
|
|
146 |
|
|
147 |
public static DatagramChannel launchWithDatagramChannel(String className) throws IOException {
|
|
148 |
return launchWithDatagramChannel(className, null);
|
|
149 |
}
|
|
150 |
}
|