You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExecuteWatchdog.java 5.9 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. package org.apache.tools.ant.taskdefs;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.util.TimeoutObserver;
  21. import org.apache.tools.ant.util.Watchdog;
  22. /**
  23. * Destroys a process running for too long.
  24. * For example:
  25. * <pre>
  26. * ExecuteWatchdog watchdog = new ExecuteWatchdog(30000);
  27. * Execute exec = new Execute(myloghandler, watchdog);
  28. * exec.setCommandLine(mycmdline);
  29. * int exitvalue = exec.execute();
  30. * if (Execute.isFailure(exitvalue) &amp;&amp; watchdog.killedProcess()) {
  31. * // it was killed on purpose by the watchdog
  32. * }
  33. * </pre>
  34. * @see Execute
  35. * @see org.apache.tools.ant.util.Watchdog
  36. * @since Ant 1.2
  37. */
  38. public class ExecuteWatchdog implements TimeoutObserver {
  39. /** the process to execute and watch for duration */
  40. private Process process;
  41. /** say whether or not the watchdog is currently monitoring a process */
  42. private volatile boolean watch = false;
  43. /** exception that might be thrown during the process execution */
  44. private Exception caught = null;
  45. /** say whether or not the process was killed due to running overtime */
  46. private volatile boolean killedProcess = false;
  47. /** will tell us whether timeout has occurred */
  48. private Watchdog watchdog;
  49. /**
  50. * Creates a new watchdog with a given timeout.
  51. *
  52. * @param timeout the timeout for the process in milliseconds.
  53. * It must be greater than 0.
  54. */
  55. public ExecuteWatchdog(long timeout) {
  56. watchdog = new Watchdog(timeout);
  57. watchdog.addTimeoutObserver(this);
  58. }
  59. /**
  60. * @param timeout the timeout value to use in milliseconds.
  61. * @see #ExecuteWatchdog(long)
  62. * @deprecated since 1.5.x.
  63. * Use constructor with a long type instead.
  64. * (1.4.x compatibility)
  65. */
  66. @Deprecated
  67. public ExecuteWatchdog(int timeout) {
  68. this((long) timeout);
  69. }
  70. /**
  71. * Watches the given process and terminates it, if it runs for too long.
  72. * All information from the previous run are reset.
  73. * @param process the process to monitor. It cannot be <code>null</code>
  74. * @throws IllegalStateException if a process is still being monitored.
  75. */
  76. public synchronized void start(Process process) {
  77. if (process == null) {
  78. throw new NullPointerException("process is null.");
  79. }
  80. if (this.process != null) {
  81. throw new IllegalStateException("Already running.");
  82. }
  83. this.caught = null;
  84. this.killedProcess = false;
  85. this.watch = true;
  86. this.process = process;
  87. watchdog.start();
  88. }
  89. /**
  90. * Stops the watcher. It will notify all threads possibly waiting
  91. * on this object.
  92. */
  93. public synchronized void stop() {
  94. watchdog.stop();
  95. cleanUp();
  96. }
  97. /**
  98. * Called after watchdog has finished.
  99. * This can be called in the watchdog thread
  100. * @param w the watchdog
  101. */
  102. @Override
  103. public synchronized void timeoutOccured(Watchdog w) {
  104. try {
  105. try {
  106. // We must check if the process was not stopped
  107. // before being here
  108. process.exitValue();
  109. } catch (IllegalThreadStateException itse) {
  110. // the process is not terminated, if this is really
  111. // a timeout and not a manual stop then kill it.
  112. if (watch) {
  113. killedProcess = true;
  114. process.destroy();
  115. }
  116. }
  117. } catch (Exception e) {
  118. caught = e;
  119. } finally {
  120. cleanUp();
  121. }
  122. }
  123. /**
  124. * reset the monitor flag and the process.
  125. */
  126. protected synchronized void cleanUp() {
  127. watch = false;
  128. process = null;
  129. }
  130. /**
  131. * This method will rethrow the exception that was possibly caught during
  132. * the run of the process. It will only remains valid once the process has
  133. * been terminated either by 'error', timeout or manual intervention.
  134. * Information will be discarded once a new process is ran.
  135. * @throws BuildException a wrapped exception over the one that was
  136. * silently swallowed and stored during the process run.
  137. */
  138. public synchronized void checkException() throws BuildException {
  139. if (caught != null) {
  140. throw new BuildException("Exception in ExecuteWatchdog.run: "
  141. + caught.getMessage(), caught);
  142. }
  143. }
  144. /**
  145. * Indicates whether or not the watchdog is still monitoring the process.
  146. * @return <code>true</code> if the process is still running, otherwise
  147. * <code>false</code>.
  148. */
  149. public boolean isWatching() {
  150. return watch;
  151. }
  152. /**
  153. * Indicates whether the last process run was killed on timeout or not.
  154. * @return <code>true</code> if the process was killed otherwise
  155. * <code>false</code>.
  156. */
  157. public boolean killedProcess() {
  158. return killedProcess;
  159. }
  160. }