1 2 23 24 package net.fenyo.gnetwatch.actions; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import java.io.*; 30 31 36 37 public class ExternalCommand { 38 private static Log log = LogFactory.getLog(ExternalCommand.class); 39 40 private boolean merge = false; 41 42 final private String [] cmdLine; 43 final private String directory; 44 45 final private StringBuffer sb = new StringBuffer (); 46 47 private Process process = null; 48 private BufferedReader reader = null; 49 private BufferedReader errReader = null; 50 51 57 public ExternalCommand(final String [] cmdLine, final String directory) { 58 this.cmdLine = cmdLine; 59 this.directory = directory; 60 } 61 62 67 public ExternalCommand(final String [] cmdLine) { 69 this.cmdLine = cmdLine; 70 this.directory = System.getProperty("java.io.tmpdir"); 71 } 72 73 79 public ExternalCommand(final String [] cmdLine, final boolean merge) { 81 this.merge = merge; 82 this.cmdLine = cmdLine; 83 this.directory = System.getProperty("java.io.tmpdir"); 84 } 85 86 93 private String readLine(Reader r) throws IOException, InterruptedException { 101 sb.setLength(0); 102 while (!Thread.currentThread().isInterrupted()) { 103 if (r.ready()) { 104 final int ret = r.read(); 105 if (ret == -1) return sb.length() != 0 ? sb.toString() : null; 106 if (ret == '\n') return sb.toString(); 107 sb.append((char) ret); 108 } else { 109 try { 110 process.exitValue(); 111 return sb.length() != 0 ? sb.toString() : null; 112 } catch (final IllegalThreadStateException ex) {} 113 Thread.sleep(100); 114 } 115 } 116 log.info("readLine(): was interrupted"); 117 throw new InterruptedException ("readLine()"); 118 } 119 120 125 public synchronized String runStdoutStderr() throws InterruptedException { 128 String retval = null; 129 try { 130 fork(); 131 retval = readStdoutStderr(); 132 } catch (final IOException ex) {} 133 try { 134 end(); 135 } catch (final IOException ex) {} 136 return retval; 137 } 138 139 144 public synchronized String runStdout() throws InterruptedException { 147 String retval = null; 148 try { 149 fork(); 150 retval = readStdout(); 151 } catch (final IOException ex) {} 152 try { 153 end(); 154 } catch (final IOException ex) {} 155 return retval; 156 } 157 158 164 public synchronized void logArgs() { 165 for (int x = 0; x < cmdLine.length; x++) 166 log.debug("arg " + x + ": " + cmdLine[x]); 167 } 168 169 176 public synchronized void fork() throws IOException { 177 final ProcessBuilder pb = new ProcessBuilder (cmdLine); 178 pb.directory(new File(directory)); 179 pb.redirectErrorStream(merge); 180 process = pb.start(); 181 182 if (process == null) throw new IOException("null process"); 183 184 reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 185 errReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); 186 } 187 188 197 public synchronized String readStdoutStderr() throws IOException, InterruptedException { 198 StringBuffer output = new StringBuffer (); 199 String str; 200 while ((str = readLine(reader)) != null) { 201 output.append(str); 202 output.append("\n"); 203 } 204 205 while ((str = readLine(errReader)) != null) { 206 output.append(str); 207 output.append("\n"); 208 } 209 210 return output.toString(); 213 } 214 215 224 public synchronized String readStdout() throws IOException, InterruptedException { 225 StringBuffer output = new StringBuffer (); 226 String str; 227 228 while ((str = readLine(reader)) != null) { 229 output.append(str); 230 output.append("\n"); 231 } 232 233 return output.toString(); 236 } 237 238 247 public synchronized String readStderr() throws IOException, InterruptedException { 248 StringBuffer output = new StringBuffer (); 249 String str; 250 251 while ((str = readLine(errReader)) != null) { 252 output.append(str); 253 output.append("\n"); 254 } 255 256 return output.toString(); 259 } 260 261 268 public synchronized String readLineStdout() throws IOException, InterruptedException { 269 return readLine(reader); 270 } 271 272 279 public synchronized String readLineStderr() throws IOException, InterruptedException { 280 return readLine(errReader); 281 } 282 283 291 public void end() throws IOException { 292 if (process != null) { 297 kill(); 298 synchronized(this) { 299 if (process.getInputStream() != null) process.getInputStream().close(); 300 if (process.getOutputStream() != null) process.getOutputStream().close(); 301 if (process.getErrorStream() != null) process.getErrorStream().close(); 302 } 303 } 304 } 305 306 313 private void kill() { 314 if (process != null) { 315 boolean terminated = false; 316 process.destroy(); 317 while (terminated == false) 318 try { 319 process.waitFor(); 320 terminated = true; 321 } catch (final InterruptedException ex) { 322 log.warn("Exception", ex); 323 } 324 } 325 } 326 } 327 | Popular Tags |