1 5 package com.tc.process; 6 7 import org.apache.commons.io.IOUtils; 8 9 import java.io.ByteArrayInputStream ; 10 import java.io.File ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.OutputStream ; 15 import java.util.Arrays ; 16 17 public class Exec { 18 19 public static Result execute(String cmd[]) throws Exception { 20 return execute(cmd, null, null, null); 21 } 22 23 public static Result execute(String cmd[], String outputLog) throws Exception { 24 return execute(cmd, outputLog, null, null); 25 } 26 27 public static Result execute(String cmd[], String outputLog, byte[] input) throws Exception { 28 return execute(cmd, outputLog, input, null); 29 } 30 31 public static Result execute(String cmd[], String outputLog, byte[] input, File workingDir) throws Exception { 32 Process process = Runtime.getRuntime().exec(cmd, null, workingDir); 33 34 Thread inputThread = new InputPumper(input == null ? new byte[] {} : input, process.getOutputStream()); 35 36 StreamCollector stderr = null; 37 StreamCollector stdout = null; 38 39 FileOutputStream fileOutput = null; 40 StreamAppender outputLogger = null; 41 42 String errString = null; 43 String outString = null; 44 45 try { 46 if (outputLog != null) { 47 errString = "stderr output redirected to file " + outputLog; 48 outString = "stdout output redirected to file " + outputLog; 49 fileOutput = new FileOutputStream (outputLog); 50 outputLogger = new StreamAppender(fileOutput); 51 outputLogger.writeInput(process.getErrorStream(), process.getInputStream()); 52 } else { 53 stderr = new StreamCollector(process.getErrorStream()); 54 stdout = new StreamCollector(process.getInputStream()); 55 stderr.start(); 56 stdout.start(); 57 } 58 59 inputThread.start(); 60 61 final int exitCode = process.waitFor(); 62 63 if (inputThread != null) inputThread.join(); 64 65 if (outputLogger != null) { 66 outputLogger.finish(); 67 } 68 69 if (stderr != null) { 70 stderr.join(); 71 errString = stderr.toString(); 72 } 73 74 if (stdout != null) { 75 stdout.join(); 76 outString = stdout.toString(); 77 } 78 79 return new Result(cmd, outString, errString, exitCode); 80 } finally { 81 closeQuietly(fileOutput); 82 } 83 } 84 85 private static void closeQuietly(OutputStream output) { 86 if (output != null) { 87 try { 88 output.close(); 89 } catch (IOException ioe) { 90 } 92 } 93 } 94 95 private static class InputPumper extends Thread { 96 private final InputStream data; 97 private final OutputStream output; 98 99 InputPumper(byte[] input, OutputStream output) { 100 this.output = output; 101 this.data = new ByteArrayInputStream (input); 102 } 103 104 public void run() { 105 try { 106 IOUtils.copy(data, output); 107 } catch (IOException e) { 108 e.printStackTrace(); 109 } finally { 110 closeQuietly(output); 111 } 112 } 113 } 114 115 public static class Result { 116 private final String stderr; 117 private final String stdout; 118 private final int exitCode; 119 private final String [] cmd; 120 121 private Result(String [] cmd, String stdout, String stderr, int exitCode) { 122 this.cmd = cmd; 123 this.stdout = stdout; 124 this.stderr = stderr; 125 this.exitCode = exitCode; 126 } 127 128 public String getStderr() { 129 return stderr; 130 } 131 132 public String getStdout() { 133 return stdout; 134 } 135 136 public int getExitCode() { 137 return exitCode; 138 } 139 140 public String toString() { 141 return "Command: " + Arrays.asList(cmd) + "\n" + "exit code: " + exitCode + "\n" + "stdout: " + stdout + "\n" 142 + "stderr: " + stderr + "\n"; 143 } 144 145 } 146 147 } 148 | Popular Tags |