KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > ProcessStreamResult


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.ProcessStreamResult
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.harness;
23
24 import java.io.*;
25 import java.sql.Timestamp JavaDoc;
26
27
28 public class ProcessStreamResult implements Runnable JavaDoc
29 {
30
31     protected InputStream in;
32     protected OutputStreamWriter outStream;
33     // Encoding to be used to read output of test jvm process
34
protected String JavaDoc encoding;
35
36     /**
37      * Flag to find out if the work was finished
38      * successfully without being interrupted
39      * in between because of a timeout setting
40      */

41     protected boolean finished;
42     protected IOException ioe;
43     protected Thread JavaDoc myThread;
44     protected long startTime;
45     
46     /**
47      * Flag to keep state of whether the myThread has timed out.
48      * When interrupted is true, the myThread will exit
49      * from its work.
50      */

51     protected boolean interrupted;
52     
53     /**
54      * time in minutes for myThread to timeout in case it
55      * has not finished its work before that.
56      * timeout handling only comes into effect only when Wait()
57      * is called.
58      */

59     protected int timeout;
60
61     public ProcessStreamResult(InputStream in, BufferedOutputStream bos,
62             String JavaDoc timemin) throws IOException, InterruptedException JavaDoc
63     {
64         this(in, bos, timemin, null, null);
65     }
66
67     public ProcessStreamResult(InputStream in, BufferedOutputStream bos,
68       String JavaDoc timemin, String JavaDoc inEncoding, String JavaDoc outEncoding)
69         throws IOException, InterruptedException JavaDoc
70     {
71         this.in = in;
72         if (outEncoding == null) {
73             this.outStream = new OutputStreamWriter(bos);
74         } else {
75             this.outStream = new OutputStreamWriter(bos, outEncoding);
76         }
77         this.encoding = inEncoding;
78         this.startTime = System.currentTimeMillis();
79         if (timemin != null)
80         {
81             Integer JavaDoc i = new Integer JavaDoc(timemin);
82             timeout = i.intValue();
83         }
84         else
85             timeout = 0;
86         myThread = new Thread JavaDoc(this);
87         myThread.setPriority(Thread.MIN_PRIORITY);
88         myThread.start();
89     }
90
91     public void run()
92     {
93         //System.out.println("Thread run... " + tname);
94
if ( in == null )
95         {
96             System.out.println("The inputstream is null");
97             System.exit(1);
98         }
99         
100         try
101         {
102             char[] ca = new char[1024];
103             int valid;
104             interrupted = false;
105             
106             // Create an InputStreamReader with encoding, if specified.
107
// Otherwise, use default.
108
InputStreamReader inStream;
109             if(encoding != null)
110                 inStream = new InputStreamReader(in, encoding);
111             else
112                 inStream = new InputStreamReader(in);
113             
114             // keep reading from the stream as long as we have not
115
// timed out
116
while (((valid = inStream.read(ca, 0, ca.length)) != -1) &&
117                     !interrupted)
118             {
119                 //System.out.println("Still reading thread: " + tname);
120
/* if (timeout > 0) {
121                     long millis = System.currentTimeMillis();
122
123                     long diff = millis - startTime;
124
125                     int mins = (int) (diff / (1000 * 60));
126
127                     if (mins > timeout) {
128                         System.out.println("Timeout, kill the thread... ");
129                         //myThread.dumpStack();
130                         synchronized (this)
131                         {
132                             interrupted = true;
133                             finished = true;
134                             notifyAll();
135                             return;
136                         }
137                     }
138                 }
139 */
outStream.write(ca, 0, valid);
140                 outStream.flush();
141             }
142         }
143         catch (IOException ioe)
144         {
145             //System.out.println(ioe);
146
//ioe.printStackTrace();
147
}
148
149         // if we timed out, then just leave
150
if ( interrupted )
151             return;
152         
153         synchronized (this)
154         {
155             // successfully finished the work, notifyAll and leave.
156
finished = true;
157             notifyAll();
158         }
159     }
160
161     /**
162      * Wait till the myThread has finished its work or incase a timeout was set on this
163      * object, then to set a flag to indicate the myThread to leave at the end of the
164      * timeout period.
165      *
166      * Behavior is as follows:
167      * 1) If timeout is set to a valid value (>0) - in this case, if myThread has not
168      * finished its work by the time this method was called, then it will wait
169      * till the timeout has elapsed or if the myThread has finished its work.
170      *
171      * 2)If timeout is not set ( <= 0) - in this case, if myThread has not
172      * finished its work by the time this method was called, then it will wait
173      * till myThread has finished its work.
174      *
175      * If timeout is set to a valid value, and the timeout amount of time has elapsed,
176      * then the interrupted flag is set to true to indicate that it is time for the
177      * myThread to stop its work and leave.
178      *
179      * @return true if the timeout happened before myThread work was finished
180      * else false
181      * @throws IOException
182      */

183     public boolean Wait() throws IOException
184     {
185         synchronized(this)
186         {
187             // It is possible that we have finished the work
188
// by the time this method Wait() was called,
189
// so need to check if that is the case, before we
190
// go into a wait.
191
if ( finished )
192                 return interrupted;
193             
194             if (timeout > 0) {
195                 long millis = System.currentTimeMillis();
196
197                 long diff = millis - startTime;
198
199                 int mins = (int) (diff / (1000 * 60));
200
201                 if (mins > timeout)
202                 {
203                     interrupted = true;
204                     return interrupted;
205                 }
206             }
207             try
208             {
209                 // find timeout in milliseconds
210
long timeoutms = timeout * 60 *1000L;
211                 
212                 if ( timeout > 0 )
213                     // wait till notified or till timeoutms has elapsed
214
wait(timeoutms);
215                 else
216                     wait(); // wait till notified
217

218                 // if myThread didnt finish its work and we reached
219
// here, that means we just timedout.
220
// In that case, indicate that we were interrupted and leave.
221
// myThread will read the value of interrupted and
222
// stop its work and leave.
223
if ( !finished )
224                     interrupted = true;
225             }
226             catch (InterruptedException JavaDoc ie)
227             {
228                 interrupted = true;
229                 System.out.println("Interrupted: " + ie.toString());
230             }
231         }
232         return interrupted;
233     }
234 }
235
Popular Tags