1 package uk.ac.vamsas.client.simpleclient;
\r
5 public abstract class WatcherElement {
\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
11 * set this to false to stop the thread
\r
13 protected boolean watchForChange = true;
\r
15 * true when the handler is being called for this watcher
\r
17 protected boolean handlerCalled = false;
\r
19 public WatcherElement(WatcherCallBack handler) {
\r
20 this.handler = handler;
\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
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
32 if (log.isDebugEnabled())
\r
33 log.debug("haltWatch completed on "+watcher.getSubject());
\r
36 * called by haltWatch before
\r
37 * clearing the FileWatcher reference.
\r
40 protected abstract void endWatch();
\r
42 * called to generate the watcher object
\r
43 * by enableWatch and in doWatch
\r
46 protected abstract void initWatch();
\r
48 * implemented for debug information purposes.
\r
49 * @return Informative string about what the watcher is watching
\r
51 protected abstract String getSubject();
\r
53 * must be called by implementations of
\r
56 protected void enableWatch() {
\r
57 if (log.isDebugEnabled())
\r
58 log.debug("enableWatch on "+getSubject());
\r
59 watchForChange=true;
\r
61 if (log.isDebugEnabled())
\r
62 log.debug("enableWatch returning on "+getSubject());
\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
69 public boolean doWatch() {
\r
70 if (!watchForChange || handler==null)
\r
73 initWatch(); // somehow not done the first time
\r
74 handlerCalled=false;
\r
77 doclock=watcher.getChangedState();
\r
78 } catch (Exception e) {
\r
79 log.error("Whilst watching "+watcher.getSubject(), e);
\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
92 handlerCalled=false;*/
\r
93 this.callHandler(doclock);
\r
98 * Calls the current eventhandler
\r
100 * @param doclock the lock on the watch file
\r
102 protected void callHandler(Lock doclock)
\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
112 handlerCalled=false;
\r
116 * @return the handler
\r
118 public WatcherCallBack getHandler() {
\r
122 * @return the handlerCalled
\r
124 public boolean isHandlerCalled() {
\r
125 return handlerCalled;
\r
129 * @return true if watcher is enabled
\r
131 public boolean isWatchEnabled() {
\r
132 return watchForChange;
\r
135 * @param handler the handler to set
\r
137 public void setHandler(WatcherCallBack handler) {
\r
138 this.handler = handler;
\r
141 * @return the watcher
\r
143 public FileWatcher getWatcher() {
\r