From d918fd6b06b03be5e7aa8688a643147d7bb74595 Mon Sep 17 00:00:00 2001 From: jprocter Date: Fri, 31 Aug 2007 09:55:48 +0000 Subject: [PATCH] added newSession methods to create a new vamsas session and connect to it. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@449 be28352e-c001-0410-b1a7-c7978e42abec --- src/uk/ac/vamsas/client/IClientFactory.java | 13 ++ .../client/simpleclient/SimpleClientFactory.java | 169 +++++++++++--------- 2 files changed, 106 insertions(+), 76 deletions(-) diff --git a/src/uk/ac/vamsas/client/IClientFactory.java b/src/uk/ac/vamsas/client/IClientFactory.java index 64250dc..6797fa6 100644 --- a/src/uk/ac/vamsas/client/IClientFactory.java +++ b/src/uk/ac/vamsas/client/IClientFactory.java @@ -44,6 +44,19 @@ public interface IClientFactory { * @throws NoDefaultSessionException if more than one session exists that the client may connect to */ IClient getIClient(ClientHandle applicationHandle, UserHandle userId) throws NoDefaultSessionException; + /** + * Create a new sesssion for the application with the current user + * @param applicationHandle + * @return + */ + IClient getNewSessionIClient(ClientHandle applicationHandle); + /** + * Create a new session for the application using a particular user identity + * @param applicationHandle + * @param userId + * @return + */ + IClient getNewSessionIClient(ClientHandle applicationHandle, UserHandle userId); /** * enumerate the active sessions this IClientFactory instance knows about. diff --git a/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java b/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java index 74650b0..4093290 100644 --- a/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java +++ b/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java @@ -28,8 +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. + * creates a session arena in the user home directory under .vamsas. Each + * session has its own subdirectory. */ public class SimpleClientFactory implements IClientFactory { @@ -42,19 +42,19 @@ public class SimpleClientFactory implements IClientFactory { private SimpleSessionManager sessionManager = null; private static final String SESSION_LIST="sessions.obj"; - //private String[] currentlyAvailableDessions = null; + // private String[] currentlyAvailableDessions = null; /** * default constructor - called by CreateClientFactory only. - * - *Inits the sessionarena to the directory .vamsas of the user home directory. - * + * + * Inits the sessionarena to the directory .vamsas of the user home directory. + * */ public SimpleClientFactory() throws IOException { // sessionArena - //retrieves user home directory + // retrieves user home directory String userHomeDirectory = System.getProperty("user.home"); if (userHomeDirectory == null || userHomeDirectory.length()<1) { @@ -68,20 +68,23 @@ public class SimpleClientFactory implements IClientFactory { /** - * Create a client factory that works with sessions at the given - * path. - * @param path path to directory called session arena, where will be created session directories and session files. + * Create a client factory that works with sessions at the given path. + * + * @param path + * path to directory called session arena, where will be created + * session directories and session files. */ public SimpleClientFactory(String path) throws IOException { this.initSessionArena(path); } /** - * Inits sessionArena to a given path. - * checks if path is valid. + * Inits sessionArena to a given path. checks if path is valid. * - * @param path path to a directory to use - * @throws IOException if the path is incorrect + * @param path + * path to a directory to use + * @throws IOException + * if the path is incorrect */ private void initSessionArena (String path) throws IOException { @@ -113,8 +116,8 @@ public class SimpleClientFactory implements IClientFactory { /** * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle) * - * Creates a IClient object, using default UserHandle with system variables:"user.name" or "USERNAME")), - "host.name" or "HOSTNAME" + * Creates a IClient object, using default UserHandle with system + * variables:"user.name" or "USERNAME")), "host.name" or "HOSTNAME" */ public IClient getIClient(ClientHandle applicationHandle) throws NoDefaultSessionException { @@ -126,11 +129,14 @@ public class SimpleClientFactory implements IClientFactory { /** * the URN should be something like simpleclient:FILEPATH URL encoded - * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, java.lang.String) + * + * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, + * java.lang.String) */ public IClient getIClient(ClientHandle applicationHandle, String sessionUrn) { -// locate session from Urn - // check that clientHandle is unique (with default user) - if not update the clientHandle urn to make it unique. +// locate session from Urn + // check that clientHandle is unique (with default user) - if not update the + // clientHandle urn to make it unique. // wait for lock and attach to session // create SimpleClient instance log.debug("Trying to create session with URN "+sessionUrn); @@ -152,33 +158,35 @@ public class SimpleClientFactory implements IClientFactory { } /** - * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, uk.ac.vamsas.client.UserHandle, java.lang.String) + * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, + * uk.ac.vamsas.client.UserHandle, java.lang.String) */ public IClient getIClient(ClientHandle applicationHandle, UserHandle userId, String sessionUrn) { -// locate session from Urn - // check Uniqueness of user + ClientHandle in the session. Update clientHandle urn accordingly. +// locate session from Urn + // check Uniqueness of user + ClientHandle in the session. Update + // clientHandle urn accordingly. // wait for lock, attach to session // create client instance IClient client = null; try { File sessionDirectory = this.convertSessionUrnToFile(sessionUrn); - //create session + // create session log.debug("found session directory "+sessionDirectory.getAbsolutePath()); VamsasSession vamsasSession = new VamsasSession(sessionDirectory); - /* 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, applicationHandle, vamsasSession);*/ + /* + * 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, + * applicationHandle, vamsasSession); + */ client = this.initClient(sessionDirectory, userId, applicationHandle); } catch (MalformedURLException e) { log.error("error while creating new IClient: incorrect session urn",e); @@ -198,21 +206,23 @@ public class SimpleClientFactory implements IClientFactory { private IClient initClient( File sessdir, UserHandle userId, ClientHandle clientHandle) throws IOException, InvalidSessionUrnException { IClient client = null; -// create session + // create session VamsasSession vamsasSession = new VamsasSession(sessdir); this.getSessionManager().addSession(new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn())); if (userId == null) - { - //create a default userHandle - // userId = new UserHandle(System.getProperty("user.name", System.getProperty("USERNAME","Joe Doe")), - // System.getProperty("host.name",System.getProperty("HOSTNAME", "Unknown") ));// clientName, clientVersion, sessionPath); - userId = new UserHandle(null, null); - - } + { + // create a default userHandle + // userId = new UserHandle(System.getProperty("user.name", + // System.getProperty("USERNAME","Joe Doe")), + // System.getProperty("host.name",System.getProperty("HOSTNAME", + // "Unknown") ));// clientName, clientVersion, sessionPath); + userId = new UserHandle(null, null); + } - //FullName and organisation should not be null (otherwise UserHandle equals method raises an java.lang.NullPointerException ) - //use current OS user and hostname, if null + // FullName and organisation should not be null (otherwise UserHandle equals + // method raises an java.lang.NullPointerException ) + // use current OS user and hostname, if null if ( userId.getFullName() == null) { userId.setFullName(System.getProperty("user.name", System.getProperty("USERNAME","Joe Doe"))); @@ -237,14 +247,15 @@ public class SimpleClientFactory implements IClientFactory { } } - //create simple client + // create simple client client = new SimpleClient(userId, clientHandle, vamsasSession); vamsasSession.addClient((SimpleClient) client); vamsasSession.setSessionManager(this.getSessionManager()); return client; } /** - * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, uk.ac.vamsas.client.UserHandle) + * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, + * uk.ac.vamsas.client.UserHandle) */ public IClient getIClient(ClientHandle applicationHandle, UserHandle userId) throws NoDefaultSessionException { @@ -256,7 +267,7 @@ public class SimpleClientFactory implements IClientFactory { throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena."); ClientHandle clientHandle =applicationHandle; - //create default clientHandle with "SimpleVamsasClientApp","0.1", + // create default clientHandle with "SimpleVamsasClientApp","0.1", if (clientHandle == null) clientHandle = new ClientHandle("SimpleVamsasClientApp","0.1"); else @@ -272,14 +283,14 @@ public class SimpleClientFactory implements IClientFactory { clientHandle.setVersion("0.1"); } } - //check if any available session(s) + // check if any available session(s) String[] availableSessions = this.getCurrentSessions(); if (availableSessions != null) - {//there are available sessions + {// there are available sessions if (availableSessions.length>1) - {//more than one session if available... can not choose + {// more than one session if available... can not choose - //represents list of session as String + // represents list of session as String StringBuffer sessionURNs = new StringBuffer(""); for (int i = 0; i< availableSessions.length ; i++) { @@ -288,40 +299,37 @@ public class SimpleClientFactory implements IClientFactory { throw new NoDefaultSessionException("Several sessions available, please pick one: "+sessionURNs); } - //check if only one session available. if yes, open it + // check if only one session available. if yes, open it if (availableSessions.length == 1) { - //only one session available, open it. + // only one session available, open it. return this.getIClient(clientHandle, availableSessions[0]); } - else + else { log.debug("No active session found"); + } } - //no session available - create a new one - + // no session available - create a new one + client = clientInNewSession(userId, clientHandle); + return client; + } + /** + * + * @param userId + * @param clientHandle + * @return + */ + private IClient clientInNewSession(UserHandle userId, + ClientHandle clientHandle) { + IClient client = null; try { - //create sessionDirectory + // create sessionDirectory File sessdir = File.createTempFile("sess", ".simpleclient", this.sessionArena); log.debug("Creating new session directory"); - if (!(sessdir.delete() && sessdir.mkdir())) + if (!(sessdir.delete() && sessdir.mkdir())) throw new IOException("Could not make session directory "+sessdir); - //create session - /* VamsasSession vamsasSession = new VamsasSession(sessdir); - - this.getSessionFile().addSession(new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn()), false); - 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);*/ client = this.initClient(sessdir, userId, clientHandle); } catch (IOException e) @@ -333,10 +341,8 @@ public class SimpleClientFactory implements IClientFactory { log.error("Unable to create new IClient. The session urn is incorrect ",e); } - return client; + return client; } - - /** * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions() */ @@ -364,6 +370,17 @@ public class SimpleClientFactory implements IClientFactory { return this.sessionManager; } + + public IClient getNewSessionIClient(ClientHandle applicationHandle) { + return clientInNewSession(null, applicationHandle); + } + + + public IClient getNewSessionIClient(ClientHandle applicationHandle, + UserHandle userId) { + return clientInNewSession(userId, applicationHandle); + } + } -- 1.7.10.2