X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FSessionFile.java;h=9da73c0b8f3bb8d552fe7fc84343427a5edfb610;hb=d2cb3f9a881f6b937adb5c54a4b20a08eb2e99db;hp=b2538abf8b922bb53e0adf8b2e900b37484a6f54;hpb=3507be9b3515184a36445c4dcef9d389b3958041;p=vamsas.git diff --git a/src/org/vamsas/client/simpleclient/SessionFile.java b/src/org/vamsas/client/simpleclient/SessionFile.java index b2538ab..9da73c0 100644 --- a/src/org/vamsas/client/simpleclient/SessionFile.java +++ b/src/org/vamsas/client/simpleclient/SessionFile.java @@ -1,26 +1,39 @@ package org.vamsas.client.simpleclient; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileInputStream; 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; + protected SessionFile(File file) { + super(); + sessionFile = file; + } + protected boolean lockFile(Lock extantlock) { if (fileLock!=null && !fileLock.isLocked()) { fileLock.release();// tidy up invalid lock + fileLock=null; } - fileLock=extantlock; + if (extantlock!=null) + fileLock=extantlock; return lockFile(); } @@ -38,20 +51,33 @@ public class SessionFile { fileLock.release(); fileLock = null; if (sessionFile != null) { - if (sessionFile.exists()) { - // TODO: see if we need to loop-wait for locks or they just block until - // lock is made... - do { - if (fileLock!=null) - fileLock.release(); - fileLock = new Lock(sessionFile); // TODO: wait around if we can't get the lock. - } while (!fileLock.isLocked()); - // fileLock = new Lock(sessionFile); - return fileLock.isLocked(); + if (!sessionFile.exists()) { + // create new file + try { + if (!sessionFile.createNewFile()) { + log.error("Failed to create file prior to locking: "+sessionFile); + return false; + } + } catch (IOException e) { + log.error("Exception when trying to create file "+sessionFile, e); + return false; + } } + // TODO: see if we need to loop-wait for locks or they just block until + // lock is made... + long tries=500; + do { + tries--; + if (fileLock!=null && !fileLock.isLocked()) + fileLock.release(); + fileLock = new Lock(sessionFile); // TODO: wait around if we can't get the lock. + } while (tries>0 && !fileLock.isLocked()); + if (!fileLock.isLocked()) + log.error("Failed to get lock for "+sessionFile); + // fileLock = new Lock(sessionFile); + return fileLock.isLocked(); } else - throw new Error( - "org.vamsas.client.simpleclient.ClientsFile.lockList called for non-initialised ClientsFile!"); + log.error("lockFile called for non-initialised SessionFile!"); // no lock possible return false; @@ -87,17 +113,73 @@ 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()); + } + /** + * remove all trace of the sessionFile file + * + */ + protected void eraseExistence() { + unlockFile(); + if (sessionFile!=null) { + sessionFile.delete(); + sessionFile = null; + } + } + /* (non-Javadoc) + * @see org.vamsas.client.simpleclient.Lock#getBufferedInputStream(boolean) + */ + public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException { + lockFile(); + return fileLock.getBufferedInputStream(atStart); + } + + /* (non-Javadoc) + * @see org.vamsas.client.simpleclient.Lock#getBufferedOutputStream(boolean) + */ + public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException { + lockFile(); + return fileLock.getBufferedOutputStream(clear); + } + /* (non-Javadoc) + * @see org.vamsas.client.simpleclient.Lock#getFileInputStream(boolean) + */ + public FileInputStream getFileInputStream(boolean atStart) throws IOException { + lockFile(); + return fileLock.getFileInputStream(atStart); + } + + /* (non-Javadoc) + * @see org.vamsas.client.simpleclient.Lock#getFileOutputStream(boolean) + */ + public FileOutputStream getFileOutputStream(boolean clear) throws IOException { + lockFile(); + return fileLock.getFileOutputStream(clear); + } + }