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 2007/07/23 17:27:44 UTC

svn commit: r558774 - in /activemq/trunk/activemq-core/src: main/java/org/apache/activemq/kaha/impl/KahaStore.java test/java/org/apache/activemq/kaha/StoreTest.java

Author: rajdavies
Date: Mon Jul 23 08:27:43 2007
New Revision: 558774

URL: http://svn.apache.org/viewvc?view=rev&rev=558774
Log:
applied patch for https://issues.apache.org/activemq/browse/AMQ-1254

Modified:
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java
    activemq/trunk/activemq-core/src/test/java/org/apache/activemq/kaha/StoreTest.java

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java?view=diff&rev=558774&r1=558773&r2=558774
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java (original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java Mon Jul 23 08:27:43 2007
@@ -21,10 +21,10 @@
 import java.io.File;
 import java.io.IOException;
 import java.nio.channels.FileLock;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Properties;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import org.apache.activemq.kaha.ContainerId;
@@ -53,13 +53,11 @@
  */
 public class KahaStore implements Store{
 
-    private static final String LOCK_FILE_NAME="store.lock";
-    
     private final static String PROPERTY_PREFIX="org.apache.activemq.kaha.Store";
     private final static boolean brokenFileLock="true".equals(System.getProperty(PROPERTY_PREFIX+".broken","false"));
     private final static boolean disableLocking="true".equals(System.getProperty(PROPERTY_PREFIX+"DisableLocking",
             "false"));
-    private static Set<String> lockSet;
+   
     private static final Log log=LogFactory.getLog(KahaStore.class);
     private File directory;
     private IndexRootContainer mapsContainer;
@@ -455,42 +453,36 @@
         }
     }
 
-    private synchronized void lock() throws IOException{
-        if(!disableLocking&&directory!=null&&lock==null){
-            Set<String> set=getVmLockSet();
-            synchronized(set){
-                if(lock==null){
-                    if(!set.add(directory.getCanonicalPath())){
-                        throw new StoreLockedExcpetion("Kaha Store "+directory.getName()
-                                +" is already opened by this application.");
-                    }
-                    if(!brokenFileLock){
-                        lock=rootIndexManager.getLock();
-                        if(lock==null){
-                            set.remove(directory.getCanonicalPath());
-                            throw new StoreLockedExcpetion("Kaha Store "+directory.getName()
-                                    +"  is already opened by another application");
-                        }
-                    }
+    private synchronized void lock() throws IOException {
+        if (!disableLocking && directory != null && lock == null) {
+            String key = getPropertyKey();
+            String property = System.getProperty(key);
+            if (null == property) {
+                if (!brokenFileLock) {
+                    lock = rootIndexManager.getLock();
+                    if (lock == null) {
+    throw new StoreLockedExcpetion("Kaha Store " + directory.getName() + "  is already opened by another application");
+                    } else
+                        System.setProperty(key, new Date().toString()); 
                 }
+            } else { //already locked
+    throw new StoreLockedExcpetion("Kaha Store " + directory.getName() + " is already opened by this application.");
             }
         }
     }
-
-    private synchronized void unlock() throws IOException{
-        if(!disableLocking&&directory!=null){
-            Set<String> set=getVmLockSet();
-            synchronized(set){
-                if(lock!=null){
-                    set.remove(directory.getCanonicalPath());
-                    if(lock.isValid()){
-                        lock.release();
-                    }
-                    lock=null;
-                }
+    private synchronized void unlock() throws IOException {
+        if (!disableLocking && (null != directory) && (null != lock)) {
+            System.getProperties().remove(getPropertyKey());
+            if (lock.isValid()) {
+                lock.release();
             }
+            lock = null;
         }
     }
+    private String getPropertyKey() throws IOException{
+        //Is replaceAll() needed?  Should test without it.
+        return getClass().getName() + ".lock." + directory.getCanonicalPath();
+    }
     
     private void checkClosed(){
         if(closed){
@@ -498,21 +490,6 @@
         }
     }
 
-    
-
-    static synchronized private Set<String> getVmLockSet(){
-        if(lockSet==null){
-            Properties properties=System.getProperties();
-            synchronized(properties){
-                lockSet=(Set<String>) properties.get("org.apache.activemq.kaha.impl.KahaStore");
-                if(lockSet==null){
-                    lockSet=new HashSet<String>();
-                }
-                properties.put(PROPERTY_PREFIX,lockSet);
-            }
-        }
-        return lockSet;
-    }
     
     /**
      * scans the directory and builds up the IndexManager and DataManager

Modified: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/kaha/StoreTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/kaha/StoreTest.java?view=diff&rev=558774&r1=558773&r2=558774
==============================================================================
--- activemq/trunk/activemq-core/src/test/java/org/apache/activemq/kaha/StoreTest.java (original)
+++ activemq/trunk/activemq-core/src/test/java/org/apache/activemq/kaha/StoreTest.java Mon Jul 23 08:27:43 2007
@@ -24,6 +24,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import org.apache.activemq.kaha.impl.StoreLockedExcpetion;
 
 import junit.framework.TestCase;
 
@@ -182,6 +183,17 @@
         for (Iterator i = testList.iterator(), j = listContainer.iterator(); i.hasNext();){
             assertEquals(i.next(),j.next());
         }
+    }
+    
+    public void testLock() throws Exception{
+        try {
+        store.doesListContainerExist("fred");
+        Store s = getStore();
+        s.doesListContainerExist("fred");
+        }catch(StoreLockedExcpetion e) {
+            return;
+        }
+        fail("Expected to catch an exception");
     }