1 30 31 32 package org.hsqldb.test; 33 34 import java.io.BufferedInputStream ; 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FileNotFoundException ; 38 import java.io.FileOutputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.io.OutputStream ; 42 import java.util.List ; 43 44 46 72 public class ExecHarness { 73 74 81 private static final String SYNTAX_MSG = 82 "SYNTAX: java org.hsqldb.test.ExecHarness targetprogram [args...]"; 83 private static final int MAX_PROG_OUTPUT = 10240; 84 85 96 public static void main(String [] sa) 97 throws IOException , FileNotFoundException , InterruptedException { 98 99 byte[] localBa = new byte[10240]; 100 101 if (sa.length < 1) { 102 System.err.println(SYNTAX_MSG); 103 System.exit(1); 104 } 105 106 String progname = sa[0]; 107 108 System.err.println( 109 "Enter any input that you want passed to SqlTool via stdin\n" 110 + "(end with EOF, like Ctrl-D or Ctrl-Z+ENTER):"); 111 112 File tmpFile = File.createTempFile("ExecHarness-", ".input"); 113 String specifiedCharSet = System.getProperty("harness.charset"); 114 String charset = ((specifiedCharSet == null) ? DEFAULT_CHARSET 115 : specifiedCharSet); 116 FileOutputStream fos = new FileOutputStream (tmpFile); 117 int i; 118 119 while ((i = System.in.read(localBa)) > 0) { 120 fos.write(localBa, 0, i); 121 } 122 123 fos.close(); 124 125 ExecHarness harness = new ExecHarness(progname); 126 127 harness.setArgs(shift(sa)); 128 harness.setInput(tmpFile); 129 harness.exec(); 130 tmpFile.delete(); 131 132 int retval = harness.getExitValue(); 133 134 System.err.println( 135 "STDOUT ******************************************"); 136 System.out.print(harness.getStdout()); 137 System.err.println( 138 "ERROUT ******************************************"); 139 System.err.print(harness.getErrout()); 140 System.err.println( 141 "*************************************************"); 142 System.err.println(progname + " exited with value " + retval); 143 harness.clear(); 144 System.exit(retval); 145 } 146 147 File input = null; 148 String program = null; 149 int exitValue = 0; 150 boolean executed = false; 151 152 String [] mtStringArray = {}; 154 String [] args = mtStringArray; 155 156 private byte[] ba = new byte[MAX_PROG_OUTPUT + 1]; 159 private String stdout = null; 160 private String errout = null; 161 private static final String DEFAULT_CHARSET = "US-ASCII"; 162 163 178 public void exec() throws IOException , InterruptedException { 179 180 InputStream stream; 181 int i; 182 int writePointer; 183 184 if (executed) { 185 throw new IllegalStateException ("You have already executed '" 186 + program + "'. Run clear()."); 187 } 188 189 Process proc = Runtime.getRuntime().exec(unshift(program, args)); 190 OutputStream outputStream = proc.getOutputStream(); 191 192 if (input != null) { 193 BufferedInputStream bis = 194 new BufferedInputStream (new FileInputStream (input)); 195 196 while ((i = bis.read(ba)) > 0) { 197 outputStream.write(ba, 0, i); 198 } 199 } 200 201 outputStream.close(); 202 203 stream = proc.getInputStream(); 204 writePointer = 0; 205 206 while ((i = stream.read(ba, writePointer, ba.length - writePointer)) 207 > 0) { 208 writePointer += i; 209 } 210 211 if (i > -1) { 212 throw new IOException (program + " generated > " + (ba.length - 1) 213 + " bytes of standard output"); 214 } 215 216 stream.close(); 217 218 executed = true; stdout = new String (ba, 0, writePointer); 220 stream = proc.getErrorStream(); 221 writePointer = 0; 222 223 while ((i = stream.read(ba, writePointer, ba.length - writePointer)) 224 > 0) { 225 writePointer += i; 226 } 227 228 if (i > -1) { 229 throw new IOException (program + " generated > " + (ba.length - 1) 230 + " bytes of error output"); 231 } 232 233 stream.close(); 234 235 errout = new String (ba, 0, writePointer); 236 exitValue = proc.waitFor(); 237 } 238 239 243 public void clear() { 244 245 args = mtStringArray; 247 executed = false; 248 stdout = errout = null; 249 input = null; 250 } 251 252 public String getStdout() { 253 return stdout; 254 } 255 256 public String getErrout() { 257 return errout; 258 } 259 260 263 public void setInput(File inFile) throws IllegalStateException { 264 265 if (executed) { 266 throw new IllegalStateException ("You have already executed '" 267 + program + "'. Run clear()."); 268 } 269 270 input = inFile; 271 } 272 273 public void setArgs(String [] inArgs) throws IllegalStateException { 274 275 if (executed) { 276 throw new IllegalStateException ("You have already executed '" 277 + program + "'. Run clear()."); 278 } 279 280 args = inArgs; 281 } 282 283 public void setArgs(List list) throws IllegalStateException { 284 setArgs(listToPrimitiveArray(list)); 285 } 286 287 int getExitValue() throws IllegalStateException { 288 289 if (!executed) { 290 throw new IllegalStateException ("You have not executed '" 291 + program + "' yet"); 292 } 293 294 return exitValue; 295 } 296 297 302 public ExecHarness(String inName) { 303 program = inName; 304 } 305 306 309 public static String [] unshift(String newHead, String [] saIn) { 310 311 String [] saOut = new String [saIn.length + 1]; 312 313 saOut[0] = newHead; 314 315 for (int i = 1; i < saOut.length; i++) { 316 saOut[i] = saIn[i - 1]; 317 } 318 319 return saOut; 320 } 321 322 public static String [] shift(String [] saIn) { 323 324 String [] saOut = new String [saIn.length - 1]; 325 326 for (int i = 0; i < saOut.length; i++) { 327 saOut[i] = saIn[i + 1]; 328 } 329 330 return saOut; 331 } 332 333 public static String [] listToPrimitiveArray(List list) { 334 335 String [] saOut = new String [list.size()]; 336 337 for (int i = 0; i < list.size(); i++) { 338 saOut[i] = (String ) list.get(i); 339 } 340 341 return saOut; 342 } 343 344 public static String [] push(String newTail, String [] saIn) { 345 346 String [] saOut = new String [saIn.length + 1]; 347 348 for (int i = 0; i < saIn.length; i++) { 349 saOut[i] = saIn[i]; 350 } 351 352 saOut[saOut.length - 1] = newTail; 353 354 return saOut; 355 } 356 357 public static String [] pop(String [] saIn) { 358 359 String [] saOut = new String [saIn.length - 1]; 360 361 for (int i = 0; i < saOut.length; i++) { 362 saOut[i] = saIn[i]; 363 } 364 365 return saOut; 366 } 367 368 public static String stringArrayToString(String [] sa) { 369 370 StringBuffer sb = new StringBuffer ("{"); 371 372 for (int i = 0; i < sa.length; i++) { 373 if (i > 0) { 374 sb.append(','); 375 } 376 377 sb.append(sa[i]); 378 } 379 380 return sb.toString() + '}'; 381 } 382 } 383 | Popular Tags |