332f7e5da5ed4d2c7db5f20653ce19a3bdbb3289
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleSessionManager.java
1 /*
2 * VAMSAS Project
3
4 * Dec 15, 2006  
5 *
6 */
7  
8 package uk.ac.vamsas.client.simpleclient;
9
10 import java.io.File;
11 import java.io.IOException;
12
13 import uk.ac.vamsas.client.SessionHandle;
14
15 /**
16  * Class to deal with sessions
17  * 
18  * retrieves list of available sessions
19  * 
20  * adds an active session
21  * 
22  * removes a close session (the last client has been closed)
23  * 
24  * 
25  */
26 public class SimpleSessionManager
27 {
28   private SessionsFile sessionFile = null;
29
30
31   protected SimpleSessionManager(File sessionsFile) throws IOException 
32   {
33     this.initManagerObjects(sessionsFile);
34   }
35
36 /**
37  * construct SessionFile objects and watchers for each
38  */
39 private void initManagerObjects(File sessionsFile) throws IOException 
40   {
41     if (this.sessionFile!=null )
42       throw new IOException("initFactoryObjects called for initialised ClientFactory object.");
43     this.sessionFile = new SessionsFile(sessionsFile);
44   }
45
46
47 /**
48  * make a new watcher object for the sessionsFile
49  * @return new SessionsFile watcher instance
50  */
51 public FileWatcher getSessionsWatcher() {
52   return new FileWatcher(this.getSessionFile().sessionFile);
53 }
54
55 /**
56  * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions()
57  */
58 public String[] getCurrentSessions() 
59
60   String[] sessions = null;
61   if (this.sessionFile!=null )
62     {
63       SessionHandle[] sessionHandles =  this.sessionFile.retrieveSessionsList();
64       if (sessionHandles != null)
65         {
66           sessions = new String[sessionHandles.length];
67           for (int i = sessionHandles.length -1; i > -1; i--)
68             {
69               SessionHandle sessionHandle = sessionHandles[i];
70               sessions [i] = sessionHandle.getSessionUrn();
71             }
72         }
73     }
74   return sessions;
75 }
76
77 /**
78  * adds SessionHandle me to the sessionList 
79  * @param newSession session to add to the session list
80  * @return session index in list or 0 if lock was invalid or addSession operation failed.
81  */
82 public int addSession(SessionHandle newSession) 
83   {
84     return this.sessionFile.addSession(newSession, false,this.getSessionsWatcher().getChangedState());
85   }
86
87 /**
88  * @return the sessionFile
89  */
90 private SessionsFile getSessionFile()
91   {
92     return this.sessionFile;
93   }
94
95 /**
96  * Removes a session from the list of  currently active session
97  * 
98  * @param session SessionHandle of the session to remove
99  */
100 protected void removeSession(SessionHandle session)
101   {
102       this.getSessionFile().removeSession(session,this.getSessionsWatcher().getChangedState());
103   }
104 }
105
106