-
- /**
- * Runs the process and blocks until it is done.
- *
- * @return Exit status of the process.
- * @throws IOException
- * In case of problems.
- */
- public int run() throws IOException {
- return runImpl();
- }
-
- private int runImpl() throws IOException {
- try {
- String fullcmd = "";
- for (String part: cmd) {
- fullcmd += " " + part;
- }
- LOG.debug("Executing '" + fullcmd + "' in directory '" + 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);
-
- try {
- proc.waitFor();
- } catch (InterruptedException e) {
- IOException exception = new IOException(
- "Process was terminated: " + this);
- exception.initCause(e);
- throw exception;
- }
- waitForReader(stdoutReader);
- waitForReader(stderrReader);
-
- stdout = myStdout.toString();
- stderr = myStderr.toString();
-
- if (proc.exitValue() != 0) {
- LOG.warn("Exit value was non-zero: " + this);
- } else {
- LOG.debug("Process finished");
- }
- return proc.exitValue();
- } catch (IOException e) {
- IOException exception = new IOException("Error executing process: "
- + this);
- exception.initCause(e);
- throw exception;
- }
- }
-
- private void waitForReader(Thread aReaderThread) {
- try {
- aReaderThread.join();
- } catch (InterruptedException e) {
- LOG
- .warn(this
- + ": error waiting for output stream reader of process to finish");
- }
- }
-
- private Thread readAndLogStream(final String aPrefix,
- final InputStream aStream, final Writer aOutput) {
- Thread inputReader = new Thread() {
- @Override
- public void run() {
- BufferedReader br = null;
- try {
- br = new BufferedReader(new InputStreamReader(aStream));
- 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);
- }
- }
- }
- }
- };
- inputReader.start();
- return inputReader;
- }
-
- @Override
- public String toString() {
+
+ /**
+ * Runs the process and blocks until it is done.
+ *
+ * @return Exit status of the process.
+ *
+ * @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 = "";
+
+ for (String part : cmd) {
+ fullcmd += (" " + part);
+ }
+
+ LOG.debug("Executing '" + fullcmd + "' in directory '" + 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);
+
+ try {
+ proc.waitFor();
+ } catch (InterruptedException e) {
+ IOException exception = new IOException(
+ "Process was terminated: " + this);
+ exception.initCause(e);
+ throw exception;
+ }
+
+ waitForReader(stdoutReader);
+ waitForReader(stderrReader);
+
+ stdout = myStdout.toString();
+ stderr = myStderr.toString();
+
+ if (proc.exitValue() != 0) {
+ LOG.warn("Exit value was non-zero: " + this);
+ } else {
+ LOG.debug("Process finished");
+ }
+
+ return proc.exitValue();
+ } catch (IOException e) {
+ 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");
+ }
+ }
+
+ /**
+ * 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;
+
+ try {
+ br = new BufferedReader(new InputStreamReader(aStream));
+
+ 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);
+ }
+ }
+ }
+ }
+ };
+
+ inputReader.start();
+
+ return inputReader;
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ @Override
+ public String toString() {