Added support for crashed client.
[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   protected 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     this.callHandler(doclock);\r
92     return true;\r
93   }\r
94 \r
95   /**\r
96    * Calls the current eventhandler\r
97    * \r
98    * @param doclock the lock on the watch file \r
99    */\r
100   protected void callHandler(Lock doclock)\r
101   {\r
102       if (log.isDebugEnabled())\r
103         log.debug("Triggering watchEvent for change on "+watcher.getSubject());\r
104       boolean finish=!handler.handleWatchEvent(this, doclock);\r
105       doclock=null; // TODO: check that lock should really be released rather than dereferenced\r
106       if (finish)\r
107         haltWatch();\r
108       else\r
109         enableWatch();\r
110       handlerCalled=false;\r
111   }\r
112   \r
113   /**\r
114    * @return the handler\r
115    */\r
116   public WatcherCallBack getHandler() {\r
117     return handler;\r
118   }\r
119   /**\r
120    * @return the handlerCalled\r
121    */\r
122   public boolean isHandlerCalled() {\r
123     return handlerCalled;\r
124   }\r
125   /**\r
126    * \r
127    * @return true if watcher is enabled\r
128    */\r
129   public boolean isWatchEnabled() {\r
130     return watchForChange;\r
131   }\r
132   /**\r
133    * @param handler the handler to set\r
134    */\r
135   public void setHandler(WatcherCallBack handler) {\r
136     this.handler = handler;\r
137   }\r
138   /**\r
139    * @return the watcher\r
140    */\r
141   public FileWatcher getWatcher() {\r
142     return watcher;\r
143   }\r
144 }\r