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
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\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
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
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
22 package uk.ac.vamsas.client.simpleclient;
\r
24 import java.io.File;
\r
25 import java.io.IOException;
\r
26 import java.util.ArrayList;
\r
28 import uk.ac.vamsas.client.SessionHandle;
\r
32 * Class to deal with sessions
\r
34 * retrieves list of available sessions
\r
36 * adds an active session
\r
38 * removes a close session (the last client has been closed)
\r
42 public class SimpleSessionManager {
\r
43 private SessionsFile sessionFile = null;
\r
45 protected SimpleSessionManager(File sessionsFile) throws IOException {
\r
46 this.initManagerObjects(sessionsFile);
\r
50 * construct SessionFile objects and watchers for each
\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
60 * make a new watcher object for the sessionsFile
\r
62 * @return new SessionsFile watcher instance
\r
64 public FileWatcher getSessionsWatcher() {
\r
65 return new FileWatcher(this.getSessionFile().sessionFile);
\r
69 * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions()
\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
86 * recover session(s) corresponding to SessionUrn
\r
88 * @return null, or one or more SimpleSessionHandle objects with the given sessionUrn
\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
100 sessions.add(sessionHandle);
\r
105 if (sessions.size()>0)
\r
107 SimpleSessionHandle[] sh = new SimpleSessionHandle[sessions.size()];
\r
108 sessions.toArray(sh);
\r
115 * adds SessionHandle me to the sessionList
\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
122 public int addSession(SimpleSessionHandle newSession) {
\r
123 return this.sessionFile.addSession(newSession, false, this
\r
124 .getSessionsWatcher().getChangedState());
\r
128 * @return the sessionFile
\r
130 private SessionsFile getSessionFile() {
\r
131 return this.sessionFile;
\r
135 * Removes a session from the list of currently active session
\r
138 * SessionHandle of the session to remove
\r
140 protected void removeSession(SessionHandle session) {
\r
141 getSessionFile().removeSession(session,
\r
142 this.getSessionsWatcher().getChangedState());
\r