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.getName());
37 private File directory;
41 private String stdout;
43 private String stderr;
46 * Creates a new SimpleProcess object.
49 public SimpleProcess(File aDirectory, String[] aCmd) {
50 directory = aDirectory;
51 cmd = Arrays.copyOf(aCmd, aCmd.length);
58 public String getStdout() {
66 public String getStderr() {
71 * Runs the process and blocks until it is done.
73 * @return Exit status of the process.
76 * In case of problems.
78 public int run() throws IOException {
82 private int runImpl() throws IOException {
84 StringBuffer fullcmd = new StringBuffer();
86 for (String part : cmd) {
87 fullcmd.append(" " + part);
90 LOG.fine("Executing '" + fullcmd + "' in directory '" + directory +
93 java.lang.Process proc = Runtime.getRuntime().exec(cmd, null,
96 // Read standard output and error in separate threads to avoid
98 StringWriter myStdout = new StringWriter();
99 StringWriter myStderr = new StringWriter();
100 Thread stdoutReader = readAndLogStream("STDOUT> ", proc
101 .getInputStream(), myStdout);
102 Thread stderrReader = readAndLogStream("STDERR> ", proc
103 .getErrorStream(), myStderr);
107 } catch (InterruptedException e) {
108 IOException exception = new IOException(
109 "Process was terminated: " + this);
110 exception.initCause(e);
114 waitForReader(stdoutReader);
115 waitForReader(stderrReader);
117 stdout = myStdout.toString();
118 stderr = myStderr.toString();
120 if (proc.exitValue() != 0) {
121 LOG.warning("Exit value was non-zero: " + this);
123 LOG.fine("Process finished");
126 return proc.exitValue();
127 } catch (IOException e) {
128 IOException exception = new IOException(
129 "Error executing process: " + this);
130 exception.initCause(e);
135 private void waitForReader(Thread aReaderThread) {
137 aReaderThread.join();
138 } catch (InterruptedException e) {
140 .log(Level.WARNING, this +
141 ": error waiting for output stream reader of process to finish", e);
145 private Thread readAndLogStream(final String aPrefix,
146 final InputStream aStream, final Writer aOutput) {
147 Thread inputReader = new Thread() {
150 BufferedReader br = null;
153 br = new BufferedReader(new InputStreamReader(aStream));
157 while ((str = br.readLine()) != null) {
158 LOG.fine(aPrefix + str);
161 } catch (IOException e) {
162 LOG.log(Level.FINE, SimpleProcess.this +
163 ": error reading input stream", e);
168 } catch (IOException e) {
169 LOG.log(Level.WARNING, "Error closing stream " + aPrefix, e);
182 public String toString() {
183 StringBuffer fullcmd = new StringBuffer();
185 for (String part : cmd) {
186 fullcmd.append(part + " ");
189 return "process(dir = '" + directory + "', cmd = '" + fullcmd + "')";