more aggressive checks for the locking, and some more documentation

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@3404 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
aaron 2010-05-20 16:16:36 +00:00
parent fc57bccde8
commit b543dd4ac4
2 changed files with 23 additions and 10 deletions

View File

@ -168,9 +168,9 @@ public class TribbleRMDTrackBuilder extends PluginManager<FeatureCodec> implemen
*/ */
private static LinearIndex writeIndexToDisk(File inputFile, FeatureCodec codec, boolean onDisk, File indexFile, boolean obtainedLock) throws IOException { private static LinearIndex writeIndexToDisk(File inputFile, FeatureCodec codec, boolean onDisk, File indexFile, boolean obtainedLock) throws IOException {
LinearIndexCreator create = new LinearIndexCreator(inputFile, codec); LinearIndexCreator create = new LinearIndexCreator(inputFile, codec);
LinearIndex index = create.createIndex(); LinearIndex index = create.createIndex(null); // we don't want to write initially, so we pass in null
// if the index doesn't exist, and we can write to the directory, and we got a lock, write to the disk // if the index doesn't exist, and we can write to the directory, and we got a lock: write to the disk
if (indexFile.getParentFile().canWrite() && if (indexFile.getParentFile().canWrite() &&
(!indexFile.exists() || indexFile.canWrite()) && (!indexFile.exists() || indexFile.canWrite()) &&
onDisk && onDisk &&
@ -182,7 +182,7 @@ public class TribbleRMDTrackBuilder extends PluginManager<FeatureCodec> implemen
// we can't write it to disk, just store it in memory // we can't write it to disk, just store it in memory
else { else {
// if they wanted to write, let them know we couldn't // if they wanted to write, let them know we couldn't
if (onDisk) logger.warn("Unable to write to " + indexFile + " for the index file, creating index in memory only"); if (onDisk) logger.info("Unable to write to " + indexFile + " for the index file, creating index in memory only");
return index; return index;
} }
} }

View File

@ -1,5 +1,6 @@
package org.broadinstitute.sting.utils.file; package org.broadinstitute.sting.utils.file;
import org.apache.log4j.Logger;
import org.broadinstitute.sting.utils.StingException; import org.broadinstitute.sting.utils.StingException;
import java.io.File; import java.io.File;
@ -13,9 +14,19 @@ import java.nio.channels.FileLock;
* a quick implementation of a file based lock, using the Java NIO classes * a quick implementation of a file based lock, using the Java NIO classes
*/ */
public class FSLock { public class FSLock {
// connect to the logger
private static Logger logger = Logger.getLogger(FSLock.class);
// the lock string
private static final String lockString = ".lock"; private static final String lockString = ".lock";
// the file we're attempting to lock
private final File lockFile; private final File lockFile;
// the file lock
private FileLock lock = null; private FileLock lock = null;
// the file channel we open
FileChannel fc = null; FileChannel fc = null;
/** /**
@ -32,20 +43,22 @@ public class FSLock {
* *
* @return boolean true if we obtained a lock * @return boolean true if we obtained a lock
*/ */
public boolean lock() { public synchronized boolean lock() {
if (lock != null) throw new IllegalStateException("Unable to lock on file " + lockFile + " there is already a lock active"); if (lock != null) throw new IllegalStateException("Unable to lock on file " + lockFile + " there is already a lock active");
if (lockFile.exists()) {
System.err.println("exits!!"); // if the file already exists, or we can't write to the parent directory, return false
if (lockFile.exists() || !lockFile.getParentFile().canWrite())
return false; return false;
}
try { try {
fc = new RandomAccessFile(lockFile,"rw").getChannel(); fc = new RandomAccessFile(lockFile,"rw").getChannel();
lock = fc.lock(); lock = fc.tryLock();
return lock != null && lock.isValid(); return lock != null && lock.isValid();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new StingException("Unable to create lock file named " + lockFile,e); logger.info("Unable to create lock file (due to no file found exception), file = " + lockFile,e);
return false;
} catch (IOException e) { } catch (IOException e) {
throw new StingException("Unable to create lock file named " + lockFile,e); logger.info("Unable to create lock file (due to IO exception), file = " + lockFile,e);
return false;
} }
} }