From 2e9acb15a289a317f04e6161bace75de35884e01 Mon Sep 17 00:00:00 2001 From: jprocter Date: Thu, 18 Feb 2010 12:08:50 +0000 Subject: [PATCH] vamsas API version 0.2 - allow new sessions or imported sessions to be created with a given session name git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@511 be28352e-c001-0410-b1a7-c7978e42abec --- src/uk/ac/vamsas/client/IClientFactory.java | 32 ++++++++++-- .../client/simpleclient/SimpleClientFactory.java | 54 +++++++++++++++----- .../vamsas/client/simpleclient/VamsasSession.java | 36 +++++++++++-- 3 files changed, 102 insertions(+), 20 deletions(-) diff --git a/src/uk/ac/vamsas/client/IClientFactory.java b/src/uk/ac/vamsas/client/IClientFactory.java index 5f16a6e..0ec1d79 100644 --- a/src/uk/ac/vamsas/client/IClientFactory.java +++ b/src/uk/ac/vamsas/client/IClientFactory.java @@ -28,7 +28,8 @@ import java.io.File; * * @author jimp * - * (it's VORBA, not CORBA!) + * @note (it's VORBA, not CORBA!) + * @history added additional sessionName argument for openAsNewSessionIClient method (v0.2) */ public interface IClientFactory { @@ -97,7 +98,7 @@ public interface IClientFactory { /** * Create a new session and import an existing vamsas document into it. - * + * Session name will inherit from vamsasDocument path. * @param applicationHandle * @param vamsasDocument * @return @@ -106,6 +107,17 @@ public interface IClientFactory { File vamsasDocument) throws InvalidSessionDocumentException; /** + * Create a new session and import an existing vamsas document into it. + * + * @param applicationHandle + * @param vamsasDocument + * @param sessionName - preferred session name (may be null) + * @return + */ + IClient openAsNewSessionIClient(ClientHandle applicationHandle, + File vamsasDocument, String sessionName) throws InvalidSessionDocumentException; + + /** * Create a new session as a particular user and import an existing vamsas * document into it. * @@ -116,8 +128,20 @@ public interface IClientFactory { */ IClient openAsNewSessionIClient(ClientHandle applicationHandle, UserHandle userId, File vamsasDocument) - throws InvalidSessionDocumentException; - + throws InvalidSessionDocumentException; + /** + * Create a new session as a particular user and import an existing vamsas + * document into it. + * + * @param applicationHandle + * @param userId + * @param vamsasDocument + * @param sessionName - preferred session name (may be null) + * @return + */ + IClient openAsNewSessionIClient(ClientHandle applicationHandle, + UserHandle userId, File vamsasDocument, String sessionName) + throws InvalidSessionDocumentException; /** * enumerate the active sessions this IClientFactory instance knows about. Can * be used by caller to pick a session on catching a diff --git a/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java b/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java index 58e9160..d85127f 100644 --- a/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java +++ b/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java @@ -223,7 +223,7 @@ public class SimpleClientFactory implements IClientFactory { * applicationHandle, vamsasSession); */ client = this.initClient(sessionDirectory, userId, applicationHandle, - null); + null,null); } catch (MalformedURLException e) { log.error("error while creating new IClient: incorrect session urn", e); client = null; @@ -253,6 +253,7 @@ public class SimpleClientFactory implements IClientFactory { * @param vamsasDocument * null or a document to pass to SimpleCLient to write into the * sessdir + * @param preferredName - name to use instead of vamsasDocument path when creating new session URN * @return the client * @throws IOException * if there are problems in session or client creation or if the @@ -261,7 +262,7 @@ public class SimpleClientFactory implements IClientFactory { * for a malformed sessdir */ private IClient initClient(File sessdir, UserHandle userId, - ClientHandle clientHandle, File vamsasDocument) throws IOException, + ClientHandle clientHandle, File vamsasDocument, String preferredName) throws IOException, InvalidSessionUrnException, InvalidSessionDocumentException { IClient client = null; // create session @@ -269,7 +270,7 @@ public class SimpleClientFactory implements IClientFactory { if (vamsasDocument == null) { vamsasSession = new VamsasSession(sessdir); } else { - vamsasSession = new VamsasSession(sessdir, vamsasDocument); + vamsasSession = new VamsasSession(sessdir, vamsasDocument,preferredName); } getSessionManager().addSession(vamsasSession.getSessionUrn()); if (userId == null) { @@ -364,7 +365,7 @@ public class SimpleClientFactory implements IClientFactory { } // no session available - create a new one try { - client = clientInNewSession(userId, clientHandle, null); + client = clientInNewSession(userId, clientHandle, null,null); } catch (Exception e) { throw new Error( "IMPLEMENTATION ERROR: unexpected exception when creating a new session to connect to.", @@ -381,10 +382,11 @@ public class SimpleClientFactory implements IClientFactory { * @param clientHandle * @param vamsasDocument * null or a document file to copy into the new session + * @param preferredName - name to be used as base for the new session URN * @return null or a valid IClient instance */ private IClient clientInNewSession(UserHandle userId, - ClientHandle clientHandle, File vamsasDocument) + ClientHandle clientHandle, File vamsasDocument, String preferredName) throws InvalidSessionDocumentException, InvalidSessionUrnException { IClient client = null; @@ -392,8 +394,14 @@ public class SimpleClientFactory implements IClientFactory { // try and make a friendly session name String sesspref = ""; if (vamsasDocument != null) { - sesspref = vamsasDocument.getName().replaceAll( - "([^-A-Za-z0-9]|\\.vdj)", ""); + if (preferredName!=null) + { + sesspref = preferredName.replaceAll( + "([^-A-Za-z0-9]|\\.vdj)", ""); + } else { + sesspref = vamsasDocument.getName().replaceAll( + "([^-A-Za-z0-9]|\\.vdj)", ""); + } } sesspref += (new java.util.Date()).toString().replaceAll("[^-A-Za-z0-9]", "_"); @@ -411,7 +419,7 @@ public class SimpleClientFactory implements IClientFactory { log.debug("Creating new session directory"); if (!(sessdir.delete() && sessdir.mkdir())) throw new IOException("Could not make session directory " + sessdir); - client = initClient(sessdir, userId, clientHandle, vamsasDocument); + client = initClient(sessdir, userId, clientHandle, vamsasDocument, preferredName); } catch (IOException e) { log.error("error while creating new IClient", e); } catch (InvalidSessionUrnException e) { @@ -449,7 +457,7 @@ public class SimpleClientFactory implements IClientFactory { public IClient getNewSessionIClient(ClientHandle applicationHandle) { try { - return clientInNewSession(null, applicationHandle, null); + return clientInNewSession(null, applicationHandle, null,null); } catch (Exception e) { log.error("Failed to create new session for app with default user.", e); } @@ -459,7 +467,7 @@ public class SimpleClientFactory implements IClientFactory { public IClient getNewSessionIClient(ClientHandle applicationHandle, UserHandle userId) { try { - return clientInNewSession(userId, applicationHandle, null); + return clientInNewSession(userId, applicationHandle, null,null); } catch (Exception e) { log.error("Failed to create new session for app and user.", e); } @@ -482,7 +490,7 @@ public class SimpleClientFactory implements IClientFactory { File vamsasDocument) throws InvalidSessionDocumentException { checkImportedDocument(vamsasDocument); try { - return clientInNewSession(null, applicationHandle, vamsasDocument); + return clientInNewSession(null, applicationHandle, vamsasDocument,null); } catch (InvalidSessionUrnException e) { throw new InvalidSessionDocumentException("Unexpected exception", e); } @@ -493,7 +501,29 @@ public class SimpleClientFactory implements IClientFactory { throws InvalidSessionDocumentException { checkImportedDocument(vamsasDocument); try { - return clientInNewSession(userId, applicationHandle, vamsasDocument); + return clientInNewSession(userId, applicationHandle, vamsasDocument,null); + } catch (InvalidSessionUrnException e) { + throw new InvalidSessionDocumentException("Unexpected exception", e); + } + } + + public IClient openAsNewSessionIClient(ClientHandle applicationHandle, + File vamsasDocument, String sessionName) + throws InvalidSessionDocumentException { + checkImportedDocument(vamsasDocument); + try { + return clientInNewSession(null, applicationHandle, vamsasDocument, sessionName); + } catch (InvalidSessionUrnException e) { + throw new InvalidSessionDocumentException("Unexpected exception", e); + } + } + + public IClient openAsNewSessionIClient(ClientHandle applicationHandle, + UserHandle userId, File vamsasDocument, String sessionName) + throws InvalidSessionDocumentException { + checkImportedDocument(vamsasDocument); + try { + return clientInNewSession(userId, applicationHandle, vamsasDocument, sessionName); } catch (InvalidSessionUrnException e) { throw new InvalidSessionDocumentException("Unexpected exception", e); } diff --git a/src/uk/ac/vamsas/client/simpleclient/VamsasSession.java b/src/uk/ac/vamsas/client/simpleclient/VamsasSession.java index 310c6c2..ffbdfa6 100644 --- a/src/uk/ac/vamsas/client/simpleclient/VamsasSession.java +++ b/src/uk/ac/vamsas/client/simpleclient/VamsasSession.java @@ -35,6 +35,7 @@ import org.apache.log4j.PatternLayout; import uk.ac.vamsas.client.ClientHandle; import uk.ac.vamsas.client.Events; +import uk.ac.vamsas.client.InvalidSessionUrnException; import uk.ac.vamsas.client.UserHandle; /** @@ -228,6 +229,23 @@ public class VamsasSession { * new data into session in this way) */ protected VamsasSession(File sessionDir1, File extVamDoc) throws IOException { + this(sessionDir1,extVamDoc,null); + } + /** + * sets up the vamsas session files and watchers in sessionDir1 + * + * @param sessionDir1 + * @param extVamDoc + * null or an existing archive to initialise the session with + * @param preferredName - optional string to use to generate a new session URI + * @throws any + * IOExceptions from creating session directory and files. + * @throws error + * if both extVamDoc and sessionDir1 already exist (cannot import + * new data into session in this way) + * MalformedURI if preferredName cannot be used to derive a URI of the form simpleclient::preferredName + */ + public VamsasSession(File sessionDir1, File extVamDoc, String preferredName) throws IOException { // TODO: refactor to separate extVamDoc path from session URN - enables non-local URLs to be locally bound to sessions. if (sessionDir1 == null) throw new Error("Null directory for VamsasSession."); @@ -250,12 +268,21 @@ public class VamsasSession { } } this.sessionDir = sessionDir1; - if (extVamDoc==null) { - sessionHandle = new SimpleSessionHandle(new SessionUrn(sessionDir).getSessionUrn(),sessionDir); - } else { + // construct session URN + SessionUrn sessUrn = new SessionUrn(sessionDir); + if (extVamDoc!=null) { // Construct Session URN from the original vamsas document. - sessionHandle = new SimpleSessionHandle(new SessionUrn(extVamDoc).getSessionUrn(),sessionDir); + sessUrn = new SessionUrn(extVamDoc); } + if (preferredName!=null) { + try { + sessUrn = new SessionUrn(preferredName); + } catch (InvalidSessionUrnException e) { + throw new Error("Malformed URI : preferredName = "+preferredName,e); + } + } + // create the session handle + sessionHandle = new SimpleSessionHandle(sessUrn.getSessionUrn(),sessionDir); initSessionObjects(); if (existingSession) { slog.debug("Initialising additional VamsasSession instance"); @@ -271,6 +298,7 @@ public class VamsasSession { log.debug("Initialised VamsasSession in " + sessionDir1); } + /** * tests presence of existing sessionfiles files in dir * -- 1.7.10.2