+/*\r
+ * This file is part of the Vamsas Client version 0.1. \r
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ * Andrew Waterhouse and Dominik Lindner.\r
+ * \r
+ * Earlier versions have also been incorporated into Jalview version 2.4 \r
+ * since 2008, and TOPALi version 2 since 2007.\r
+ * \r
+ * The Vamsas Client is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ * \r
+ * The Vamsas Client is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public License\r
+ * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
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
+ private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory\r
+ .getLog(VamsasFileWatcherElement.class);\r
+\r
protected FileWatcher watcher = null;\r
+\r
protected WatcherCallBack handler = null;\r
+\r
/**\r
* set this to false to stop the thread\r
*/\r
protected boolean watchForChange = true;\r
+\r
/**\r
* true when the handler is being called for this watcher\r
*/\r
public WatcherElement(WatcherCallBack handler) {\r
this.handler = handler;\r
}\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
+ * will instruct watcher to stop and wait around for one WATCH_SLEEP before\r
+ * returning. If no thread is running then it returns immediately.\r
*/\r
public void haltWatch() {\r
// set the flag to skip this watch element.\r
- watchForChange=false;\r
+ watchForChange = false;\r
if (log.isDebugEnabled())\r
- log.debug("haltWatch on "+watcher.getSubject());\r
+ log.debug("haltWatch on " + watcher.getSubject());\r
endWatch();\r
if (log.isDebugEnabled())\r
- log.debug("haltWatch completed on "+watcher.getSubject());\r
+ log.debug("haltWatch completed on " + watcher.getSubject());\r
}\r
+\r
/**\r
- * called by haltWatch before \r
- * clearing the FileWatcher reference.\r
- *\r
+ * called by haltWatch before clearing the FileWatcher reference.\r
+ * \r
*/\r
protected abstract void endWatch();\r
+\r
/**\r
- * called to generate the watcher object\r
- * by enableWatch and in doWatch\r
- *\r
+ * called to generate the watcher object by enableWatch and in doWatch\r
+ * \r
*/\r
protected abstract void initWatch();\r
+\r
/**\r
* implemented for debug information purposes.\r
+ * \r
* @return Informative string about what the watcher is watching\r
*/\r
protected abstract String getSubject();\r
+\r
/**\r
- * must be called by implementations of \r
- * enablewatch\r
+ * must be called by implementations of enablewatch\r
*/\r
protected void enableWatch() {\r
if (log.isDebugEnabled())\r
- log.debug("enableWatch on "+getSubject());\r
- watchForChange=true;\r
+ log.debug("enableWatch on " + getSubject());\r
+ watchForChange = true;\r
initWatch();\r
if (log.isDebugEnabled())\r
- log.debug("enableWatch returning on "+getSubject());\r
+ log.debug("enableWatch returning on " + getSubject());\r
}\r
\r
/**\r
* Originally from the uk.ac.vamsas.test.simpleclient.ArchiveClient method\r
+ * \r
* @return true if the handler was called for a changeEvent\r
*/\r
public boolean doWatch() {\r
- if (!watchForChange || handler==null)\r
+ if (!watchForChange || handler == null)\r
return false;\r
- if (watcher==null)\r
+ if (watcher == null)\r
initWatch(); // somehow not done the first time\r
- handlerCalled=false;\r
- Lock doclock=null;\r
+ handlerCalled = false;\r
+ Lock doclock = null;\r
try {\r
- doclock=watcher.getChangedState();\r
+ doclock = watcher.getChangedState();\r
} catch (Exception e) {\r
- log.error("Whilst watching "+watcher.getSubject(), e);\r
+ log.error("Whilst watching " + watcher.getSubject(), e);\r
}\r
- if (doclock==null)\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
+ /*\r
+ * handlerCalled=true; if (log.isDebugEnabled())\r
+ * log.debug("Triggering watchEvent for change on "+watcher.getSubject());\r
+ * boolean finish=!handler.handleWatchEvent(this, doclock); doclock=null; //\r
+ * TODO: check that lock should really be released rather than dereferenced\r
+ * if (finish) haltWatch(); else enableWatch(); handlerCalled=false;\r
+ */\r
this.callHandler(doclock);\r
return true;\r
}\r
/**\r
* Calls the current eventhandler\r
* \r
- * @param doclock the lock on the watch file \r
+ * @param doclock\r
+ * the lock on the watch file\r
*/\r
- protected void callHandler(Lock doclock)\r
- {\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
+ protected void callHandler(Lock doclock) {\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\r
+ // than dereferenced\r
+ if (finish)\r
+ haltWatch();\r
+ else\r
+ enableWatch();\r
+ handlerCalled = false;\r
}\r
- \r
+\r
/**\r
* @return the handler\r
*/\r
public WatcherCallBack getHandler() {\r
return handler;\r
}\r
+\r
/**\r
* @return the handlerCalled\r
*/\r
public boolean isHandlerCalled() {\r
return handlerCalled;\r
}\r
+\r
/**\r
* \r
* @return true if watcher is enabled\r
public boolean isWatchEnabled() {\r
return watchForChange;\r
}\r
+\r
/**\r
- * @param handler the handler to set\r
+ * @param handler\r
+ * the handler to set\r
*/\r
public void setHandler(WatcherCallBack handler) {\r
this.handler = handler;\r
}\r
+\r
/**\r
* @return the watcher\r
*/\r