From 560faafab5d9db56486c7ceee0e788f3034d1cb1 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 28 Nov 2024 12:04:48 +0100 Subject: [PATCH] Ported fix --- .../api/contexts/ContextCache.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/api/contexts/ContextCache.java b/src/main/java/org/gcube/informationsystem/resourceregistry/api/contexts/ContextCache.java index 1484e27..3f39e47 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/api/contexts/ContextCache.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/api/contexts/ContextCache.java @@ -62,9 +62,12 @@ public class ContextCache { protected Map contextFullNameToUUID; protected Tree contextsTree; + protected boolean initialized; + public ContextCache() { Calendar now = Calendar.getInstance(); cleanCache(now); + initialized = false; } public void cleanCache() { @@ -72,7 +75,7 @@ public class ContextCache { } protected void cleanCache(Calendar calendar) { - this.contexts = null; + this.contexts = new ArrayList<>(); this.uuidToContext = new LinkedHashMap<>(); this.uuidToContextFullName = new LinkedHashMap<>(); this.contextFullNameToUUID = new TreeMap<>(); @@ -84,6 +87,7 @@ public class ContextCache { this.expiringTime.setTimeInMillis(calendar.getTimeInMillis()); this.expiringTime.add(Calendar.MILLISECOND, -1); this.expiringTime.add(Calendar.MILLISECOND, expiringTimeout); + initialized = false; } public void renew() throws ResourceRegistryException { @@ -103,14 +107,17 @@ public class ContextCache { public synchronized void refreshContextsIfNeeded() throws ResourceRegistryException { Calendar now = Calendar.getInstance(); - if((now.after(expiringTime) || (contexts==null)) && contextCacheRenewal!=null) { + if((now.after(expiringTime) || (!initialized)) && contextCacheRenewal!=null) { try { List contexts = contextCacheRenewal.renew(); setContexts(now, contexts); + initialized = true; } catch (ResourceRegistryException e) { - logger.error("Unable to refresh Cache", e); - if(contexts==null) { + if(!initialized) { + logger.error("Unable to initialize Context Cache", e); throw e; + }else { + logger.error("Unable to refresh Context Cache", e); } } @@ -129,7 +136,6 @@ public class ContextCache { protected void setContexts(Calendar calendar, List contexts) { cleanCache(calendar); - this.contexts = new ArrayList<>(); for(Context c : contexts) { UUID uuid = c.getID(); @@ -232,7 +238,8 @@ public class ContextCache { return new TreeMap<>(contextFullNameToUUID); } - public Tree getContextsTree() { + public Tree getContextsTree() throws ResourceRegistryException { + refreshContextsIfNeeded(); return contextsTree; }