applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleSessionManager.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.client.simpleclient;\r
23 \r
24 import java.io.File;\r
25 import java.io.IOException;\r
26 \r
27 import uk.ac.vamsas.client.SessionHandle;\r
28 \r
29 /**\r
30  * Class to deal with sessions\r
31  * \r
32  * retrieves list of available sessions\r
33  * \r
34  * adds an active session\r
35  * \r
36  * removes a close session (the last client has been closed)\r
37  * \r
38  * \r
39  */\r
40 public class SimpleSessionManager {\r
41   private SessionsFile sessionFile = null;\r
42 \r
43   protected SimpleSessionManager(File sessionsFile) throws IOException {\r
44     this.initManagerObjects(sessionsFile);\r
45   }\r
46 \r
47   /**\r
48    * construct SessionFile objects and watchers for each\r
49    */\r
50   private void initManagerObjects(File sessionsFile) throws IOException {\r
51     if (this.sessionFile != null)\r
52       throw new IOException(\r
53           "initFactoryObjects called for initialised ClientFactory object.");\r
54     this.sessionFile = new SessionsFile(sessionsFile);\r
55   }\r
56 \r
57   /**\r
58    * make a new watcher object for the sessionsFile\r
59    * \r
60    * @return new SessionsFile watcher instance\r
61    */\r
62   public FileWatcher getSessionsWatcher() {\r
63     return new FileWatcher(this.getSessionFile().sessionFile);\r
64   }\r
65 \r
66   /**\r
67    * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions()\r
68    */\r
69   public String[] getCurrentSessions() {\r
70     String[] sessions = null;\r
71     if (this.sessionFile != null) {\r
72       SessionHandle[] sessionHandles = this.sessionFile.retrieveSessionsList();\r
73       if (sessionHandles != null) {\r
74         sessions = new String[sessionHandles.length];\r
75         for (int i = sessionHandles.length - 1; i > -1; i--) {\r
76           SessionHandle sessionHandle = sessionHandles[i];\r
77           sessions[i] = sessionHandle.getSessionUrn();\r
78         }\r
79       }\r
80     }\r
81     return sessions;\r
82   }\r
83 \r
84   /**\r
85    * adds SessionHandle me to the sessionList\r
86    * \r
87    * @param newSession\r
88    *          session to add to the session list\r
89    * @return session index in list or 0 if lock was invalid or addSession\r
90    *         operation failed.\r
91    */\r
92   public int addSession(SessionHandle newSession) {\r
93     return this.sessionFile.addSession(newSession, false, this\r
94         .getSessionsWatcher().getChangedState());\r
95   }\r
96 \r
97   /**\r
98    * @return the sessionFile\r
99    */\r
100   private SessionsFile getSessionFile() {\r
101     return this.sessionFile;\r
102   }\r
103 \r
104   /**\r
105    * Removes a session from the list of currently active session\r
106    * \r
107    * @param session\r
108    *          SessionHandle of the session to remove\r
109    */\r
110   protected void removeSession(SessionHandle session) {\r
111     this.getSessionFile().removeSession(session,\r
112         this.getSessionsWatcher().getChangedState());\r
113   }\r
114 }\r