1 18 package org.apache.geronimo.interop.util; 19 20 import java.io.BufferedInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.File ; 23 import java.io.InputStream ; 24 import java.io.PrintStream ; 25 26 import org.apache.geronimo.interop.SystemException; 27 28 29 public class ProcessUtil 30 { 32 39 40 42 public static ProcessUtil getInstance() { 43 53 54 return new ProcessUtil(); 55 } 56 57 59 private String _cmd; 60 61 private boolean _echo; 62 63 private PrintStream _echoStream; 64 65 private int _exitValue; 66 67 private byte[] _errorBytes; 68 69 private byte[] _inputBytes; 70 71 73 protected ProcessUtil() { 74 } 77 78 80 public void setEcho(boolean echo) { 81 _echo = echo; 82 if (_echo) { 83 _echoStream = System.out; 84 } else { 85 _echoStream = null; 86 } 87 } 88 89 public void setEcho(PrintStream stream) { 90 _echo = stream != null; 91 _echoStream = stream; 92 } 93 94 public void run(String cmd) { 95 run(cmd, null, null); 96 } 97 98 public void run(String cmd, String [] env, String dir) { 99 _cmd = cmd; 100 Process process; 101 try { 102 if (_echo) { 103 _echoStream.println(cmd); 104 } 105 File dirFile = dir == null ? null : new File (dir); 106 process = Runtime.getRuntime().exec(cmd, env, dirFile); 107 } catch (Exception ex) { 108 throw new SystemException(ex); 109 } 110 run(process); 111 } 112 113 public void run(String [] cmd, String [] env, String dir) { 114 _cmd = cmd.length == 0 ? "" : cmd[0]; 115 for (int i = 1; i < cmd.length; i++) { 116 _cmd += " " + cmd[i]; 117 } 118 Process process; 119 try { 120 if (_echo) { 121 _echoStream.println(cmd); 122 } 123 File dirFile = dir == null ? null : new File (dir); 124 process = Runtime.getRuntime().exec(cmd, env, dirFile); 125 } catch (Exception ex) { 126 throw new SystemException(ex); 127 } 128 run(process); 129 } 130 131 public void run(Process process) { 132 try { 133 GetBytesThread errorThread = new GetBytesThread(process.getErrorStream()); 134 GetBytesThread inputThread = new GetBytesThread(process.getInputStream()); 135 errorThread.start(); 136 inputThread.start(); 137 process.waitFor(); 138 errorThread.join(); 139 inputThread.join(); 140 _errorBytes = errorThread.getBytes(); 141 _inputBytes = inputThread.getBytes(); 142 _exitValue = process.exitValue(); 143 } catch (Exception ex) { 144 throw new SystemException(ex); 145 } 146 } 147 148 public void checkStatus() { 149 if (_exitValue != 0) { 150 String result = getResult(); 152 throw new SystemException("Command Failed: " + _cmd 153 + "\nExit Status: " + _exitValue 154 + (result.length() == 0 ? "" : ("\nOutput: " + getResult()))); 155 } 156 } 157 158 public int exitValue() { 159 return _exitValue; 160 } 161 162 public String getResult() { 163 return new String (getResultBytes()); 164 } 165 166 public String getError() { 167 return new String (getErrorBytes()); 168 } 169 170 public String getInput() { 171 return new String (getInputBytes()); 172 } 173 174 public byte[] getResultBytes() { 175 byte[] bytes = new byte[_errorBytes.length + _inputBytes.length]; 176 System.arraycopy(_errorBytes, 0, bytes, 0, _errorBytes.length); 177 System.arraycopy(_inputBytes, 0, bytes, _errorBytes.length, _inputBytes.length); 178 return bytes; 179 } 180 181 public byte[] getErrorBytes() { 182 return _errorBytes; 183 } 184 185 public byte[] getInputBytes() { 186 return _inputBytes; 187 } 188 189 private class GetBytesThread extends Thread { 190 InputStream _input; 191 192 ByteArrayOutputStream _bytes; 193 194 GetBytesThread(InputStream input) { 195 _input = new BufferedInputStream (input); 196 _bytes = new ByteArrayOutputStream (); 197 setDaemon(true); 198 } 199 200 public void run() { 201 try { 202 int c; 203 while ((c = _input.read()) != -1) { 204 _bytes.write(c); 205 if (_echo) { 206 _echoStream.print((char) c); 207 if (c == '\n') { 208 _echoStream.flush(); 209 } 210 } 211 } 212 } catch (Exception ex) { 213 throw new SystemException(ex); 214 } 215 } 216 217 public byte[] getBytes() { 218 return _bytes.toByteArray(); 219 } 220 } 221 } 222 | Popular Tags |