Compare commits
1 Commits
feature/sg
...
master
Author | SHA1 | Date |
---|---|---|
Luca Frosini | 96b10b3b1d |
|
@ -62,9 +62,12 @@ public class ContextCache {
|
||||||
protected Map<String, UUID> contextFullNameToUUID;
|
protected Map<String, UUID> contextFullNameToUUID;
|
||||||
protected Tree<Context> contextsTree;
|
protected Tree<Context> contextsTree;
|
||||||
|
|
||||||
|
protected boolean initialized;
|
||||||
|
|
||||||
public ContextCache() {
|
public ContextCache() {
|
||||||
Calendar now = Calendar.getInstance();
|
Calendar now = Calendar.getInstance();
|
||||||
cleanCache(now);
|
cleanCache(now);
|
||||||
|
initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanCache() {
|
public void cleanCache() {
|
||||||
|
@ -72,7 +75,7 @@ public class ContextCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void cleanCache(Calendar calendar) {
|
protected void cleanCache(Calendar calendar) {
|
||||||
this.contexts = null;
|
this.contexts = new ArrayList<>();
|
||||||
this.uuidToContext = new LinkedHashMap<>();
|
this.uuidToContext = new LinkedHashMap<>();
|
||||||
this.uuidToContextFullName = new LinkedHashMap<>();
|
this.uuidToContextFullName = new LinkedHashMap<>();
|
||||||
this.contextFullNameToUUID = new TreeMap<>();
|
this.contextFullNameToUUID = new TreeMap<>();
|
||||||
|
@ -84,6 +87,7 @@ public class ContextCache {
|
||||||
this.expiringTime.setTimeInMillis(calendar.getTimeInMillis());
|
this.expiringTime.setTimeInMillis(calendar.getTimeInMillis());
|
||||||
this.expiringTime.add(Calendar.MILLISECOND, -1);
|
this.expiringTime.add(Calendar.MILLISECOND, -1);
|
||||||
this.expiringTime.add(Calendar.MILLISECOND, expiringTimeout);
|
this.expiringTime.add(Calendar.MILLISECOND, expiringTimeout);
|
||||||
|
initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void renew() throws ResourceRegistryException {
|
public void renew() throws ResourceRegistryException {
|
||||||
|
@ -103,14 +107,17 @@ public class ContextCache {
|
||||||
|
|
||||||
public synchronized void refreshContextsIfNeeded() throws ResourceRegistryException {
|
public synchronized void refreshContextsIfNeeded() throws ResourceRegistryException {
|
||||||
Calendar now = Calendar.getInstance();
|
Calendar now = Calendar.getInstance();
|
||||||
if((now.after(expiringTime) || (contexts==null)) && contextCacheRenewal!=null) {
|
if((now.after(expiringTime) || (!initialized)) && contextCacheRenewal!=null) {
|
||||||
try {
|
try {
|
||||||
List<Context> contexts = contextCacheRenewal.renew();
|
List<Context> contexts = contextCacheRenewal.renew();
|
||||||
setContexts(now, contexts);
|
setContexts(now, contexts);
|
||||||
|
initialized = true;
|
||||||
} catch (ResourceRegistryException e) {
|
} catch (ResourceRegistryException e) {
|
||||||
logger.error("Unable to refresh Cache", e);
|
if(!initialized) {
|
||||||
if(contexts==null) {
|
logger.error("Unable to initialize Context Cache", e);
|
||||||
throw 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<Context> contexts) {
|
protected void setContexts(Calendar calendar, List<Context> contexts) {
|
||||||
cleanCache(calendar);
|
cleanCache(calendar);
|
||||||
this.contexts = new ArrayList<>();
|
|
||||||
|
|
||||||
for(Context c : contexts) {
|
for(Context c : contexts) {
|
||||||
UUID uuid = c.getID();
|
UUID uuid = c.getID();
|
||||||
|
@ -232,7 +238,8 @@ public class ContextCache {
|
||||||
return new TreeMap<>(contextFullNameToUUID);
|
return new TreeMap<>(contextFullNameToUUID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tree<Context> getContextsTree() {
|
public Tree<Context> getContextsTree() throws ResourceRegistryException {
|
||||||
|
refreshContextsIfNeeded();
|
||||||
return contextsTree;
|
return contextsTree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue