Added support for crashed client.
[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      if (doclock==null)
57        {//no change detected
58           this.cycleCountSinceModif ++;
59           if (this.isTimeOutEnable && cycleCountSinceModif > timeoutBeforeLastCycle)
60             {
61             if(this.handler != null )
62               synchronized (this.handler)
63               {
64                 this.callHandler(doclock);
65               }
66             }
67         //log.debug("no modification");
68           return false;
69        }
70      this.cycleCountSinceModif =0; //change detected
71      if(this.handler != null )
72        synchronized (this.handler)
73        {
74          this.callHandler(doclock);
75        }
76    
77      return true;
78     }
79   
80  
81   /**
82    * count of cycles since last modification on the file
83    */
84   protected int  cycleCountSinceModif  =0;
85   
86   /**
87    * resets count of watch cycles  (default value : 0)
88    *
89    */
90   protected void resetCycleCount ()
91     {
92       this.cycleCountSinceModif = 0;
93     }
94   
95   /**
96    * Increases the count of cycles
97    *
98    */
99   protected void increaseCycleCount ()
100     {
101       this.cycleCountSinceModif ++;
102     }
103
104   /**
105    * Enable the time out if the timeout is greater than zero
106    * @param timeoutBeforeLastCycle the timeoutBeforeLastCycle to set
107    */
108   public void setTimeoutBeforeLastCycle(int timeoutBeforeLastCycle) {
109     
110     this.timeoutBeforeLastCycle = timeoutBeforeLastCycle;
111     if (this.timeoutBeforeLastCycle>0)
112       isTimeOutEnable = true;
113   }
114   
115   /**
116    * Disables the checking on the count of cycles
117    *
118    */
119   public void disableCycleTimeOut ()
120     {
121     this.isTimeOutEnable = false;
122     }
123 }