+ if (client == null) {
+ log.error("Null client passed to removeClient");
+ return;
+ }
+ // ClientSessionFileWatcherElement cwe=getClientWatcherElement();
+ // if (cwe!=null && cwe.isWatchEnabled()) {
+ // cwe.haltWatch();
+ // };
+ // set handler to check is the the last active client of the session
+ // Wait for several watchers cycle to see if the current client was the last
+ // client active in the session.
+ // if yes, close the session
+
+ // getClientWatcherElement().setHandler(new RemoveClientWatchCallBack
+ // (client));
+ // getClientWatcherElement().setTimeoutBeforeLastCycle(this.watchCycleCountBeforeLastClient);
+ log.info("remove client from list");
+ if (clistWatchElement != null) {
+ clistWatchElement.haltWatch();
+ clistWatchElement.watched.unlockFile();
+ }
+ // clist.clearList();
+ // clist.unlockFile();
+ log.info("list cleared");
+ // if (cwe!=null) {
+ // cwe.enableWatch();
+
+ log.debug("Stopping EventGenerator..");
+ client.evgen.stopWatching();
+ // cwe.setHandler(null);
+ // ask to the client to copy application data into the document
+ client.evgen._raise(Events.DOCUMENT_FINALIZEAPPDATA, null, client, null);
+ boolean closeSession = isLastActiveClient(client);
+ if (closeSession) {
+ log.debug("Raising request-to-save event");
+ client.evgen._raise(Events.DOCUMENT_REQUESTTOCLOSE, null, client, null);
+ client.evgen._raise(Events.SESSION_SHUTDOWN, null, client
+ .getSessionHandle(), null);
+ }
+ // cwe.haltWatch();
+ client.evgen.stopWatching();
+ try {
+ log.debug("Attempting to release active client locks");
+ client.releaseActiveClientFile();
+ } catch (IOException e) {
+ log.error("error during active file client release");
+ }
+ tidyUp();
+ if (closeSession) {
+ log.debug("Last active client: closing session");
+ log.info("Closing session");
+ getSessionManager().removeSession(client.getSessionHandle());
+ }
+ }
+
+ /**
+ * close every file and stop.
+ */
+ private void tidyUp() {
+ if (clist != null)
+ clist.unlockFile();
+ clist = null;
+ storedocfile.unlockFile();
+ storedocfile = null;
+ closeSessionLog();
+ }
+
+ private boolean isLastActiveClient(SimpleClient client) {
+ log.debug("Testing if current client is the last one.");
+ log
+ .debug("current client lockfile is '" + client.getClientlockFile()
+ + "'");
+ boolean noOtherActiveClient = true;
+ // create, if need, subdirectory to contain client files
+ File clientlockFileDir = new File(this.sessionDir, clientFileDirectory);
+ if (!clientlockFileDir.exists()) {
+ log
+ .error("Something wrong the active client file does not exits... should not happen");
+ return false;
+ }
+
+ try {
+
+ // no check every file in the directory and try to get lock on it.
+ File[] clientFiles = clientlockFileDir.listFiles();
+ if (clientFiles == null || clientFiles.length == 0) {// there is not file
+ // on the directory.
+ // the current
+ // client should be
+ // the last one.
+ return true;