b3235d54af0f3fb0f5cf534cb04a2dd39f88a2a2
[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     // set the flag to skip this watch element.\r
28     watchForChange=false;\r
29     if (log.isDebugEnabled())\r
30       log.debug("haltWatch on "+watcher.getSubject());\r
31     endWatch();\r
32     if (log.isDebugEnabled())\r
33       log.debug("haltWatch completed on "+watcher.getSubject());\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     if (log.isDebugEnabled())\r
58       log.debug("enableWatch on "+getSubject());\r
59     watchForChange=true;\r
60     initWatch();\r
61     if (log.isDebugEnabled())\r
62       log.debug("enableWatch returning on "+getSubject());\r
63   }\r
64 \r
65   /**\r
66    * Originally from the uk.ac.vamsas.test.simpleclient.ArchiveClient method\r
67    * @return true if the handler was called for a changeEvent\r
68    */\r
69   public boolean doWatch() {\r
70     if (!watchForChange || handler==null)\r
71       return false;\r
72     if (watcher==null)\r
73       initWatch(); // somehow not done the first time\r
74     handlerCalled=false;\r
75     Lock doclock=null;\r
76     try {\r
77       doclock=watcher.getChangedState();\r
78     } catch (Exception e) {\r
79       log.error("Whilst watching "+watcher.getSubject(), e);\r
80     }\r
81     if (doclock==null)\r
82       return false;\r
83    /* handlerCalled=true;\r
84     if (log.isDebugEnabled())\r
85       log.debug("Triggering watchEvent for change on "+watcher.getSubject());\r
86       boolean finish=!handler.handleWatchEvent(this, doclock);\r
87     doclock=null; // TODO: check that lock should really be released rather than dereferenced\r
88     if (finish)\r
89       haltWatch();\r
90     else\r
91       enableWatch();\r
92     handlerCalled=false;*/\r
93     this.callHandler(doclock);\r
94     return true;\r
95   }\r
96 \r
97   /**\r
98    * Calls the current eventhandler\r
99    * \r
100    * @param doclock the lock on the watch file \r
101    */\r
102   protected void callHandler(Lock doclock)\r
103   {\r
104       if (log.isDebugEnabled())\r
105         log.debug("Triggering watchEvent for change on "+watcher.getSubject());\r
106       boolean finish=!handler.handleWatchEvent(this, doclock);\r
107       doclock=null; // TODO: check that lock should really be released rather than dereferenced\r
108       if (finish)\r
109         haltWatch();\r
110       else\r
111         enableWatch();\r
112       handlerCalled=false;\r
113   }\r
114   \r
115   /**\r
116    * @return the handler\r
117    */\r
118   public WatcherCallBack getHandler() {\r
119     return handler;\r
120   }\r
121   /**\r
122    * @return the handlerCalled\r
123    */\r
124   public boolean isHandlerCalled() {\r
125     return handlerCalled;\r
126   }\r
127   /**\r
128    * \r
129    * @return true if watcher is enabled\r
130    */\r
131   public boolean isWatchEnabled() {\r
132     return watchForChange;\r
133   }\r
134   /**\r
135    * @param handler the handler to set\r
136    */\r
137   public void setHandler(WatcherCallBack handler) {\r
138     this.handler = handler;\r
139   }\r
140   /**\r
141    * @return the watcher\r
142    */\r
143   public FileWatcher getWatcher() {\r
144     return watcher;\r
145   }\r
146 }\r