Added ContextUtility class

This commit is contained in:
Luca Frosini 2020-11-05 15:58:06 +01:00
parent 663bfd5eeb
commit 1ae4a9074c
2 changed files with 58 additions and 6 deletions

View File

@ -48,9 +48,9 @@ public class ContextCache {
private void cleanCache(Calendar now) {
this.contexts = null;
this.uuidToContext = null;
this.uuidToContextFullName = null;
this.contextFullNameToUUID = null;
this.uuidToContext = new HashMap<>();
this.uuidToContextFullName = new HashMap<>();
this.contextFullNameToUUID = new HashMap<>();
this.creationTime = Calendar.getInstance();
this.creationTime.setTimeInMillis(now.getTimeInMillis());
this.expiringTime = Calendar.getInstance();
@ -105,7 +105,6 @@ public class ContextCache {
private void setContexts(List<Context> contexts) {
this.contexts = new ArrayList<>();
this.uuidToContext = new HashMap<>();
for(Context c : contexts) {
UUID uuid = c.getHeader().getUUID();
@ -129,8 +128,7 @@ public class ContextCache {
}
}
this.uuidToContextFullName = new HashMap<>();
this.contextFullNameToUUID = new HashMap<>();
for(Context context : contexts) {
UUID uuid = context.getHeader().getUUID();

View File

@ -0,0 +1,54 @@
package org.gcube.informationsystem.resourceregistry.api.contexts;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JavaType;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.informationsystem.utils.ElementMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ContextUtility {
private static Logger logger = LoggerFactory.getLogger(ContextUtility.class);
public static Set<UUID> getContextUUIDSet(String jsonArray) throws IOException {
ObjectMapper mapper = ElementMapper.getObjectMapper();
JavaType type = mapper.getTypeFactory().constructCollectionType(HashSet.class, UUID.class);
return mapper.readValue(jsonArray, type);
}
public static Set<String> getContextFullNameSet(String jsonArray) throws Exception {
Set<UUID> uuids = getContextUUIDSet(jsonArray);
return getContextFullNameSet(uuids);
}
public static Set<String> getContextFullNameSet(Collection<UUID> uuids) throws Exception {
ContextCache contextCache = ContextCache.getInstance();
Set<String> contextFullNames = new HashSet<>();
try {
Map<UUID, String> uuidToContextFullNameAssociation = contextCache.getUUIDToContextFullNameAssociation();
if(!uuidToContextFullNameAssociation.keySet().containsAll(uuids)) {
logger.debug("{} does not contain all the contexts identified by the following UUID list. Trying to invalidate the cache", ContextCache.class.getSimpleName(), uuids);
contextCache.cleanCache();
uuidToContextFullNameAssociation = contextCache.getUUIDToContextFullNameAssociation();
}
for(UUID uuid : uuids) {
String fullName = uuidToContextFullNameAssociation.get(uuid);
if(fullName==null) {
throw new Exception("Unable to get the full name of context with UUID " + uuid.toString());
}
contextFullNames.add(fullName);
}
}catch (Exception e) {
throw new Exception("Unable to convert Set<UUID> to Set<String> containing Context Full Name. Be sure the cache has been configured to retrieve the context list or provide a valid UUID list", e);
}
return contextFullNames;
}
}