import org.apache.log4j.FileAppender;
import uk.ac.vamsas.client.ClientHandle;
+import uk.ac.vamsas.client.IClient;
import uk.ac.vamsas.client.UserHandle;
/**
* Does all the IO operations for a SimpleClient instance accessing
*/
public static final String MODIFIEDDOC_FILE="modified";
+
+ private SimpleSessionManager sessionManager = null;
+
/**
* called to clear update flag after a successful offline storage event
*/
private void initLog() throws IOException {
// LATER: make dedicated appender format for session log.
Appender app = slog.getAppender("SESSION_LOG");
- slog.addAppender(new FileAppender(app.getLayout(), new File(sessionDir, SESSION_LOG).getAbsolutePath()));
+ // slog.addAppender(new FileAppender(app.getLayout(), new File(sessionDir, SESSION_LOG).getAbsolutePath()));
+
+ //Appender app = slog.getAppender("SESSION_LOG");
+ if (app == null) log.info("No appender for SESSION_LOG");
+
+ if (slog!= null && app != null)
+ {
+ if (app instanceof FileAppender)
+ {
+ File sessionLogFile = new File(this.sessionDir, ((FileAppender)app).getFile());
+ slog.addAppender(new FileAppender(app.getLayout(), sessionLogFile.getAbsolutePath()));
+ }
+ // slog.removeAppender("SESSION_LOG");
+ }
}
/**
/**
* sets up the vamsas session files and watchers in sessionDir
- * @param sessionDir
+ * @param sessionDir1
*/
- protected VamsasSession(File sessionDir) throws IOException {
- if (sessionDir==null)
+ protected VamsasSession(File sessionDir1) throws IOException {
+ if (sessionDir1==null)
throw new Error("Null directory for VamsasSession.");
- if (sessionDir.exists()) {
- if (!sessionDir.isDirectory() || !sessionDir.canWrite() || !sessionDir.canRead())
- throw new IOException("Cannot access '"+sessionDir+"' as a read/writable Directory.");
- if (checkSessionFiles(sessionDir)) {
+ if (sessionDir1.exists()) {
+ if (!sessionDir1.isDirectory() || !sessionDir1.canWrite() || !sessionDir1.canRead())
+ throw new IOException("Cannot access '"+sessionDir1+"' as a read/writable Directory.");
+ if (checkSessionFiles(sessionDir1)) {
+ createSessionFiles();
+ }
// session files exist in the directory
- this.sessionDir = sessionDir;
+ this.sessionDir = sessionDir1;
initSessionObjects();
slog.debug("Initialising additional VamsasSession instance");
- log.debug("Attached to VamsasSession in "+sessionDir);
- }
+ log.debug("Attached to VamsasSession in "+sessionDir1);
+ //}
} else {
// start from scratch
- if (!sessionDir.mkdir())
- throw new IOException("Failed to make VamsasSession directory in "+sessionDir);
- this.sessionDir = sessionDir;
+ if (!sessionDir1.mkdir())
+ throw new IOException("Failed to make VamsasSession directory in "+sessionDir1);
+ this.sessionDir = sessionDir1;
createSessionFiles();
initSessionObjects();
slog.debug("Session directory created.");
- log.debug("Initialised VamsasSession in "+sessionDir);
+ log.debug("Initialised VamsasSession in "+sessionDir1);
}
}
/**
return va;
}
/**
- * create a uniquely named file in the session Directory
+ * create a uniquely named uk.ac.vamsas.client.simpleclient.ClientsFile.addClient(ClientHandle)ile in the session Directory
* @see java.io.File.createTempFile
* @param pref Prefix for name
* @param suff Suffix for name
SessionFile tempFile = new SessionFile(tfile);
return tempFile;
}
+
+ /**
+ * add a IClient to the session
+ *
+ * add the client to the client list file
+ * @param client client to add to the session
+ */
+ protected void addClient(IClient client)
+ {
+ if (client == null)
+ this.slog.error("Try to add a null client to the session ");
+ else
+ this.clist.addClient(client.getClientHandle(), getClientWatcher().getChangedState());
+ }
+
+/**
+ *
+ * removes a client from the current session
+ * removes the client from the session client list
+ * if the client is the last one from the session (ClientList), the current session is removed
+ * from active session list.
+ *
+ * @param client client to remove
+ */
+ protected void removeClient(IClient client)
+ {
+ if (client == null)
+ {
+ this.slog.error("Try to remove a null client.");
+ return;
+ }
+ this.clist.removeClient(client.getClientHandle(), getClientWatcher().getChangedState());
+ if (this.clist.retrieveClientList() == null|| this.clist.retrieveClientList().length<1)
+ {//assume it is the last client has been removed shutting down session
+ System.out.println("last client removed: removing session");
+ this.getSessionManager().removeSession(client.getSessionHandle());
+
+ }
+ else
+ {
+ this.slog.debug("Still "+this.clist.retrieveClientList().length +" active clients");
+ System.out.println("Still "+(this.clist.retrieveClientList()==null?"null":this.clist.retrieveClientList().length+"") +" active clients");
+ }
+ }
+/**
+ * @return the sessionManager
+ */
+protected SimpleSessionManager getSessionManager() {
+ return sessionManager;
+}
+/**
+ * @param sessionManager the sessionManager to set
+ */
+protected void setSessionManager(SimpleSessionManager sessionManager) {
+ this.sessionManager = sessionManager;
+}
}