+/*
+* VAMSAS Project
+*
+* Dec 15, 2006
+*
+*/
+
+package uk.ac.vamsas.client.simpleclient;
+
+import java.io.File;
+import java.io.IOException;
+
+import uk.ac.vamsas.client.SessionHandle;
+
+/**
+ * Class to deal with sessions
+ *
+ * retrieves list of available sessions
+ *
+ * adds an active session
+ *
+ * removes a close session (the last client has been closed)
+ *
+ *
+ */
+public class SimpleSessionManager
+{
+ private SessionsFile sessionFile = null;
+
+
+ protected SimpleSessionManager(File sessionsFile) throws IOException
+ {
+ this.initManagerObjects(sessionsFile);
+ }
+
+/**
+ * construct SessionFile objects and watchers for each
+ */
+private void initManagerObjects(File sessionsFile) throws IOException
+ {
+ if (this.sessionFile!=null )
+ throw new IOException("initFactoryObjects called for initialised ClientFactory object.");
+ this.sessionFile = new SessionsFile(sessionsFile);
+ }
+
+
+/**
+ * make a new watcher object for the sessionsFile
+ * @return new SessionsFile watcher instance
+ */
+public FileWatcher getSessionsWatcher() {
+ return new FileWatcher(this.getSessionFile().sessionFile);
+}
+
+/**
+ * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions()
+ */
+public String[] getCurrentSessions()
+{
+ String[] sessions = null;
+ if (this.sessionFile!=null )
+ {
+ SessionHandle[] sessionHandles = this.sessionFile.retrieveSessionsList();
+ if (sessionHandles != null)
+ {
+ sessions = new String[sessionHandles.length];
+ for (int i = sessionHandles.length -1; i > -1; i--)
+ {
+ SessionHandle sessionHandle = sessionHandles[i];
+ sessions [i] = sessionHandle.getSessionUrn();
+ }
+ }
+ }
+ return sessions;
+}
+
+/**
+ * adds SessionHandle me to the sessionList
+ * @param newSession session to add to the session list
+ * @return session index in list or 0 if lock was invalid or addSession operation failed.
+ */
+public int addSession(SessionHandle newSession)
+ {
+ return this.sessionFile.addSession(newSession, false,this.getSessionsWatcher().getChangedState());
+ }
+
+/**
+ * @return the sessionFile
+ */
+private SessionsFile getSessionFile()
+ {
+ return this.sessionFile;
+ }
+
+/**
+ * Removes a session from the list of currently active session
+ *
+ * @param session SessionHandle of the session to remove
+ */
+protected void removeSession(SessionHandle session)
+ {
+ this.getSessionFile().removeSession(session,this.getSessionsWatcher().getChangedState());
+ }
+}
+
+