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:
013: protected String input = "";
014:
015: protected List exceptions = new ArrayList();
016:
017: protected OutputStream stdin;
018:
019: protected InputStream stdout;
020:
021: protected InputStream stderr;
022:
023: protected StringBuffer outputBuffer = new StringBuffer();
024:
025: protected StringBuffer errorBuffer = new StringBuffer();
026:
027: protected int exitCode = -1;
028:
029: private long startTime;
030:
031: private long endTime;
032:
033: private String command = "";
034:
035: public CommandRunner() {
036: }
037:
038: public CommandRunner(String command, String input) {
039: this .command = command;
040: this .input = input;
041: }
042:
043: public void start() throws Exception {
044: Runtime rt = Runtime.getRuntime();
045: startTime = System.currentTimeMillis();
046: process = rt.exec(command);
047: stdin = process.getOutputStream();
048: stdout = process.getInputStream();
049: stderr = process.getErrorStream();
050:
051: new Thread(new OuputReadingRunnable(stdout, outputBuffer),
052: "CommandRunner stdout").start();
053: new Thread(new OuputReadingRunnable(stderr, errorBuffer),
054: "CommandRunner error").start();
055:
056: sendInput();
057: }
058:
059: public void run() throws Exception {
060: start();
061: join();
062: }
063:
064: public void join() throws Exception {
065: process.waitFor();
066: endTime = System.currentTimeMillis();
067: exitCode = process.exitValue();
068: }
069:
070: public void kill() throws Exception {
071: if (process != null) {
072: process.destroy();
073: join();
074: }
075: }
076:
077: protected void setCommand(String command) {
078: this .command = command;
079: }
080:
081: public String getCommand() {
082: return command;
083: }
084:
085: public String getOutput() {
086: return outputBuffer.toString();
087: }
088:
089: public String getError() {
090: return errorBuffer.toString();
091: }
092:
093: public List getExceptions() {
094: return exceptions;
095: }
096:
097: public boolean hasExceptions() {
098: return exceptions.size() > 0;
099: }
100:
101: public boolean wroteToErrorStream() {
102: return errorBuffer.length() > 0;
103: }
104:
105: public boolean wroteToOutputStream() {
106: return outputBuffer.length() > 0;
107: }
108:
109: public int getExitCode() {
110: return exitCode;
111: }
112:
113: public void exceptionOccurred(Exception e) {
114: exceptions.add(e);
115: }
116:
117: public long getExecutionTime() {
118: return endTime - startTime;
119: }
120:
121: protected void sendInput() throws Exception {
122: Thread thread = new Thread() {
123: public void run() {
124: try {
125: stdin.write(input.getBytes("UTF-8"));
126: stdin.flush();
127: stdin.close();
128: } catch (Exception e) {
129: exceptionOccurred(e);
130: }
131: }
132: };
133: thread.start();
134: thread.join();
135:
136: }
137:
138: void readOutput(InputStream input, StringBuffer buffer) {
139: try {
140: int c;
141: while ((c = input.read()) != -1)
142: buffer.append((char) c);
143: } catch (Exception e) {
144: exceptionOccurred(e);
145: }
146: }
147:
148: private class OuputReadingRunnable implements Runnable {
149: public InputStream inputStream;
150:
151: public StringBuffer buffer;
152:
153: public OuputReadingRunnable(InputStream input,
154: StringBuffer buffer) {
155: this .inputStream = input;
156: this .buffer = buffer;
157: }
158:
159: public void run() {
160: readOutput(inputStream, buffer);
161: }
162: }
163: }
|