KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.TimedProcess
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 /**
25  * This class is a wrapper of Process to provide a waitFor() method
26  * that forcibly terminates the process if it does not
27  * complete within the specified time.
28  *
29  * @author Phil Lopez
30  *
31  */

32 public class TimedProcess
33 {
34
35   private Process JavaDoc process;
36
37   public TimedProcess(Process JavaDoc process)
38   {
39     this.process = process;
40   }
41
42   public int waitFor(int sec)
43   {
44     int exitValue = -1;
45
46     // Create a thread to wait for the process to die
47
WaitForProcess t = new WaitForProcess(process);
48     t.start();
49     
50     // Give the process sec seconds to terminate
51
try
52     {
53       t.join(sec * 1000);
54
55       // Otherwise, interrupt the thread...
56
if (t.isAlive())
57       {
58         t.interrupt();
59         
60         System.err.println("Server Process did not complete in time. Destroying...");
61         // ...and destroy the process with gusto
62
process.destroy();
63       }
64       else
65       {
66         // process shut down, so it is right to get the exit value from it
67
exitValue = t.getProcessExitValue();
68       }
69     }
70     catch (InterruptedException JavaDoc e)
71     {
72       e.printStackTrace();
73     }
74   
75     return exitValue;
76   }
77 } // public class TimedProcess
78

79
80 class WaitForProcess
81   extends Thread JavaDoc
82 {
83   private Process JavaDoc process;
84   private int processExitValue;
85   
86   public WaitForProcess(Process JavaDoc process)
87   {
88     this.process = process;
89   }
90
91   public int getProcessExitValue()
92   {
93     return processExitValue;
94   }
95
96   public void run()
97   {
98     // Our whole goal in life here is to waitFor() the process.
99
// However, we're actually going to catch the InterruptedException for it!
100
try
101     {
102       processExitValue = process.waitFor();
103     }
104     catch (InterruptedException JavaDoc e)
105     {
106       // Don't do anything here; the thread will die of natural causes
107
}
108   }
109 } // class WaitForProcess
110

111
Popular Tags