56a04400542a251ed128faa458b260310bcd9e67
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / VamsasFileWatcherThread.java
1 package uk.ac.vamsas.client.simpleclient;\r
2 \r
3 import java.util.Iterator;\r
4 import java.util.Vector;\r
5 \r
6 import org.apache.commons.logging.Log;\r
7 import org.apache.commons.logging.LogFactory;\r
8 \r
9 /**\r
10  * Watches a bunch of VamsasFile states, calling \r
11  * the associated event handler when anything changes.\r
12  * @author JimP\r
13  *\r
14  */\r
15 public class VamsasFileWatcherThread extends Thread {\r
16   private Log log = LogFactory.getLog(VamsasFileWatcherThread.class);  \r
17   /* (non-Javadoc)\r
18    * @see java.lang.Thread#run()\r
19    */\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
25   }\r
26   public void addElement(WatcherElement welement) {\r
27     elements.addElement(welement);\r
28   }\r
29   public void removeElemenet(WatcherElement welement) {\r
30     elements.removeElement(welement);\r
31   }\r
32   public void clearElements() {\r
33     elements.clear();\r
34   }\r
35   /**\r
36    * true if the thread is running\r
37    */\r
38   boolean running=false;\r
39   /**\r
40    * true if the watcher loop is in progress\r
41    */\r
42   boolean watching=false;\r
43   public void haltWatchers() {\r
44     if (!watching)\r
45       return;\r
46     watching=false;\r
47     // wait arount for WATCH_SLEEP milliseconds before returning\r
48     // in the hope that the watcher loop has stopped\r
49     try {\r
50       interrupt();\r
51       long time = System.currentTimeMillis()+WATCH_SLEEP;\r
52       while (running && time>System.currentTimeMillis()) {\r
53         Thread.sleep(1);\r
54       }\r
55     } catch (Exception e) {};\r
56     if (running)\r
57       log.warn("haltWatchers returning whilst thread is still running.");\r
58   }\r
59   \r
60   /**\r
61    * time between checks for changes of state on the file\r
62    */\r
63   public int WATCH_SLEEP=30; \r
64   /**\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
67    */\r
68   public void run() {\r
69     running=true;\r
70     watching=true;\r
71     log.debug("Starting WatcherThread poll loop");\r
72     while (watching) {\r
73       boolean wait=true;\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
78           wait=false;\r
79           log.debug("Event generated for watcher on "+watch.getWatcher().getSubject());\r
80         }\r
81       }\r
82       if (watching && wait) {\r
83         try {\r
84           Thread.sleep(WATCH_SLEEP);\r
85         }\r
86         catch (InterruptedException e) {};\r
87       }\r
88     }\r
89     log.debug("Finishing WatcherThread poll loop");\r
90     running=false;\r
91   }\r
92   /* (non-Javadoc)\r
93    * @see java.lang.Thread#interrupt()\r
94    */\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
98   }\r
99   \r
100 }\r