1 package uk.ac.vamsas.client.simpleclient;
\r
3 import java.util.Iterator;
\r
4 import java.util.Vector;
\r
6 import org.apache.commons.logging.Log;
\r
7 import org.apache.commons.logging.LogFactory;
\r
10 * Watches a bunch of VamsasFile states, calling
\r
11 * the associated event handler when anything changes.
\r
15 public class VamsasFileWatcherThread extends Thread {
\r
16 private Log log = LogFactory.getLog(VamsasFileWatcherThread.class);
\r
18 * @see java.lang.Thread#run()
\r
20 EventGeneratorThread client=null;
\r
21 private Vector elements=null;
\r
22 public VamsasFileWatcherThread(EventGeneratorThread client) {
\r
23 this.client = client;
\r
24 elements=new Vector();
\r
26 public void addElement(WatcherElement welement) {
\r
27 elements.addElement(welement);
\r
29 public void removeElemenet(WatcherElement welement) {
\r
30 elements.removeElement(welement);
\r
32 public void clearElements() {
\r
36 * true if the thread is running
\r
38 boolean running=false;
\r
40 * true if the watcher loop is in progress
\r
42 boolean watching=false;
\r
43 public void haltWatchers() {
\r
47 // wait arount for WATCH_SLEEP milliseconds before returning
\r
48 // in the hope that the watcher loop has stopped
\r
51 long time = System.currentTimeMillis()+WATCH_SLEEP;
\r
52 while (running && time>System.currentTimeMillis()) {
\r
55 } catch (Exception e) {};
\r
57 log.warn("haltWatchers returning whilst thread is still running.");
\r
61 * time between checks for changes of state on the file
\r
63 public int WATCH_SLEEP=30;
\r
65 * check each watcher in sequence, monitoring any events generated.
\r
66 * Then wait WATCH_SLEEP milliseconds before checking all again (if there were no events)
\r
71 log.debug("Starting WatcherThread poll loop");
\r
74 Iterator watchers=elements.iterator();
\r
75 while (watching && watchers.hasNext()) {
\r
76 WatcherElement watch = (WatcherElement) watchers.next();
\r
77 if (watch.doWatch()) {
\r
79 log.debug("Event generated for watcher on "+watch.getWatcher().getSubject());
\r
82 if (watching && wait) {
\r
84 Thread.sleep(WATCH_SLEEP);
\r
86 catch (InterruptedException e) {};
\r
89 log.debug("Finishing WatcherThread poll loop");
\r
93 * @see java.lang.Thread#interrupt()
\r
95 public void interrupt() {
\r
96 // TODO: make thread gracefully interrupt watchers so that any handlers finish doing what they were doing
\r
97 // super.interrupt();
\r