X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FSessionFile.java;h=30abd99dda4ef1a56995811a693e021fd66f0051;hb=65a8158d49f3c25e78deba6ddd2f4a83fc695821;hp=b43b1a99d2842ea480713559a16902f33e657f52;hpb=d96929b52a9e440e9de58d1b8ad71dbb42032ccb;p=vamsas.git diff --git a/src/org/vamsas/client/simpleclient/SessionFile.java b/src/org/vamsas/client/simpleclient/SessionFile.java index b43b1a9..30abd99 100644 --- a/src/org/vamsas/client/simpleclient/SessionFile.java +++ b/src/org/vamsas/client/simpleclient/SessionFile.java @@ -5,14 +5,17 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + /** * Basic methods for classes handling locked IO on files * monitored by all (simpleclient) clients in a vamsas session. * @author jimp - * + *TODO: support non nio file locking capable systems */ public class SessionFile { - + private static Log log = LogFactory.getLog(SessionFile.class); protected File sessionFile; protected Lock fileLock = null; @@ -55,8 +58,7 @@ public class SessionFile { return fileLock.isLocked(); } } else - throw new Error( - "org.vamsas.client.simpleclient.SessionFile.lockFile called for non-initialised SessionFile!"); + log.error("lockFile called for non-initialised SessionFile!"); // no lock possible return false; @@ -92,17 +94,30 @@ public class SessionFile { fileLock.rafile.length()); tos.close(); } catch (FileNotFoundException e1) { - System.err.println("Can't create temp file for "+sessionFile.getName()); - e1.printStackTrace(System.err); + log.warn("Can't create temp file for "+sessionFile.getName(),e1); tempfile=null; } catch (IOException e1) { - System.err - .println("Error when copying content to temp file for "+sessionFile.getName()); - e1.printStackTrace(System.err); + log.warn("Error when copying content to temp file for "+sessionFile.getName(),e1); tempfile=null; } } return tempfile; } - + /** + * Replaces data in sessionFile with data from file handled by another sessionFile + * passes up any exceptions. + * @param newData source for new data + */ + protected void updateFrom(Lock extantLock, SessionFile newData) throws IOException { + log.debug("Updating "+sessionFile.getAbsolutePath()+" from "+newData.sessionFile.getAbsolutePath()); + if (newData==null) + throw new IOException("Null newData object."); + if (newData.sessionFile==null) + throw new IOException("Null SessionFile in newData."); + + lockFile(extantLock); + newData.lockFile(); + fileLock.rafile.getChannel().transferFrom(newData.fileLock.rafile.getChannel(), 0, + newData.fileLock.rafile.length()); + } }