diff --git a/source/java/org/alfresco/repo/policy/CachedPolicyFactory.java b/source/java/org/alfresco/repo/policy/CachedPolicyFactory.java index 68b108c9e0..9a59a86591 100644 --- a/source/java/org/alfresco/repo/policy/CachedPolicyFactory.java +++ b/source/java/org/alfresco/repo/policy/CachedPolicyFactory.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.alfresco.util.LockHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -91,43 +92,41 @@ import org.apache.commons.logging.LogFactory; return super.create(binding); } - lock.readLock().lock(); - + LockHelper.tryLock(lock.readLock(), 100); try { P policyInterface = singleCache.get(binding); - if (policyInterface == null) + if (policyInterface != null) { - // Upgrade read lock to write lock - lock.readLock().unlock(); - lock.writeLock().lock(); - - try - { - // Check again - policyInterface = singleCache.get(binding); - if (policyInterface == null) - { - policyInterface = super.create(binding); - singleCache.put(binding, policyInterface); - - if (logger.isDebugEnabled()) - logger.debug("Cached delegate interface " + policyInterface + " for " + binding + " and policy " + getPolicyClass()); - } - } - finally - { - // Downgrade lock to read - lock.readLock().lock(); - lock.writeLock().unlock(); - } + return policyInterface; } - return policyInterface; } finally { lock.readLock().unlock(); } + + // There wasn't one + LockHelper.tryLock(lock.writeLock(), 100); + try + { + P policyInterface = singleCache.get(binding); + if (policyInterface != null) + { + return policyInterface; + } + policyInterface = super.create(binding); + singleCache.put(binding, policyInterface); + + if (logger.isDebugEnabled()) + logger.debug("Cached delegate interface " + policyInterface + " for " + binding + " and policy " + getPolicyClass()); + + return policyInterface; + } + finally + { + lock.writeLock().unlock(); + } } @@ -140,45 +139,42 @@ import org.apache.commons.logging.LogFactory; return super.createList(binding); } - lock.readLock().lock(); - + LockHelper.tryLock(lock.readLock(), 100); try { Collection

policyInterfaces = listCache.get(binding); - if (policyInterfaces == null) + if (policyInterfaces != null) { - // Upgrade read lock to write lock - lock.readLock().unlock(); - lock.writeLock().lock(); - - try - { - // Check again - policyInterfaces = listCache.get(binding); - if (policyInterfaces == null) - { - policyInterfaces = super.createList(binding); - listCache.put(binding, policyInterfaces); - - if (logger.isDebugEnabled()) - logger.debug("Cached delegate interface collection " + policyInterfaces + " for " + binding + " and policy " + getPolicyClass()); - } - } - finally - { - // Downgrade lock to read - lock.readLock().lock(); - lock.writeLock().unlock(); - } + return policyInterfaces; } - return policyInterfaces; - } + } finally { lock.readLock().unlock(); } - } + + // There wasn't one + LockHelper.tryLock(lock.writeLock(), 100); + try + { + Collection

policyInterfaces = listCache.get(binding); + if (policyInterfaces != null) + { + return policyInterfaces; + } + policyInterfaces = super.createList(binding); + listCache.put(binding, policyInterfaces); + if (logger.isDebugEnabled()) + logger.debug("Cached delegate interface collection " + policyInterfaces + " for " + binding + " and policy " + getPolicyClass()); + + return policyInterfaces; + } + finally + { + lock.writeLock().unlock(); + } + } /** * Clear entries in the cache based on binding changes. @@ -191,8 +187,7 @@ import org.apache.commons.logging.LogFactory; { if (binding == null) { - lock.writeLock().lock(); - + LockHelper.tryLock(lock.writeLock(), 100); try { // A specific binding has not been provided, so clear all entries @@ -230,8 +225,7 @@ import org.apache.commons.logging.LogFactory; // Remove all invalid bindings if (invalidBindings.size() > 0) { - lock.writeLock().lock(); - + LockHelper.tryLock(lock.writeLock(), 100); try { for (B invalidBinding : invalidBindings) @@ -249,5 +243,4 @@ import org.apache.commons.logging.LogFactory; } } } - } diff --git a/source/java/org/alfresco/repo/policy/ClassBehaviourIndex.java b/source/java/org/alfresco/repo/policy/ClassBehaviourIndex.java index c20ed87a71..d2ef8f61bc 100644 --- a/source/java/org/alfresco/repo/policy/ClassBehaviourIndex.java +++ b/source/java/org/alfresco/repo/policy/ClassBehaviourIndex.java @@ -25,6 +25,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.namespace.QName; +import org.alfresco.util.LockHelper; /** @@ -88,13 +89,10 @@ import org.alfresco.service.namespace.QName; } - /* (non-Javadoc) - * @see org.alfresco.repo.policy.BehaviourIndex#getAll() - */ - @SuppressWarnings("unchecked") + @Override public Collection getAll() { - lock.readLock().lock(); + LockHelper.tryLock(lock.readLock(), 100); try { @@ -110,13 +108,11 @@ import org.alfresco.service.namespace.QName; } - /* (non-Javadoc) - * @see org.alfresco.repo.policy.BehaviourIndex#find() - */ + @Override @SuppressWarnings("unchecked") public Collection find(B binding) { - lock.readLock().lock(); + LockHelper.tryLock(lock.readLock(), 100); try { @@ -158,18 +154,14 @@ import org.alfresco.service.namespace.QName; } - /* (non-Javadoc) - * @see org.alfresco.repo.policy.BehaviourIndex#find() - */ + @Override public void addChangeObserver(BehaviourChangeObserver observer) { observers.add(observer); } - /* (non-Javadoc) - * @see org.alfresco.repo.policy.BehaviourIndex#getFilter() - */ + @Override public BehaviourFilter getFilter() { return filter; @@ -183,7 +175,7 @@ import org.alfresco.service.namespace.QName; */ public void putClassBehaviour(BehaviourDefinition behaviour) { - lock.writeLock().lock(); + LockHelper.tryLock(lock.writeLock(), 100); try { classMap.put(behaviour); @@ -202,7 +194,7 @@ import org.alfresco.service.namespace.QName; */ public void putServiceBehaviour(BehaviourDefinition behaviour) { - lock.writeLock().lock(); + LockHelper.tryLock(lock.writeLock(), 100); try { serviceMap.put(behaviour); @@ -220,7 +212,7 @@ import org.alfresco.service.namespace.QName; */ public void removeClassBehaviour(BehaviourDefinition behaviour) { - lock.writeLock().lock(); + LockHelper.tryLock(lock.writeLock(), 100); try { classMap.remove(behaviour);