Added facilities to use the client outside of container

This commit is contained in:
Luca Frosini 2023-03-02 14:51:22 +01:00
parent 022b3acc8b
commit 6639589e9e
2 changed files with 25 additions and 7 deletions

View File

@ -12,14 +12,24 @@ public class ResourceRegistryClientFactory {
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryClientFactory.class); private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryClientFactory.class);
public static ResourceRegistryClient create() { public static String getResourceRegistryURL() {
String address = String.format("%s/%s", ServiceInstance.getServiceURL(),Constants.SERVICE_NAME); String address = String.format("%s/%s", ServiceInstance.getServiceURL(),Constants.SERVICE_NAME);
return address;
}
public static String getResourceRegistryURL(String context) {
String address = String.format("%s/%s", ServiceInstance.getServiceURL(context),Constants.SERVICE_NAME);
return address;
}
public static ResourceRegistryClient create() {
String address = getResourceRegistryURL();
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address); logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
return new ResourceRegistryClientImpl(address); return new ResourceRegistryClientImpl(address);
} }
public static ResourceRegistryClient create(String context) { public static ResourceRegistryClient create(String context) {
String address = String.format("%s/%s", ServiceInstance.getServiceURL(context),Constants.SERVICE_NAME); String address = getResourceRegistryURL(context);
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address); logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
return new ResourceRegistryClientImpl(address); return new ResourceRegistryClientImpl(address);
} }

View File

@ -60,9 +60,12 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
protected final String address; protected final String address;
protected Map<String, String> headers; protected Map<String, String> headers;
protected boolean hierarchicalMode; protected boolean hierarchicalMode;
protected boolean includeContextsInHeader; protected boolean includeContextsInHeader;
protected ContextCache contextCache;
@Override @Override
public boolean isHierarchicalMode() { public boolean isHierarchicalMode() {
return hierarchicalMode; return hierarchicalMode;
@ -136,11 +139,19 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
} }
public ResourceRegistryClientImpl(String address) { public ResourceRegistryClientImpl(String address) {
this(address, true);
}
public ResourceRegistryClientImpl(String address, boolean sharedContextCache) {
this.address = address; this.address = address;
this.hierarchicalMode = false; this.hierarchicalMode = false;
this.includeContextsInHeader = false; this.includeContextsInHeader = false;
this.headers = new HashMap<>(); this.headers = new HashMap<>();
ContextCache contextCache = ContextCache.getInstance(); if(sharedContextCache) {
contextCache = ContextCache.getInstance();
}else {
contextCache = new ContextCache();
}
contextCache.setContextCacheRenewal(contextCacheRenewal); contextCache.setContextCacheRenewal(contextCacheRenewal);
} }
@ -173,7 +184,6 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
@Override @Override
public List<Context> getAllContext() throws ResourceRegistryException { public List<Context> getAllContext() throws ResourceRegistryException {
ContextCache contextCache = ContextCache.getInstance();
return contextCache.getContexts(); return contextCache.getContexts();
} }
@ -226,8 +236,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
@Override @Override
public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException { public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
ContextCache contextCache = ContextCache.getInstance(); Context context = contextCache.getContextByUUID(uuid);;
Context context = ContextCache.getInstance().getContextByUUID(uuid);;
if(context == null) { if(context == null) {
context = getContextFromServer(ContextPath.CURRENT_CONTEXT_PATH_PART); context = getContextFromServer(ContextPath.CURRENT_CONTEXT_PATH_PART);
contextCache.cleanCache(); contextCache.cleanCache();
@ -245,7 +254,6 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
@Override @Override
public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException { public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
String contextFullName = org.gcube.common.context.ContextUtility.getCurrentContextFullName(); String contextFullName = org.gcube.common.context.ContextUtility.getCurrentContextFullName();
ContextCache contextCache = ContextCache.getInstance();
UUID uuid = contextCache.getUUIDByFullName(contextFullName); UUID uuid = contextCache.getUUIDByFullName(contextFullName);
Context context = null; Context context = null;
if(uuid == null) { if(uuid == null) {