From 18c3d69d22e2a45b04539f8e4eee917efd7a813f Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 2 Mar 2023 14:51:31 +0100 Subject: [PATCH] Added facilities to use the client outside of container --- .../ResourceRegistryContextClientFactory.java | 14 +++++- .../ResourceRegistryContextClientImpl.java | 45 ++++++------------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientFactory.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientFactory.java index 2effa3f..c115ca3 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientFactory.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientFactory.java @@ -12,14 +12,24 @@ public class ResourceRegistryContextClientFactory { private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientFactory.class); - public static ResourceRegistryContextClient create() { + public static String getResourceRegistryURL() { String address = String.format("%s/%s", ServiceInstance.getServiceURL(),Constants.SERVICE_NAME); + return address; + } + + public static String getResourceRegistryURL(String context) { + String address = String.format("%s/%s", ServiceInstance.getServiceURL(context),Constants.SERVICE_NAME); + return address; + } + + public static ResourceRegistryContextClient create() { + String address = getResourceRegistryURL(); logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address); return new ResourceRegistryContextClientImpl(address); } public static ResourceRegistryContextClient create(String context) { - String address = String.format("%s/%s", ServiceInstance.getServiceURL(context),Constants.SERVICE_NAME); + String address = getResourceRegistryURL(context); logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address); return new ResourceRegistryContextClientImpl(address); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java index 2e34b04..8905bdc 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ResourceRegistryContextClientImpl.java @@ -41,6 +41,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex protected Map headers; + protected ContextCache contextCache; + protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() { @Override @@ -64,15 +66,22 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } public ResourceRegistryContextClientImpl(String address) { + this(address, true); + } + + public ResourceRegistryContextClientImpl(String address, boolean sharedContextCache) { this.address = address; this.headers = new HashMap<>(); - ContextCache contextCache = ContextCache.getInstance(); + if(sharedContextCache) { + contextCache = ContextCache.getInstance(); + }else { + contextCache = new ContextCache(); + } contextCache.setContextCacheRenewal(contextCacheRenewal); } private void forceCacheRefresh() { try { - ContextCache contextCache = ContextCache.getInstance(); contextCache.cleanCache(); contextCache.refreshContextsIfNeeded(); }catch (Exception e) { @@ -104,7 +113,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex @Override public List all() throws ResourceRegistryException { - ContextCache contextCache = ContextCache.getInstance(); return contextCache.getContexts(); } @@ -217,8 +225,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex @Override public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException { - ContextCache contextCache = ContextCache.getInstance(); - Context context = ContextCache.getInstance().getContextByUUID(uuid);; + Context context = contextCache.getContextByUUID(uuid);; if(context == null) { String contextJson = readFromServer(uuid.toString()); try { @@ -240,28 +247,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex @Override public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException { String contextFullName = ContextUtility.getCurrentContextFullName(); - ContextCache contextCache = ContextCache.getInstance(); UUID uuid = contextCache.getUUIDByFullName(contextFullName); - Context context = null; - if(uuid == null) { - String contextJson = readFromServer(ContextPath.CURRENT_CONTEXT_PATH_PART); - try { - context = ElementMapper.unmarshal(Context.class, contextJson); - } catch (IOException e) { - throw new RuntimeException(e); - } - contextCache.cleanCache(); - contextCache.refreshContextsIfNeeded(); - Context c = contextCache.getContextByUUID(context.getHeader().getUUID()); - if(c!=null){ - context = c; - }else { - logger.error("Current Context is {}. It is possibile to get it from the server but not from the cache. This is very strange and should not occur.", contextFullName); - } - }else { - context = contextCache.getContextByUUID(uuid); - } - return context; + return read(uuid); } @Override @@ -381,8 +368,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex boolean deleted = true; - - logger.info("{} with UUID {} {}", Context.NAME, uuid, deleted ? " successfully deleted" : "was NOT deleted"); return deleted; @@ -394,9 +379,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex throw new RuntimeException(e); }finally { try { - ContextCache contextCache = ContextCache.getInstance(); - contextCache.cleanCache(); - contextCache.refreshContextsIfNeeded(); + forceCacheRefresh(); }catch (Exception e) { }