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;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
34 public class SimpleProcess {
35 private static final Logger LOG = Logger.getLogger(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.fine("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.warning("Exit value was non-zero: " + this);
124 LOG.fine("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) {
144 ": error waiting for output stream reader of process to finish",
149 private Thread readAndLogStream(final String aPrefix,
150 final InputStream aStream, final Writer aOutput) {
151 Thread inputReader = new Thread() {
154 BufferedReader br = null;
157 br = new BufferedReader(new InputStreamReader(aStream));
161 while ((str = br.readLine()) != null) {
162 LOG.fine(aPrefix + str);
165 } catch (IOException e) {
166 LOG.log(Level.FINE, SimpleProcess.this +
167 ": error reading input stream", e);
172 } catch (IOException e) {
173 LOG.log(Level.WARNING, "Error closing stream " +
187 public String toString() {
188 StringBuffer fullcmd = new StringBuffer();
190 for (String part : cmd) {
191 fullcmd.append(part + " ");
194 return "process(dir = '" + directory + "', cmd = '" + fullcmd + "')";