2 package org
.de
.metux
.util
;
4 import java
.util
.Properties
;
5 import java
.io
.BufferedReader
;
6 import java
.io
.IOException
;
7 import java
.io
.InterruptedIOException
;
8 import java
.io
.InputStreamReader
;
11 import java
.lang
.IllegalThreadStateException
;
15 public interface IExecCallback
17 public void OutputLine(String text
);
18 public void ErrorLine(String text
);
19 public void debug(String text
);
20 public void error(String text
);
23 class default_cb
implements IExecCallback
25 public void OutputLine(String line
)
27 System
.out
.println(">> "+line
);
29 public void ErrorLine(String line
)
31 System
.out
.println("!> "+line
);
33 public void debug(String text
)
36 public void error(String text
)
38 System
.err
.println("EXEC ERROR: "+text
);
42 class collect_buf
implements IExecCallback
44 public String buffer
= "";
45 boolean add_lf
= false;
47 public void OutputLine(String line
)
49 buffer
+= line
+(add_lf ?
"\n" : "");
51 public void ErrorLine(String line
)
53 System
.out
.println("!> "+line
);
55 public void debug(String text
)
58 public void error(String text
)
60 System
.err
.println("EXEC ERROR: "+text
);
64 IExecCallback callback
;
66 BufferedReader stdout
;
67 BufferedReader stderr
;
70 public int exit_code
= 0;
72 class FetchStdoutThread
extends Thread
79 while ((line
=stdout
.readLine())!=null)
80 callback
.OutputLine(line
);
84 System
.err
.println("EXEC (read-stdout) IO EXCEPTION: "+e
);
89 class FetchStderrThread
extends Thread
96 while ((line
=stderr
.readLine())!=null)
97 callback
.OutputLine(line
);
101 System
.err
.println("EXEC (read-stderr) IO EXCEPTION: "+e
);
108 callback
= new default_cb();
111 public Exec(IExecCallback cb
)
116 // run the command and catch output
117 // if it failed, return null
118 public String
run_catch(String cmdline
)
120 return run_catch(cmdline
,false);
123 public String
run_catch(String cmdline
, boolean lf
)
125 IExecCallback old_cb
= callback
;
126 collect_buf cb
= new collect_buf();
130 boolean res
= run(cmdline
);
137 System
.err
.println("run_catch() error: \""+cb
.buffer
+"\"");
142 private boolean is_interrupted(IOException e
)
144 String except
= e
.toString();
145 return (except
.indexOf("Interrupted system call")!=-1);
148 public boolean run(String cmdline
)
150 return run(cmdline
,(File
)null);
153 public boolean run(String cmdline
, File dir
)
155 String cmds
[] = cmds
= new String
[3];
157 cmds
[0] = "/bin/bash";
159 cmds
[2] = cmdline
+ " 2>&1";
161 callback
.debug("executing: \""+cmdline
+"\"");
164 dir
= new File(System
.getProperty("user.dir"));
169 process
= Runtime
.getRuntime().exec(cmds
);
171 process
= Runtime
.getRuntime().exec(cmds
,null,dir
);
173 catch (IOException e
)
175 callback
.error("Stage::exec() could not execute: "+cmdline
+
176 " IOException "+e
.toString());
181 stdout
= new BufferedReader(new InputStreamReader(process
.getInputStream()));
182 stderr
= new BufferedReader(new InputStreamReader(process
.getErrorStream()));
184 FetchStderrThread err
= new FetchStderrThread();
185 FetchStdoutThread out
= new FetchStdoutThread();
192 exit_code
= process
.waitFor();
193 callback
.debug("subprocess exited with: "+exit_code
);
194 return (exit_code
==0);
196 catch (InterruptedException e
)
198 callback
.error("InterruptedException in Stage::exec()"+e
);
206 try { stdout
.close(); }
207 catch (IOException e
) { }
208 try { stderr
.close(); }
209 catch (IOException e
) { }
212 public static String
exec_catch(String cmd
)
214 return new Exec().run_catch(cmd
);
217 public static boolean exec_command(String cmd
)
219 return new Exec().run(cmd
);
222 public static boolean exec_command(String cmd
, IExecCallback cb
)
224 return new Exec(cb
).run(cmd
);