partially implemented simpleclient.EventGenerator
[vamsas.git] / src / org / vamsas / client / simpleclient / EventGeneratorThread.java
1 package org.vamsas.client.simpleclient;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeSupport;
5 import java.util.Hashtable;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.vamsas.client.Events;
10
11 /**
12  * monitors watcher objects and generates events.
13  */
14 public class EventGeneratorThread extends Thread implements Runnable {
15   private static Log log = LogFactory.getLog(EventGeneratorThread.class);
16   private SimpleClient client;
17   private Hashtable handlers; // manager object
18   private VamsasSession session;
19   
20   protected FileWatcher clientfile=null;
21   protected FileWatcher vamsasfile=null;
22   protected FileWatcher storeFile=null;
23   
24   private boolean watch=false;
25   
26   
27   EventGeneratorThread(VamsasSession s, SimpleClient _client, Hashtable eventhandlers) {
28     if (eventhandlers==null || s==null || _client==null)
29       throw new Error("Null arguments to EventGeneratorThread constructor.");
30     handlers = eventhandlers;
31     session = s;
32     client = _client;
33     setName(s.sessionDir.getName());
34     initWatchers();
35   }
36   
37   private void initWatchers() {
38     if (clientfile==null)
39       clientfile = session.getClientWatcher();
40     if (vamsasfile ==null)
41       vamsasfile = session.getDocWatcher();
42     if (storeFile == null)
43       storeFile = session.getStoreWatcher();
44     clientfile.setState();
45     vamsasfile.setState();
46     storeFile.setState();
47   }
48   boolean ownsf = false;
49   /**
50    * scans all watchers and fires changeEvents if necessary
51    * @return number of events generated.
52    */
53   private int checkforEvents() {
54     Lock watchlock;
55     //TODO : leave slog.info messages for the events that occur.
56     int raised=0;
57     // could make this general - but for now keep simple
58     if ((watchlock=storeFile.getChangedState())!=null) {
59       // TODO: define the storeFile semaphore mechanism : file exists - all clients inform their apps, and then the client that wrote the file should delete the file (it should hold the lock to it).
60       if (storeFile.exists) { 
61         PropertyChangeSupport h = (PropertyChangeSupport) handlers.get(Events.DOCUMENT_FINALIZEAPPDATA);
62         if (h!=null) {
63           log.debug("Triggering DOCUMENT_FINALIZEAPPDATA");
64           raised++;
65           h.firePropertyChange(client.getSessionUrn(), null, client);
66           // expect client to 
67           vamsasfile.setState();
68         }
69       }
70     }
71     if ((watchlock=clientfile.getChangedState())!=null) {
72       // see what happened to the clientfile - compare our internal version with the one in the file, or just send the updated list out...?
73       raised++;
74     }
75     if ((watchlock=vamsasfile.getChangedState())!=null) {
76       // pass IClientDocument instance to app handler ?
77     }
78     return raised;
79   }
80   
81   private void initEvents() {
82     
83   }
84   
85   /**
86    * probably don't need any of these below.
87    */
88   /* (non-Javadoc)
89    * @see java.lang.Thread#destroy()
90    */
91   public void destroy() {
92     super.destroy();
93   }
94   /* (non-Javadoc)
95    * @see java.lang.Thread#interrupt()
96    */
97   public void interrupt() {
98     // TODO Auto-generated method stub
99     super.interrupt();
100   }
101   /* (non-Javadoc)
102    * @see java.lang.Thread#isInterrupted()
103    */
104   public boolean isInterrupted() {
105     // TODO Auto-generated method stub
106     return super.isInterrupted();
107   }
108   /* (non-Javadoc)
109    * @see java.lang.Thread#run()
110    */
111   public void run() {
112     // TODO Auto-generated method stub
113     super.run();
114   }
115   
116
117 }