KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > Watchdog


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.util;
20
21 import java.util.Enumeration JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 /**
25  * Generalization of <code>ExecuteWatchdog</code>
26  *
27  * @since Ant 1.5
28  *
29  * @see org.apache.tools.ant.taskdefs.ExecuteWatchdog
30  *
31  */

32 public class Watchdog implements Runnable JavaDoc {
33
34     private Vector JavaDoc observers = new Vector JavaDoc(1);
35     private long timeout = -1;
36     /**
37      * marked as volatile to stop the compiler caching values or (in java1.5+,
38      * reordering access)
39      */

40     private volatile boolean stopped = false;
41     /**
42      * Error string.
43      * {@value}
44      */

45     public static final String JavaDoc ERROR_INVALID_TIMEOUT = "timeout less than 1.";
46
47     /**
48      * Constructor for Watchdog.
49      * @param timeout the timeout to use in milliseconds (must be >= 1).
50      */

51     public Watchdog(long timeout) {
52         if (timeout < 1) {
53             throw new IllegalArgumentException JavaDoc(ERROR_INVALID_TIMEOUT);
54         }
55         this.timeout = timeout;
56     }
57
58     /**
59      * Add a timeout observer.
60      * @param to the timeout observer to add.
61      */

62     public void addTimeoutObserver(TimeoutObserver to) {
63         //no need to synchronize, as Vector is always synchronized
64
observers.addElement(to);
65     }
66
67     /**
68      * Remove a timeout observer.
69      * @param to the timeout observer to remove.
70      */

71     public void removeTimeoutObserver(TimeoutObserver to) {
72         //no need to synchronize, as Vector is always synchronized
73
observers.removeElement(to);
74     }
75
76     /**
77      * Inform the observers that a timeout has occurred.
78      * This happens in the watchdog thread.
79      */

80     protected final void fireTimeoutOccured() {
81         Enumeration JavaDoc e = observers.elements();
82         while (e.hasMoreElements()) {
83             ((TimeoutObserver) e.nextElement()).timeoutOccured(this);
84         }
85     }
86
87     /**
88      * Start the watch dog.
89      */

90     public synchronized void start() {
91         stopped = false;
92         Thread JavaDoc t = new Thread JavaDoc(this, "WATCHDOG");
93         t.setDaemon(true);
94         t.start();
95     }
96
97     /**
98      * Stop the watch dog.
99      */

100     public synchronized void stop() {
101         stopped = true;
102         notifyAll();
103     }
104
105     /**
106      * The run method of the watch dog thread.
107      * This simply does a wait for the timeout time, and
108      * if the stop flag has not been set when the wait has returned or
109      * has been interrupted, the watch dog listeners are informed.
110      */

111     public synchronized void run() {
112         final long until = System.currentTimeMillis() + timeout;
113         long now;
114         while (!stopped && until > (now = System.currentTimeMillis())) {
115             try {
116                 wait(until - now);
117             } catch (InterruptedException JavaDoc e) {
118                 // Ignore exception
119
}
120         }
121         if (!stopped) {
122             fireTimeoutOccured();
123         }
124     }
125
126 }
127
Popular Tags