From: pmarguerite Date: Fri, 15 Dec 2006 15:03:10 +0000 (+0000) Subject: Moves sessionFile management to SimpleSessionManager calling SessionsFile class... X-Git-Tag: Release_0.2~201 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=9cb512e89c7e3016e8149d9e3df8ae6fcf9a410d;p=vamsas.git Moves sessionFile management to SimpleSessionManager calling SessionsFile class (dealing directly with the file) git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@319 be28352e-c001-0410-b1a7-c7978e42abec --- diff --git a/src/uk/ac/vamsas/client/simpleclient/SessionsFile.java b/src/uk/ac/vamsas/client/simpleclient/SessionsFile.java index d391d0a..230c633 100644 --- a/src/uk/ac/vamsas/client/simpleclient/SessionsFile.java +++ b/src/uk/ac/vamsas/client/simpleclient/SessionsFile.java @@ -1,54 +1,8 @@ -/* EMBL - The European Bioinformatics institute -* MSD Group +/* + * * VAMSAS Project -* -* Copyright (c) 2005-2006 Thr European Bioinformatics Institute. All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* -* 1. Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright -* notice, this list of conditions and the following disclaimer in -* the documentation and/or other materials provided with the -* distribution. -* -* 3. The name MSD must not be used to endorse or promote products -* derived from this software without prior written permission. For -* written permission, please contact msd-help@ebi.ac.uk -* -* 4. Products derived from this software may not be called "MSD" -* nor may "MSD" appear in their names without prior written -* permission of the MSD developers. -* -* 5. Redistributions of any form whatsoever must retain the following -* acknowledgment: -* "This product includes software developed by MSD -* (http://www.ebi.ac.uk/)" -* -* THIS SOFTWARE IS PROVIDED BY THE MSD GROUP ``AS IS'' AND ANY -* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ENSEMBL GROUP OR -* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED -* OF THE POSSIBILITY OF SUCH DAMAGE. -* -* The European Bioinformatics Institute may publish revised and/or new -* versions of this license with new releases of VAMSAS software. -*============================================================================== -* -* @author Pierre MARGUERITE * -* Dec 13, 2006 - VamsasClientV4 +* Dec 13, 2006 - VamsasClient * */ @@ -101,7 +55,7 @@ public class SessionsFile extends ListFile { private SessionHandle[] retrieveSessionHandles() { if (lockFile()) { try { - SessionHandle[] clients=null; + SessionHandle[] sessions =null; if (this.fileLock.length()>0) { ObjectInputStream is = new ObjectInputStream(this.fileLock.getBufferedInputStream(true)); @@ -109,14 +63,15 @@ public class SessionsFile extends ListFile { o=is.readObject(); if (o!=null) { try { - clients = (SessionHandle[]) o; + sessions = (SessionHandle[]) o; } catch (Exception e) { log.error("Garbage in the clientHandle list "+this.sessionFile,e); } } + // is.close(); } - return clients; + return sessions; } catch (FileNotFoundException e) { // e.printStackTrace(System.err); log.error(e); @@ -142,41 +97,41 @@ public class SessionsFile extends ListFile { } /** - * get list from the locked ClientList. + * get list from the locked SessionsList. * @param extantlock - * @return clientList or null if lock failed (or file was empty) + * @return sessionList or null if lock failed (or file was empty) */ public SessionHandle[] retrieveSessionsList(Lock extantlock) { if (lockFile(extantlock)) { - SessionHandle[] clients = retrieveSessionHandles(); + SessionHandle[] sessions = retrieveSessionHandles(); unlockFile(); - return clients; + return sessions; } return null; } /** - * adds clientHandle me to the clientList under an existing lock extantLock. - * @param me + * adds SessionHandle me to the sessionList under an existing lock extantLock. + * @param newSession * @param extantLock - * @return client index in list or 0 if lock was invalid or addClient operation failed. + * @return session index in list or 0 if lock was invalid or addSession operation failed. */ - public int addSession(SessionHandle me, Lock extantLock) { - return addSession(me, true, extantLock); + public int addSession(SessionHandle newSession, Lock extantLock) { + return addSession(newSession, true, extantLock); } /** * adds SessionsHandle me to the sessionsList under an existing lock. - * @param me - sessionsHandle - * @param disambig - if true then add will fail if an identical clientHandle already exists + * @param newSession - sessionsHandle + * @param disambig - if true then add will fail if an identical sessionHandle already exists * @param extantLock - existing lock - * @return client index in list or 0 if addClient (or the lock) failed. + * @return client index in list or 0 if addSession (or the lock) failed. */ - public int addSession(SessionHandle session, boolean disambig, Lock extantLock) { + public int addSession(SessionHandle newSession, boolean disambig, Lock extantLock) { if (lockFile(extantLock)) { - this.syncnum = addSession(session, disambig); + this.syncnum = addSession(newSession, disambig); unlockFile(); return this.syncnum; } @@ -185,23 +140,26 @@ public class SessionsFile extends ListFile { /** * removes the current session from the SessionsList without complaint if the session isn't in the sessionsList already. - * @param me client handle to be removed - * @param clientlock existing lock passed from watcher. + * @param session session handle to be removed + * @param sessionlock existing lock passed from watcher. */ - public void removeSession(SessionHandle session, Lock clientlock) { + public void removeSession(SessionHandle session, Lock sessionlock) { int mynum =-1; - if (lockFile(clientlock)) { + if (lockFile(sessionlock)) { + SessionHandle[] sessions = retrieveSessionHandles(); if (sessions != null) { - if ((this.syncnum<=0 || this.syncnum>sessions.length) || sessions[this.syncnum-1]!=session) { - for (int i = 0, j = sessions.length; i < j; i++) - if (sessions[i].equals(session)) { + if ((this.syncnum<=0 || this.syncnum>sessions.length) || !sessions[this.syncnum-1].equals(session)) { + for (int i = 0, j = sessions.length; i < j; i++) + { if (sessions[i].equals(session)) { mynum=i; break; } + } } else { mynum=this.syncnum-1; } + if (mynum>-1) { SessionHandle[] newlist = new SessionHandle[sessions.length - 1]; for (int k=0,i = 0, j = sessions.length; i < j; i++) @@ -213,14 +171,14 @@ public class SessionsFile extends ListFile { } unlockFile(); } else { - throw new Error("Couldn't get lock for "+((sessionFile==null) ? "Unitialised sessionFile in SessionsFile" : this.sessionFile.getAbsolutePath())); + throw new Error("Couldn't get lock for "+((this.sessionFile==null) ? "Unitialised sessionFile in SessionsFile" : this.sessionFile.getAbsolutePath())); } } /** * Adds a SessionHandle to the SessionList file - optionally disambiguating * the SessionHandle (modifes the URN). * Note: Caller is left to release the lock on the SessionList. - * @param me + * @param session * @param disambiguate - * flag indicating if the URN for me should be disambiguated to * differentiate between sessions. @@ -288,6 +246,7 @@ public class SessionsFile extends ListFile { new ObjectOutputStream(this.fileLock.getBufferedOutputStream(true)); log.debug("About to write "+clients.length+" sessionHandles to output stream."); os.writeObject(clients); + // os.flush(); os.close(); // All done - remove the backup. if (this.backup) diff --git a/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java b/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java index f93fa28..d948f92 100644 --- a/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java +++ b/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java @@ -28,7 +28,8 @@ import uk.ac.vamsas.client.UserHandle; /** * - * + * creates a session arena in the user home directory under .vamsas. + * Each session has its own subdirectory. */ public class SimpleClientFactory implements IClientFactory { @@ -38,7 +39,7 @@ public class SimpleClientFactory implements IClientFactory { private String vamsasSubdirectoryName = ".vamsas"; - private SessionsFile sessionFile = null; + private SimpleSessionManager sessionManager = null; private static final String SESSION_LIST="sessions.obj"; //private String[] currentlyAvailableDessions = null; @@ -62,7 +63,7 @@ public class SimpleClientFactory implements IClientFactory { String sessionArenaPath = userHomeDirectory.concat(File.separator.concat(this.vamsasSubdirectoryName)); this.initSessionArena(sessionArenaPath); - this.initFactoryObjects(); + // this.initFactoryObjects(); } @@ -105,36 +106,8 @@ public class SimpleClientFactory implements IClientFactory { } } - /** - * construct SessionFile objects and watchers for each - */ - private void initFactoryObjects() throws IOException { - if (this.sessionFile!=null ) - throw new IOException("initFactoryObjects called for initialised ClientFactory object."); - this.sessionFile = new SessionsFile(new File(this.sessionArena,SESSION_LIST)); + - } - /** - * @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; - } /** @@ -187,7 +160,7 @@ public class SimpleClientFactory implements IClientFactory { // check Uniqueness of user + ClientHandle in the session. Update clientHandle urn accordingly. // wait for lock, attach to session // create client instance - SimpleClient client = null; + IClient client = null; try { File sessionDirectory = this.convertSessionUrnToFile(sessionUrn); @@ -195,7 +168,7 @@ public class SimpleClientFactory implements IClientFactory { log.debug("found session directory "+sessionDirectory.getAbsolutePath()); VamsasSession vamsasSession = new VamsasSession(sessionDirectory); - if (userId == null) + /* if (userId == null) { //create a default userHandle //with current OS user and hostname @@ -205,7 +178,8 @@ public class SimpleClientFactory implements IClientFactory { //create simple client - client = new SimpleClient(userId, applicationHandle, vamsasSession); + client = new SimpleClient(userId, applicationHandle, vamsasSession);*/ + client = this.initClient(sessionDirectory, userId, applicationHandle); } catch (MalformedURLException e) { log.error("error while creating new IClient: incorrect session urn",e); client = null; @@ -220,6 +194,29 @@ public class SimpleClientFactory implements IClientFactory { return client; } + + private IClient initClient( File sessdir, UserHandle userId, ClientHandle clientHandle) throws IOException, InvalidSessionUrnException + { + IClient client = null; +// create session + VamsasSession vamsasSession = new VamsasSession(sessdir); + + this.getSessionManager().addSession(new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn())); + if (userId == null) + { + //create a default userHandle + //with current OS user and hostname + userId = new UserHandle(System.getProperty("user.name", System.getProperty("USERNAME","Joe Doe")), + System.getProperty("host.name",System.getProperty("HOSTNAME", "Unknown") ));// clientName, clientVersion, sessionPath); + } + + + //create simple client + client = new SimpleClient(userId, clientHandle, vamsasSession); + vamsasSession.addClient(client); + vamsasSession.setSessionManager(this.getSessionManager()); + return client; + } /** * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, uk.ac.vamsas.client.UserHandle) */ @@ -228,7 +225,7 @@ public class SimpleClientFactory implements IClientFactory { // create new session // register SimpleClient and UserHandles in session // create client instance - SimpleClient client = null; + IClient client = null; if (this.sessionArena==null) throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena."); @@ -271,7 +268,7 @@ public class SimpleClientFactory implements IClientFactory { if (!(sessdir.delete() && sessdir.mkdir())) throw new IOException("Could not make session directory "+sessdir); //create session - VamsasSession vamsasSession = new VamsasSession(sessdir); + /* VamsasSession vamsasSession = new VamsasSession(sessdir); this.getSessionFile().addSession(new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn()), false); if (userId == null) @@ -284,7 +281,8 @@ public class SimpleClientFactory implements IClientFactory { //create simple client - client = new SimpleClient(userId, clientHandle, vamsasSession); + client = new SimpleClient(userId, clientHandle, vamsasSession);*/ + client = this.initClient(sessdir, userId, clientHandle); } catch (IOException e) { @@ -300,15 +298,30 @@ public class SimpleClientFactory implements IClientFactory { /** + * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions() + */ + public String[] getCurrentSessions() { + String [] sessions = null; + try { + sessions =this.getSessionManager().getCurrentSessions(); + } catch (IOException e) { + log.error("Unable to get available sessions",e); + sessions = null; + } + return sessions; + } + + + /** * @return the sessionFile */ - private SessionsFile getSessionFile() throws IOException + private SimpleSessionManager getSessionManager() throws IOException { - if (this.sessionFile == null) + if (this.sessionManager == null) { - this.sessionFile = new SessionsFile( new File (this.sessionArena, SESSION_LIST)); + this.sessionManager = new SimpleSessionManager( new File (this.sessionArena, SESSION_LIST)); } - return this.sessionFile; + return this.sessionManager; } diff --git a/src/uk/ac/vamsas/client/simpleclient/SimpleSessionManager.java b/src/uk/ac/vamsas/client/simpleclient/SimpleSessionManager.java new file mode 100644 index 0000000..332f7e5 --- /dev/null +++ b/src/uk/ac/vamsas/client/simpleclient/SimpleSessionManager.java @@ -0,0 +1,106 @@ +/* +* 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()); + } +} + +