X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fio%2FSimpleProcess.java;h=ed748a24da408d8dfed7ee73e30687ee83db9338;hb=8de36ff0206c996baf3ee4adc3e2293b12ff5f39;hp=3e0e7f853eade21fb9fa57b45c7bbfd1c3893f3d;hpb=89c06d4d52b46c154128c97d6e758fa1f4fc7a6e;p=utils diff --git a/support/general/src/main/java/org/wamblee/io/SimpleProcess.java b/support/general/src/main/java/org/wamblee/io/SimpleProcess.java index 3e0e7f85..ed748a24 100644 --- a/support/general/src/main/java/org/wamblee/io/SimpleProcess.java +++ b/support/general/src/main/java/org/wamblee/io/SimpleProcess.java @@ -28,53 +28,33 @@ import java.io.PrintStream; import java.io.StringWriter; import java.io.Writer; - /** - * DOCUMENT ME! - * + * * @author $author$ * @version $Revision$ */ public class SimpleProcess { - /** - * DOCUMENT ME! - */ private static final Log LOG = LogFactory.getLog(SimpleProcess.class); - /** - * DOCUMENT ME! - */ private File directory; - /** - * DOCUMENT ME! - */ private String[] cmd; - /** - * DOCUMENT ME! - */ private String stdout; - /** - * DOCUMENT ME! - */ private String stderr; -/** + /** * Creates a new SimpleProcess object. - * - * @param aDirectory DOCUMENT ME! - * @param aCmd DOCUMENT ME! + * */ public SimpleProcess(File aDirectory, String[] aCmd) { - directory = aDirectory; - cmd = aCmd; + directory = aDirectory; + cmd = aCmd; } /** - * DOCUMENT ME! - * + * * @return the stdout */ public String getStdout() { @@ -82,8 +62,7 @@ public class SimpleProcess { } /** - * DOCUMENT ME! - * + * * @return the stderr */ public String getStderr() { @@ -92,22 +71,16 @@ public class SimpleProcess { /** * Runs the process and blocks until it is done. - * + * * @return Exit status of the process. - * - * @throws IOException In case of problems. + * + * @throws IOException + * In case of problems. */ public int run() throws IOException { return runImpl(); } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - * - * @throws IOException DOCUMENT ME! - */ private int runImpl() throws IOException { try { String fullcmd = ""; @@ -116,26 +89,26 @@ public class SimpleProcess { fullcmd += (" " + part); } - LOG.debug("Executing '" + fullcmd + "' in directory '" + directory - + "'"); + LOG.debug("Executing '" + fullcmd + "' in directory '" + directory + + "'"); - java.lang.Process proc = Runtime.getRuntime() - .exec(cmd, null, directory); + java.lang.Process proc = Runtime.getRuntime().exec(cmd, null, + directory); // Read standard output and error in separate threads to avoid // deadlock. - StringWriter myStdout = new StringWriter(); - StringWriter myStderr = new StringWriter(); - Thread stdoutReader = readAndLogStream("STDOUT> ", - proc.getInputStream(), myStdout); - Thread stderrReader = readAndLogStream("STDERR> ", - proc.getErrorStream(), myStderr); + StringWriter myStdout = new StringWriter(); + StringWriter myStderr = new StringWriter(); + Thread stdoutReader = readAndLogStream("STDOUT> ", proc + .getInputStream(), myStdout); + Thread stderrReader = readAndLogStream("STDERR> ", proc + .getErrorStream(), myStderr); try { proc.waitFor(); } catch (InterruptedException e) { IOException exception = new IOException( - "Process was terminated: " + this); + "Process was terminated: " + this); exception.initCause(e); throw exception; } @@ -143,8 +116,8 @@ public class SimpleProcess { waitForReader(stdoutReader); waitForReader(stderrReader); - stdout = myStdout.toString(); - stderr = myStderr.toString(); + stdout = myStdout.toString(); + stderr = myStderr.toString(); if (proc.exitValue() != 0) { LOG.warn("Exit value was non-zero: " + this); @@ -154,77 +127,59 @@ public class SimpleProcess { return proc.exitValue(); } catch (IOException e) { - IOException exception = new IOException("Error executing process: " - + this); + IOException exception = new IOException( + "Error executing process: " + this); exception.initCause(e); throw exception; } } - /** - * DOCUMENT ME! - * - * @param aReaderThread DOCUMENT ME! - */ private void waitForReader(Thread aReaderThread) { try { aReaderThread.join(); } catch (InterruptedException e) { - LOG.warn(this - + ": error waiting for output stream reader of process to finish"); + LOG + .warn(this + + ": error waiting for output stream reader of process to finish"); } } - /** - * DOCUMENT ME! - * - * @param aPrefix DOCUMENT ME! - * @param aStream DOCUMENT ME! - * @param aOutput DOCUMENT ME! - * - * @return DOCUMENT ME! - */ private Thread readAndLogStream(final String aPrefix, final InputStream aStream, final Writer aOutput) { Thread inputReader = new Thread() { - @Override - public void run() { - BufferedReader br = null; + @Override + public void run() { + BufferedReader br = null; - try { - br = new BufferedReader(new InputStreamReader(aStream)); + try { + br = new BufferedReader(new InputStreamReader(aStream)); - String str; + String str; - while ((str = br.readLine()) != null) { - LOG.debug(aPrefix + str); - aOutput.write(str); - } - } catch (IOException e) { - LOG.warn(SimpleProcess.this - + ": error reading input stream", e); - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException e) { - LOG.warn("Error closing stream " + aPrefix); - } + while ((str = br.readLine()) != null) { + LOG.debug(aPrefix + str); + aOutput.write(str); + } + } catch (IOException e) { + LOG.warn(SimpleProcess.this + + ": error reading input stream", e); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + LOG.warn("Error closing stream " + aPrefix); } } } - }; + } + }; inputReader.start(); return inputReader; } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ @Override public String toString() { String fullcmd = "";