KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > ExecuteWatchdog


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

18
19 package org.apache.tools.ant.taskdefs;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.util.TimeoutObserver;
23 import org.apache.tools.ant.util.Watchdog;
24
25 /**
26  * Destroys a process running for too long.
27  * For example:
28  * <pre>
29  * ExecuteWatchdog watchdog = new ExecuteWatchdog(30000);
30  * Execute exec = new Execute(myloghandler, watchdog);
31  * exec.setCommandLine(mycmdline);
32  * int exitvalue = exec.execute();
33  * if (Execute.isFailure(exitvalue) &amp;&amp; watchdog.killedProcess()) {
34  * // it was killed on purpose by the watchdog
35  * }
36  * </pre>
37
38  * @see Execute
39  * @see org.apache.tools.ant.util.Watchdog
40  * @since Ant 1.2
41  */

42 public class ExecuteWatchdog implements TimeoutObserver {
43
44     /** the process to execute and watch for duration */
45     private Process JavaDoc process;
46
47     /** say whether or not the watchdog is currently monitoring a process */
48     private volatile boolean watch = false;
49
50     /** exception that might be thrown during the process execution */
51     private Exception JavaDoc caught = null;
52
53     /** say whether or not the process was killed due to running overtime */
54     private volatile boolean killedProcess = false;
55
56     /** will tell us whether timeout has occurred */
57     private Watchdog watchdog;
58
59     /**
60      * Creates a new watchdog with a given timeout.
61      *
62      * @param timeout the timeout for the process in milliseconds.
63      * It must be greater than 0.
64      */

65     public ExecuteWatchdog(long timeout) {
66         watchdog = new Watchdog(timeout);
67         watchdog.addTimeoutObserver(this);
68     }
69
70     /**
71      * @param timeout the timeout value to use in milliseconds.
72      * @see #ExecuteWatchdog(long)
73      * @deprecated since 1.5.x.
74      * Use constructor with a long type instead.
75      * (1.4.x compatibility)
76      */

77     public ExecuteWatchdog(int timeout) {
78         this((long) timeout);
79     }
80
81     /**
82      * Watches the given process and terminates it, if it runs for too long.
83      * All information from the previous run are reset.
84      * @param process the process to monitor. It cannot be <tt>null</tt>
85      * @throws IllegalStateException if a process is still being monitored.
86      */

87     public synchronized void start(Process JavaDoc process) {
88         if (process == null) {
89             throw new NullPointerException JavaDoc("process is null.");
90         }
91         if (this.process != null) {
92             throw new IllegalStateException JavaDoc("Already running.");
93         }
94         this.caught = null;
95         this.killedProcess = false;
96         this.watch = true;
97         this.process = process;
98         watchdog.start();
99     }
100
101     /**
102      * Stops the watcher. It will notify all threads possibly waiting
103      * on this object.
104      */

105     public synchronized void stop() {
106         watchdog.stop();
107         cleanUp();
108     }
109
110     /**
111      * Called after watchdog has finished.
112      * This can be called in the watchdog thread
113      * @param w the watchdog
114      */

115     public synchronized void timeoutOccured(Watchdog w) {
116         try {
117             try {
118                 // We must check if the process was not stopped
119
// before being here
120
process.exitValue();
121             } catch (IllegalThreadStateException JavaDoc itse) {
122                 // the process is not terminated, if this is really
123
// a timeout and not a manual stop then kill it.
124
if (watch) {
125                     killedProcess = true;
126                     process.destroy();
127                 }
128             }
129         } catch (Exception JavaDoc e) {
130             caught = e;
131         } finally {
132             cleanUp();
133         }
134     }
135
136     /**
137      * reset the monitor flag and the process.
138      */

139     protected synchronized void cleanUp() {
140         watch = false;
141         process = null;
142     }
143
144     /**
145      * This method will rethrow the exception that was possibly caught during
146      * the run of the process. It will only remains valid once the process has
147      * been terminated either by 'error', timeout or manual intervention.
148      * Information will be discarded once a new process is ran.
149      * @throws BuildException a wrapped exception over the one that was
150      * silently swallowed and stored during the process run.
151      */

152     public synchronized void checkException() throws BuildException {
153         if (caught != null) {
154             throw new BuildException("Exception in ExecuteWatchdog.run: "
155                                      + caught.getMessage(), caught);
156         }
157     }
158
159     /**
160      * Indicates whether or not the watchdog is still monitoring the process.
161      * @return <tt>true</tt> if the process is still running, otherwise
162      * <tt>false</tt>.
163      */

164     public boolean isWatching() {
165         return watch;
166     }
167
168     /**
169      * Indicates whether the last process run was killed on timeout or not.
170      * @return <tt>true</tt> if the process was killed otherwise
171      * <tt>false</tt>.
172      */

173     public boolean killedProcess() {
174         return killedProcess;
175     }
176 }
177
178
Popular Tags