001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.process;
006:
007: import org.apache.commons.io.IOUtils;
008:
009: import java.io.ByteArrayInputStream;
010: import java.io.File;
011: import java.io.FileOutputStream;
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.io.OutputStream;
015: import java.util.Arrays;
016:
017: public class Exec {
018:
019: public static Result execute(String cmd[]) throws Exception {
020: return execute(cmd, null, null, null);
021: }
022:
023: public static Result execute(String cmd[], String outputLog)
024: throws Exception {
025: return execute(cmd, outputLog, null, null);
026: }
027:
028: public static Result execute(String cmd[], String outputLog,
029: byte[] input) throws Exception {
030: return execute(cmd, outputLog, input, null);
031: }
032:
033: public static Result execute(String cmd[], String outputLog,
034: byte[] input, File workingDir) throws Exception {
035: Process process = Runtime.getRuntime().exec(cmd, null,
036: workingDir);
037:
038: Thread inputThread = new InputPumper(
039: input == null ? new byte[] {} : input, process
040: .getOutputStream());
041:
042: StreamCollector stderr = null;
043: StreamCollector stdout = null;
044:
045: FileOutputStream fileOutput = null;
046: StreamAppender outputLogger = null;
047:
048: String errString = null;
049: String outString = null;
050:
051: try {
052: if (outputLog != null) {
053: errString = "stderr output redirected to file "
054: + outputLog;
055: outString = "stdout output redirected to file "
056: + outputLog;
057: fileOutput = new FileOutputStream(outputLog);
058: outputLogger = new StreamAppender(fileOutput);
059: outputLogger.writeInput(process.getErrorStream(),
060: process.getInputStream());
061: } else {
062: stderr = new StreamCollector(process.getErrorStream());
063: stdout = new StreamCollector(process.getInputStream());
064: stderr.start();
065: stdout.start();
066: }
067:
068: inputThread.start();
069:
070: final int exitCode = process.waitFor();
071:
072: if (inputThread != null)
073: inputThread.join();
074:
075: if (outputLogger != null) {
076: outputLogger.finish();
077: }
078:
079: if (stderr != null) {
080: stderr.join();
081: errString = stderr.toString();
082: }
083:
084: if (stdout != null) {
085: stdout.join();
086: outString = stdout.toString();
087: }
088:
089: return new Result(cmd, outString, errString, exitCode);
090: } finally {
091: closeQuietly(fileOutput);
092: }
093: }
094:
095: private static void closeQuietly(OutputStream output) {
096: if (output != null) {
097: try {
098: output.close();
099: } catch (IOException ioe) {
100: // quiet
101: }
102: }
103: }
104:
105: private static class InputPumper extends Thread {
106: private final InputStream data;
107: private final OutputStream output;
108:
109: InputPumper(byte[] input, OutputStream output) {
110: this .output = output;
111: this .data = new ByteArrayInputStream(input);
112: }
113:
114: public void run() {
115: try {
116: IOUtils.copy(data, output);
117: } catch (IOException e) {
118: e.printStackTrace();
119: } finally {
120: closeQuietly(output);
121: }
122: }
123: }
124:
125: public static class Result {
126: private final String stderr;
127: private final String stdout;
128: private final int exitCode;
129: private final String[] cmd;
130:
131: private Result(String[] cmd, String stdout, String stderr,
132: int exitCode) {
133: this .cmd = cmd;
134: this .stdout = stdout;
135: this .stderr = stderr;
136: this .exitCode = exitCode;
137: }
138:
139: public String getStderr() {
140: return stderr;
141: }
142:
143: public String getStdout() {
144: return stdout;
145: }
146:
147: public int getExitCode() {
148: return exitCode;
149: }
150:
151: public String toString() {
152: return "Command: " + Arrays.asList(cmd) + "\n"
153: + "exit code: " + exitCode + "\n" + "stdout: "
154: + stdout + "\n" + "stderr: " + stderr + "\n";
155: }
156:
157: }
158:
159: }
|