Using context cache

This commit is contained in:
Luca Frosini 2020-11-04 17:38:57 +01:00
parent ec8e1ec5f6
commit 6b3538ccf3
1 changed files with 106 additions and 13 deletions

View File

@ -1,17 +1,22 @@
package org.gcube.informationsystem.resourceregistry.context;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.common.gxhttp.reference.GXConnection;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.informationsystem.context.reference.entities.Context;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
import org.gcube.informationsystem.utils.ElementMapper;
@ -27,9 +32,19 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
protected final String address;
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
@Override
public List<Context> renew() throws ResourceRegistryException {
return getAllContextFromServer();
}
};
public ResourceRegistryContextClientImpl(String address) {
this.address = address;
ContextCache contextCache = ContextCache.getInstance();
contextCache.setContextCacheRenewal(contextCacheRenewal);
}
protected String internalCreate(Context context) throws ContextAlreadyPresentException, ResourceRegistryException {
@ -56,6 +71,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.trace("{} successfully created", c);
return c;
} catch(ResourceRegistryException e) {
@ -64,6 +80,14 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
} finally {
try {
ContextCache contextCache = ContextCache.getInstance();
contextCache.cleanCache();
contextCache.refreshContextsIfNeeded();
}catch (Exception e) {
}
}
}
@ -102,20 +126,67 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
@Override
public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
try {
String res = read(uuid.toString());
return ElementMapper.unmarshal(Context.class, res);
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
ContextCache contextCache = ContextCache.getInstance();
Context context = ContextCache.getInstance().getContextByUUID(uuid);;
if(context == null) {
String contextJson = readFromServer(uuid.toString());
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("Context with UUID {} is {}. It is possibile to get it from the server but not from the cache. This is very strange and should not occur.", uuid, context);
}
}
return context;
}
public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
String contextFullName = ResourceRegistryContextClientFactory.getCurrentContextFullName();
ContextCache contextCache = ContextCache.getInstance();
UUID uuid = contextCache.getUUIDByFullName(contextFullName);
Context context = null;
if(uuid == null) {
String contextJson = readFromServer(AccessPath.CURRENT_CONTEXT);
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;
}
@Override
public String read(String uuid) throws ContextNotFoundException, ResourceRegistryException {
try {
return ElementMapper.marshal(read(UUID.fromString(uuid)));
} catch (ContextNotFoundException e) {
throw e;
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
} catch (ResourceRegistryException e) {
throw e;
}
}
public String readFromServer(String uuid) throws ContextNotFoundException, ResourceRegistryException {
try {
logger.trace("Going to read {} with UUID {}", Context.NAME, uuid);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
@ -129,7 +200,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
logger.debug("Got {} is {}", Context.NAME, c);
return c;
} catch(ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;
@ -165,6 +235,14 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
} finally {
try {
ContextCache contextCache = ContextCache.getInstance();
contextCache.cleanCache();
contextCache.refreshContextsIfNeeded();
}catch (Exception e) {
}
}
}
@ -221,6 +299,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
boolean deleted = true;
logger.info("{} with UUID {} {}", Context.NAME, uuid,
deleted ? " successfully deleted" : "was NOT deleted");
return deleted;
@ -230,11 +310,18 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
} catch(Exception e) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException(e);
}finally {
try {
ContextCache contextCache = ContextCache.getInstance();
contextCache.cleanCache();
contextCache.refreshContextsIfNeeded();
}catch (Exception e) {
}
}
}
@Override
public List<Context> all() throws ResourceRegistryException {
public List<Context> getAllContextFromServer() throws ResourceRegistryException {
try {
logger.trace("Going to read {} with UUID {}", Context.NAME);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
@ -257,4 +344,10 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
}
}
@Override
public List<Context> all() throws ResourceRegistryException {
ContextCache contextCache = ContextCache.getInstance();
return contextCache.getContexts();
}
}