git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1326252 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Davies 2012-04-15 04:50:53 +00:00
parent 2f0ef2e670
commit 50d3e8e09a
1 changed files with 36 additions and 25 deletions

View File

@ -51,36 +51,47 @@ public class LockFile {
return; return;
} }
if( lockCounter>0 ) { if (lockCounter > 0) {
return; return;
} }
IOHelper.mkdirs(file.getParentFile()); IOHelper.mkdirs(file.getParentFile());
if (System.getProperty(getVmLockKey()) != null) { synchronized (LockFile.class) {
throw new IOException("File '" + file + "' could not be locked as lock is already held for this jvm."); if (System.getProperty(getVmLockKey()) != null) {
throw new IOException("File '" + file + "' could not be locked as lock is already held for this jvm.");
}
System.setProperty(getVmLockKey(), new Date().toString());
} }
if (lock == null) { try {
readFile = new RandomAccessFile(file, "rw"); if (lock == null) {
IOException reason = null; readFile = new RandomAccessFile(file, "rw");
try { IOException reason = null;
lock = readFile.getChannel().tryLock(0, Math.max(1, readFile.getChannel().size()), false); try {
} catch (OverlappingFileLockException e) { lock = readFile.getChannel().tryLock(0, Math.max(1, readFile.getChannel().size()), false);
reason = IOExceptionSupport.create("File '" + file + "' could not be locked.",e); } catch (OverlappingFileLockException e) {
} catch (IOException ioe) { reason = IOExceptionSupport.create("File '" + file + "' could not be locked.", e);
reason = ioe; } catch (IOException ioe) {
} reason = ioe;
if (lock != null) { }
lockCounter++; if (lock != null) {
System.setProperty(getVmLockKey(), new Date().toString()); lockCounter++;
} else { System.setProperty(getVmLockKey(), new Date().toString());
// new read file for next attempt } else {
closeReadFile(); // new read file for next attempt
if (reason != null) { closeReadFile();
throw reason; if (reason != null) {
throw reason;
}
throw new IOException("File '" + file + "' could not be locked.");
} }
throw new IOException("File '" + file + "' could not be locked.");
}
}
} finally {
synchronized (LockFile.class) {
if (lock == null) {
System.getProperties().remove(getVmLockKey());
}
}
} }
} }
@ -92,7 +103,7 @@ public class LockFile {
} }
lockCounter--; lockCounter--;
if( lockCounter!=0 ) { if (lockCounter != 0) {
return; return;
} }
@ -107,7 +118,7 @@ public class LockFile {
} }
closeReadFile(); closeReadFile();
if( deleteOnUnlock ) { if (deleteOnUnlock) {
file.delete(); file.delete();
} }
} }