1 package org.vamsas.client.simpleclient;
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.beans.PropertyChangeSupport;
6 import java.util.Hashtable;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.vamsas.client.Events;
13 * monitors watcher objects and generates events.
15 public class EventGeneratorThread extends Thread implements Runnable {
16 private static Log log = LogFactory.getLog(EventGeneratorThread.class);
17 private SimpleClient client;
18 private Hashtable handlers; // manager object
19 private VamsasSession session;
22 * list with all the clientHandles for the session
24 protected FileWatcher clientfile=null;
26 * the session's vamsasDocument
28 protected FileWatcher vamsasfile=null;
30 * written to by client when its app calls storeDocument.
32 protected FileWatcher storeFile=null;
34 private boolean watch=false;
37 EventGeneratorThread(VamsasSession s, SimpleClient _client, Hashtable eventhandlers) {
38 if (eventhandlers==null || s==null || _client==null)
39 throw new Error("Null arguments to EventGeneratorThread constructor.");
40 handlers = eventhandlers;
43 setName(s.sessionDir.getName());
47 private void initWatchers() {
49 clientfile = session.getClientWatcher();
50 if (vamsasfile ==null)
51 vamsasfile = session.getDocWatcher();
52 if (storeFile == null)
53 storeFile = session.getStoreWatcher();
54 clientfile.setState();
55 vamsasfile.setState();
58 boolean ownsf = false;
60 * scans all watchers and fires changeEvents if necessary
61 * @return number of events generated.
63 private int checkforEvents() {
65 //TODO : leave slog.info messages for the events that occur.
67 // could make this general - but for now keep simple
68 if ((watchlock=storeFile.getChangedState())!=null) {
69 // 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).
70 if (storeFile.exists) {
71 PropertyChangeSupport h = (PropertyChangeSupport) handlers.get(Events.DOCUMENT_FINALIZEAPPDATA);
73 log.debug("Triggering DOCUMENT_FINALIZEAPPDATA");
75 h.firePropertyChange(client.getSessionUrn(), null, client);
77 vamsasfile.setState();
81 if ((watchlock=clientfile.getChangedState())!=null) {
82 // see what happened to the clientfile - compare our internal version with the one in the file, or just send the updated list out...?
85 * Generated when a new vamsas client is attached to a session (Handle is
86 * passed) Note: the newly created client does not receive the event.
88 public static final String CLIENT_CREATION = "org.vamsas.client.events.clientCreateEvent";
91 * Generated when a vamsas client leaves a session (Handle is passed to all
93 public static final String CLIENT_FINALIZATION = "org.vamsas.client.events.clientFinalizationEvent";
94 */ // again - as the test.
97 if ((watchlock=vamsasfile.getChangedState())!=null) {
100 * Generated when a client has finished updating the document. Passes
101 * applicationHandle of client so the updating client can recognise its own
103 public static final String DOCUMENT_UPDATE = "org.vamsas.client.events.documentUpdateEvent";
105 // read apphandle from 'lastUpdate' session file.
106 // pass apphandle name to appHandler ?
110 * Generated when a new vamsas document is created (perhaps from some existing
111 * Vamsas data) so an application may do its own data space initialization.
112 * TODO: decide if this is called when an app is connected to a stored
114 public static final String DOCUMENT_CREATE = "org.vamsas.client.events.documentCreateEvent";
116 // check if this session's appInit flag is set - if not - generate event for this app.
117 // prolly don't need this at the moment - when an app does getDocument it can to the initing then.
121 * Generated prior to session Shutdown, after the last participating vamsas
122 * client has finalized.
123 * TODO: decide on purpose of this ? is this for benefit of multi-session Apps only ?
124 public static final String SESSION_SHUTDOWN = "org.vamsas.client.events.SessionShutdownEvent";
128 * Generated for all clients when any client calls IClient.storeDocument() to
129 * allow them to store any updates before an offline copy of the session is
130 * created. Any client that handles this should call the
131 * IClient.getDocument(), update and then IClient.updateDocument in the same
133 * EventName: <Vamsas-session URN>
134 * NewValue: org.vamsas.client.IClient for session.
136 public static final String DOCUMENT_FINALIZEAPPDATA = "org.vamsas.client.events.DocumentFinalizeAppData";
138 // watch for finalization semaphore (last finalised sessionFile).
141 * Generated by Vorba stub after the penultimate client makes a call to
142 * closeDocument(). Sequence is as follows : 1. All other vamsas clients have
143 * called closeDocument() 2. Final living client monitors closures, and
144 * realises that it is last. 3. Final client generates event to prompt
145 * associated application to inquire if the user wishes to save the document
146 * for future reference.
147 * * Any call to closeDocument in a thread other than the registered
148 * EventListener will block until the RequestToClose handler has exited.
151 // public static final String DOCUMENT_REQUESTTOCLOSE = "org.vamas.client.DocumentRequestToCloseEvent";
156 private void initEvents() {
160 * Events raised by IClient and propagated to others in session
164 * number of milliseconds between any file state check.
167 protected void wait(int u) {
170 long l = System.currentTimeMillis()+POLL_UNIT*u;
171 while (System.currentTimeMillis()<l)
176 private boolean block_document_updates=false;
177 int STORE_WAIT=5; // how many units before we decide all clients have finalized their appdatas
180 * client App requests offline storage of vamsas data.
181 * Call blocks whilst other apps do any appData finalizing
182 * and then returns (after locking the vamsasDocument in the session)
183 * Note - the calling app may also receive events through the EventGeneratorThread for document updates.
185 * @return Lock for session.vamArchive
186 * @param STORE_WAIT indicates how lock the call will block for when nothing appears to be happening to the session.
188 protected Lock want_to_store() {
189 log.debug("Setting flag for document_update requests to be ignored");
190 block_document_updates=true;
191 log.debug("Waiting for other apps to do FinalizeApp handling.");
193 session.addStoreDocumentRequest(client.getClientHandle(), client.getUserHandle());
194 } catch (Exception e) {
195 log.warn("Whilst writing StoreDocumentRequest for "+client.getClientHandle().getClientUrn()+" "+client.getUserHandle(),
197 log.info("trying to continue.");
199 // LATER: refine this semaphore process
200 // to make a robust signalling mechanism:
201 // app1 requests, app1..n do something (or don't - they may be dead),
202 // app1 realises all apps have done their thing, it then continues with synchronized data.
203 // this probably needs two files - a request file,
204 // and a response file which is acknowledged by the app1 requestor for each app.
205 // eventually, no more responses are received for the request, and the app can then only continue with its store.
207 while (units<STORE_WAIT) {
209 if (storeFile.hasChanged() || vamsasfile.hasChanged())
215 block_document_updates=false;
216 log.debug("Cleared flag for ignoring document_update requests");
217 // wait around again (until our own watcher has woken up and synchronized).
218 while (units<STORE_WAIT) {
220 if (storeFile.hasChanged() || vamsasfile.hasChanged())
227 log.debug("finished waiting.");
228 return session.vamArchive.getLock();
231 * count handlers for a particular vamsas event
232 * @param event string enumeration from org.vamsas.client.Events
233 * @return -1 for an invalid event, otherwise the number of handlers
235 protected int countHandlersFor(String event) {
236 if (handlers.containsKey(event)) {
237 PropertyChangeSupport handler = (PropertyChangeSupport) handlers.get(event);
238 PropertyChangeListener[] listeners;
240 return ((listeners=handler.getPropertyChangeListeners())==null)
241 ? -1 : listeners.length;
246 * probably don't need any of these below.
249 * @see java.lang.Thread#destroy()
251 public void destroy() {
255 * @see java.lang.Thread#interrupt()
257 public void interrupt() {
258 // TODO Auto-generated method stub
262 * @see java.lang.Thread#isInterrupted()
264 public boolean isInterrupted() {
265 // TODO Auto-generated method stub
266 return super.isInterrupted();
269 * @see java.lang.Thread#run()
272 // TODO Auto-generated method stub