source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/util/Watchdog.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 3.1 KB
Line 
1/*
2 * Copyright 2002-2005 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
18package org.apache.tools.ant.util;
19
20import java.util.Enumeration;
21import java.util.Vector;
22
23/**
24 * Generalization of <code>ExecuteWatchdog</code>
25 *
26 * @since Ant 1.5
27 *
28 * @see org.apache.tools.ant.taskdefs.ExecuteWatchdog
29 *
30 */
31public class Watchdog implements Runnable {
32
33 private Vector observers = new Vector(1);
34 private long timeout = -1;
35 private boolean stopped = false;
36
37 /**
38 * Constructor for Watchdog.
39 * @param timeout the timeout to use in milliseconds (must be >= 1).
40 */
41 public Watchdog(long timeout) {
42 if (timeout < 1) {
43 throw new IllegalArgumentException("timeout less than 1.");
44 }
45 this.timeout = timeout;
46 }
47
48 /**
49 * Add a timeout observer.
50 * @param to the timeout observer to add.
51 */
52 public void addTimeoutObserver(TimeoutObserver to) {
53 observers.addElement(to);
54 }
55
56 /**
57 * Remove a timeout observer.
58 * @param to the timeout observer to remove.
59 */
60 public void removeTimeoutObserver(TimeoutObserver to) {
61 observers.removeElement(to);
62 }
63
64 /**
65 * Inform the observers that a timeout has occured.
66 */
67 protected final void fireTimeoutOccured() {
68 Enumeration e = observers.elements();
69 while (e.hasMoreElements()) {
70 ((TimeoutObserver) e.nextElement()).timeoutOccured(this);
71 }
72 }
73
74 /**
75 * Start the watch dog.
76 */
77 public synchronized void start() {
78 stopped = false;
79 Thread t = new Thread(this, "WATCHDOG");
80 t.setDaemon(true);
81 t.start();
82 }
83
84 /**
85 * Stop the watch dog.
86 */
87 public synchronized void stop() {
88 stopped = true;
89 notifyAll();
90 }
91
92 /**
93 * The run method of the watch dog thread.
94 * This simply does a wait for the timeout time, and
95 * if the stop flag has not been set when the wait has returned or
96 * has been interrupted, the watch dog listeners are informed.
97 */
98 public synchronized void run() {
99 final long until = System.currentTimeMillis() + timeout;
100 long now;
101 while (!stopped && until > (now = System.currentTimeMillis())) {
102 try {
103 wait(until - now);
104 } catch (InterruptedException e) {
105 // Ignore exception
106 }
107 }
108 if (!stopped) {
109 fireTimeoutOccured();
110 }
111 }
112
113}
Note: See TracBrowser for help on using the repository browser.