Added Context UUID cache

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@148297 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-05-04 15:24:36 +00:00
parent b8d082399c
commit e559991510
2 changed files with 45 additions and 9 deletions

View File

@ -191,6 +191,8 @@ public class ContextManagementImpl implements ContextManagement {
logger.info("Trying to rename {} with UUID {} to {}", Context.NAME,
contextUUID, newName);
ContextUtility.invalidCurrentContextUUIDCache();
orientGraph = SecurityContextMapper.getSecurityContextFactory(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
PermissionMode.WRITER).getTx();
@ -206,7 +208,7 @@ public class ContextManagementImpl implements ContextManagement {
Edge edge = iteratorEdge.next();
if (iteratorEdge.hasNext()) {
throw new ContextException("");
throw new ContextException("Seems that the Context has more than one Parent. THIS IS REALLY STRANGE AND SHOULD NOT OCCUR. PLEASE CONTACT THE ADMINISTRATOR");
}
Vertex parent = edge.getVertex(Direction.OUT);
@ -251,6 +253,8 @@ public class ContextManagementImpl implements ContextManagement {
"Trying to move {} with UUID {} as child of {} with UUID {}",
Context.NAME, contextToMoveUUID, Context.NAME, newParentUUID);
ContextUtility.invalidCurrentContextUUIDCache();
orientGraph = SecurityContextMapper.getSecurityContextFactory(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
PermissionMode.WRITER).getTx();
@ -316,6 +320,8 @@ public class ContextManagementImpl implements ContextManagement {
try {
logger.info("Trying to remove {} with UUID {}", Context.NAME, uuid);
ContextUtility.invalidCurrentContextUUIDCache();
orientGraph = SecurityContextMapper.getSecurityContextFactory(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
PermissionMode.WRITER).getTx();

View File

@ -3,7 +3,9 @@
*/
package org.gcube.informationsystem.resourceregistry.context;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import org.gcube.common.authorization.client.Constants;
@ -38,6 +40,22 @@ public class ContextUtility {
private static final Logger logger = LoggerFactory
.getLogger(ContextUtility.class);
private static Map<String, UUID> contextUUIDCache;
static {
contextUUIDCache = new HashMap<>();
}
protected static void invalidContextUUIDCache(){
contextUUIDCache = new HashMap<>();
}
protected static void invalidCurrentContextUUIDCache(){
String scope = getCurrentContext();
contextUUIDCache.remove(scope);
}
public static UUID addToActualContext(OrientGraph orientGraph, Element element)
throws ContextException {
UUID contextUUID = ContextUtility.getActualContextUUID();
@ -76,6 +94,8 @@ public class ContextUtility {
}
return authorizationEntry.getContext();
}
public static UUID getActualContextUUID() throws ContextException {
OrientGraphFactory factory = null;
@ -87,14 +107,24 @@ public class ContextUtility {
throw new ContextException("Null Token and Scope. Please set your token first.");
}
logger.trace("Trying to get context UUID for scope {}", scope);
factory = SecurityContextMapper
.getSecurityContextFactory(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
PermissionMode.READER);
orientGraph = factory.getTx();
Vertex context = ContextUtility.getContextVertexByFullName(
orientGraph, scope);
return Utility.getUUID(context);
UUID uuid = contextUUIDCache.get(scope);
if(uuid == null){
logger.trace("UUID for scope {} is not in cache. Going to query it", scope);
factory = SecurityContextMapper
.getSecurityContextFactory(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
PermissionMode.READER);
orientGraph = factory.getTx();
Vertex context = ContextUtility.getContextVertexByFullName(
orientGraph, scope);
uuid = Utility.getUUID(context);
contextUUIDCache.put(scope, uuid);
}
return uuid;
} catch (ContextException e) {
throw e;
} catch (Exception e) {