Fix cache in NodeTypeUtility to be thread safe.

Inspired by @cetra3 in PR: https://github.com/Alfresco/records-management/pull/8
This commit is contained in:
Tom Page
2020-04-03 10:00:11 +01:00
parent cc31204ddf
commit 25f87a54ce

View File

@@ -26,8 +26,7 @@
*/
package org.alfresco.module.org_alfresco_module_rm.util;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.namespace.QName;
@@ -41,11 +40,12 @@ import org.alfresco.util.ParameterCheck;
*/
public class NodeTypeUtility
{
/** Static cache for results of types that are instances of other Alfresco types. */
private static ConcurrentHashMap<String, Boolean> instanceOfCache = new ConcurrentHashMap<>();
/** Dictionary service */
private DictionaryService dictionaryService;
private static Map<String, Boolean> instanceOfCache = new WeakHashMap<>();
/**
* @param dictionaryService dictionary service
*/
@@ -66,24 +66,8 @@ public class NodeTypeUtility
ParameterCheck.mandatory("className", className);
ParameterCheck.mandatory("ofClassName", ofClassName);
boolean result = false;
String key = className.toString() + "|" + ofClassName.toString();
if (instanceOfCache.containsKey(key))
{
result = instanceOfCache.get(key);
}
else
{
if (ofClassName.equals(className) ||
dictionaryService.isSubClass(className, ofClassName))
{
result = true;
}
instanceOfCache.put(key, result);
}
return result;
return instanceOfCache.computeIfAbsent(key, k ->
(ofClassName.equals(className) || dictionaryService.isSubClass(className, ofClassName)));
}
}