applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / VamsasFileWatcherThread.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.client.simpleclient;\r
23 \r
24 import java.util.Iterator;\r
25 import java.util.Vector;\r
26 \r
27 import org.apache.commons.logging.Log;\r
28 import org.apache.commons.logging.LogFactory;\r
29 \r
30 /**\r
31  * Watches a bunch of VamsasFile states, calling the associated event handler\r
32  * when anything changes.\r
33  * \r
34  * @author JimP\r
35  * \r
36  */\r
37 public class VamsasFileWatcherThread extends Thread {\r
38   private Log log = LogFactory.getLog(VamsasFileWatcherThread.class);\r
39 \r
40   /*\r
41    * (non-Javadoc)\r
42    * \r
43    * @see java.lang.Thread#run()\r
44    */\r
45   EventGeneratorThread client = null;\r
46 \r
47   private Vector elements = null;\r
48 \r
49   public VamsasFileWatcherThread(EventGeneratorThread client) {\r
50     this.client = client;\r
51     elements = new Vector();\r
52   }\r
53 \r
54   public void addElement(WatcherElement welement) {\r
55     elements.addElement(welement);\r
56   }\r
57 \r
58   public void removeElemenet(WatcherElement welement) {\r
59     elements.removeElement(welement);\r
60   }\r
61 \r
62   public void clearElements() {\r
63     elements.clear();\r
64   }\r
65 \r
66   /**\r
67    * true if the thread is running\r
68    */\r
69   boolean running = false;\r
70 \r
71   /**\r
72    * true if the watcher loop is in progress\r
73    */\r
74   boolean watching = false;\r
75 \r
76   public void haltWatchers() {\r
77     if (!watching)\r
78       return;\r
79     watching = false;\r
80     // wait arount for WATCH_SLEEP milliseconds before returning\r
81     // in the hope that the watcher loop has stopped\r
82     try {\r
83       interrupt();\r
84       long time = System.currentTimeMillis() + WATCH_SLEEP;\r
85       while (running && time > System.currentTimeMillis()) {\r
86         Thread.sleep(1);\r
87       }\r
88     } catch (Exception e) {\r
89     }\r
90     ;\r
91     if (running)\r
92       log.warn("haltWatchers returning whilst thread is still running.");\r
93   }\r
94 \r
95   /**\r
96    * time between checks for changes of state on the file\r
97    */\r
98   public int WATCH_SLEEP = 30;\r
99 \r
100   /**\r
101    * check each watcher in sequence, monitoring any events generated. Then wait\r
102    * WATCH_SLEEP milliseconds before checking all again (if there were no\r
103    * events)\r
104    */\r
105   public void run() {\r
106     running = true;\r
107     watching = true;\r
108     log.debug("Starting WatcherThread poll loop");\r
109     while (watching) {\r
110       boolean wait = true;\r
111       Iterator watchers = elements.iterator();\r
112       while (watching && watchers.hasNext()) {\r
113         WatcherElement watch = (WatcherElement) watchers.next();\r
114         if (watch.doWatch()) {\r
115           wait = false;\r
116           log.debug("Event generated for watcher on "\r
117               + watch.getWatcher().getSubject());\r
118         }\r
119       }\r
120       if (watching && wait) {\r
121         try {\r
122           Thread.sleep(WATCH_SLEEP);\r
123         } catch (InterruptedException e) {\r
124         }\r
125         ;\r
126       }\r
127     }\r
128     log.debug("Finishing WatcherThread poll loop");\r
129     running = false;\r
130   }\r
131 \r
132   /*\r
133    * (non-Javadoc)\r
134    * \r
135    * @see java.lang.Thread#interrupt()\r
136    */\r
137   public void interrupt() {\r
138     // TODO: make thread gracefully interrupt watchers so that any handlers\r
139     // finish doing what they were doing\r
140     // super.interrupt();\r
141   }\r
142 \r
143 }\r