+
+ /**
+ * Handler for the client watcher. after a client have been removed
+ *
+ * Checks if the client is not the last active one.
+ *
+ * If (the current client is not in the client list readd it;)
+ */
+ private class RemoveClientWatchCallBack implements WatcherCallBack
+ {
+
+ private SimpleClient client ;
+
+ /**
+ *Inits the handler with the client to check in the list
+ * @param client client to monitor in the client list
+ */
+ protected RemoveClientWatchCallBack (SimpleClient client)
+ {
+ this.client = client;
+ }
+
+ /**
+ * If the client list is modified, checks if the current is still in the list. otherwise, readds ti.
+ * @return true to enable watcher, or false to disable it in future WatcherThread cycles.
+ */
+ public boolean handleWatchEvent(WatcherElement watcher, Lock lock)
+ {
+ // boolean isWatchEnable = watcher.isWatchEnabled();
+ if (client != null)
+ {
+
+ //checks if the client is not already in the lists
+ ClientHandle[] cl = clist.retrieveClientList(lock);//clist.retrieveClientList();
+// ask to the client to cpoy application data into the document
+ client.evgen._raise(Events.DOCUMENT_FINALIZEAPPDATA, null, client,null);
+
+ if(cl == null || cl.length<1 )
+ {//no client has registered as active
+ //the client is the last one, so close current session
+ log.info("last client removed: closing session");
+
+// close document
+ client.evgen._raise(Events.DOCUMENT_REQUESTTOCLOSE, null, client,null);
+ log.debug("close document request done");
+
+ getSessionManager().removeSession(client.getSessionHandle());
+ log.debug("Session removed");
+ }
+ else
+ {
+ log.debug("not the last client found ");
+// ask to the client to cpoy application data into the document
+ // client.evgen._raise(Events.DOCUMENT_FINALIZEAPPDATA, null, client,null);
+
+ }
+ // watcher.haltWatch();
+ log.debug("Stopping EventGenerator..");
+ client.evgen.stopWatching();
+
+
+ }
+ watcher.setHandler(null);//Do not check if the client is the last client. watcher will shutdown anyway
+
+ return false;
+ }
+ }
+