| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package com.netscape.javascript.qa.drivers; |
| 6 |
|
| 7 |
import java.io.*; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class ObservedTask { |
| 23 |
public String commandLine; |
| 24 |
|
| 25 |
public StringBuffer input = new StringBuffer(); |
| 26 |
public StringBuffer error = new StringBuffer(); |
| 27 |
int exitValue; |
| 28 |
|
| 29 |
public Object observer; |
| 30 |
|
| 31 |
public ObservedTask(String cl, Object observer) { |
| 32 |
this.commandLine = cl; |
| 33 |
this.observer = observer; |
| 34 |
} |
| 35 |
|
| 36 |
public StringBuffer getInput() { |
| 37 |
return input; |
| 38 |
} |
| 39 |
|
| 40 |
public StringBuffer getError() { |
| 41 |
return error; |
| 42 |
} |
| 43 |
|
| 44 |
int getExitValue() { |
| 45 |
return exitValue; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public void exec() throws IOException { |
| 52 |
Runtime rt = Runtime.getRuntime(); |
| 53 |
|
| 54 |
try { |
| 55 |
Process proc = rt.exec(commandLine); |
| 56 |
OutputStream os = rt.getLocalizedOutputStream(proc.getOutputStream()); |
| 57 |
|
| 58 |
if ( this.observer instanceof RefEnv ) { |
| 59 |
os.write("quit();\n".getBytes()); |
| 60 |
} |
| 61 |
|
| 62 |
os.flush(); |
| 63 |
os.close(); |
| 64 |
|
| 65 |
InputStreamReader is; |
| 66 |
|
| 67 |
is = new InputStreamReader(proc.getErrorStream()); |
| 68 |
(new Thread(new StreamReader(error,is))).start(); |
| 69 |
|
| 70 |
is = new InputStreamReader(proc.getInputStream()); |
| 71 |
(new Thread(new StreamReader(input,is))).start(); |
| 72 |
|
| 73 |
proc.waitFor(); |
| 74 |
exitValue = proc.exitValue(); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
if ( System.getProperty("os.name").startsWith("AIX") || |
| 84 |
System.getProperty("os.name").startsWith("HP") ) { |
| 85 |
pause(20000); |
| 86 |
} else { |
| 87 |
pause( 10000 ); |
| 88 |
} |
| 89 |
|
| 90 |
} catch ( Exception e ) { |
| 91 |
java.lang.System.out.println( e ); |
| 92 |
e.printStackTrace(); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public void print() { |
| 100 |
System.out.println("Input Stream of Process:"); |
| 101 |
System.out.println(input); |
| 102 |
System.out.println("Error Stream of Process:"); |
| 103 |
System.out.println(error); |
| 104 |
System.out.println("Exit Value of Process: " + exitValue); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static void pause(int length) { |
| 111 |
try { |
| 112 |
Thread.currentThread().sleep(length); |
| 113 |
} catch (InterruptedException ex) { |
| 114 |
System.err.println( ex ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static public void main(String args[]) { |
| 122 |
if (args.length < 1) { |
| 123 |
System.err.println("USAGE: java RunIt <command line>"); |
| 124 |
System.exit(1); |
| 125 |
} |
| 126 |
try { |
| 127 |
ObservedTask task = new ObservedTask(args[0], null); |
| 128 |
task.exec(); |
| 129 |
task.print(); |
| 130 |
pause(10000); |
| 131 |
} catch (Exception e) { |
| 132 |
System.err.println("ERROR Exception thrown: " + e); |
| 133 |
System.exit(2); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
class StreamReader implements Runnable { |
| 142 |
StringBuffer buffer; |
| 143 |
InputStreamReader inputStreamReader; |
| 144 |
|
| 145 |
StreamReader(StringBuffer b, InputStreamReader i) { |
| 146 |
buffer = b; |
| 147 |
inputStreamReader = i; |
| 148 |
} |
| 149 |
|
| 150 |
public void run() { |
| 151 |
try { |
| 152 |
int ch; |
| 153 |
while ((ch = inputStreamReader.read()) != -1) { |
| 154 |
buffer.append((char) ch); |
| 155 |
} |
| 156 |
} catch (IOException ex) { |
| 157 |
System.err.println("Error IOException thrown " + ex); |
| 158 |
ex.printStackTrace(); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |