1 45 package org.exolab.jms.net.jvm; 46 47 import java.io.BufferedReader ; 48 import java.io.InputStream ; 49 import java.io.InputStreamReader ; 50 import java.io.IOException ; 51 import java.io.OutputStream ; 52 import java.io.PrintStream ; 53 54 import org.apache.commons.logging.Log; 55 import org.apache.commons.logging.LogFactory; 56 57 58 65 public class Executor { 66 67 70 private final String _command; 71 72 75 private final OutputStream _out; 76 77 80 private final OutputStream _err; 81 82 85 private Thread _outThread; 86 87 90 private Thread _errThread; 91 92 95 private volatile Process _process; 96 97 100 private static final Log _log = LogFactory.getLog(Executor.class); 101 102 103 109 public Executor(String command) { 110 this(command, System.out, System.err); 111 } 112 113 119 public Executor(String command, OutputStream log) { 120 this(command, log, log); 121 } 122 123 131 public Executor(String command, OutputStream out, OutputStream err) { 132 if (command == null) { 133 throw new IllegalArgumentException ("Argument 'command' is null"); 134 } 135 if (out == null) { 136 throw new IllegalArgumentException ("Argument 'out' is null"); 137 } 138 if (err == null) { 139 throw new IllegalArgumentException ("Argument 'err' is null"); 140 } 141 _command = command; 142 _out = out; 143 _err = err; 144 } 145 146 151 public void start() throws IOException { 152 _log.debug("Starting " + _command); 153 _process = Runtime.getRuntime().exec(_command); 154 Reader out = new Reader (_process.getInputStream(), _out); 155 Reader err = new Reader (_process.getErrorStream(), _err); 156 _outThread = new Thread (out, "StdOut<" + _command + ">"); 157 _errThread = new Thread (err, "StdErr<" + _command + ">"); 158 _outThread.start(); 159 _errThread.start(); 160 161 } 162 163 168 public int waitFor() { 169 boolean done = false; 170 int status = 1; 171 172 while (!done) { 173 try { 174 status = _process.waitFor(); 175 done = true; 176 } catch (InterruptedException ignore) { 177 } 179 } 180 181 while (true) { 182 try { 183 _outThread.join(); 184 break; 185 } catch (InterruptedException ignore) { 186 } 188 } 189 190 while (true) { 191 try { 192 _errThread.join(); 193 break; 194 } catch (InterruptedException ignore) { 195 } 197 } 198 199 _outThread = null; 200 _errThread = null; 201 _process = null; 202 203 return status; 204 } 205 206 212 public int run() throws IOException { 213 start(); 214 return waitFor(); 215 } 216 217 220 public void stop() { 221 Process process = _process; 222 if (process != null) { 223 _log.debug("Stopping " + _command); 224 process.destroy(); 225 } 226 } 227 228 232 class Reader implements Runnable { 233 234 237 private volatile Thread _thread = null; 238 239 242 private BufferedReader _input = null; 243 244 247 private PrintStream _output = null; 248 249 252 private Exception _exception = null; 253 254 261 public Reader(InputStream input, OutputStream output) { 262 _input = new BufferedReader (new InputStreamReader (input)); 263 _output = new PrintStream (output); 264 } 265 266 269 public void run() { 270 _thread = Thread.currentThread(); 271 String line; 272 try { 273 while (_thread != null && (line = _input.readLine()) != null) { 274 _output.println(line); 275 } 276 } catch (IOException exception) { 277 _exception = exception; 279 _thread = null; 280 } 281 _input = null; 282 _output = null; 283 } 284 285 288 public void stop() { 289 if (_thread != null) { 290 Thread interrupter = _thread; 291 _thread = null; 292 interrupter.interrupt(); 293 } 294 } 295 296 300 public Exception getException() { 301 return _exception; 302 } 303 } 304 305 } 306 | Popular Tags |