Added facilities to use the client outside of container
This commit is contained in:
parent
2084b6d913
commit
18c3d69d22
|
@ -12,14 +12,24 @@ public class ResourceRegistryContextClientFactory {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientFactory.class);
|
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);
|
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);
|
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
|
||||||
return new ResourceRegistryContextClientImpl(address);
|
return new ResourceRegistryContextClientImpl(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ResourceRegistryContextClient create(String context) {
|
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);
|
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
|
||||||
return new ResourceRegistryContextClientImpl(address);
|
return new ResourceRegistryContextClientImpl(address);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
|
|
||||||
protected Map<String, String> headers;
|
protected Map<String, String> headers;
|
||||||
|
|
||||||
|
protected ContextCache contextCache;
|
||||||
|
|
||||||
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
|
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -64,15 +66,22 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResourceRegistryContextClientImpl(String address) {
|
public ResourceRegistryContextClientImpl(String address) {
|
||||||
|
this(address, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResourceRegistryContextClientImpl(String address, boolean sharedContextCache) {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void forceCacheRefresh() {
|
private void forceCacheRefresh() {
|
||||||
try {
|
try {
|
||||||
ContextCache contextCache = ContextCache.getInstance();
|
|
||||||
contextCache.cleanCache();
|
contextCache.cleanCache();
|
||||||
contextCache.refreshContextsIfNeeded();
|
contextCache.refreshContextsIfNeeded();
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
|
@ -104,7 +113,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Context> all() throws ResourceRegistryException {
|
public List<Context> all() throws ResourceRegistryException {
|
||||||
ContextCache contextCache = ContextCache.getInstance();
|
|
||||||
return contextCache.getContexts();
|
return contextCache.getContexts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,8 +225,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
|
public Context read(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) {
|
||||||
String contextJson = readFromServer(uuid.toString());
|
String contextJson = readFromServer(uuid.toString());
|
||||||
try {
|
try {
|
||||||
|
@ -240,28 +247,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
@Override
|
@Override
|
||||||
public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
|
public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
|
||||||
String contextFullName = ContextUtility.getCurrentContextFullName();
|
String contextFullName = ContextUtility.getCurrentContextFullName();
|
||||||
ContextCache contextCache = ContextCache.getInstance();
|
|
||||||
UUID uuid = contextCache.getUUIDByFullName(contextFullName);
|
UUID uuid = contextCache.getUUIDByFullName(contextFullName);
|
||||||
Context context = null;
|
return read(uuid);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -381,8 +368,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
|
|
||||||
boolean deleted = true;
|
boolean deleted = true;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
logger.info("{} with UUID {} {}", Context.NAME, uuid,
|
logger.info("{} with UUID {} {}", Context.NAME, uuid,
|
||||||
deleted ? " successfully deleted" : "was NOT deleted");
|
deleted ? " successfully deleted" : "was NOT deleted");
|
||||||
return deleted;
|
return deleted;
|
||||||
|
@ -394,9 +379,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}finally {
|
}finally {
|
||||||
try {
|
try {
|
||||||
ContextCache contextCache = ContextCache.getInstance();
|
forceCacheRefresh();
|
||||||
contextCache.cleanCache();
|
|
||||||
contextCache.refreshContextsIfNeeded();
|
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue