applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / ClientSessionFileWatcherElement.java
1 /*\r
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
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \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
13  *  \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
18  * \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
21  */\r
22 package uk.ac.vamsas.client.simpleclient;\r
23 \r
24 public class ClientSessionFileWatcherElement extends SessionFileWatcherElement {\r
25 \r
26   private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory\r
27       .getLog(VamsasFileWatcherElement.class);\r
28 \r
29   /**\r
30    * count of watch cycle before considering there is no other active client.\r
31    */\r
32   private int timeoutBeforeLastCycle = -1;\r
33 \r
34   private boolean isTimeOutEnable = false;\r
35 \r
36   /**\r
37    * @param watcher\r
38    * @param handler\r
39    */\r
40   public ClientSessionFileWatcherElement(SessionFile watcher,\r
41       WatcherCallBack handler) {\r
42     super(watcher, handler);\r
43   }\r
44 \r
45   /**\r
46    * @param watcher\r
47    * @param handler\r
48    * @param enableWatching\r
49    */\r
50   public ClientSessionFileWatcherElement(SessionFile watcher,\r
51       WatcherCallBack handler, boolean enableWatching) {\r
52     super(watcher, handler, enableWatching);\r
53   }\r
54 \r
55   /**\r
56    * @see uk.ac.vamsas.client.simpleclient.WatcherElement#doWatch() * @return\r
57    *      true if the handler was called for a changeEvent\r
58    */\r
59   public boolean doWatch() {\r
60     if (!watchForChange || handler == null) { // log.debug("!watchForChange || handler==null");\r
61       return false;\r
62     }\r
63     if (watcher == null)\r
64       initWatch(); // somehow not done the first time\r
65     handlerCalled = false;\r
66     Lock doclock = null;\r
67     try {\r
68       doclock = watcher.getChangedState();\r
69     } catch (Exception e) {\r
70       log.error("Whilst watching " + watcher.getSubject(), e);\r
71     }\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
79           }\r
80       }\r
81       // log.debug("no modification");\r
82       return false;\r
83     }\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
89       }\r
90 \r
91     return true;\r
92   }\r
93 \r
94   /**\r
95    * count of cycles since last modification on the file\r
96    */\r
97   protected int cycleCountSinceModif = 0;\r
98 \r
99   /**\r
100    * resets count of watch cycles (default value : 0)\r
101    * \r
102    */\r
103   protected void resetCycleCount() {\r
104     this.cycleCountSinceModif = 0;\r
105   }\r
106 \r
107   /**\r
108    * Increases the count of cycles\r
109    * \r
110    */\r
111   protected void increaseCycleCount() {\r
112     this.cycleCountSinceModif++;\r
113   }\r
114 \r
115   /**\r
116    * Enable the time out if the timeout is greater than zero\r
117    * \r
118    * @param timeoutBeforeLastCycle\r
119    *          the timeoutBeforeLastCycle to set\r
120    */\r
121   public void setTimeoutBeforeLastCycle(int timeoutBeforeLastCycle) {\r
122 \r
123     this.timeoutBeforeLastCycle = timeoutBeforeLastCycle;\r
124     if (this.timeoutBeforeLastCycle > 0)\r
125       isTimeOutEnable = true;\r
126   }\r
127 \r
128   /**\r
129    * Disables the checking on the count of cycles\r
130    * \r
131    */\r
132   public void disableCycleTimeOut() {\r
133     this.isTimeOutEnable = false;\r
134   }\r
135 }\r