Aligning context client

This commit is contained in:
Luca Frosini 2020-11-04 19:34:07 +01:00
parent 6b3538ccf3
commit 53e16847b6
3 changed files with 84 additions and 53 deletions

View File

@ -13,39 +13,28 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.context.Conte
*/
public interface ResourceRegistryContextClient {
public Context create(Context context)
throws ContextAlreadyPresentException, ResourceRegistryException;
public Context create(Context context) throws ContextAlreadyPresentException, ResourceRegistryException;
public String create(String context)
throws ContextAlreadyPresentException, ResourceRegistryException;
public Context read(Context context)
throws ContextNotFoundException, ResourceRegistryException;
public Context read(UUID uuid)
throws ContextNotFoundException, ResourceRegistryException;
public String read(String uuid)
throws ContextNotFoundException, ResourceRegistryException;
public Context update(Context context)
throws ResourceRegistryException;
public String create(String context) throws ContextAlreadyPresentException, ResourceRegistryException;
public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException;
public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException;
public String read(String uuid) throws ContextNotFoundException, ResourceRegistryException;
public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException;
public Context update(Context context) throws ResourceRegistryException;
public String update(String context) throws ResourceRegistryException;
public boolean delete(Context context) throws ContextNotFoundException, ResourceRegistryException;
public boolean delete(UUID uuid) throws ContextNotFoundException, ResourceRegistryException;
public boolean delete(String uuid) throws ContextNotFoundException, ResourceRegistryException;
public String update(String context)
throws ResourceRegistryException;
public boolean delete(Context context)
throws ContextNotFoundException, ResourceRegistryException;
public boolean delete(UUID uuid)
throws ContextNotFoundException, ResourceRegistryException;
public boolean delete(String uuid)
throws ContextNotFoundException, ResourceRegistryException;
public List<Context> all() throws ResourceRegistryException;
}

View File

@ -47,6 +47,16 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
contextCache.setContextCacheRenewal(contextCacheRenewal);
}
private void forceCacheRefresh() {
try {
ContextCache contextCache = ContextCache.getInstance();
contextCache.cleanCache();
contextCache.refreshContextsIfNeeded();
}catch (Exception e) {
logger.warn("Unable to force cache refresh.", e);
}
}
protected String internalCreate(Context context) throws ContextAlreadyPresentException, ResourceRegistryException {
try {
Header header = context.getHeader();
@ -70,8 +80,9 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString);
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
logger.trace("{} successfully created", c);
forceCacheRefresh();
logger.trace("{} successfully created", c);
return c;
} catch(ResourceRegistryException e) {
@ -80,14 +91,6 @@ 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) {
}
}
}
@ -135,8 +138,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
} catch (IOException e) {
throw new RuntimeException(e);
}
contextCache.cleanCache();
contextCache.refreshContextsIfNeeded();
forceCacheRefresh();
Context c = contextCache.getContextByUUID(context.getHeader().getUUID());
if(c!=null){
context = c;
@ -147,7 +149,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
return context;
}
public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
@Override
public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
String contextFullName = ResourceRegistryContextClientFactory.getCurrentContextFullName();
ContextCache contextCache = ContextCache.getInstance();
UUID uuid = contextCache.getUUIDByFullName(contextFullName);
@ -226,6 +229,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString);
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
forceCacheRefresh();
logger.trace("{} successfully updated", c);
return c;
@ -235,14 +240,6 @@ 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) {
}
}
}
@ -321,7 +318,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
}
}
public List<Context> getAllContextFromServer() throws ResourceRegistryException {
protected List<Context> getAllContextFromServer() throws ResourceRegistryException {
try {
logger.trace("Going to read {} with UUID {}", Context.NAME);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);

View File

@ -14,7 +14,9 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.context.impl.entities.ContextImpl;
import org.gcube.informationsystem.context.reference.entities.Context;
import org.gcube.informationsystem.context.reference.relations.IsParentOf;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
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.ContextException;
@ -450,4 +452,47 @@ public class ResourceRegistryContextClientTest extends ContextTest {
}
@Test
public void testGetAllContexts() throws Exception {
List<Context> contexts = resourceRegistryContextClient.all();
logger.debug("{}", contexts);
ContextCache contextCache = ContextCache.getInstance();
Map<UUID, String> uuidToContextFullName = contextCache.getUUIDToContextFullNameAssociation();
logger.debug("{}", uuidToContextFullName);
for(Context c : contexts) {
UUID uuid = c.getHeader().getUUID();
if(c.getParent()!=null) {
IsParentOf isParentOf = c.getParent();
Context parentContext = isParentOf.getSource();
UUID parentUUID = parentContext.getHeader().getUUID();
Assert.assertEquals(parentContext, contextCache.getContextByUUID(parentUUID));
List<IsParentOf> children = parentContext.getChildren();
boolean found = false;
for(IsParentOf ipo : children) {
if(ipo.equals(isParentOf)) {
found = true;
break;
}
}
Assert.assertTrue(found);
logger.debug("{} : {} (parent {} : {})", c.getHeader().getUUID(), contextCache.getContextFullNameByUUID(uuid), parentUUID, contextCache.getContextFullNameByUUID(parentUUID));
}else {
logger.debug("{} : {}", c.getHeader().getUUID(), contextCache.getContextFullNameByUUID(uuid));
}
}
Context currentContext = resourceRegistryContextClient.readCurrentContext();
logger.debug("Current context : {}", currentContext);
for(Context c : contexts) {
UUID uuid = c.getHeader().getUUID();
Context context = resourceRegistryContextClient.read(uuid);
String fullName = ContextCache.getInstance().getContextFullNameByUUID(uuid);
logger.debug("{} - {} : {}", uuid, fullName, context);
}
}
}