1 19 20 package com.sslexplorer.boot; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.util.Vector ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 37 public class CommandRunner extends Thread { 38 final static Log log = LogFactory.getLog(CommandRunner.class); 39 40 42 private String [] arguments; 43 private StringBuffer outBuf = new StringBuffer (); 44 private Process process; 45 private boolean includeOutputOnException; 46 47 54 public CommandRunner(String [] arguments) { 55 this.arguments = arguments; 56 } 57 58 65 public CommandRunner(Vector arguments) { 66 this.arguments = new String [arguments.size()]; 67 arguments.copyInto(this.arguments); 68 } 69 70 75 public String getOutput() { 76 return outBuf.toString(); 77 } 78 79 86 public void runCommand() throws Exception { 87 try { 88 89 if (log.isDebugEnabled() || "true".equals(System.getProperty("sslexplorer.debugSystemCommands", "false"))) { 90 91 StringBuffer debug = new StringBuffer ("Running command '"); 92 for (int i = 0; i < arguments.length; i++) { 93 if (i > 0) { 94 debug.append(" "); 95 } 96 debug.append("\""); 97 debug.append(arguments[i]); 98 debug.append("\""); 99 } 100 101 if(log.isDebugEnabled()) { 102 log.debug(debug.toString()); 103 } 104 105 if("true".equals(System.getProperty("sslexplorer.debugSystemCommands", "false"))) { 106 log.error("CMD " + debug.toString()); 107 } 108 } 109 110 process = Runtime.getRuntime().exec(arguments); 111 InputStream in = process.getInputStream(); 112 start(); 113 BufferedReader reader = new BufferedReader (new InputStreamReader (in)); 114 String line = null; 115 while ((line = reader.readLine()) != null) { 116 if ("true".equals(System.getProperty("sslexplorer.debugSystemCommands", "false"))) { 117 log.error("STDOUT " + line); 118 } 119 synchronized (outBuf) { 120 if (outBuf.length() > 0) { 121 outBuf.append("\n"); 122 } 123 outBuf.append(line); 124 } 125 } 126 process.waitFor(); 127 if (process.exitValue() != 0) { 128 log.error("Command '" + arguments[0] + "' failed with exit code " + process.exitValue()); 129 log.error("Start of command output ---->"); 130 log.error(outBuf.toString()); 131 log.error("<---- End of command output"); 132 throw new Exception ("Command returned exit code " + process.exitValue() + ". " 133 + (includeOutputOnException ? outBuf.toString() : "Check the logs for more detail.")); 134 } 135 } finally { 136 } 137 } 138 139 145 public void setIncludeOutputOnException(boolean includeOutputOnException) { 146 this.includeOutputOnException = includeOutputOnException; 147 148 } 149 150 public void run() { 151 try { 152 BufferedReader reader = new BufferedReader (new InputStreamReader (process.getErrorStream())); 153 String line = null; 154 while ((line = reader.readLine()) != null) { 155 if ("true".equals(System.getProperty("sslexplorer.debugSystemCommands", "false"))) { 156 log.error("STDERR " + line); 157 } 158 synchronized (outBuf) { 159 if (outBuf.length() > 0) { 160 outBuf.append("\n"); 161 } 162 outBuf.append(line); 163 } 164 } 165 } catch (IOException ioe) { 166 } 167 } 168 } | Popular Tags |