001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003:
004: package fitnesse.components;
005:
006: import java.io.*;
007:
008: import java.util.*;
009:
010: public class CommandRunner {
011: protected Process process;
012: protected String input = "";
013: protected List exceptions = new ArrayList();
014: protected OutputStream stdin;
015: protected InputStream stdout;
016: protected InputStream stderr;
017: protected StringBuffer outputBuffer = new StringBuffer();
018: protected StringBuffer errorBuffer = new StringBuffer();
019: protected int exitCode = -1;
020: private long startTime;
021: private long endTime;
022: private String command = "";
023:
024: public CommandRunner() {
025: }
026:
027: public CommandRunner(String command, String input) {
028: this .command = command;
029: this .input = input;
030: }
031:
032: public void start() throws Exception {
033: Runtime rt = Runtime.getRuntime();
034: startTime = System.currentTimeMillis();
035: process = rt.exec(command);
036: stdin = process.getOutputStream();
037: stdout = process.getInputStream();
038: stderr = process.getErrorStream();
039:
040: new Thread(new OuputReadingRunnable(stdout, outputBuffer),
041: "CommandRunner stdout").start();
042: new Thread(new OuputReadingRunnable(stderr, errorBuffer),
043: "CommandRunner error").start();
044:
045: sendInput();
046: }
047:
048: public void run() throws Exception {
049: start();
050: join();
051: }
052:
053: public void join() throws Exception {
054: process.waitFor();
055: endTime = System.currentTimeMillis();
056: exitCode = process.exitValue();
057: }
058:
059: public void kill() throws Exception {
060: if (process != null) {
061: process.destroy();
062: join();
063: }
064: }
065:
066: protected void setCommand(String command) {
067: this .command = command;
068: }
069:
070: public String getCommand() {
071: return command;
072: }
073:
074: public String getOutput() {
075: return outputBuffer.toString();
076: }
077:
078: public String getError() {
079: return errorBuffer.toString();
080: }
081:
082: public List getExceptions() {
083: return exceptions;
084: }
085:
086: public boolean hasExceptions() {
087: return exceptions.size() > 0;
088: }
089:
090: public boolean wroteToErrorStream() {
091: return errorBuffer.length() > 0;
092: }
093:
094: public boolean wroteToOutputStream() {
095: return outputBuffer.length() > 0;
096: }
097:
098: public int getExitCode() {
099: return exitCode;
100: }
101:
102: public void exceptionOccurred(Exception e) {
103: exceptions.add(e);
104: }
105:
106: public long getExecutionTime() {
107: return endTime - startTime;
108: }
109:
110: protected void sendInput() throws Exception {
111: Thread thread = new Thread() {
112: public void run() {
113: try {
114: stdin.write(input.getBytes("UTF-8"));
115: stdin.flush();
116: stdin.close();
117: } catch (Exception e) {
118: exceptionOccurred(e);
119: }
120: }
121: };
122: thread.start();
123: thread.join();
124:
125: }
126:
127: private void readOutput(InputStream input, StringBuffer buffer) {
128: try {
129: int c;
130: while ((c = input.read()) != -1)
131: buffer.append((char) c);
132: } catch (Exception e) {
133: exceptionOccurred(e);
134: }
135: }
136:
137: private class OuputReadingRunnable implements Runnable {
138: public InputStream input;
139: public StringBuffer buffer;
140:
141: public OuputReadingRunnable(InputStream input,
142: StringBuffer buffer) {
143: this .input = input;
144: this .buffer = buffer;
145: }
146:
147: public void run() {
148: readOutput(input, buffer);
149: }
150: }
151: }
|