+package uk.ac.vamsas.client.simpleclient;\r
+\r
+\r
+\r
+public abstract class WatcherElement {\r
+\r
+ private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(VamsasFileWatcherElement.class);\r
+ protected FileWatcher watcher = null;\r
+ protected WatcherCallBack handler = null;\r
+ /**\r
+ * set this to false to stop the thread\r
+ */\r
+ private boolean watchForChange = true;\r
+ /**\r
+ * true when the handler is being called for this watcher\r
+ */\r
+ protected boolean handlerCalled = false;\r
+\r
+ public WatcherElement(WatcherCallBack handler) {\r
+ this.handler = handler;\r
+ }\r
+ /**\r
+ * will instruct watcher to stop and wait around for one WATCH_SLEEP\r
+ * before returning. If no thread is running then it returns immediately.\r
+ */\r
+ public void haltWatch() {\r
+ if (log.isDebugEnabled())\r
+ log.debug("haltWatch on "+watcher.getSubject());\r
+ // set the flag to skip this watch element.\r
+ watchForChange=false;\r
+ endWatch();\r
+ // watcher=null;\r
+ \r
+ }\r
+ /**\r
+ * called by haltWatch before \r
+ * clearing the FileWatcher reference.\r
+ *\r
+ */\r
+ protected abstract void endWatch();\r
+ /**\r
+ * called to generate the watcher object\r
+ * by enableWatch and in doWatch\r
+ *\r
+ */\r
+ protected abstract void initWatch();\r
+ /**\r
+ * implemented for debug information purposes.\r
+ * @return Informative string about what the watcher is watching\r
+ */\r
+ protected abstract String getSubject();\r
+ /**\r
+ * must be called by implementations of \r
+ * enablewatch\r
+ */\r
+ protected void enableWatch() {\r
+ watchForChange=true;\r
+ initWatch();\r
+ if (log.isDebugEnabled())\r
+ log.debug("enableWatch returning on "+getSubject());\r
+ }\r
+\r
+ /**\r
+ * Originally from the uk.ac.vamsas.test.simpleclient.ArchiveClient method\r
+ * @return true if the handler was called for a changeEvent\r
+ */\r
+ public boolean doWatch() {\r
+ if (!watchForChange || handler==null)\r
+ return false;\r
+ if (watcher==null)\r
+ initWatch(); // somehow not done the first time\r
+ handlerCalled=false;\r
+ Lock doclock=null;\r
+ try {\r
+ doclock=watcher.getChangedState();\r
+ } catch (Exception e) {\r
+ log.error("Whilst watching "+watcher.getSubject(), e);\r
+ }\r
+ if (doclock==null)\r
+ return false;\r
+ handlerCalled=true;\r
+ if (log.isDebugEnabled())\r
+ log.debug("Triggering watchEvent for change on "+watcher.getSubject());\r
+ boolean finish=!handler.handleWatchEvent(this, doclock);\r
+ doclock=null; // TODO: check that lock should really be released rather than dereferenced\r
+ if (finish)\r
+ haltWatch();\r
+ else\r
+ enableWatch();\r
+ handlerCalled=false;\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * @return the handler\r
+ */\r
+ public WatcherCallBack getHandler() {\r
+ return handler;\r
+ }\r
+ /**\r
+ * @return the handlerCalled\r
+ */\r
+ public boolean isHandlerCalled() {\r
+ return handlerCalled;\r
+ }\r
+ /**\r
+ * \r
+ * @return true if watcher is enabled\r
+ */\r
+ public boolean isWatchEnabled() {\r
+ return watchForChange;\r
+ }\r
+ /**\r
+ * @param handler the handler to set\r
+ */\r
+ public void setHandler(WatcherCallBack handler) {\r
+ this.handler = handler;\r
+ }\r
+ /**\r
+ * @return the watcher\r
+ */\r
+ public FileWatcher getWatcher() {\r
+ return watcher;\r
+ }\r
+}\r