https://issues.apache.org/jira/browse/AMQ-4705 - ensure jvm lock system property is cleared in the event of lock release throwing exception.

This commit is contained in:
gtully 2015-10-14 15:08:03 +01:00
parent af09b4586b
commit b285d10188
1 changed files with 12 additions and 7 deletions

View File

@ -42,6 +42,7 @@ public class LockFile {
private int lockCounter; private int lockCounter;
private final boolean deleteOnUnlock; private final boolean deleteOnUnlock;
private volatile boolean locked; private volatile boolean locked;
private String lockSystemPropertyName = "";
private static final Logger LOG = LoggerFactory.getLogger(LockFile.class); private static final Logger LOG = LoggerFactory.getLogger(LockFile.class);
@ -64,10 +65,11 @@ public class LockFile {
IOHelper.mkdirs(file.getParentFile()); IOHelper.mkdirs(file.getParentFile());
synchronized (LockFile.class) { synchronized (LockFile.class) {
if (System.getProperty(getVmLockKey()) != null) { lockSystemPropertyName = getVmLockKey();
throw new IOException("File '" + file + "' could not be locked as lock is already held for this jvm."); if (System.getProperty(lockSystemPropertyName) != null) {
throw new IOException("File '" + file + "' could not be locked as lock is already held for this jvm. Value: " + System.getProperty(lockSystemPropertyName));
} }
System.setProperty(getVmLockKey(), new Date().toString()); System.setProperty(lockSystemPropertyName, new Date().toString());
} }
try { try {
if (lock == null) { if (lock == null) {
@ -86,7 +88,7 @@ public class LockFile {
randomAccessLockFile.getChannel().force(true); randomAccessLockFile.getChannel().force(true);
lastModified = file.lastModified(); lastModified = file.lastModified();
lockCounter++; lockCounter++;
System.setProperty(getVmLockKey(), new Date().toString()); System.setProperty(lockSystemPropertyName, new Date().toString());
locked = true; locked = true;
} else { } else {
// new read file for next attempt // new read file for next attempt
@ -101,7 +103,7 @@ public class LockFile {
} finally { } finally {
synchronized (LockFile.class) { synchronized (LockFile.class) {
if (lock == null) { if (lock == null) {
System.getProperties().remove(getVmLockKey()); System.getProperties().remove(lockSystemPropertyName);
} }
} }
} }
@ -123,10 +125,13 @@ public class LockFile {
if (lock != null) { if (lock != null) {
try { try {
lock.release(); lock.release();
System.getProperties().remove(getVmLockKey());
} catch (Throwable ignore) { } catch (Throwable ignore) {
} finally {
if (lockSystemPropertyName != null) {
System.getProperties().remove(lockSystemPropertyName);
}
lock = null;
} }
lock = null;
} }
closeReadFile(); closeReadFile();