f8e4f12ab386dc74e85d402a2b01d1d8df45460f
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / ClientSessionFileWatcherElement.java
1
2 package uk.ac.vamsas.client.simpleclient;
3
4
5 public class ClientSessionFileWatcherElement extends SessionFileWatcherElement {
6
7   private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(VamsasFileWatcherElement.class);
8   
9  /**
10   * count of watch cycle before considering there is no other active client.
11   */
12   private int timeoutBeforeLastCycle = -1;
13   
14   private boolean isTimeOutEnable = false;
15   
16   /**
17    * @param watcher
18    * @param handler
19    */
20   public ClientSessionFileWatcherElement(SessionFile watcher,
21       WatcherCallBack handler) {
22     super(watcher, handler);
23   }
24
25   /**
26    * @param watcher
27    * @param handler
28    * @param enableWatching
29    */
30   public ClientSessionFileWatcherElement(SessionFile watcher,
31       WatcherCallBack handler, boolean enableWatching) {
32     super(watcher, handler, enableWatching);
33   }
34
35   /**
36    * @see uk.ac.vamsas.client.simpleclient.WatcherElement#doWatch()
37    * * @return true if the handler was called for a changeEvent
38      */
39   public boolean doWatch()
40     {
41       if (!watchForChange || handler==null)
42       { //log.debug("!watchForChange || handler==null");
43         return false;
44       }
45       if (watcher==null)
46         initWatch(); // somehow not done the first time
47       handlerCalled=false;
48       Lock doclock=null;
49       try 
50         {
51           doclock=watcher.getChangedState();
52         } 
53      catch (Exception e) {
54         log.error("Whilst watching "+watcher.getSubject(), e);
55       }
56     // log.debug("got lock watcher");
57      if (doclock==null)
58        {//no change detected
59           this.cycleCountSinceModif ++;
60           if (this.isTimeOutEnable && cycleCountSinceModif > timeoutBeforeLastCycle)
61             {
62             if(this.handler != null )
63               synchronized (this.handler)
64               {
65                 this.callHandler(doclock);
66               }
67             }
68         //log.debug("no modification");
69           return false;
70        }
71      if (this.isTimeOutEnable) this.cycleCountSinceModif =0; //change detected
72      if(this.handler != null )
73        synchronized (this.handler)
74        {
75          this.callHandler(doclock);
76        }
77    
78      return true;
79     }
80   
81  
82   /**
83    * count of cycles since last modification on the file
84    */
85   protected int  cycleCountSinceModif  =0;
86   
87   /**
88    * resets count of watch cycles  (default value : 0)
89    *
90    */
91   protected void resetCycleCount ()
92     {
93       this.cycleCountSinceModif = 0;
94     }
95   
96   /**
97    * Increases the count of cycles
98    *
99    */
100   protected void increaseCycleCount ()
101     {
102       this.cycleCountSinceModif ++;
103     }
104
105   /**
106    * Enable the time out if the timeout is greater than zero
107    * @param timeoutBeforeLastCycle the timeoutBeforeLastCycle to set
108    */
109   public void setTimeoutBeforeLastCycle(int timeoutBeforeLastCycle) {
110     
111     this.timeoutBeforeLastCycle = timeoutBeforeLastCycle;
112     if (this.timeoutBeforeLastCycle>0)
113       isTimeOutEnable = true;
114   }
115   
116   /**
117    * Disables the checking on the count of cycles
118    *
119    */
120   public void disableCycleTimeOut ()
121     {
122     this.isTimeOutEnable = false;
123     }
124 }