+ 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;