git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@558774 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Davies 2007-07-23 15:27:43 +00:00
parent 6ce8c3bf87
commit 20364163e9
2 changed files with 37 additions and 48 deletions

View File

@ -21,10 +21,10 @@ package org.apache.activemq.kaha.impl;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.apache.activemq.kaha.ContainerId; import org.apache.activemq.kaha.ContainerId;
@ -53,13 +53,11 @@ import org.apache.commons.logging.LogFactory;
*/ */
public class KahaStore implements Store{ 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 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 brokenFileLock="true".equals(System.getProperty(PROPERTY_PREFIX+".broken","false"));
private final static boolean disableLocking="true".equals(System.getProperty(PROPERTY_PREFIX+"DisableLocking", private final static boolean disableLocking="true".equals(System.getProperty(PROPERTY_PREFIX+"DisableLocking",
"false")); "false"));
private static Set<String> lockSet;
private static final Log log=LogFactory.getLog(KahaStore.class); private static final Log log=LogFactory.getLog(KahaStore.class);
private File directory; private File directory;
private IndexRootContainer mapsContainer; private IndexRootContainer mapsContainer;
@ -457,39 +455,33 @@ public class KahaStore implements Store{
private synchronized void lock() throws IOException { private synchronized void lock() throws IOException {
if (!disableLocking && directory != null && lock == null) { if (!disableLocking && directory != null && lock == null) {
Set<String> set=getVmLockSet(); String key = getPropertyKey();
synchronized(set){ String property = System.getProperty(key);
if(lock==null){ if (null == property) {
if(!set.add(directory.getCanonicalPath())){
throw new StoreLockedExcpetion("Kaha Store "+directory.getName()
+" is already opened by this application.");
}
if (!brokenFileLock) { if (!brokenFileLock) {
lock = rootIndexManager.getLock(); lock = rootIndexManager.getLock();
if (lock == null) { if (lock == null) {
set.remove(directory.getCanonicalPath()); throw new StoreLockedExcpetion("Kaha Store " + directory.getName() + " is already opened by another application");
throw new StoreLockedExcpetion("Kaha Store "+directory.getName() } else
+" is already opened by another application"); 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 { private synchronized void unlock() throws IOException {
if(!disableLocking&&directory!=null){ if (!disableLocking && (null != directory) && (null != lock)) {
Set<String> set=getVmLockSet(); System.getProperties().remove(getPropertyKey());
synchronized(set){
if(lock!=null){
set.remove(directory.getCanonicalPath());
if (lock.isValid()) { if (lock.isValid()) {
lock.release(); lock.release();
} }
lock = null; lock = null;
} }
} }
} private String getPropertyKey() throws IOException{
//Is replaceAll() needed? Should test without it.
return getClass().getName() + ".lock." + directory.getCanonicalPath();
} }
private void checkClosed(){ private void checkClosed(){
@ -499,21 +491,6 @@ public class KahaStore implements Store{
} }
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 * scans the directory and builds up the IndexManager and DataManager
* @throws IOException * @throws IOException

View File

@ -24,6 +24,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.activemq.kaha.impl.StoreLockedExcpetion;
import junit.framework.TestCase; 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{ protected Store getStore() throws IOException{
return StoreFactory.open(name, "rw"); return StoreFactory.open(name, "rw");