diff --git a/config/alfresco/application-context-core.xml b/config/alfresco/application-context-core.xml
index 5e9b9851a9..32fc63545e 100644
--- a/config/alfresco/application-context-core.xml
+++ b/config/alfresco/application-context-core.xml
@@ -43,6 +43,7 @@
* An intermediary class such as this is required in order to avoid starting * Hazelcast instances when clustering is not configured/required. Otherwise * simply by defining a HazelcastInstance bean clustering would spring into life. + *
+ * Please note this class provides non-static access deliberately, and should be
+ * injected into any clients that require its services.
*
* @author Matt Ward
*/
public class HazelcastInstanceFactory
{
- public Config config;
-
- public HazelcastInstance newInstance()
+ private Config config;
+ private HazelcastInstance hazelcastInstance;
+ /** Guards {@link #config} and {@link #hazelcastInstance} */
+ private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
+
+ public HazelcastInstance getInstance()
{
- return Hazelcast.newHazelcastInstance(config);
+ rwLock.readLock().lock();
+ try
+ {
+ if (hazelcastInstance != null)
+ {
+ return hazelcastInstance;
+ }
+ }
+ finally
+ {
+ rwLock.readLock().unlock();
+ }
+
+ // hazelcastInstance is null, so create it.
+ rwLock.writeLock().lock();
+ try
+ {
+ // Double check condition hasn't changed in between locks.
+ if (hazelcastInstance == null)
+ {
+ hazelcastInstance = Hazelcast.newHazelcastInstance(config);
+ }
+ return hazelcastInstance;
+ }
+ finally
+ {
+ rwLock.writeLock().unlock();
+ }
}
/**
- * @param config the config to set
+ * Checks whether hazelcast has been given a valid cluster name. If so,
+ * then clustering is considered enabled. This condition should be checked
+ * before calling {@link #getInstance()}.
+ *
+ * @return true if clustering is enabled, false otherwise.
+ */
+ public boolean isClusteringEnabled()
+ {
+ rwLock.readLock().lock();
+ try
+ {
+ String clusterName = config.getGroupConfig().getName();
+ return (PropertyCheck.isValidPropertyString(clusterName));
+ }
+ finally
+ {
+ rwLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Retrieve the name of the cluster for the configuration used by this factory.
+ *
+ * @return String - the cluster name.
+ */
+ public String getClusterName()
+ {
+ rwLock.readLock().lock();
+ try
+ {
+ String clusterName = config.getGroupConfig().getName();
+ return clusterName;
+ }
+ finally
+ {
+ rwLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Sets the Hazelcast configuration that will be used by this factory when
+ * creating the HazelcastInstance.
+ *
+ * @param config Hazelcast configuration
*/
public void setConfig(Config config)
{
- this.config = config;
+ rwLock.writeLock().lock();
+ try
+ {
+ this.config = config;
+ }
+ finally
+ {
+ rwLock.writeLock().unlock();
+ }
}
}
diff --git a/source/java/org/alfresco/repo/webdav/LockStoreFactory.java b/source/java/org/alfresco/repo/webdav/LockStoreFactory.java
index 207ede744e..85ce0f773c 100644
--- a/source/java/org/alfresco/repo/webdav/LockStoreFactory.java
+++ b/source/java/org/alfresco/repo/webdav/LockStoreFactory.java
@@ -26,5 +26,5 @@ package org.alfresco.repo.webdav;
*/
public interface LockStoreFactory
{
- LockStore getLockStore();
+ LockStore createLockStore();
}
diff --git a/source/java/org/alfresco/repo/webdav/LockStoreFactoryImpl.java b/source/java/org/alfresco/repo/webdav/LockStoreFactoryImpl.java
index bcb63393e6..ea15b79aa1 100644
--- a/source/java/org/alfresco/repo/webdav/LockStoreFactoryImpl.java
+++ b/source/java/org/alfresco/repo/webdav/LockStoreFactoryImpl.java
@@ -22,14 +22,13 @@ import java.util.concurrent.ConcurrentMap;
import org.alfresco.repo.cluster.HazelcastInstanceFactory;
import org.alfresco.service.cmr.repository.NodeRef;
-import org.alfresco.util.PropertyCheck;
import com.hazelcast.core.HazelcastInstance;
/**
* Default implementation of the {@link LockStoreFactory} interface. Creates {@link LockStore}s
- * backed by a Hazelcast distributed Map if the clusterName property is set,
+ * backed by a Hazelcast distributed Map if clustering is enabled,
* otherwise it creates a non-clustered {@link SimpleLockStore}.
*
* @see LockStoreFactory
@@ -40,22 +39,21 @@ public class LockStoreFactoryImpl implements LockStoreFactory
{
private static final String HAZELCAST_MAP_NAME = "webdav-locks";
private HazelcastInstanceFactory hazelcastInstanceFactory;
- private String clusterName;
/**
* This method should be used sparingly and the created {@link LockStore}s should be
* retained (this factory does not cache instances of them).
*/
@Override
- public synchronized LockStore getLockStore()
+ public synchronized LockStore createLockStore()
{
- if (!PropertyCheck.isValidPropertyString(clusterName))
+ if (!hazelcastInstanceFactory.isClusteringEnabled())
{
return new SimpleLockStore();
}
else
{
- HazelcastInstance instance = hazelcastInstanceFactory.newInstance();
+ HazelcastInstance instance = hazelcastInstanceFactory.getInstance();
ConcurrentMap