Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/drivers/ObservedTask.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package com.netscape.javascript.qa.drivers;
6
7 import java.io.*;
8
9 /**
10 * This class is simple utility class that is used to run a process
11 * which expects no user input. ObserverdTask stores the exit status of
12 * the process along with standard output and error.
13 *
14 * This class is used by the harness only when testing the C version
15 * of the JavaScript Engine. It is not used by Rhino (JavaScript in
16 * Java).
17 *
18 * @author [email protected]
19 * @author Nick Lerissa
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 * Execute the process and return when the process is complete.
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 // unfortunately the following pause seems to
77 // need to be here otherwise we get a crash
78
79 // On AIX, Process.waitFor() doesn't seem to wait for
80 // the process to complete before continuing. Bad.
81 // Need to find a workaround.
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 * Simple print method used for debugging
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 * Simple pause method used for debugging.
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 * main used for debugging.
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 * A simple class that reads the contents of and InputStreamReader
139 * This class is used to read the output and error streams of the process.
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 }