mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
First-cut dynamic Dictionary & Namespaces - updated caches to be cluster-aware & tenant-aware
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6675 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -29,22 +29,153 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
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.repo.cache.SimpleCache;
|
||||
import org.alfresco.repo.tenant.TenantService;
|
||||
import org.alfresco.service.namespace.NamespaceException;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Simple in-memory namespace DAO
|
||||
*/
|
||||
public class NamespaceDAOImpl implements NamespaceDAO
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(NamespaceDAOImpl.class);
|
||||
|
||||
/**
|
||||
* Lock objects
|
||||
*/
|
||||
private ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
private Lock readLock = lock.readLock();
|
||||
private Lock writeLock = lock.writeLock();
|
||||
|
||||
// internal caches that are clusterable
|
||||
private SimpleCache<String, List<String>> urisCache;
|
||||
private SimpleCache<String, Map<String, String>> prefixesCache;
|
||||
|
||||
private List<String> uris = new ArrayList<String>();
|
||||
private HashMap<String, String> prefixes = new HashMap<String, String>();
|
||||
// Dependencies
|
||||
private TenantService tenantService;
|
||||
|
||||
|
||||
public void setTenantService(TenantService tenantService)
|
||||
{
|
||||
this.tenantService = tenantService;
|
||||
}
|
||||
|
||||
public void setUrisCache(SimpleCache<String, List<String>> urisCache)
|
||||
{
|
||||
this.urisCache = urisCache;
|
||||
}
|
||||
|
||||
public void setPrefixesCache(SimpleCache<String, Map<String, String>> prefixesCache)
|
||||
{
|
||||
this.prefixesCache = prefixesCache;
|
||||
}
|
||||
|
||||
|
||||
private DictionaryDAO dictionaryDAO;
|
||||
|
||||
public void registerDictionary(DictionaryDAO dictionaryDAO)
|
||||
{
|
||||
this.dictionaryDAO = dictionaryDAO;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialise empty namespaces
|
||||
*/
|
||||
public void init()
|
||||
{
|
||||
String tenantDomain = getTenantDomain();
|
||||
|
||||
putUrisCtx(tenantDomain, new ArrayList<String>());
|
||||
putPrefixesCtx(tenantDomain, new HashMap<String, String>());
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Empty namespaces initialised");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the namespaces
|
||||
*/
|
||||
public void destroy()
|
||||
{
|
||||
String tenantDomain = getTenantDomain();
|
||||
|
||||
removeUrisCtx(tenantDomain);
|
||||
removePrefixesCtx(tenantDomain);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Namespaces destroyed");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the namespaces (by resetting the dictionary)
|
||||
*/
|
||||
private void reset()
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Resetting namespaces ...");
|
||||
}
|
||||
|
||||
if (dictionaryDAO == null)
|
||||
{
|
||||
// Unexpected
|
||||
throw new AlfrescoRuntimeException("Dictionary should be registered in order to perform reset");
|
||||
}
|
||||
else
|
||||
{
|
||||
dictionaryDAO.reset();
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("... resetting namespaces completed");
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.ref.NamespacePrefixResolver#getURIs()
|
||||
*/
|
||||
public Collection<String> getURIs()
|
||||
{
|
||||
return Collections.unmodifiableCollection(uris);
|
||||
if (! tenantService.isTenantUser())
|
||||
{
|
||||
return Collections.unmodifiableCollection(getUrisCtx());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get tenant-specific URIs
|
||||
List<String> domainUris = getUrisCtx();
|
||||
|
||||
// Get non-tenant-specific URIs (and filter out, if overridden)
|
||||
List<String> urisFiltered = new ArrayList<String>();
|
||||
for(String uri : getUrisCtx(""))
|
||||
{
|
||||
if (domainUris.contains(uri))
|
||||
{
|
||||
// overridden, hence skip this default prefix
|
||||
continue;
|
||||
}
|
||||
urisFiltered.add(uri);
|
||||
}
|
||||
|
||||
// default (non-overridden) + tenant-specific
|
||||
urisFiltered.addAll(domainUris);
|
||||
|
||||
return Collections.unmodifiableCollection(urisFiltered);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,8 +183,33 @@ public class NamespaceDAOImpl implements NamespaceDAO
|
||||
* @see org.alfresco.repo.ref.NamespacePrefixResolver#getPrefixes()
|
||||
*/
|
||||
public Collection<String> getPrefixes()
|
||||
{
|
||||
return Collections.unmodifiableCollection(prefixes.keySet());
|
||||
{
|
||||
if (! tenantService.isTenantUser())
|
||||
{
|
||||
return Collections.unmodifiableCollection(getPrefixesCtx().keySet());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get tenant-specific prefixes
|
||||
Collection<String> domainPrefixes = getPrefixesCtx().keySet();
|
||||
|
||||
// Get non-tenant-specific URIs (and filter out, if overridden)
|
||||
List<String> prefixesFiltered = new ArrayList<String>();
|
||||
for(String prefix : getPrefixesCtx("").keySet())
|
||||
{
|
||||
if (domainPrefixes.contains(prefix))
|
||||
{
|
||||
// overridden, hence skip this default prefix
|
||||
continue;
|
||||
}
|
||||
prefixesFiltered.add(prefix);
|
||||
}
|
||||
|
||||
// default (non-overridden) + tenant-specific
|
||||
prefixesFiltered.addAll(domainPrefixes);
|
||||
|
||||
return Collections.unmodifiableCollection(prefixesFiltered);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,11 +218,11 @@ public class NamespaceDAOImpl implements NamespaceDAO
|
||||
*/
|
||||
public void addURI(String uri)
|
||||
{
|
||||
if (uris.contains(uri))
|
||||
if (getUrisCtx().contains(uri))
|
||||
{
|
||||
throw new NamespaceException("URI " + uri + " has already been defined");
|
||||
}
|
||||
uris.add(uri);
|
||||
getUrisCtx().add(uri);
|
||||
}
|
||||
|
||||
|
||||
@@ -75,11 +231,11 @@ public class NamespaceDAOImpl implements NamespaceDAO
|
||||
*/
|
||||
public void addPrefix(String prefix, String uri)
|
||||
{
|
||||
if (!uris.contains(uri))
|
||||
if (!getUrisCtx().contains(uri))
|
||||
{
|
||||
throw new NamespaceException("Namespace URI " + uri + " does not exist");
|
||||
}
|
||||
prefixes.put(prefix, uri);
|
||||
getPrefixesCtx().put(prefix, uri);
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +244,7 @@ public class NamespaceDAOImpl implements NamespaceDAO
|
||||
*/
|
||||
public void removeURI(String uri)
|
||||
{
|
||||
uris.remove(uri);
|
||||
getUrisCtx().remove(uri);
|
||||
}
|
||||
|
||||
|
||||
@@ -97,7 +253,7 @@ public class NamespaceDAOImpl implements NamespaceDAO
|
||||
*/
|
||||
public void removePrefix(String prefix)
|
||||
{
|
||||
prefixes.remove(prefix);
|
||||
getPrefixesCtx().remove(prefix);
|
||||
}
|
||||
|
||||
|
||||
@@ -106,7 +262,25 @@ public class NamespaceDAOImpl implements NamespaceDAO
|
||||
*/
|
||||
public String getNamespaceURI(String prefix)
|
||||
{
|
||||
return prefixes.get(prefix);
|
||||
if (! tenantService.isTenantUser())
|
||||
{
|
||||
return getPrefixesCtx().get(prefix);
|
||||
}
|
||||
else
|
||||
{
|
||||
// first look for tenant-specific prefix
|
||||
String uri = getPrefixesCtx().get(prefix);
|
||||
if (uri != null)
|
||||
{
|
||||
// found tenant specific uri
|
||||
return uri;
|
||||
}
|
||||
else
|
||||
{
|
||||
// try with default (non-tenant-specific) prefix
|
||||
return getPrefixesCtx("").get(prefix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -115,16 +289,255 @@ public class NamespaceDAOImpl implements NamespaceDAO
|
||||
*/
|
||||
public Collection<String> getPrefixes(String URI)
|
||||
{
|
||||
Collection<String> uriPrefixes = new ArrayList<String>();
|
||||
for (String key : prefixes.keySet())
|
||||
if (! tenantService.isTenantUser())
|
||||
{
|
||||
String uri = prefixes.get(key);
|
||||
if ((uri != null) && (uri.equals(URI)))
|
||||
Collection<String> uriPrefixes = new ArrayList<String>();
|
||||
for (String key : getPrefixesCtx().keySet())
|
||||
{
|
||||
uriPrefixes.add(key);
|
||||
String uri = getPrefixesCtx().get(key);
|
||||
if ((uri != null) && (uri.equals(URI)))
|
||||
{
|
||||
uriPrefixes.add(key);
|
||||
}
|
||||
}
|
||||
return uriPrefixes;
|
||||
}
|
||||
return uriPrefixes;
|
||||
else
|
||||
{
|
||||
// check domain prefixes
|
||||
Collection<String> domainUriPrefixes = new ArrayList<String>();
|
||||
for (String key : getPrefixesCtx().keySet())
|
||||
{
|
||||
String uri = getPrefixesCtx().get(key);
|
||||
if ((uri != null) && (uri.equals(URI)))
|
||||
{
|
||||
domainUriPrefixes.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
// check non-domain prefixes
|
||||
Collection<String> uriPrefixes = new ArrayList<String>();
|
||||
for (String key : getPrefixesCtx("").keySet())
|
||||
{
|
||||
String uri = getPrefixesCtx("").get(key);
|
||||
if ((uri != null) && (uri.equals(URI)))
|
||||
{
|
||||
if (domainUriPrefixes != null)
|
||||
{
|
||||
if (domainUriPrefixes.contains(key))
|
||||
{
|
||||
// overridden, hence skip this default prefix
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
uriPrefixes.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
if (domainUriPrefixes != null)
|
||||
{
|
||||
// default (non-overridden) + domain
|
||||
uriPrefixes.addAll(domainUriPrefixes);
|
||||
}
|
||||
|
||||
return uriPrefixes;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URIs from the cache (in the context of the current user's tenant domain)
|
||||
*
|
||||
* @return URIs
|
||||
*/
|
||||
private List<String> getUrisCtx()
|
||||
{
|
||||
return getUrisCtx(getTenantDomain());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URIs from the cache
|
||||
*
|
||||
* @param tenantDomain
|
||||
* @return URIs
|
||||
*/
|
||||
private List<String> getUrisCtx(String tenantDomain)
|
||||
{
|
||||
List<String> uris = null;
|
||||
try
|
||||
{
|
||||
readLock.lock();
|
||||
uris = urisCache.get(tenantDomain);
|
||||
}
|
||||
finally
|
||||
{
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
|
||||
if (uris == null)
|
||||
{
|
||||
reset(); // reset caches - may have been invalidated (e.g. in a cluster)
|
||||
|
||||
try
|
||||
{
|
||||
readLock.lock();
|
||||
uris = urisCache.get(tenantDomain);
|
||||
}
|
||||
finally
|
||||
{
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
if (uris == null)
|
||||
{
|
||||
// unexpected
|
||||
throw new AlfrescoRuntimeException("Failed to re-initialise urisCache " + tenantDomain);
|
||||
}
|
||||
}
|
||||
return uris;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put URIs into the cache
|
||||
*
|
||||
* @param tenantDomain
|
||||
* @param uris
|
||||
*/
|
||||
private void putUrisCtx(String tenantDomain, List<String> uris)
|
||||
{
|
||||
try
|
||||
{
|
||||
writeLock.lock();
|
||||
urisCache.put(tenantDomain, uris);
|
||||
}
|
||||
finally
|
||||
{
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove URIs from the cache
|
||||
*
|
||||
* @param tenantDomain
|
||||
*/
|
||||
private void removeUrisCtx(String tenantDomain)
|
||||
{
|
||||
try
|
||||
{
|
||||
writeLock.lock();
|
||||
if (urisCache.get(tenantDomain) != null)
|
||||
{
|
||||
urisCache.get(tenantDomain).clear();
|
||||
urisCache.remove(tenantDomain);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get prefixes from the cache
|
||||
*
|
||||
* @return prefixes
|
||||
*/
|
||||
private Map<String, String> getPrefixesCtx()
|
||||
{
|
||||
return getPrefixesCtx(getTenantDomain());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get prefixes from the cache
|
||||
*
|
||||
* @param tenantDomain
|
||||
* @return prefixes
|
||||
*/
|
||||
private Map<String, String> getPrefixesCtx(String tenantDomain)
|
||||
{
|
||||
Map<String, String> prefixes = null;
|
||||
try
|
||||
{
|
||||
readLock.lock();
|
||||
prefixes = prefixesCache.get(tenantDomain);
|
||||
}
|
||||
finally
|
||||
{
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
if (prefixes == null)
|
||||
{
|
||||
reset(); // reset caches - may have been invalidated (e.g. in a cluster)
|
||||
|
||||
try
|
||||
{
|
||||
readLock.lock();
|
||||
prefixes = prefixesCache.get(tenantDomain);
|
||||
}
|
||||
finally
|
||||
{
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
if (prefixes == null)
|
||||
{
|
||||
// unexpected
|
||||
throw new AlfrescoRuntimeException("Failed to re-initialise prefixesCache " + tenantDomain);
|
||||
}
|
||||
}
|
||||
|
||||
return prefixes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put prefixes into the cache
|
||||
*
|
||||
* @param tenantDomain
|
||||
* @param prefixes
|
||||
*/
|
||||
private void putPrefixesCtx(String tenantDomain, Map<String, String> prefixes)
|
||||
{
|
||||
try
|
||||
{
|
||||
writeLock.lock();
|
||||
prefixesCache.put(tenantDomain, prefixes);
|
||||
}
|
||||
finally
|
||||
{
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove prefixes from the cache
|
||||
*
|
||||
* @param tenantDomain
|
||||
*/
|
||||
private void removePrefixesCtx(String tenantDomain)
|
||||
{
|
||||
try
|
||||
{
|
||||
writeLock.lock();
|
||||
if (prefixesCache.get(tenantDomain) != null)
|
||||
{
|
||||
prefixesCache.get(tenantDomain).clear();
|
||||
prefixesCache.remove(tenantDomain);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Local helper - returns tenant domain (or empty string if default non-tenant)
|
||||
*/
|
||||
private String getTenantDomain()
|
||||
{
|
||||
return tenantService.getCurrentUserDomain();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user