001: /*
002: * Created on May 4, 2006
003: */
004: package com.openedit.util;
005:
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.util.List;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import com.openedit.OpenEditException;
014:
015: public class Exec {
016: private static final Log log = LogFactory.getLog(Exec.class);
017: protected boolean fieldTrackOutput;
018: protected String fieldErrorOutput;
019: protected String fieldStandardOutput;
020:
021: public boolean runExec(List com) throws OpenEditException {
022: setErrorOutput(null);
023: setStandardOutput(null);
024: String[] inCommand = (String[]) com.toArray(new String[com
025: .size()]);
026: try {
027: log.info("Running: " + com);
028:
029: Process proc = Runtime.getRuntime().exec(inCommand);
030: int ret = 0;
031: if (log.isDebugEnabled() || isTrackOutput()) {
032: //run again to catch the error this time (keeps things fast due to thread starts)
033: InputStreamHandler reader1 = new InputStreamHandler();
034: reader1.setStream(proc.getInputStream());
035:
036: InputStreamHandler errreader = new InputStreamHandler();
037: errreader.setStream(proc.getErrorStream());
038: reader1.start();
039:
040: errreader.start();
041:
042: ret = proc.waitFor();
043:
044: reader1.join();
045: errreader.join();
046: String stdo = reader1.getBuffer().toString();
047: if (stdo.length() > 0) {
048: setStandardOutput(stdo);
049: }
050: String stder = errreader.getBuffer().toString();
051: if (stder.length() > 0) {
052: setErrorOutput(stder);
053: }
054: } else {
055: ret = proc.waitFor();
056: }
057: if (ret != 0) {
058: log.error("error on: " + com);
059: return false;
060: }
061: return true;
062:
063: } catch (Exception ex) {
064: throw new OpenEditException(ex);
065: }
066: }
067:
068: public boolean isTrackOutput() {
069: return fieldTrackOutput;
070: }
071:
072: public void setTrackOutput(boolean inTrackOutput) {
073: fieldTrackOutput = inTrackOutput;
074: }
075:
076: public String getErrorOutput() {
077: return fieldErrorOutput;
078: }
079:
080: public void setErrorOutput(String inErrorOutput) {
081: fieldErrorOutput = inErrorOutput;
082: }
083:
084: public String getStandardOutput() {
085: return fieldStandardOutput;
086: }
087:
088: public void setStandardOutput(String inStandardOutput) {
089: fieldStandardOutput = inStandardOutput;
090: }
091:
092: class InputStreamHandler extends Thread {
093:
094: protected InputStream fieldStream;
095: protected StringBuffer fieldBuffer;
096:
097: /**
098: * @return Returns the buffer.
099: */
100: public StringBuffer getBuffer() {
101: if (fieldBuffer == null) {
102: fieldBuffer = new StringBuffer();
103: }
104: return fieldBuffer;
105: }
106:
107: /**
108: * @param inBuffer The buffer to set.
109: */
110: public void setBuffer(StringBuffer inBuffer) {
111: fieldBuffer = inBuffer;
112: }
113:
114: /**
115: * @return Returns the stream.
116: */
117: public InputStream getStream() {
118: return fieldStream;
119: }
120:
121: /**
122: * @param inStream The stream to set.
123: */
124: public void setStream(InputStream inStream) {
125: fieldStream = inStream;
126: }
127:
128: public void run() {
129: try {
130: int nextChar;
131: StringBuffer b = new StringBuffer();
132: while ((nextChar = getStream().read()) != -1) {
133: if (nextChar == '\n') {
134: String line = b.toString();
135: getBuffer().append(line);
136: getBuffer().append((char) nextChar);
137:
138: log.info(line);
139: b = new StringBuffer();
140: } else {
141: b.append((char) nextChar);
142: }
143: }
144: getBuffer().append(b.toString());
145: } catch (IOException ex) {
146: //ignore?
147: log.error(ex);
148: }
149: }
150: }
151:
152: }
|