mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Fix AR-350: Transactional caches for node ownership and permissions statuses
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2354 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -1,151 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.cache;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A cache implementation that maintains items up to a threshold size. If the threshold size is reached
|
||||
* it begins removing old items during get() calls as they time-out after a specified timeout value.
|
||||
* <p>
|
||||
* If the threshold value is not reached, the items are not removed unless specifically requested with
|
||||
* a call to remove() or clear().
|
||||
* <p>
|
||||
* If the max size value is reached then no more items are added to the cache until some are removed
|
||||
* either explicitly or automically via timed-out values.
|
||||
*
|
||||
* @author Kevin Roast
|
||||
*/
|
||||
public class AutoExpireCache<K extends Serializable, V extends Serializable> implements SimpleCache<K, V>
|
||||
{
|
||||
// TODO: configure these values via Spring
|
||||
private final long TIMEDIFF = 1000000L * 1000L * 60L * 5L; // 5 mins in nano-seconds
|
||||
private final int MAXSIZE = 4096; // maximum size of the cache
|
||||
private final float THRESHOLD = 0.75f; // before we start removing items
|
||||
private int maxsize = MAXSIZE;
|
||||
private float threshold = THRESHOLD;
|
||||
private Map<Object, CacheItem<K, V>> cache = new HashMap<Object, CacheItem<K, V>>(maxsize, 1.0f);
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public AutoExpireCache()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public AutoExpireCache(int maxsize, float threshold)
|
||||
{
|
||||
maxsize = maxsize;
|
||||
threshold = threshold;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.cache.SimpleCache#get(K)
|
||||
*/
|
||||
public synchronized V get(K key)
|
||||
{
|
||||
CacheItem<K, V> wrapper = cache.get(key);
|
||||
if (wrapper != null)
|
||||
{
|
||||
// we cache values till a specific timeout then remove them
|
||||
// this also gives other values a chance to get added if we are nearing the max size
|
||||
if (cache.size() > (maxsize * threshold) &&
|
||||
System.nanoTime() > (wrapper.Timestamp + TIMEDIFF))
|
||||
{
|
||||
//if (log.isDebugEnabled())
|
||||
// log.debug("*****Removing timedout key: " + key);
|
||||
cache.remove(key);
|
||||
wrapper = null;
|
||||
}
|
||||
}
|
||||
return wrapper != null ? wrapper.Value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.cache.SimpleCache#put(K, V)
|
||||
*/
|
||||
public synchronized void put(K key, V value)
|
||||
{
|
||||
if (cache.size() < maxsize)
|
||||
{
|
||||
//if (log.isDebugEnabled())
|
||||
// log.debug("***Adding new key: " + key + " size: " + cache.size());
|
||||
cache.put(key, new CacheItem(key, value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.cache.SimpleCache#remove(K)
|
||||
*/
|
||||
public synchronized void remove(K key)
|
||||
{
|
||||
cache.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.cache.SimpleCache#clear()
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
// better to let the GC deal with removing the old map in one shot
|
||||
// rather than try to clear each slot slowly using clear()
|
||||
cache = new HashMap<Object, CacheItem<K, V>>(maxsize, 1.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.cache.SimpleCache#contains(K)
|
||||
*/
|
||||
public synchronized boolean contains(K key)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simple wrapper class for a cache item.
|
||||
* Stores a timestamp of when the item was added so it can be purged from the cache later.
|
||||
*/
|
||||
private static class CacheItem<K, V>
|
||||
{
|
||||
CacheItem(K key, V value)
|
||||
{
|
||||
this.key = key;
|
||||
Value = value;
|
||||
Timestamp = System.nanoTime();
|
||||
}
|
||||
|
||||
public int hashCode()
|
||||
{
|
||||
return key.hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if (o instanceof CacheItem == false) return false;
|
||||
return key.equals( ((CacheItem)o).key );
|
||||
}
|
||||
|
||||
private K key;
|
||||
long Timestamp;
|
||||
V Value;
|
||||
}
|
||||
}
|
@@ -20,7 +20,7 @@ import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.cache.AutoExpireCache;
|
||||
import org.alfresco.repo.cache.SimpleCache;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
|
||||
@@ -40,7 +40,7 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
|
||||
|
||||
private AuthenticationService authenticationService;
|
||||
|
||||
private static AutoExpireCache<NodeRef, String> ownerCache = new AutoExpireCache<NodeRef, String>(1024, 0.75f);
|
||||
private SimpleCache<NodeRef, String> nodeOwnerCache;
|
||||
|
||||
public OwnableServiceImpl()
|
||||
{
|
||||
@@ -59,16 +59,27 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
|
||||
this.authenticationService = authenticationService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ownerCache a transactionally-safe cache of node owners
|
||||
*/
|
||||
public void setNodeOwnerCache(SimpleCache<NodeRef, String> ownerCache)
|
||||
{
|
||||
this.nodeOwnerCache = ownerCache;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception
|
||||
{
|
||||
if (nodeService == null)
|
||||
{
|
||||
throw new IllegalArgumentException("A node service must be set");
|
||||
throw new IllegalArgumentException("Property 'nodeService' has not been set");
|
||||
}
|
||||
if (authenticationService == null)
|
||||
{
|
||||
throw new IllegalArgumentException("An authentication service must be set");
|
||||
throw new IllegalArgumentException("Property 'authenticationService' has not been set");
|
||||
}
|
||||
if (nodeOwnerCache == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Property 'nodeOwnerCache' has not been set");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +87,7 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
|
||||
|
||||
public String getOwner(NodeRef nodeRef)
|
||||
{
|
||||
String userName = ownerCache.get(nodeRef);
|
||||
String userName = nodeOwnerCache.get(nodeRef);
|
||||
|
||||
if (userName == null)
|
||||
{
|
||||
@@ -89,7 +100,7 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
|
||||
{
|
||||
userName = DefaultTypeConverter.INSTANCE.convert(String.class, nodeService.getProperty(nodeRef, ContentModel.PROP_CREATOR));
|
||||
}
|
||||
ownerCache.put(nodeRef, userName);
|
||||
nodeOwnerCache.put(nodeRef, userName);
|
||||
}
|
||||
|
||||
return userName;
|
||||
@@ -97,8 +108,6 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
|
||||
|
||||
public void setOwner(NodeRef nodeRef, String userName)
|
||||
{
|
||||
ownerCache.remove(nodeRef);
|
||||
|
||||
if (!nodeService.hasAspect(nodeRef, ContentModel.ASPECT_OWNABLE))
|
||||
{
|
||||
HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>(1, 1.0f);
|
||||
@@ -109,6 +118,7 @@ public class OwnableServiceImpl implements OwnableService, InitializingBean
|
||||
{
|
||||
nodeService.setProperty(nodeRef, ContentModel.PROP_OWNER, userName);
|
||||
}
|
||||
nodeOwnerCache.put(nodeRef, userName);
|
||||
}
|
||||
|
||||
public void takeOwnership(NodeRef nodeRef)
|
||||
|
@@ -25,7 +25,7 @@ import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.providers.dao.User;
|
||||
|
||||
import org.alfresco.repo.cache.AutoExpireCache;
|
||||
import org.alfresco.repo.cache.SimpleCache;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationComponent;
|
||||
import org.alfresco.repo.security.permissions.DynamicAuthority;
|
||||
import org.alfresco.repo.security.permissions.NodePermissionEntry;
|
||||
@@ -62,8 +62,8 @@ public class PermissionServiceImpl implements PermissionServiceSPI, Initializing
|
||||
|
||||
private static Log log = LogFactory.getLog(PermissionServiceImpl.class);
|
||||
|
||||
private static AutoExpireCache<Serializable, AccessStatus> accessCache = new AutoExpireCache<Serializable, AccessStatus>(4096, 0.75f);
|
||||
|
||||
/** a transactionally-safe cache to be injected */
|
||||
private SimpleCache<Serializable, AccessStatus> accessCache;
|
||||
|
||||
/*
|
||||
* Access to the model
|
||||
@@ -147,33 +147,46 @@ public class PermissionServiceImpl implements PermissionServiceSPI, Initializing
|
||||
this.dynamicAuthorities = dynamicAuthorities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the permissions access cache.
|
||||
*
|
||||
* @param accessCache a transactionally safe cache
|
||||
*/
|
||||
public void setAccessCache(SimpleCache<Serializable, AccessStatus> accessCache)
|
||||
{
|
||||
this.accessCache = accessCache;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception
|
||||
{
|
||||
if (dictionaryService == null)
|
||||
{
|
||||
throw new IllegalArgumentException("There must be a dictionary service");
|
||||
throw new IllegalArgumentException("Property 'dictionaryService' has not been set");
|
||||
}
|
||||
if (modelDAO == null)
|
||||
{
|
||||
throw new IllegalArgumentException("There must be a permission model service");
|
||||
throw new IllegalArgumentException("Property 'modelDAO' has not been set");
|
||||
}
|
||||
if (nodeService == null)
|
||||
{
|
||||
throw new IllegalArgumentException("There must be a node service");
|
||||
throw new IllegalArgumentException("Property 'nodeService' has not been set");
|
||||
}
|
||||
if (permissionsDAO == null)
|
||||
{
|
||||
throw new IllegalArgumentException("There must be a permission dao");
|
||||
throw new IllegalArgumentException("Property 'permissionsDAO' has not been set");
|
||||
}
|
||||
if (authenticationComponent == null)
|
||||
{
|
||||
throw new IllegalArgumentException("There must be an authentication component");
|
||||
throw new IllegalArgumentException("Property 'authenticationComponent' has not been set");
|
||||
}
|
||||
if(authorityService == null)
|
||||
{
|
||||
throw new IllegalArgumentException("There must be an authority service");
|
||||
throw new IllegalArgumentException("Property 'authorityService' has not been set");
|
||||
}
|
||||
if (accessCache == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Property 'accessCache' has not been set");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
@@ -405,9 +418,9 @@ public class PermissionServiceImpl implements PermissionServiceSPI, Initializing
|
||||
* dynamically so they must all be used) the NodeRef ID and the permission reference itself.
|
||||
* This gives a unique key for each permission test.
|
||||
*/
|
||||
static Serializable generateKey(Set auths, NodeRef ref, PermissionReference perm)
|
||||
static Serializable generateKey(Set<String> auths, NodeRef ref, PermissionReference perm)
|
||||
{
|
||||
HashSet key = new HashSet(auths);
|
||||
HashSet<Serializable> key = new HashSet<Serializable>(auths);
|
||||
key.add(ref.getId());
|
||||
key.add(perm.toString());
|
||||
return key;
|
||||
|
Reference in New Issue
Block a user