diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java index 629c8a0..5b73900 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java @@ -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(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java index 5eaa865..70a853b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java @@ -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 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) {