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 private 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 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
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 watchForChange=true;
\r
59 if (log.isDebugEnabled())
\r
60 log.debug("enableWatch returning on "+getSubject());
\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
67 public boolean doWatch() {
\r
68 if (!watchForChange || handler==null)
\r
71 initWatch(); // somehow not done the first time
\r
72 handlerCalled=false;
\r
75 doclock=watcher.getChangedState();
\r
76 } catch (Exception e) {
\r
77 log.error("Whilst watching "+watcher.getSubject(), e);
\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
90 handlerCalled=false;
\r
95 * @return the handler
\r
97 public WatcherCallBack getHandler() {
\r
101 * @return the handlerCalled
\r
103 public boolean isHandlerCalled() {
\r
104 return handlerCalled;
\r
108 * @return true if watcher is enabled
\r
110 public boolean isWatchEnabled() {
\r
111 return watchForChange;
\r
114 * @param handler the handler to set
\r
116 public void setHandler(WatcherCallBack handler) {
\r
117 this.handler = handler;
\r
120 * @return the watcher
\r
122 public FileWatcher getWatcher() {
\r