From 20364163e9a75fbacb8e8546591d254ac6fbcdf8 Mon Sep 17 00:00:00 2001 From: Robert Davies Date: Mon, 23 Jul 2007 15:27:43 +0000 Subject: [PATCH] applied patch for https://issues.apache.org/activemq/browse/AMQ-1254 git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@558774 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/activemq/kaha/impl/KahaStore.java | 73 +++++++------------ .../org/apache/activemq/kaha/StoreTest.java | 12 +++ 2 files changed, 37 insertions(+), 48 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java b/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java index 01dfb4bfa7..f172b97ee1 100644 --- a/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java +++ b/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java @@ -21,10 +21,10 @@ package org.apache.activemq.kaha.impl; 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 @@ import org.apache.commons.logging.LogFactory; */ 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 lockSet; + private static final Log log=LogFactory.getLog(KahaStore.class); private File directory; private IndexRootContainer mapsContainer; @@ -455,42 +453,36 @@ public class KahaStore implements Store{ } } - private synchronized void lock() throws IOException{ - if(!disableLocking&&directory!=null&&lock==null){ - Set 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 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){ @@ -499,21 +491,6 @@ public class KahaStore implements Store{ } - - static synchronized private Set getVmLockSet(){ - if(lockSet==null){ - Properties properties=System.getProperties(); - synchronized(properties){ - lockSet=(Set) properties.get("org.apache.activemq.kaha.impl.KahaStore"); - if(lockSet==null){ - lockSet=new HashSet(); - } - properties.put(PROPERTY_PREFIX,lockSet); - } - } - return lockSet; - } - /** * scans the directory and builds up the IndexManager and DataManager * @throws IOException diff --git a/activemq-core/src/test/java/org/apache/activemq/kaha/StoreTest.java b/activemq-core/src/test/java/org/apache/activemq/kaha/StoreTest.java index 0c4b31ea5d..91bb8d3096 100644 --- a/activemq-core/src/test/java/org/apache/activemq/kaha/StoreTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/kaha/StoreTest.java @@ -24,6 +24,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.activemq.kaha.impl.StoreLockedExcpetion; import junit.framework.TestCase; @@ -184,6 +185,17 @@ public class StoreTest extends TestCase{ } } + 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"); + } + protected Store getStore() throws IOException{ return StoreFactory.open(name, "rw");