From: jprocter Date: Thu, 28 Jun 2007 14:38:02 +0000 (+0000) Subject: use the simpleClient's lockFactory for the client's lockfile to avoid platform depend... X-Git-Tag: Release_0.2~106 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=d6ac358fdc0e79968dd68a5bad050a763403191a;p=vamsas.git use the simpleClient's lockFactory for the client's lockfile to avoid platform dependent lock creation issues. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@414 be28352e-c001-0410-b1a7-c7978e42abec --- diff --git a/src/uk/ac/vamsas/client/simpleclient/SimpleClient.java b/src/uk/ac/vamsas/client/simpleclient/SimpleClient.java index 9cbdcb7..fccfba7 100644 --- a/src/uk/ac/vamsas/client/simpleclient/SimpleClient.java +++ b/src/uk/ac/vamsas/client/simpleclient/SimpleClient.java @@ -51,9 +51,7 @@ public class SimpleClient implements IClient { - private java.nio.channels.FileLock activeClientFilelock = null; - - private FileChannel activeClientFileChannel = null; + private Lock activeClientFilelock = null; private File clientlockFile = null; /** @@ -515,16 +513,19 @@ public class SimpleClient implements IClient { log.debug("Releasing active client file"); if( activeClientFilelock != null) - // Release the lock + {// Release the lock activeClientFilelock.release(); - - if (activeClientFileChannel != null) - // Close the file - activeClientFileChannel.close(); + activeClientFilelock = null; + } else { + log.debug("ReleaseActiveClientFile called when client has no lock on its clientLockFile"); + } if (this.clientlockFile != null) { + if (this.clientlockFile.exists()) this.clientlockFile.delete(); log.debug("deleted active client lock file"); + } else { + log.debug("ReleaseActiveClientFile called when client has no clientLockFile"); } } @@ -550,56 +551,15 @@ public class SimpleClient implements IClient { } } this.clientlockFile = new File (clientlockFileDir, this.getClientHandle().getClientUrn().replaceAll("[:;/\\\\]+","")); - - log.debug("Creating active client lock file "+ this.clientlockFile.getAbsolutePath()); - if (clientlockFile.exists()) - {//should not happen, file should be deleted when the application closes or when a crashed application has been detected - log.error("client lock file already exits"); - } - try { - //create the empty file - if(! clientlockFile.createNewFile()) - { - log.error("Unable to create active client lock file"); - - return; - } - else - log.debug("file created"); - // Get a file channel for the file - FileChannel channel = new RandomAccessFile(clientlockFile, "rw").getChannel(); - - // Use the file channel to create a lock on the file. - // This method blocks until it can retrieve the lock. - java.nio.channels.FileLock activeClientFilelock ;// = channel.lock(); - - // Try acquiring the lock without blocking. This method returns - // null or throws an exception if the file is already locked. - try - { - activeClientFilelock = channel.tryLock(); - log.debug("Got lock"); - } - catch (OverlappingFileLockException e) - { - // File is already locked in this thread or virtual machine - log.error("Oups the file is already locked",e); - - } - - // Release the lock - /* lock.release(); - - // Close the file - channel.close();*/ - } catch (Exception e) { - log.error("Error during lock file creation",e); - } - - + log.debug("Creating active client lock file "+ this.clientlockFile.getAbsolutePath()); + Lock clientLock = uk.ac.vamsas.client.simpleclient.LockFactory.getLock(clientlockFile, false); + if (clientLock==null || !clientLock.isLocked()) + { + log.fatal("IMPLEMENTATION ERROR: Couldn't get a lock for the client lock file "+clientlockFile); + } + activeClientFilelock = clientLock; } - /** * @return the clientlockFile */