better command line processing options (load, save) and slower update poll check.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / WatcherElement.java
1 package uk.ac.vamsas.client.simpleclient;\r
2 \r
3 \r
4 \r
5 public abstract class WatcherElement {\r
6 \r
7   private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(VamsasFileWatcherElement.class);\r
8   protected FileWatcher watcher = null;\r
9   protected WatcherCallBack handler = null;\r
10   /**\r
11    * set this to false to stop the thread\r
12    */\r
13   private boolean watchForChange = true;\r
14   /**\r
15    * true when the handler is being called for this watcher\r
16    */\r
17   protected boolean handlerCalled = false;\r
18 \r
19   public WatcherElement(WatcherCallBack handler) {\r
20     this.handler = handler;\r
21   }\r
22   /**\r
23    * will instruct watcher to stop and wait around for one WATCH_SLEEP\r
24    * before returning. If no thread is running then it returns immediately.\r
25    */\r
26   public void haltWatch() {\r
27     if (log.isDebugEnabled())\r
28       log.debug("haltWatch on "+watcher.getSubject());\r
29     // set the flag to skip this watch element.\r
30     watchForChange=false;\r
31     endWatch();\r
32     // watcher=null;\r
33     \r
34   }\r
35   /**\r
36    * called by haltWatch before \r
37    * clearing the FileWatcher reference.\r
38    *\r
39    */\r
40   protected abstract void endWatch();\r
41   /**\r
42    * called to generate the watcher object\r
43    * by enableWatch and in doWatch\r
44    *\r
45    */\r
46   protected abstract void initWatch();\r
47   /**\r
48    * implemented for debug information purposes.\r
49    * @return Informative string about what the watcher is watching\r
50    */\r
51   protected abstract String getSubject();\r
52   /**\r
53    * must be called by implementations of \r
54    * enablewatch\r
55    */\r
56   protected void enableWatch() {\r
57     watchForChange=true;\r
58     initWatch();\r
59     if (log.isDebugEnabled())\r
60       log.debug("enableWatch returning on "+getSubject());\r
61   }\r
62 \r
63   /**\r
64    * Originally from the uk.ac.vamsas.test.simpleclient.ArchiveClient method\r
65    * @return true if the handler was called for a changeEvent\r
66    */\r
67   public boolean doWatch() {\r
68     if (!watchForChange || handler==null)\r
69       return false;\r
70     if (watcher==null)\r
71       initWatch(); // somehow not done the first time\r
72     handlerCalled=false;\r
73     Lock doclock=null;\r
74     try {\r
75       doclock=watcher.getChangedState();\r
76     } catch (Exception e) {\r
77       log.error("Whilst watching "+watcher.getSubject(), e);\r
78     }\r
79     if (doclock==null)\r
80       return false;\r
81     handlerCalled=true;\r
82     if (log.isDebugEnabled())\r
83       log.debug("Triggering watchEvent for change on "+watcher.getSubject());\r
84       boolean finish=!handler.handleWatchEvent(this, doclock);\r
85     doclock=null; // TODO: check that lock should really be released rather than dereferenced\r
86     if (finish)\r
87       haltWatch();\r
88     else\r
89       enableWatch();\r
90     handlerCalled=false;\r
91     return true;\r
92   }\r
93 \r
94   /**\r
95    * @return the handler\r
96    */\r
97   public WatcherCallBack getHandler() {\r
98     return handler;\r
99   }\r
100   /**\r
101    * @return the handlerCalled\r
102    */\r
103   public boolean isHandlerCalled() {\r
104     return handlerCalled;\r
105   }\r
106   /**\r
107    * \r
108    * @return true if watcher is enabled\r
109    */\r
110   public boolean isWatchEnabled() {\r
111     return watchForChange;\r
112   }\r
113   /**\r
114    * @param handler the handler to set\r
115    */\r
116   public void setHandler(WatcherCallBack handler) {\r
117     this.handler = handler;\r
118   }\r
119   /**\r
120    * @return the watcher\r
121    */\r
122   public FileWatcher getWatcher() {\r
123     return watcher;\r
124   }\r
125 }\r