imported from svn
[metux-java.git] / util / Exec.java
blob1eb7d0906ba8e047feb938a3be3735384d15b40c
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;
9 import java.io.File;
11 import java.lang.IllegalThreadStateException;
13 public class Exec
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;
65 boolean running;
66 BufferedReader stdout;
67 BufferedReader stderr;
68 Process process;
70 public int exit_code = 0;
72 class FetchStdoutThread extends Thread
74 public void run()
76 try
78 String line;
79 while ((line=stdout.readLine())!=null)
80 callback.OutputLine(line);
82 catch (IOException e)
84 System.err.println("EXEC (read-stdout) IO EXCEPTION: "+e);
89 class FetchStderrThread extends Thread
91 public void run()
93 try
95 String line;
96 while ((line=stderr.readLine())!=null)
97 callback.OutputLine(line);
99 catch (IOException e)
101 System.err.println("EXEC (read-stderr) IO EXCEPTION: "+e);
106 public Exec()
108 callback = new default_cb();
111 public Exec(IExecCallback cb)
113 callback = 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();
127 cb.add_lf = lf;
128 callback = cb;
130 boolean res = run(cmdline);
131 callback = old_cb;
133 if (res)
134 return cb.buffer;
135 else
137 System.err.println("run_catch() error: \""+cb.buffer+"\"");
138 return null;
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";
158 cmds[1] = "-c";
159 cmds[2] = cmdline + " 2>&1";
161 callback.debug("executing: \""+cmdline+"\"");
163 if (dir==null)
164 dir = new File(System.getProperty("user.dir"));
166 try
168 if (dir==null)
169 process = Runtime.getRuntime().exec(cmds);
170 else
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());
178 return false;
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();
187 err.start();
188 out.start();
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);
199 return false;
203 public void abort()
205 running = false;
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);