From: jprocter Date: Wed, 2 Nov 2005 16:11:46 +0000 (+0000) Subject: refactored common locked IO methods from ClientsFile to SessionFile X-Git-Tag: Release_0.2~435 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=3507be9b3515184a36445c4dcef9d389b3958041;p=vamsas.git refactored common locked IO methods from ClientsFile to SessionFile git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@85 be28352e-c001-0410-b1a7-c7978e42abec --- diff --git a/src/org/vamsas/client/simpleclient/ClientsFile.java b/src/org/vamsas/client/simpleclient/ClientsFile.java index 6cc2219..012c9d6 100644 --- a/src/org/vamsas/client/simpleclient/ClientsFile.java +++ b/src/org/vamsas/client/simpleclient/ClientsFile.java @@ -20,73 +20,18 @@ import java.util.Vector; * Handler for the clientsFile within a vamsas session thread. * @author jim */ -public class ClientsFile { - private File filelist; - +public class ClientsFile extends SessionFile { /** - * number of my client in list (not known at start but used when known to make - * lock) + * number of my client in list - passed back when a client + * is added to list, and used (if valid) for quickly + * looking up presence of client handle in the list. */ private int syncnum = 1; public ClientsFile(File filelist) throws IOException { - this.filelist = filelist; - if (!this.filelist.exists()) - this.filelist.createNewFile(); - } - - private Lock listlock = null; - - protected boolean lockList(Lock extantlock) { - if (listlock!=null && !listlock.isLocked()) { - listlock.release();// tidy up invalid lock - } - listlock=extantlock; - return lockList(); - } - /** - * Get a lock for the ClientsFile - * - * @return true if lock was made - */ - protected boolean lockList() { - if (listlock != null) - if (listlock.isLocked()) - return true; - else - // lock failed for some reason. - listlock.release(); - listlock = null; - if (filelist != null) { - if (filelist.exists()) { - // TODO: see if we need to loop-wait for locks or they just block until - // lock is made... - do { - if (listlock!=null) - listlock.release(); - listlock = new Lock(filelist); // TODO: wait around if we can't get the lock. - } while (!listlock.isLocked()); - // listlock = new Lock(filelist); - return listlock.isLocked(); - } - } else - throw new Error( - "org.vamsas.client.simpleclient.ClientsFile.lockList called for non-initialised ClientsFile!"); - - // no lock possible - return false; - } - - /** - * Explicitly release the ClientsFile lock. - * - * @return true if lock was released. - */ - protected void unlockList() { - if (listlock != null) { - listlock.release(); - listlock = null; - } + this.sessionFile = filelist; + if (!this.sessionFile.exists()) + this.sessionFile.createNewFile(); } /** @@ -96,12 +41,12 @@ public class ClientsFile { * @return list of clients */ private ClientHandle[] retrieveClientHandles() { - if (lockList()) { + if (lockFile()) { try { ClientHandle[] clients=null; - if (this.listlock.rafile.length()>0) { + if (this.fileLock.rafile.length()>0) { ObjectInputStream is = new ObjectInputStream(new BufferedInputStream( - new java.io.FileInputStream(filelist))); + new java.io.FileInputStream(sessionFile))); Object o; o=is.readObject(); if (o!=null) { @@ -109,7 +54,7 @@ public class ClientsFile { clients = (ClientHandle[]) o; } catch (Exception e) { - System.err.println("Garbage in the clientHandle list "+this.filelist); + System.err.println("Garbage in the clientHandle list "+this.sessionFile); } } } @@ -128,9 +73,9 @@ public class ClientsFile { * @return clientList */ public ClientHandle[] retrieveClientList() { - if (lockList()) { + if (lockFile()) { ClientHandle[] clients = retrieveClientHandles(); - unlockList(); + unlockFile(); return clients; } return null; @@ -141,9 +86,9 @@ public class ClientsFile { * @return clientList or null if lock failed (or file was empty) */ public ClientHandle[] retrieveClientList(Lock extantlock) { - if (lockList(extantlock)) { + if (lockFile(extantlock)) { ClientHandle[] clients = retrieveClientHandles(); - unlockList(); + unlockFile(); return clients; } return null; @@ -167,9 +112,9 @@ public class ClientsFile { */ public int addClient(ClientHandle me, boolean disambig, Lock extantLock) { - if (lockList(extantLock)) { + if (lockFile(extantLock)) { syncnum = addClient(me, disambig); - unlockList(); + unlockFile(); return syncnum; } return 0; @@ -186,7 +131,7 @@ public class ClientsFile { public int addClient(ClientHandle me) { syncnum = addClient(me, true); - unlockList(); + unlockFile(); return syncnum; } @@ -197,7 +142,7 @@ public class ClientsFile { */ public void removeClient(ClientHandle me, Lock clientlock) { int mynum=-1; - if (lockList(clientlock)) { + if (lockFile(clientlock)) { ClientHandle[] clients = retrieveClientHandles(); if (clients != null) { if ((syncnum<=0 || syncnum>clients.length) || clients[syncnum-1]!=me) { @@ -218,9 +163,9 @@ public class ClientsFile { throw new Error("Failed to write new clientList!"); // failed to put the clientList to disk. } } - unlockList(); + unlockFile(); } else { - throw new Error("Couldn't get lock for "+((filelist==null) ? "Unitialised filelist in ClientsFile" : filelist.getAbsolutePath())); + throw new Error("Couldn't get lock for "+((sessionFile==null) ? "Unitialised sessionFile in ClientsFile" : sessionFile.getAbsolutePath())); } } /** @@ -236,7 +181,7 @@ public class ClientsFile { */ protected int addClient(ClientHandle me, boolean disambiguate) { int newclient = 0; - if (lockList()) { + if (lockFile()) { ClientHandle[] clients = retrieveClientHandles(); if (me.getClientUrn()==null) { // TODO: move this into ClientUrn as a standard form method. @@ -276,35 +221,21 @@ public class ClientsFile { } return newclient; } - + /** - * safely writes clients array to the file referred to by filelist. + * safely writes clients array to the file referred to by sessionFile. * * @param clients * @return true if successful write. Throws Errors otherwise. */ protected boolean putClientList(ClientHandle[] clients) { - if (lockList()) { - File templist = null; - try { - templist = File.createTempFile(filelist.getName(),".old", filelist.getParentFile()); - FileOutputStream tos = new FileOutputStream(templist); - tos.getChannel().transferFrom(listlock.rafile.getChannel(), 0, - listlock.rafile.length()); - tos.close(); - } catch (FileNotFoundException e1) { - System.err.println("Can't create temp file for clientlist"); - e1.printStackTrace(System.err); - } catch (IOException e1) { - System.err - .println("Error when copying content to temp file for clientlist"); - e1.printStackTrace(System.err); - } + if (lockFile()) { + File templist = backupSessionFile(); if (templist != null) { try { - listlock.rafile.setLength(0); + fileLock.rafile.setLength(0); ObjectOutputStream os = new ObjectOutputStream( - new BufferedOutputStream(new FileOutputStream(this.filelist))); + new BufferedOutputStream(new FileOutputStream(this.sessionFile))); os.writeObject(clients); os.close(); // All done - remove the backup. @@ -313,7 +244,7 @@ public class ClientsFile { } catch (Exception e) { if (templist != null) { System.err - .println("Serious - problems writing to filelist. Backup in " + .println("Serious - problems writing to sessionFile. Backup in " + templist.getAbsolutePath()); e.printStackTrace(System.err); } @@ -324,8 +255,8 @@ public class ClientsFile { } } else { throw new Error("Could not lock the clientList: " - + ((filelist == null) ? "Unitialized ClientsFile" - : " failed to get lock on " + filelist.getAbsolutePath())); + + ((sessionFile == null) ? "Unitialized ClientsFile" + : " failed to get lock on " + sessionFile.getAbsolutePath())); } // successful! return true; diff --git a/src/org/vamsas/client/simpleclient/FileWatcher.java b/src/org/vamsas/client/simpleclient/FileWatcher.java index 40038ad..a3225db 100644 --- a/src/org/vamsas/client/simpleclient/FileWatcher.java +++ b/src/org/vamsas/client/simpleclient/FileWatcher.java @@ -95,7 +95,7 @@ public class FileWatcher { } /** * updates internal record of file state when caller has intentionally - * modified subject. + * modified subject. (ignores locked-state of subject) */ public void setState() { if (subject!=null) { diff --git a/src/org/vamsas/client/simpleclient/SessionFile.java b/src/org/vamsas/client/simpleclient/SessionFile.java new file mode 100644 index 0000000..b2538ab --- /dev/null +++ b/src/org/vamsas/client/simpleclient/SessionFile.java @@ -0,0 +1,103 @@ +package org.vamsas.client.simpleclient; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * Basic methods for classes handling locked IO on files + * monitored by all (simpleclient) clients in a vamsas session. + * @author jimp + * + */ +public class SessionFile { + + protected File sessionFile; + protected Lock fileLock = null; + + protected boolean lockFile(Lock extantlock) { + if (fileLock!=null && !fileLock.isLocked()) { + fileLock.release();// tidy up invalid lock + } + fileLock=extantlock; + return lockFile(); + } + + /** + * Get a lock for the SessionFile + * + * @return true if lock was made + */ + protected boolean lockFile() { + if (fileLock != null) + if (fileLock.isLocked()) + return true; + else + // lock failed for some reason. + 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(); + } + } else + throw new Error( + "org.vamsas.client.simpleclient.ClientsFile.lockList called for non-initialised ClientsFile!"); + + // no lock possible + return false; + } + + /** + * Explicitly release the SessionFile's lock. + * + * @return true if lock was released. + */ + protected void unlockFile() { + if (fileLock != null) { + fileLock.release(); + fileLock = null; + } + } + + /** + * Makes a backup of the sessionFile. + * @return Backed up SessionFile or null if failed to make backup. + */ + protected File backupSessionFile() { + return backupSessionFile(null, sessionFile.getName(),".old", sessionFile.getParentFile()); + } + + protected File backupSessionFile(Lock extantLock, String backupPrefix, String backupSuffix, File backupDir) { + File tempfile=null; + if (lockFile(extantLock)) { + try { + tempfile = File.createTempFile(backupPrefix, backupSuffix, backupDir); + FileOutputStream tos = new FileOutputStream(tempfile); + tos.getChannel().transferFrom(fileLock.rafile.getChannel(), 0, + fileLock.rafile.length()); + tos.close(); + } catch (FileNotFoundException e1) { + System.err.println("Can't create temp file for "+sessionFile.getName()); + e1.printStackTrace(System.err); + tempfile=null; + } catch (IOException e1) { + System.err + .println("Error when copying content to temp file for "+sessionFile.getName()); + e1.printStackTrace(System.err); + tempfile=null; + } + } + return tempfile; + } + +}