2 * This file is part of the Vamsas Client version 0.2.
\r
3 * Copyright 2010 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 public class ClientSessionFileWatcherElement extends SessionFileWatcherElement {
\r
26 private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
\r
27 .getLog(VamsasFileWatcherElement.class);
\r
30 * count of watch cycle before considering there is no other active client.
\r
32 private int timeoutBeforeLastCycle = -1;
\r
34 private boolean isTimeOutEnable = false;
\r
40 public ClientSessionFileWatcherElement(SessionFile watcher,
\r
41 WatcherCallBack handler) {
\r
42 super(watcher, handler);
\r
48 * @param enableWatching
\r
50 public ClientSessionFileWatcherElement(SessionFile watcher,
\r
51 WatcherCallBack handler, boolean enableWatching) {
\r
52 super(watcher, handler, enableWatching);
\r
56 * @see uk.ac.vamsas.client.simpleclient.WatcherElement#doWatch() * @return
\r
57 * true if the handler was called for a changeEvent
\r
59 public boolean doWatch() {
\r
60 if (!watchForChange || handler == null) { // log.debug("!watchForChange || handler==null");
\r
63 if (watcher == null)
\r
64 initWatch(); // somehow not done the first time
\r
65 handlerCalled = false;
\r
66 Lock doclock = null;
\r
68 doclock = watcher.getChangedState();
\r
69 } catch (Exception e) {
\r
70 log.error("Whilst watching " + watcher.getSubject(), e);
\r
72 // log.debug("got lock watcher");
\r
73 if (doclock == null) {// no change detected
\r
74 this.cycleCountSinceModif++;
\r
75 if (this.isTimeOutEnable && cycleCountSinceModif > timeoutBeforeLastCycle) {
\r
76 if (this.handler != null)
\r
77 synchronized (this.handler) {
\r
78 this.callHandler(doclock);
\r
81 // log.debug("no modification");
\r
84 if (this.isTimeOutEnable)
\r
85 this.cycleCountSinceModif = 0; // change detected
\r
86 if (this.handler != null)
\r
87 synchronized (this.handler) {
\r
88 this.callHandler(doclock);
\r
95 * count of cycles since last modification on the file
\r
97 protected int cycleCountSinceModif = 0;
\r
100 * resets count of watch cycles (default value : 0)
\r
103 protected void resetCycleCount() {
\r
104 this.cycleCountSinceModif = 0;
\r
108 * Increases the count of cycles
\r
111 protected void increaseCycleCount() {
\r
112 this.cycleCountSinceModif++;
\r
116 * Enable the time out if the timeout is greater than zero
\r
118 * @param timeoutBeforeLastCycle
\r
119 * the timeoutBeforeLastCycle to set
\r
121 public void setTimeoutBeforeLastCycle(int timeoutBeforeLastCycle) {
\r
123 this.timeoutBeforeLastCycle = timeoutBeforeLastCycle;
\r
124 if (this.timeoutBeforeLastCycle > 0)
\r
125 isTimeOutEnable = true;
\r
129 * Disables the checking on the count of cycles
\r
132 public void disableCycleTimeOut() {
\r
133 this.isTimeOutEnable = false;
\r