From f7f0c666c7183293400a45614a4febb3eb9f23d5 Mon Sep 17 00:00:00 2001 From: jprocter Date: Fri, 14 Sep 2007 13:47:14 +0000 Subject: [PATCH] implement new client factory methods to open a stored session document in a new vamsas session. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@464 be28352e-c001-0410-b1a7-c7978e42abec --- .../client/simpleclient/SimpleClientFactory.java | 116 +++++++++++++++++--- 1 file changed, 100 insertions(+), 16 deletions(-) diff --git a/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java b/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java index 8cc74a1..27aaf77 100644 --- a/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java +++ b/src/uk/ac/vamsas/client/simpleclient/SimpleClientFactory.java @@ -20,6 +20,7 @@ import org.apache.commons.logging.LogFactory; import uk.ac.vamsas.client.ClientHandle; import uk.ac.vamsas.client.IClient; import uk.ac.vamsas.client.IClientFactory; +import uk.ac.vamsas.client.InvalidSessionDocumentException; import uk.ac.vamsas.client.InvalidSessionUrnException; import uk.ac.vamsas.client.NoDefaultSessionException; import uk.ac.vamsas.client.SessionHandle; @@ -183,11 +184,14 @@ public class SimpleClientFactory implements IClientFactory { * //create simple client client = new SimpleClient(userId, * applicationHandle, vamsasSession); */ - client = this.initClient(sessionDirectory, userId, applicationHandle); + client = this.initClient(sessionDirectory, userId, applicationHandle, null); } catch (MalformedURLException e) { log.error("error while creating new IClient: incorrect session urn", e); client = null; - + } catch (InvalidSessionDocumentException e) + { + log.error("error while creating new IClient: invalid session document", e); + client = null; } catch (InvalidSessionUrnException e) { log.error("error while creating new IClient: incorrect session urn", e); client = null; @@ -197,12 +201,27 @@ public class SimpleClientFactory implements IClientFactory { } return client; } - + /** + * initialise the vamsas session state and create a SimpleClient object to connect to it + * @param sessdir newly created or existing session directory + * @param userId + * @param clientHandle + * @param vamsasDocument null or a document to pass to SimpleCLient to write into the sessdir + * @return the client + * @throws IOException if there are problems in session or client creation or if the session already has a vamsasDocument + * @throws InvalidSessionUrnException for a malformed sessdir + */ private IClient initClient(File sessdir, UserHandle userId, - ClientHandle clientHandle) throws IOException, InvalidSessionUrnException { + ClientHandle clientHandle, File vamsasDocument) throws IOException, InvalidSessionUrnException, InvalidSessionDocumentException { IClient client = null; // create session - VamsasSession vamsasSession = new VamsasSession(sessdir); + VamsasSession vamsasSession = null; + if (vamsasDocument==null) + { + vamsasSession = new VamsasSession(sessdir); + } else { + vamsasSession = new VamsasSession(sessdir, vamsasDocument); + } this.getSessionManager().addSession( new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn())); @@ -296,33 +315,55 @@ public class SimpleClientFactory implements IClientFactory { } } // no session available - create a new one - - client = clientInNewSession(userId, clientHandle); + try { + client = clientInNewSession(userId, clientHandle, null); + } catch (Exception e) + { + throw new Error("IMPLEMENTATION ERROR: unexpected exception when creating a new session to connect to.",e); + } return client; } /** - * + * create a new session directory and possibly import an existing document into it * @param userId * @param clientHandle - * @return + * @param vamsasDocument null or a document file to copy into the new session + * @return null or a valid IClient instance */ private IClient clientInNewSession(UserHandle userId, - ClientHandle clientHandle) { + ClientHandle clientHandle, File vamsasDocument) throws InvalidSessionDocumentException, InvalidSessionUrnException{ IClient client = null; try { + // try and make a friendly session name + String sesspref = ""; + if (vamsasDocument!=null) + { + sesspref = vamsasDocument.getName().replaceAll("([^-A-Za-z0-9]|\\.vdj)", ""); + } + sesspref += (new java.util.Date()).toString().replaceAll("[^-A-Za-z0-9]","_"); // create sessionDirectory - File sessdir = File.createTempFile("sess", ".simpleclient", - this.sessionArena); + File sessdir = new File(sessionArena, sesspref+".simpleclient"); + if (sessdir.exists()) + { + // make a unique session name + sessdir = File.createTempFile(sesspref, ".simpleclient", + sessionArena); + } else { + if (!sessdir.createNewFile()) + { + throw new Error("VAMSAS Implementation error : sesspref friendly session name is invalid on this platform - please tell the authors!"); + } + } log.debug("Creating new session directory"); if (!(sessdir.delete() && sessdir.mkdir())) throw new IOException("Could not make session directory " + sessdir); - client = this.initClient(sessdir, userId, clientHandle); + client = initClient(sessdir, userId, clientHandle, vamsasDocument); } catch (IOException e) { log.error("error while creating new IClient", e); } catch (InvalidSessionUrnException e) { - log.error("Unable to create new IClient. The session urn is incorrect ", + log.error("Unable to create new IClient. The new session urn is malformed.", e); } @@ -355,12 +396,55 @@ public class SimpleClientFactory implements IClientFactory { } public IClient getNewSessionIClient(ClientHandle applicationHandle) { - return clientInNewSession(null, applicationHandle); + try { + return clientInNewSession(null, applicationHandle, null); + } + catch (Exception e) { + log.error("Failed to create new session for app with default user.",e); + } + return null; } public IClient getNewSessionIClient(ClientHandle applicationHandle, UserHandle userId) { - return clientInNewSession(userId, applicationHandle); + try { + return clientInNewSession(userId, applicationHandle, null); + } catch (Exception e) { + log.error("Failed to create new session for app and user.",e); + } + return null; + } + private void checkImportedDocument(File vamsasDocument) throws InvalidSessionDocumentException + { + if (!vamsasDocument.exists()) + { + throw new InvalidSessionDocumentException("File "+vamsasDocument+" does not exist"); + } + if (!vamsasDocument.canRead()) + { + throw new InvalidSessionDocumentException("File "+vamsasDocument+" does not exist"); + } + } + public IClient openAsNewSessionIClient(ClientHandle applicationHandle, + File vamsasDocument) throws InvalidSessionDocumentException { + checkImportedDocument(vamsasDocument); + try { + return clientInNewSession(null, applicationHandle, vamsasDocument); + } catch (InvalidSessionUrnException e) + { + throw new InvalidSessionDocumentException("Unexpected exception", e); + } + } + + public IClient openAsNewSessionIClient(ClientHandle applicationHandle, + UserHandle userId, File vamsasDocument) throws InvalidSessionDocumentException { + checkImportedDocument(vamsasDocument); + try { + return clientInNewSession(userId, applicationHandle, vamsasDocument); + } catch (InvalidSessionUrnException e) + { + throw new InvalidSessionDocumentException("Unexpected exception", e); + } } } -- 1.7.10.2