+/*\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
import java.util.Iterator;\r
import org.apache.commons.logging.LogFactory;\r
\r
/**\r
- * Watches a bunch of VamsasFile states, calling \r
- * the associated event handler when anything changes.\r
+ * Watches a bunch of VamsasFile states, calling the associated event handler\r
+ * when anything changes.\r
+ * \r
* @author JimP\r
- *\r
+ * \r
*/\r
public class VamsasFileWatcherThread extends Thread {\r
- private Log log = LogFactory.getLog(VamsasFileWatcherThread.class); \r
- /* (non-Javadoc)\r
+ private Log log = LogFactory.getLog(VamsasFileWatcherThread.class);\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
* @see java.lang.Thread#run()\r
*/\r
- EventGeneratorThread client=null;\r
- private Vector elements=null;\r
+ EventGeneratorThread client = null;\r
+\r
+ private Vector elements = null;\r
+\r
public VamsasFileWatcherThread(EventGeneratorThread client) {\r
this.client = client;\r
- elements=new Vector();\r
+ elements = new Vector();\r
}\r
+\r
public void addElement(WatcherElement welement) {\r
elements.addElement(welement);\r
}\r
+\r
public void removeElemenet(WatcherElement welement) {\r
elements.removeElement(welement);\r
}\r
+\r
public void clearElements() {\r
elements.clear();\r
}\r
+\r
/**\r
* true if the thread is running\r
*/\r
- boolean running=false;\r
+ boolean running = false;\r
+\r
/**\r
* true if the watcher loop is in progress\r
*/\r
- boolean watching=false;\r
+ boolean watching = false;\r
+\r
public void haltWatchers() {\r
if (!watching)\r
return;\r
- watching=false;\r
+ watching = false;\r
// wait arount for WATCH_SLEEP milliseconds before returning\r
// in the hope that the watcher loop has stopped\r
try {\r
interrupt();\r
- long time = System.currentTimeMillis()+WATCH_SLEEP;\r
- while (running && time>System.currentTimeMillis()) {\r
+ long time = System.currentTimeMillis() + WATCH_SLEEP;\r
+ while (running && time > System.currentTimeMillis()) {\r
Thread.sleep(1);\r
}\r
- } catch (Exception e) {};\r
+ } catch (Exception e) {\r
+ }\r
+ ;\r
if (running)\r
log.warn("haltWatchers returning whilst thread is still running.");\r
}\r
- \r
+\r
/**\r
* time between checks for changes of state on the file\r
*/\r
- public int WATCH_SLEEP=30; \r
+ public int WATCH_SLEEP = 30;\r
+\r
/**\r
- * check each watcher in sequence, monitoring any events generated.\r
- * Then wait WATCH_SLEEP milliseconds before checking all again (if there were no events)\r
+ * check each watcher in sequence, monitoring any events generated. Then wait\r
+ * WATCH_SLEEP milliseconds before checking all again (if there were no\r
+ * events)\r
*/\r
public void run() {\r
- running=true;\r
- watching=true;\r
+ running = true;\r
+ watching = true;\r
log.debug("Starting WatcherThread poll loop");\r
while (watching) {\r
- boolean wait=true;\r
- Iterator watchers=elements.iterator();\r
+ boolean wait = true;\r
+ Iterator watchers = elements.iterator();\r
while (watching && watchers.hasNext()) {\r
WatcherElement watch = (WatcherElement) watchers.next();\r
if (watch.doWatch()) {\r
- wait=false;\r
- log.debug("Event generated for watcher on "+watch.getWatcher().getSubject());\r
+ wait = false;\r
+ log.debug("Event generated for watcher on "\r
+ + watch.getWatcher().getSubject());\r
}\r
}\r
if (watching && wait) {\r
try {\r
Thread.sleep(WATCH_SLEEP);\r
+ } catch (InterruptedException e) {\r
}\r
- catch (InterruptedException e) {};\r
+ ;\r
}\r
}\r
log.debug("Finishing WatcherThread poll loop");\r
- running=false;\r
+ running = false;\r
}\r
- /* (non-Javadoc)\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
* @see java.lang.Thread#interrupt()\r
*/\r
public void interrupt() {\r
- // TODO: make thread gracefully interrupt watchers so that any handlers finish doing what they were doing\r
+ // TODO: make thread gracefully interrupt watchers so that any handlers\r
+ // finish doing what they were doing\r
// super.interrupt();\r
}\r
- \r
+\r
}\r