+ return noOtherActiveClient;
+ }
+
+ /**
+ * 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;
+
+ private boolean manualCheckOfClientCount = false;
+
+ /**
+ * 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) {
+ // if lock is null, no client has been added since last, clear.
+ // the client is then the last client
+ if (client != null) {
+
+ if (lock == null) {
+
+ // checks if the client is not already in the lists
+ // ClientHandle[] cl =
+ // clist.retrieveClientList();//lock);//clist.retrieveClientList();
+
+ boolean islastClient = true;
+ if (manualCheckOfClientCount) {
+ log.debug("manual checking of count of client");
+ // checks if the client is not already in the lists
+ ClientHandle[] cl = clist.retrieveClientList();// lock);//clist.retrieveClientList();
+ if (cl == null || cl.length < 1)
+ // {//no client has registered as active
+ {
+ islastClient = true;
+ log.debug("list is empty");
+ } else
+ islastClient = false;
+ log.debug("list is not empty");
+ }
+ // if(cl == null || cl.length<1 )
+ // {//no client has registered as active
+ if (islastClient) {
+ // the client is the last one, so close current session
+ log.info("FROMCLIENTLIST WATCHER: last client removed: closing session");
+ closeSession(client);
+ }
+ } 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);
+
+ // / }
+
+ }
+ log.debug("Stopping EventGenerator..");
+ // TODO: ensure ClientsFile lock is really released!!
+ // clist.unlockFile();
+ client.evgen.stopWatching();
+ }
+ watcher.setHandler(null);// Do not check if the client is the last
+ // client. watcher will shutdown anyway
+ // watcher.haltWatch();
+ // watcher.
+ return false;
+ }
+ }
+
+ /**
+ * closes the current session, and send an event to the last client to close
+ * the document
+ *
+ * @param client
+ * the last client of the client
+ */
+ private void closeSession(SimpleClient client) {
+ // close document
+ client.evgen._raise(Events.DOCUMENT_REQUESTTOCLOSE, null, client, null);
+ log.debug("close document request done");
+ closeSession(client.getSessionHandle());
+ }
+
+ /**
+ * CLoses the current session
+ *
+ * @param sessionHandle
+ * sessionHandle of the session to remove
+ */
+ private void closeSession(SessionHandle sessionHandle) {
+ getSessionManager().removeSession(sessionHandle);
+ log.debug("Session removed");