1 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 42 public class ExecuteWatchdog implements TimeoutObserver { 43 44 45 private Process process; 46 47 48 private volatile boolean watch = false; 49 50 51 private Exception caught = null; 52 53 54 private volatile boolean killedProcess = false; 55 56 57 private Watchdog watchdog; 58 59 65 public ExecuteWatchdog(long timeout) { 66 watchdog = new Watchdog(timeout); 67 watchdog.addTimeoutObserver(this); 68 } 69 70 77 public ExecuteWatchdog(int timeout) { 78 this((long) timeout); 79 } 80 81 87 public synchronized void start(Process process) { 88 if (process == null) { 89 throw new NullPointerException ("process is null."); 90 } 91 if (this.process != null) { 92 throw new IllegalStateException ("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 105 public synchronized void stop() { 106 watchdog.stop(); 107 cleanUp(); 108 } 109 110 115 public synchronized void timeoutOccured(Watchdog w) { 116 try { 117 try { 118 process.exitValue(); 121 } catch (IllegalThreadStateException itse) { 122 if (watch) { 125 killedProcess = true; 126 process.destroy(); 127 } 128 } 129 } catch (Exception e) { 130 caught = e; 131 } finally { 132 cleanUp(); 133 } 134 } 135 136 139 protected synchronized void cleanUp() { 140 watch = false; 141 process = null; 142 } 143 144 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 164 public boolean isWatching() { 165 return watch; 166 } 167 168 173 public boolean killedProcess() { 174 return killedProcess; 175 } 176 } 177 178 | Popular Tags |