You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ra...@apache.org on 2012/04/15 06:50:53 UTC

svn commit: r1326252 - /activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/LockFile.java

Author: rajdavies
Date: Sun Apr 15 04:50:53 2012
New Revision: 1326252

URL: http://svn.apache.org/viewvc?rev=1326252&view=rev
Log:
Applied patch for https://issues.apache.org/jira/browse/AMQ-3301

Modified:
    activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/LockFile.java

Modified: activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/LockFile.java
URL: http://svn.apache.org/viewvc/activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/LockFile.java?rev=1326252&r1=1326251&r2=1326252&view=diff
==============================================================================
--- activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/LockFile.java (original)
+++ activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/LockFile.java Sun Apr 15 04:50:53 2012
@@ -51,36 +51,47 @@ public class LockFile {
             return;
         }
 
-        if( lockCounter>0 ) {
+        if (lockCounter > 0) {
             return;
         }
 
         IOHelper.mkdirs(file.getParentFile());
-        if (System.getProperty(getVmLockKey()) != null) {
-            throw new IOException("File '" + file + "' could not be locked as lock is already held for this jvm.");
+        synchronized (LockFile.class) {
+            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) {
-            readFile = new RandomAccessFile(file, "rw");
-            IOException reason = null;
-            try {
-                lock = readFile.getChannel().tryLock(0, Math.max(1, readFile.getChannel().size()), false);
-            } catch (OverlappingFileLockException e) {
-                reason = IOExceptionSupport.create("File '" + file + "' could not be locked.",e);
-            } catch (IOException ioe) {
-                reason = ioe;
+        try {
+            if (lock == null) {
+                readFile = new RandomAccessFile(file, "rw");
+                IOException reason = null;
+                try {
+                    lock = readFile.getChannel().tryLock(0, Math.max(1, readFile.getChannel().size()), false);
+                } catch (OverlappingFileLockException e) {
+                    reason = IOExceptionSupport.create("File '" + file + "' could not be locked.", e);
+                } catch (IOException ioe) {
+                    reason = ioe;
+                }
+                if (lock != null) {
+                    lockCounter++;
+                    System.setProperty(getVmLockKey(), new Date().toString());
+                } else {
+                    // new read file for next attempt
+                    closeReadFile();
+                    if (reason != null) {
+                        throw reason;
+                    }
+                    throw new IOException("File '" + file + "' could not be locked.");
+                }
+
             }
-            if (lock != null) {
-                lockCounter++;
-                System.setProperty(getVmLockKey(), new Date().toString());
-            } else {
-                // new read file for next attempt
-                closeReadFile();
-                if (reason != null) {
-                    throw reason;
+        } finally {
+            synchronized (LockFile.class) {
+                if (lock == null) {
+                    System.getProperties().remove(getVmLockKey());
                 }
-                throw new IOException("File '" + file + "' could not be locked.");
             }
-
         }
     }
 
@@ -92,7 +103,7 @@ public class LockFile {
         }
 
         lockCounter--;
-        if( lockCounter!=0 ) {
+        if (lockCounter != 0) {
             return;
         }
 
@@ -107,7 +118,7 @@ public class LockFile {
         }
         closeReadFile();
 
-        if( deleteOnUnlock ) {
+        if (deleteOnUnlock) {
             file.delete();
         }
     }