From 6fd63fba9b32d6c8da1862e76f2407a08d246c95 Mon Sep 17 00:00:00 2001 From: jprocter Date: Fri, 31 Aug 2007 16:11:15 +0000 Subject: [PATCH] refactoring to enable initialisation of session from an existing vamsas archive and bug fix to store session document in an external file. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@453 be28352e-c001-0410-b1a7-c7978e42abec --- .../vamsas/client/simpleclient/VamsasSession.java | 54 ++++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/uk/ac/vamsas/client/simpleclient/VamsasSession.java b/src/uk/ac/vamsas/client/simpleclient/VamsasSession.java index 45766d4..477cec5 100644 --- a/src/uk/ac/vamsas/client/simpleclient/VamsasSession.java +++ b/src/uk/ac/vamsas/client/simpleclient/VamsasSession.java @@ -196,13 +196,25 @@ public class VamsasSession { public static final String VAMSAS_OBJ = "VamDoc.jar"; /** - * sets up the vamsas session files and watchers in sessionDir + * sets up the vamsas session files and watchers in sessionDir1 * @param sessionDir1 */ protected VamsasSession(File sessionDir1) throws IOException { + this(sessionDir1, 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 + * @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) + */ + protected VamsasSession(File sessionDir1, File extVamDoc) throws IOException { if (sessionDir1 == null) throw new Error("Null directory for VamsasSession."); if (sessionDir1.exists()) { + if (extVamDoc!=null && extVamDoc.exists()) + throw new Error("Client Initialisation Error: Cannot join an existing session directory with an existing vamsas document to import."); if (!sessionDir1.isDirectory() || !sessionDir1.canWrite() || !sessionDir1.canRead()) throw new IOException("Cannot access '" + sessionDir1 @@ -220,7 +232,7 @@ public class VamsasSession { if (!sessionDir1.mkdir()) throw new IOException("Failed to make VamsasSession directory in " + sessionDir1); - createSessionFiles(); + createSessionFiles(extVamDoc); initSessionObjects(); slog.debug("Session directory created."); log.debug("Initialised VamsasSession in " + sessionDir1); @@ -245,6 +257,14 @@ public class VamsasSession { * */ private void createSessionFiles() throws IOException { + createSessionFiles(null); + } + /** + * + * @param extVamDoc null or an existing vamsas document to initialise session with + * @throws IOException + */ + private void createSessionFiles(File extVamDoc) throws IOException { if (sessionDir == null) throw new IOException( "Invalid call to createSessionFiles() with null sessionDir"); @@ -252,8 +272,32 @@ public class VamsasSession { File v_doc = new File(sessionDir, VAMSAS_OBJ); if (!c_file.exists() && c_file.createNewFile()) log.debug("Created new ClientFile " + c_file); // don't care if this works or not - if (!v_doc.exists() && v_doc.createNewFile()) - log.debug("Created new Vamsas Session Document File " + v_doc); + if (!v_doc.exists()) + { + if (extVamDoc==null) + { + if (v_doc.createNewFile()) + { + log.debug("Created new Vamsas Session Document File " + v_doc); + } else { + log.warn("Didn't create Vamsas Session Document file in "+v_doc); + } + } else { + log.debug("Creating new session document from "+extVamDoc); + try { + SessionFile sesdoc = new SessionFile(new File(sessionDir, VAMSAS_OBJ)); + SessionFile extdoc = new SessionFile(extVamDoc); + sesdoc.updateFrom(null, extdoc); + } catch (Exception e) + { + v_doc.delete(); + v_doc.createNewFile(); + log.warn("Problem initialising new session ("+v_doc+") from existing vamsas document ("+extVamDoc+")",e); + throw new IOException("Couldn't initialise session from existing vamsas document"); + } + log.debug("Session document initialised from "+extVamDoc); + } + } } /** @@ -353,7 +397,7 @@ public class VamsasSession { while (!vamArchive.lockFile()) log.info("Trying to get lock for " + vamArchive.sessionFile); // TODO: LATER: decide if a provenance entry should be written in the exported document recording the export from the session - newdoc.updateFrom(extlock, vamArchive); + newdoc.updateFrom(null, vamArchive); // LATER: LATER: fix use of updateFrom for file systems where locks cannot be made (because they don't have a lockManager, ie NFS/Unix, etc). vamArchive.unLock(); newdoc.unlockFile(); -- 1.7.10.2