2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.io;
18 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.StringWriter;
24 import java.io.Writer;
25 import java.util.Arrays;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
35 public class SimpleProcess {
36 private static final Log LOG = LogFactory.getLog(SimpleProcess.class);
38 private File directory;
42 private String stdout;
44 private String stderr;
47 * Creates a new SimpleProcess object.
50 public SimpleProcess(File aDirectory, String[] aCmd) {
51 directory = aDirectory;
52 cmd = Arrays.copyOf(aCmd, aCmd.length);
59 public String getStdout() {
67 public String getStderr() {
72 * Runs the process and blocks until it is done.
74 * @return Exit status of the process.
77 * In case of problems.
79 public int run() throws IOException {
83 private int runImpl() throws IOException {
85 StringBuffer fullcmd = new StringBuffer();
87 for (String part : cmd) {
88 fullcmd.append(" " + part);
91 LOG.debug("Executing '" + fullcmd + "' in directory '" + directory +
94 java.lang.Process proc = Runtime.getRuntime().exec(cmd, null,
97 // Read standard output and error in separate threads to avoid
99 StringWriter myStdout = new StringWriter();
100 StringWriter myStderr = new StringWriter();
101 Thread stdoutReader = readAndLogStream("STDOUT> ", proc
102 .getInputStream(), myStdout);
103 Thread stderrReader = readAndLogStream("STDERR> ", proc
104 .getErrorStream(), myStderr);
108 } catch (InterruptedException e) {
109 IOException exception = new IOException(
110 "Process was terminated: " + this);
111 exception.initCause(e);
115 waitForReader(stdoutReader);
116 waitForReader(stderrReader);
118 stdout = myStdout.toString();
119 stderr = myStderr.toString();
121 if (proc.exitValue() != 0) {
122 LOG.warn("Exit value was non-zero: " + this);
124 LOG.debug("Process finished");
127 return proc.exitValue();
128 } catch (IOException e) {
129 IOException exception = new IOException(
130 "Error executing process: " + this);
131 exception.initCause(e);
136 private void waitForReader(Thread aReaderThread) {
138 aReaderThread.join();
139 } catch (InterruptedException e) {
142 ": error waiting for output stream reader of process to finish");
146 private Thread readAndLogStream(final String aPrefix,
147 final InputStream aStream, final Writer aOutput) {
148 Thread inputReader = new Thread() {
151 BufferedReader br = null;
154 br = new BufferedReader(new InputStreamReader(aStream));
158 while ((str = br.readLine()) != null) {
159 LOG.debug(aPrefix + str);
162 } catch (IOException e) {
163 LOG.warn(SimpleProcess.this +
164 ": error reading input stream", e);
169 } catch (IOException e) {
170 LOG.warn("Error closing stream " + aPrefix);
183 public String toString() {
184 StringBuffer fullcmd = new StringBuffer();
186 for (String part : cmd) {
187 fullcmd.append(part + " ");
190 return "process(dir = '" + directory + "', cmd = '" + fullcmd + "')";