RM-732: CLONE - "caveat" functionality of the (dod5015) Records Management Module is not cluster-aware

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/BRANCHES/V2.0@52115 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2013-07-04 01:41:20 +00:00
parent 586106f26d
commit e5c487db07
2 changed files with 309 additions and 103 deletions

View File

@@ -893,6 +893,41 @@
<property name="caveatAspects" ref="caveatAspects"/> <property name="caveatAspects" ref="caveatAspects"/>
<property name="caveatModels" ref="caveatModels"/> <property name="caveatModels" ref="caveatModels"/>
<property name="caveatConfig" ref="caveatConfigCache"/>
</bean>
<!-- ===================================== -->
<!-- Record Management Caveat Config Cache -->
<!-- ===================================== -->
<!-- The cross-transaction shared cache for in-memory CaveatConfig -->
<bean name="caveatConfigSharedCache" class="org.alfresco.repo.cache.EhCacheAdapter">
<property name="cache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" >
<property name="cacheManager">
<ref bean="internalEHCacheManager" />
</property>
<property name="cacheName">
<value>org.alfresco.cache.caveatConfigCache</value>
</property>
</bean>
</property>
</bean>
<!-- The transactional cache for in-memory CaveatConfig -->
<bean name="caveatConfigCache" class="org.alfresco.repo.cache.TransactionalCache">
<property name="sharedCache">
<ref bean="caveatConfigSharedCache" />
</property>
<property name="name">
<value>org.alfresco.caveatConfigTransactionalCache</value>
</property>
<property name="maxCacheSize" value="500" />
<property name="mutable" value="true" />
<property name="disableSharedCache" value="${system.cache.disableMutableSharedCaches}" />
</bean> </bean>
<bean id="caveatAspects" class="java.util.ArrayList" > <bean id="caveatAspects" class="java.util.ArrayList" >

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2005-2011 Alfresco Software Limited. * Copyright (C) 2005-2013 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
@@ -22,18 +22,22 @@ import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
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 java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint.MatchLogic; import org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint.MatchLogic;
import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel; import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel;
import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.content.ContentServicePolicies; import org.alfresco.repo.content.ContentServicePolicies;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.node.NodeServicePolicies; import org.alfresco.repo.node.NodeServicePolicies;
@@ -97,16 +101,25 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
private static final QName DATATYPE_TEXT = DataTypeDefinition.TEXT; private static final QName DATATYPE_TEXT = DataTypeDefinition.TEXT;
/**
* Lock objects
*/
private ReadWriteLock lock = new ReentrantReadWriteLock();
private Lock readLock = lock.readLock();
private Lock writeLock = lock.writeLock();
/* /*
* Caveat Config * Caveat Config (Shared) config
* first string is property name * first string is property name
* second string is authority name (user or group full name) * second string is authority name (user or group full name)
* third string is list of values of property * third string is list of values of property
*/ */
private SimpleCache<String, Map<String, List<String>>> caveatConfig;
// TODO - convert to SimpleCache to be cluster-aware (for dynamic changes to caveat config across a cluster) public void setCaveatConfig(SimpleCache<String, Map<String, List<String>>> caveatConfig)
private Map<String, Map<String, List<String>>> caveatConfig = new ConcurrentHashMap<String, Map<String, List<String>>>(2); {
this.caveatConfig = caveatConfig;
}
public void setPolicyComponent(PolicyComponent policyComponent) public void setPolicyComponent(PolicyComponent policyComponent)
{ {
@@ -252,6 +265,12 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
validateAndReset(childAssocRef.getChildRef()); validateAndReset(childAssocRef.getChildRef());
} }
/**
* Validate the caveat config and optionally update the cache.
*
* @param nodeRef The nodeRef of the config
* @param updateCache Set to <code>true</code> to update the cache
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void validateAndReset(NodeRef nodeRef) protected void validateAndReset(NodeRef nodeRef)
{ {
@@ -409,17 +428,31 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
} }
} }
// Valid, so update try
caveatConfig.clear(); {
writeLock.lock();
// we can't just clear the cache, as all puts to the cache afterwards in this transaction will be ignored
// first delete all keys that are now not in the config
caveatConfig.getKeys().retainAll(caveatConfigMap.keySet());
for (Map.Entry<String, Object> conEntry : caveatConfigMap.entrySet()) for (Map.Entry<String, Object> conEntry : caveatConfigMap.entrySet())
{ {
String conStr = conEntry.getKey(); String conStr = conEntry.getKey();
Map<String, List<String>> caveatMap = (Map<String, List<String>>)conEntry.getValue(); Map<String, List<String>> caveatMap = (Map<String, List<String>>)conEntry.getValue();
Map<String, List<String>> cacheValue = caveatConfig.get(conStr);
if (cacheValue == null || !cacheValue.equals(caveatMap))
{
// update the cache
caveatConfig.put(conStr, caveatMap); caveatConfig.put(conStr, caveatMap);
} }
} }
}
finally
{
writeLock.unlock();
}
}
catch (JSONException e) catch (JSONException e)
{ {
throw new AlfrescoRuntimeException("Invalid caveat config syntax: "+e); throw new AlfrescoRuntimeException("Invalid caveat config syntax: "+e);
@@ -495,9 +528,19 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
} }
// Get list of all caveat qualified names // Get list of all caveat qualified names
public Set<String> getRMConstraintNames() public Collection<String> getRMConstraintNames()
{ {
return caveatConfig.keySet(); Collection<String> rmConstraintNames = Collections.emptySet();
try
{
readLock.lock();
rmConstraintNames = caveatConfig.getKeys();
}
finally
{
readLock.unlock();
}
return Collections.unmodifiableCollection(rmConstraintNames);
} }
// Get allowed values for given caveat (for current user) // Get allowed values for given caveat (for current user)
@@ -511,7 +554,6 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
if (! (AuthenticationUtil.isMtEnabled() && AuthenticationUtil.isRunAsUserTheSystemUser())) if (! (AuthenticationUtil.isMtEnabled() && AuthenticationUtil.isRunAsUserTheSystemUser()))
{ {
// note: userName and userGroupNames must not be null // note: userName and userGroupNames must not be null
Map<String, List<String>> caveatConstraintDef = caveatConfig.get(constraintName);
Set<String> userGroupFullNames = authorityService.getAuthoritiesForUser(userName); Set<String> userGroupFullNames = authorityService.getAuthoritiesForUser(userName);
allowedValues = getRMAllowedValues(userName, userGroupFullNames, constraintName); allowedValues = getRMAllowedValues(userName, userGroupFullNames, constraintName);
} }
@@ -525,7 +567,16 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
Set<String>allowedValues = new HashSet<String>(); Set<String>allowedValues = new HashSet<String>();
// note: userName and userGroupNames must not be null // note: userName and userGroupNames must not be null
Map<String, List<String>> caveatConstraintDef = caveatConfig.get(constraintName); Map<String, List<String>> caveatConstraintDef = null;
try
{
readLock.lock();
caveatConstraintDef = caveatConfig.get(constraintName);
}
finally
{
readLock.unlock();
}
if (caveatConstraintDef != null) if (caveatConstraintDef != null)
{ {
@@ -547,7 +598,7 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
List<String>ret = new ArrayList<String>(); List<String>ret = new ArrayList<String>();
ret.addAll(allowedValues); ret.addAll(allowedValues);
return ret; return Collections.unmodifiableList(ret);
} }
/** /**
@@ -692,24 +743,54 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
* *
* @param listName the name of the RMConstraintList * @param listName the name of the RMConstraintList
* @param authorityName * @param authorityName
* @param values * @param value
* @throws AlfrescoRuntimeException if either the list or the authority do not already exist. * @throws AlfrescoRuntimeException if either the list or the authority do not already exist.
*/ */
public void addRMConstraintListValue(String listName, String authorityName, String value) public void addRMConstraintListValue(String listName, String authorityName, String value)
{ {
Map<String, List<String>> members = caveatConfig.get(listName); Map<String, List<String>> members = null;
try
{
readLock.lock();
members = caveatConfig.get(listName);
if(members == null) if(members == null)
{ {
throw new AlfrescoRuntimeException("unable to add to list, list not defined:"+ listName); throw new AlfrescoRuntimeException("unable to add to list, list not defined:"+ listName);
} }
try
{
readLock.unlock();
writeLock.lock();
// check again
members = caveatConfig.get(listName);
if(members == null)
{
throw new AlfrescoRuntimeException("unable to add to list, list not defined:"+ listName);
}
List<String> values = members.get(authorityName); List<String> values = members.get(authorityName);
if(values == null) if(values == null)
{ {
throw new AlfrescoRuntimeException("Unable to add to authority in list. Authority not member listName: "+ listName + " authorityName:" +authorityName); throw new AlfrescoRuntimeException("Unable to add to authority in list. Authority not member listName: "+ listName + " authorityName:" +authorityName);
} }
values.add(value); values.add(value);
caveatConfig.put(listName, members);
updateOrCreateCaveatConfig(convertToJSONString(caveatConfig)); updateOrCreateCaveatConfig(convertToJSONString(caveatConfig));
} }
finally
{
readLock.lock();
writeLock.unlock();
}
}
finally
{
readLock.unlock();
}
}
/** /**
* Get the member details of the specified list * Get the member details of the specified list
@@ -718,7 +799,24 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
*/ */
public Map<String, List<String>> getListDetails(String listName) public Map<String, List<String>> getListDetails(String listName)
{ {
return caveatConfig.get(listName); Map<String, List<String>> listDetails = null;
try
{
readLock.lock();
listDetails = caveatConfig.get(listName);
}
finally
{
readLock.unlock();
}
if (listDetails == null)
{
return Collections.emptyMap();
}
else
{
return Collections.unmodifiableMap(listDetails);
}
} }
public List<QName> getRMCaveatModels() public List<QName> getRMCaveatModels()
@@ -738,21 +836,31 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
*/ */
public void updateRMConstraintListAuthority(String listName, String authorityName, List<String>values) public void updateRMConstraintListAuthority(String listName, String authorityName, List<String>values)
{ {
Map<String, List<String>> members = caveatConfig.get(listName); Map<String, List<String>> members = null;
try
{
writeLock.lock();
members = caveatConfig.get(listName);
if(members == null) if(members == null)
{ {
// Create the new list, with the authority name // Create the new list, with the authority name
Map<String, List<String>> constraint = new HashMap<String, List<String>>(0); Map<String, List<String>> constraint = new HashMap<String, List<String>>(0);
constraint.put(authorityName, values); constraint.put(authorityName, new ArrayList<String>(values));
caveatConfig.put(listName, constraint); members = constraint;
} }
else else
{ {
members.put(authorityName, values); members.put(authorityName, new ArrayList<String>(values));
} }
caveatConfig.put(listName, members);
updateOrCreateCaveatConfig(convertToJSONString(caveatConfig)); updateOrCreateCaveatConfig(convertToJSONString(caveatConfig));
} }
finally
{
writeLock.unlock();
}
}
/** /**
* Replace the authorities for a value in a list * Replace the authorities for a value in a list
@@ -764,8 +872,10 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
public void updateRMConstraintListValue(String listName, String valueName, List<String>authorities) public void updateRMConstraintListValue(String listName, String valueName, List<String>authorities)
{ {
// members contains member, values[] Map<String, List<String>> members = null;
Map<String, List<String>> members = caveatConfig.get(listName); try
{
writeLock.lock();
if(members == null) if(members == null)
{ {
@@ -800,15 +910,35 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
} }
vals.add(valueName); vals.add(valueName);
} }
caveatConfig.put(listName, members);
updateOrCreateCaveatConfig(convertToJSONString(caveatConfig)); updateOrCreateCaveatConfig(convertToJSONString(caveatConfig));
} }
finally
{
writeLock.unlock();
}
}
public void removeRMConstraintListValue(String listName, String valueName) public void removeRMConstraintListValue(String listName, String valueName)
{ {
// members contains member, values[] Map<String, List<String>> members = null;
Map<String, List<String>> members = caveatConfig.get(listName); try
{
readLock.lock();
members = caveatConfig.get(listName);
if(members == null)
{
// list does not exist
}
else
{
try
{
readLock.unlock();
writeLock.lock();
// check again
members = caveatConfig.get(listName);
if(members == null) if(members == null)
{ {
// list does not exist // list does not exist
@@ -828,10 +958,24 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
List<String> vals = members.get(authority); List<String> vals = members.get(authority);
vals.remove(valueName); vals.remove(valueName);
} }
caveatConfig.put(listName, members);
}
} }
updateOrCreateCaveatConfig(convertToJSONString(caveatConfig)); updateOrCreateCaveatConfig(convertToJSONString(caveatConfig));
} }
finally
{
readLock.lock();
writeLock.unlock();
}
}
}
finally
{
readLock.unlock();
}
} }
/** /**
@@ -843,26 +987,37 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
*/ */
public void removeRMConstraintListAuthority(String listName, String authorityName) public void removeRMConstraintListAuthority(String listName, String authorityName)
{ {
Map<String, List<String>> members = caveatConfig.get(listName); Map<String, List<String>> members = null;
try
{
writeLock.lock();
members = caveatConfig.get(listName);
if(members != null) if(members != null)
{ {
members.remove(listName); members.remove(listName);
} }
caveatConfig.put(listName, members);
updateOrCreateCaveatConfig(convertToJSONString(caveatConfig)); updateOrCreateCaveatConfig(convertToJSONString(caveatConfig));
}
finally
{
writeLock.unlock();
}
} }
/** /**
* @param config the configuration to convert * @param config the configuration to convert
* @return a String containing the JSON representation of the configuration. * @return a String containing the JSON representation of the configuration.
*/ */
private String convertToJSONString(Map<String, Map<String, List<String>>> config) private String convertToJSONString(SimpleCache<String, Map<String, List<String>>> config)
{ {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
try try
{ {
Set<String> listNames = config.keySet(); Collection<String> listNames = config.getKeys();
for(String listName : listNames) for(String listName : listNames)
{ {
Map<String, List<String>> members = config.get(listName); Map<String, List<String>> members = config.get(listName);
@@ -932,14 +1087,30 @@ public class RMCaveatConfigComponentImpl implements ContentServicePolicies.OnCon
public void deleteRMConstraint(String listName) public void deleteRMConstraint(String listName)
{ {
try
{
writeLock.lock();
caveatConfig.remove(listName); caveatConfig.remove(listName);
updateOrCreateCaveatConfig(convertToJSONString(caveatConfig)); updateOrCreateCaveatConfig(convertToJSONString(caveatConfig));
} }
finally
{
writeLock.unlock();
}
}
public void addRMConstraint(String listName) public void addRMConstraint(String listName)
{ {
try
{
writeLock.lock();
Map<String, List<String>> emptyConstraint = new HashMap<String, List<String>>(0); Map<String, List<String>> emptyConstraint = new HashMap<String, List<String>>(0);
caveatConfig.put(listName, emptyConstraint); caveatConfig.put(listName, emptyConstraint);
updateOrCreateCaveatConfig(convertToJSONString(caveatConfig)); updateOrCreateCaveatConfig(convertToJSONString(caveatConfig));
} }
finally
{
writeLock.unlock();
}
}
} }