43 |
import java.io.OutputStream; |
import java.io.OutputStream; |
44 |
import java.util.HashMap; |
import java.util.HashMap; |
45 |
import java.util.LinkedList; |
import java.util.LinkedList; |
46 |
|
import java.util.List; |
47 |
|
import java.util.Map; |
48 |
|
|
49 |
/** |
/** |
50 |
* Represents one external process. Each instance of this class is in |
* Represents one external process. Each instance of this class is in |
94 |
InputStream stdout; // process output stream |
InputStream stdout; // process output stream |
95 |
InputStream stderr; // process error stream |
InputStream stderr; // process error stream |
96 |
int exitValue; // process exit value |
int exitValue; // process exit value |
97 |
|
boolean redirect; // redirect stderr -> stdout |
98 |
|
|
99 |
// |
// |
100 |
// Dedicated thread that does all the fork()'ing and wait()'ing |
// Dedicated thread that does all the fork()'ing and wait()'ing |
199 |
{ |
{ |
200 |
try |
try |
201 |
{ |
{ |
202 |
process.nativeSpawn(process.cmd, process.env, process.dir); |
process.nativeSpawn(process.cmd, process.env, process.dir, |
203 |
|
process.redirect); |
204 |
process.state = RUNNING; |
process.state = RUNNING; |
205 |
activeMap.put(new Long(process.pid), process); |
activeMap.put(new Long(process.pid), process); |
206 |
} |
} |
219 |
} |
} |
220 |
|
|
221 |
// Constructor |
// Constructor |
222 |
private VMProcess(String[] cmd, String[] env, File dir) throws IOException |
private VMProcess(String[] cmd, String[] env, File dir, boolean redirect) |
223 |
|
throws IOException |
224 |
{ |
{ |
225 |
|
|
226 |
// Initialize this process |
// Initialize this process |
228 |
this.cmd = cmd; |
this.cmd = cmd; |
229 |
this.env = env; |
this.env = env; |
230 |
this.dir = dir; |
this.dir = dir; |
231 |
|
this.redirect = redirect; |
232 |
|
|
233 |
// Add process to the new process work list and wakeup processThread |
// Add process to the new process work list and wakeup processThread |
234 |
synchronized (workList) |
synchronized (workList) |
281 |
|
|
282 |
// Invoked by native code (from nativeSpawn()) to record process info. |
// Invoked by native code (from nativeSpawn()) to record process info. |
283 |
private void setProcessInfo(OutputStream stdin, |
private void setProcessInfo(OutputStream stdin, |
284 |
InputStream stdout, InputStream stderr, long pid) |
InputStream stdout, InputStream stderr, long pid) |
285 |
{ |
{ |
286 |
this.stdin = stdin; |
this.stdin = stdin; |
287 |
this.stdout = stdout; |
this.stdout = stdout; |
288 |
this.stderr = stderr; |
if (stderr == null) |
289 |
|
this.stderr = new InputStream() |
290 |
|
{ |
291 |
|
public int read() throws IOException |
292 |
|
{ |
293 |
|
return -1; |
294 |
|
} |
295 |
|
}; |
296 |
|
else |
297 |
|
this.stderr = stderr; |
298 |
this.pid = pid; |
this.pid = pid; |
299 |
} |
} |
300 |
|
|
303 |
*/ |
*/ |
304 |
static Process exec(String[] cmd, String[] env, File dir) throws IOException |
static Process exec(String[] cmd, String[] env, File dir) throws IOException |
305 |
{ |
{ |
306 |
return new VMProcess(cmd, env, dir); |
return new VMProcess(cmd, env, dir, false); |
307 |
|
} |
308 |
|
|
309 |
|
static Process exec(List<String> cmd, Map<String, String> env, |
310 |
|
File dir, boolean redirect) throws IOException |
311 |
|
{ |
312 |
|
String[] acmd = cmd.toArray(new String[cmd.size()]); |
313 |
|
String[] aenv = new String[env.size()]; |
314 |
|
|
315 |
|
int i = 0; |
316 |
|
for (Map.Entry<String, String> entry : env.entrySet()) |
317 |
|
aenv[i++] = entry.getKey() + "=" + entry.getValue(); |
318 |
|
|
319 |
|
return new VMProcess(acmd, aenv, dir, redirect); |
320 |
} |
} |
321 |
|
|
322 |
public OutputStream getOutputStream() |
public OutputStream getOutputStream() |
375 |
* |
* |
376 |
* @throws IOException if the O/S process could not be created. |
* @throws IOException if the O/S process could not be created. |
377 |
*/ |
*/ |
378 |
native void nativeSpawn(String[] cmd, String[] env, File dir) |
native void nativeSpawn(String[] cmd, String[] env, File dir, |
379 |
|
boolean redirect) |
380 |
throws IOException; |
throws IOException; |
381 |
|
|
382 |
/** |
/** |