2 * This file is part of the Vamsas Client version 0.1.
\r
3 * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\r
9 * The Vamsas Client is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Lesser General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or
\r
12 * (at your option) any later version.
\r
14 * The Vamsas Client is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public License
\r
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
\r
22 package uk.ac.vamsas.client.simpleclient;
\r
24 import java.util.Iterator;
\r
25 import java.util.Vector;
\r
27 import org.apache.commons.logging.Log;
\r
28 import org.apache.commons.logging.LogFactory;
\r
31 * Watches a bunch of VamsasFile states, calling the associated event handler
\r
32 * when anything changes.
\r
37 public class VamsasFileWatcherThread extends Thread {
\r
38 private Log log = LogFactory.getLog(VamsasFileWatcherThread.class);
\r
43 * @see java.lang.Thread#run()
\r
45 EventGeneratorThread client = null;
\r
47 private Vector elements = null;
\r
49 public VamsasFileWatcherThread(EventGeneratorThread client) {
\r
50 this.client = client;
\r
51 elements = new Vector();
\r
54 public void addElement(WatcherElement welement) {
\r
55 elements.addElement(welement);
\r
58 public void removeElemenet(WatcherElement welement) {
\r
59 elements.removeElement(welement);
\r
62 public void clearElements() {
\r
67 * true if the thread is running
\r
69 boolean running = false;
\r
72 * true if the watcher loop is in progress
\r
74 boolean watching = false;
\r
76 public void haltWatchers() {
\r
80 // wait arount for WATCH_SLEEP milliseconds before returning
\r
81 // in the hope that the watcher loop has stopped
\r
84 long time = System.currentTimeMillis() + WATCH_SLEEP;
\r
85 while (running && time > System.currentTimeMillis()) {
\r
88 } catch (Exception e) {
\r
92 log.warn("haltWatchers returning whilst thread is still running.");
\r
96 * time between checks for changes of state on the file
\r
98 public int WATCH_SLEEP = 30;
\r
101 * check each watcher in sequence, monitoring any events generated. Then wait
\r
102 * WATCH_SLEEP milliseconds before checking all again (if there were no
\r
105 public void run() {
\r
108 log.debug("Starting WatcherThread poll loop");
\r
110 boolean wait = true;
\r
111 Iterator watchers = elements.iterator();
\r
112 while (watching && watchers.hasNext()) {
\r
113 WatcherElement watch = (WatcherElement) watchers.next();
\r
114 if (watch.doWatch()) {
\r
116 log.debug("Event generated for watcher on "
\r
117 + watch.getWatcher().getSubject());
\r
120 if (watching && wait) {
\r
122 Thread.sleep(WATCH_SLEEP);
\r
123 } catch (InterruptedException e) {
\r
128 log.debug("Finishing WatcherThread poll loop");
\r
135 * @see java.lang.Thread#interrupt()
\r
137 public void interrupt() {
\r
138 // TODO: make thread gracefully interrupt watchers so that any handlers
\r
139 // finish doing what they were doing
\r
140 // super.interrupt();
\r