verson 0.2 LGPL licensed source and jars
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleSessionManager.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.2. \r
3  * Copyright 2010 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 import java.util.ArrayList;\r
27 \r
28 import uk.ac.vamsas.client.SessionHandle;\r
29 \r
30 \r
31 /**\r
32  * Class to deal with sessions\r
33  * \r
34  * retrieves list of available sessions\r
35  * \r
36  * adds an active session\r
37  * \r
38  * removes a close session (the last client has been closed)\r
39  * \r
40  * \r
41  */\r
42 public class SimpleSessionManager {\r
43   private SessionsFile sessionFile = null;\r
44 \r
45   protected SimpleSessionManager(File sessionsFile) throws IOException {\r
46     this.initManagerObjects(sessionsFile);\r
47   }\r
48 \r
49   /**\r
50    * construct SessionFile objects and watchers for each\r
51    */\r
52   private void initManagerObjects(File sessionsFile) throws IOException {\r
53     if (this.sessionFile != null)\r
54       throw new IOException(\r
55           "initFactoryObjects called for initialised ClientFactory object.");\r
56     this.sessionFile = new SessionsFile(sessionsFile);\r
57   }\r
58 \r
59   /**\r
60    * make a new watcher object for the sessionsFile\r
61    * \r
62    * @return new SessionsFile watcher instance\r
63    */\r
64   public FileWatcher getSessionsWatcher() {\r
65     return new FileWatcher(this.getSessionFile().sessionFile);\r
66   }\r
67 \r
68   /**\r
69    * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions()\r
70    */\r
71   public String[] getCurrentSessions() {\r
72     String[] sessions = null;\r
73     if (this.sessionFile != null) {\r
74       SimpleSessionHandle[] sessionHandles = this.sessionFile.retrieveSessionsList();\r
75       if (sessionHandles != null) {\r
76         sessions = new String[sessionHandles.length];\r
77         for (int i = sessionHandles.length - 1; i > -1; i--) {\r
78           SimpleSessionHandle sessionHandle = sessionHandles[i];\r
79           sessions[i] = sessionHandle.getSessionUrn();\r
80         }\r
81       }\r
82     }\r
83     return sessions;\r
84   }\r
85   /**\r
86    * recover session(s) corresponding to SessionUrn\r
87    * @param urn\r
88    * @return null, or one or more SimpleSessionHandle objects with the given sessionUrn\r
89    */\r
90   public SimpleSessionHandle[] getSessionFor(SessionUrn urn) {\r
91     ArrayList sessions=new ArrayList();\r
92     if (sessionFile != null) {\r
93       SessionHandle pattern = new SessionHandle(urn.getSessionUrn());\r
94       SimpleSessionHandle[] sessionHandles = sessionFile.retrieveSessionsList();\r
95       if (sessionHandles != null) {\r
96         for (int i = sessionHandles.length - 1; i > -1; i--) {\r
97           SimpleSessionHandle sessionHandle = sessionHandles[i];\r
98           if (sessionHandle.equals(pattern))\r
99           {\r
100             sessions.add(sessionHandle);\r
101           }\r
102         }\r
103       }\r
104     }\r
105     if (sessions.size()>0)\r
106     {\r
107       SimpleSessionHandle[] sh = new SimpleSessionHandle[sessions.size()];\r
108       sessions.toArray(sh);\r
109       return sh;\r
110     }\r
111     return null;\r
112   }\r
113 \r
114   /**\r
115    * adds SessionHandle me to the sessionList\r
116    * \r
117    * @param newSession\r
118    *          session to add to the session list\r
119    * @return session index in list or 0 if lock was invalid or addSession\r
120    *         operation failed.\r
121    */\r
122   public int addSession(SimpleSessionHandle newSession) {\r
123     return this.sessionFile.addSession(newSession, false, this\r
124         .getSessionsWatcher().getChangedState());\r
125   }\r
126 \r
127   /**\r
128    * @return the sessionFile\r
129    */\r
130   private SessionsFile getSessionFile() {\r
131     return this.sessionFile;\r
132   }\r
133 \r
134   /**\r
135    * Removes a session from the list of currently active session\r
136    * \r
137    * @param session\r
138    *          SessionHandle of the session to remove\r
139    */\r
140   protected void removeSession(SessionHandle session) {\r
141     getSessionFile().removeSession(session,\r
142         this.getSessionsWatcher().getChangedState());\r
143   }\r
144 }\r