finally introduced the builder pattern for Launcher, to stop combinatorial explosion of the launch method

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@18944 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2009-06-16 06:38:22 +00:00
1 parent ca576cf3d4
commit bcd8e37595
16 files changed
+273 -87

No files matched your search

+25 -5
View File
@@ -82,7 +82,7 @@ public abstract class Proc {
*/
public static final class LocalProc extends Proc {
private final Process proc;
private final Thread copier;
private final Thread copier,copier2;
private final OutputStream out;
private final EnvVars cookie;
@@ -107,9 +107,22 @@ public abstract class Proc {
}
public LocalProc(String[] cmd,String[] env,InputStream in,OutputStream out, File workDir) throws IOException {
this(cmd,env,in,out,null,workDir);
}
/**
* @param err
* null to redirect stderr to stdout.
*/
public LocalProc(String[] cmd,String[] env,InputStream in,OutputStream out,OutputStream err,File workDir) throws IOException {
this( calcName(cmd),
environment(new ProcessBuilder(cmd),env).directory(workDir).redirectErrorStream(true),
in, out );
stderr(environment(new ProcessBuilder(cmd),env).directory(workDir),err),
in, out, err );
}
private static ProcessBuilder stderr(ProcessBuilder pb, OutputStream stderr) {
if(stderr==null) pb.redirectErrorStream(true);
return pb;
}
private static ProcessBuilder environment(ProcessBuilder pb, String[] env) {
@@ -124,7 +137,7 @@ public abstract class Proc {
return pb;
}
private LocalProc( String name, ProcessBuilder procBuilder, InputStream in, OutputStream out ) throws IOException {
private LocalProc( String name, ProcessBuilder procBuilder, InputStream in, OutputStream out, OutputStream err ) throws IOException {
Logger.getLogger(Proc.class.getName()).log(Level.FINE, "Running: {0}", name);
this.out = out;
this.cookie = ProcessTreeKiller.createCookie();
@@ -136,6 +149,12 @@ public abstract class Proc {
new StdinCopyThread(name+": stdin copier",in,proc.getOutputStream()).start();
else
proc.getOutputStream().close();
if(err!=null) {
copier2 = new StreamCopyThread(name+": stderr copier", proc.getErrorStream(), err);
copier2.start();
} else {
copier2 = null;
}
}
/**
@@ -149,7 +168,8 @@ public abstract class Proc {
// problems like that shows up as inifinite wait in join(), which confuses great many users.
// So let's do a timed wait here and try to diagnose the problem
copier.join(10*1000);
if(copier.isAlive()) {
copier2.join(10*1000);
if(copier.isAlive() || copier2.isAlive()) {
// looks like handles are leaking.
// closing these handles should terminate the threads.
String msg = "Process leaked file descriptors. See http://hudson.gotdns.com/wiki/display/HUDSON/Spawning+processes+from+build for more information";