Added facilities to use the client outside of container

This commit is contained in:
Luca Frosini 2023-03-02 14:51:31 +01:00
parent 2084b6d913
commit 18c3d69d22
2 changed files with 26 additions and 33 deletions

View File

@ -12,14 +12,24 @@ public class ResourceRegistryContextClientFactory {
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientFactory.class);
public static ResourceRegistryContextClient create() {
public static String getResourceRegistryURL() {
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 ResourceRegistryContextClient create() {
String address = getResourceRegistryURL();
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
return new ResourceRegistryContextClientImpl(address);
}
public static ResourceRegistryContextClient 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);
return new ResourceRegistryContextClientImpl(address);
}

View File

@ -41,6 +41,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
protected Map<String, String> headers;
protected ContextCache contextCache;
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
@Override
@ -64,15 +66,22 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
}
public ResourceRegistryContextClientImpl(String address) {
this(address, true);
}
public ResourceRegistryContextClientImpl(String address, boolean sharedContextCache) {
this.address = address;
this.headers = new HashMap<>();
ContextCache contextCache = ContextCache.getInstance();
if(sharedContextCache) {
contextCache = ContextCache.getInstance();
}else {
contextCache = new ContextCache();
}
contextCache.setContextCacheRenewal(contextCacheRenewal);
}
private void forceCacheRefresh() {
try {
ContextCache contextCache = ContextCache.getInstance();
contextCache.cleanCache();
contextCache.refreshContextsIfNeeded();
}catch (Exception e) {
@ -104,7 +113,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
@Override
public List<Context> all() throws ResourceRegistryException {
ContextCache contextCache = ContextCache.getInstance();
return contextCache.getContexts();
}
@ -217,8 +225,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
@Override
public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
ContextCache contextCache = ContextCache.getInstance();
Context context = ContextCache.getInstance().getContextByUUID(uuid);;
Context context = contextCache.getContextByUUID(uuid);;
if(context == null) {
String contextJson = readFromServer(uuid.toString());
try {
@ -240,28 +247,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
@Override
public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
String contextFullName = ContextUtility.getCurrentContextFullName();
ContextCache contextCache = ContextCache.getInstance();
UUID uuid = contextCache.getUUIDByFullName(contextFullName);
Context context = null;
if(uuid == null) {
String contextJson = readFromServer(ContextPath.CURRENT_CONTEXT_PATH_PART);
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;
return read(uuid);
}
@Override
@ -381,8 +368,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
boolean deleted = true;
logger.info("{} with UUID {} {}", Context.NAME, uuid,
deleted ? " successfully deleted" : "was NOT deleted");
return deleted;
@ -394,9 +379,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
throw new RuntimeException(e);
}finally {
try {
ContextCache contextCache = ContextCache.getInstance();
contextCache.cleanCache();
contextCache.refreshContextsIfNeeded();
forceCacheRefresh();
}catch (Exception e) {
}