1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.util.TimeoutObserver;
|
---|
22 | import org.apache.tools.ant.util.Watchdog;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Destroys a process running for too long.
|
---|
26 | * For example:
|
---|
27 | * <pre>
|
---|
28 | * ExecuteWatchdog watchdog = new ExecuteWatchdog(30000);
|
---|
29 | * Execute exec = new Execute(myloghandler, watchdog);
|
---|
30 | * exec.setCommandLine(mycmdline);
|
---|
31 | * int exitvalue = exec.execute();
|
---|
32 | * if (Execute.isFailure(exitvalue) && watchdog.killedProcess()) {
|
---|
33 | * // it was killed on purpose by the watchdog
|
---|
34 | * }
|
---|
35 | * </pre>
|
---|
36 |
|
---|
37 | * @see Execute
|
---|
38 | * @see org.apache.tools.ant.util.Watchdog
|
---|
39 | * @since Ant 1.2
|
---|
40 | */
|
---|
41 | public class ExecuteWatchdog implements TimeoutObserver {
|
---|
42 |
|
---|
43 | /** the process to execute and watch for duration */
|
---|
44 | private Process process;
|
---|
45 |
|
---|
46 | /** say whether or not the watchdog is currently monitoring a process */
|
---|
47 | private boolean watch = false;
|
---|
48 |
|
---|
49 | /** exception that might be thrown during the process execution */
|
---|
50 | private Exception caught = null;
|
---|
51 |
|
---|
52 | /** say whether or not the process was killed due to running overtime */
|
---|
53 | private boolean killedProcess = false;
|
---|
54 |
|
---|
55 | /** will tell us whether timeout has occurred */
|
---|
56 | private Watchdog watchdog;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Creates a new watchdog with a given timeout.
|
---|
60 | *
|
---|
61 | * @param timeout the timeout for the process in milliseconds.
|
---|
62 | * It must be greater than 0.
|
---|
63 | */
|
---|
64 | public ExecuteWatchdog(long timeout) {
|
---|
65 | watchdog = new Watchdog(timeout);
|
---|
66 | watchdog.addTimeoutObserver(this);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @see #ExecuteWatchdog(long)
|
---|
71 | * @deprecated Use constructor with a long type instead.
|
---|
72 | * (1.4.x compatibility)
|
---|
73 | */
|
---|
74 | public ExecuteWatchdog(int timeout) {
|
---|
75 | this((long) timeout);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Watches the given process and terminates it, if it runs for too long.
|
---|
80 | * All information from the previous run are reset.
|
---|
81 | * @param process the process to monitor. It cannot be <tt>null</tt>
|
---|
82 | * @throws IllegalStateException if a process is still being monitored.
|
---|
83 | */
|
---|
84 | public synchronized void start(Process process) {
|
---|
85 | if (process == null) {
|
---|
86 | throw new NullPointerException("process is null.");
|
---|
87 | }
|
---|
88 | if (this.process != null) {
|
---|
89 | throw new IllegalStateException("Already running.");
|
---|
90 | }
|
---|
91 | this.caught = null;
|
---|
92 | this.killedProcess = false;
|
---|
93 | this.watch = true;
|
---|
94 | this.process = process;
|
---|
95 | watchdog.start();
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Stops the watcher. It will notify all threads possibly waiting
|
---|
100 | * on this object.
|
---|
101 | */
|
---|
102 | public synchronized void stop() {
|
---|
103 | watchdog.stop();
|
---|
104 | watch = false;
|
---|
105 | process = null;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Called after watchdog has finished.
|
---|
110 | */
|
---|
111 | public void timeoutOccured(Watchdog w) {
|
---|
112 | try {
|
---|
113 | try {
|
---|
114 | // We must check if the process was not stopped
|
---|
115 | // before being here
|
---|
116 | process.exitValue();
|
---|
117 | } catch (IllegalThreadStateException itse) {
|
---|
118 | // the process is not terminated, if this is really
|
---|
119 | // a timeout and not a manual stop then kill it.
|
---|
120 | if (watch) {
|
---|
121 | killedProcess = true;
|
---|
122 | process.destroy();
|
---|
123 | }
|
---|
124 | }
|
---|
125 | } catch (Exception e) {
|
---|
126 | caught = e;
|
---|
127 | } finally {
|
---|
128 | cleanUp();
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * reset the monitor flag and the process.
|
---|
134 | */
|
---|
135 | protected void cleanUp() {
|
---|
136 | watch = false;
|
---|
137 | process = null;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * This method will rethrow the exception that was possibly caught during
|
---|
142 | * the run of the process. It will only remains valid once the process has
|
---|
143 | * been terminated either by 'error', timeout or manual intervention.
|
---|
144 | * Information will be discarded once a new process is ran.
|
---|
145 | * @throws BuildException a wrapped exception over the one that was
|
---|
146 | * silently swallowed and stored during the process run.
|
---|
147 | */
|
---|
148 | public void checkException() throws BuildException {
|
---|
149 | if (caught != null) {
|
---|
150 | throw new BuildException("Exception in ExecuteWatchdog.run: "
|
---|
151 | + caught.getMessage(), caught);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Indicates whether or not the watchdog is still monitoring the process.
|
---|
157 | * @return <tt>true</tt> if the process is still running, otherwise
|
---|
158 | * <tt>false</tt>.
|
---|
159 | */
|
---|
160 | public boolean isWatching() {
|
---|
161 | return watch;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Indicates whether the last process run was killed on timeout or not.
|
---|
166 | * @return <tt>true</tt> if the process was killed otherwise
|
---|
167 | * <tt>false</tt>.
|
---|
168 | */
|
---|
169 | public boolean killedProcess() {
|
---|
170 | return killedProcess;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|