Using ContextCache to get context representation
This commit is contained in:
parent
3ea46fbac1
commit
d651f8251e
|
@ -6,6 +6,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
|
@ -25,6 +26,7 @@ public class ResourceRegistryClientFactory {
|
|||
|
||||
protected static Map<String, ResourceRegistryClient> clients;
|
||||
|
||||
protected static List<String> addresses;
|
||||
protected static boolean hierarchicalMode;
|
||||
|
||||
public static boolean isHierarchicalMode() {
|
||||
|
@ -46,8 +48,6 @@ public class ResourceRegistryClientFactory {
|
|||
hierarchicalMode = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
|
||||
private static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'";
|
||||
private static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
|
||||
|
@ -58,7 +58,16 @@ public class ResourceRegistryClientFactory {
|
|||
private static String serviceEndpointNameFormat = "$resource/Profile/Name/text() eq '%1s'";
|
||||
private static String serviceEndpointstatusFormat = "$resource/Profile/RunTime/Status/text() eq 'READY'";
|
||||
|
||||
|
||||
public static String getCurrentContextFullName() {
|
||||
String token = SecurityTokenProvider.instance.get();
|
||||
AuthorizationEntry authorizationEntry = null;
|
||||
try {
|
||||
authorizationEntry = org.gcube.common.authorization.client.Constants.authorizationService().get(token);
|
||||
} catch(Exception e) {
|
||||
return ScopeProvider.instance.get();
|
||||
}
|
||||
return authorizationEntry.getContext();
|
||||
}
|
||||
|
||||
private static SimpleQuery queryForService(){
|
||||
return ICFactory.queryFor(GCoreEndpoint.class)
|
||||
|
@ -100,41 +109,29 @@ public class ResourceRegistryClientFactory {
|
|||
}
|
||||
|
||||
|
||||
public static ResourceRegistryClient create(){
|
||||
|
||||
public static ResourceRegistryClient create() {
|
||||
String address = null;
|
||||
|
||||
if(FORCED_URL!=null){
|
||||
return new ResourceRegistryClientImpl(FORCED_URL);
|
||||
address = FORCED_URL;
|
||||
}else {
|
||||
|
||||
if(addresses==null) {
|
||||
addresses = getAddresses();
|
||||
}
|
||||
|
||||
String key = null;
|
||||
if (SecurityTokenProvider.instance.get() == null) {
|
||||
if (ScopeProvider.instance.get() == null) {
|
||||
throw new RuntimeException(
|
||||
"Null Token and Scope. Please set your token first.");
|
||||
}
|
||||
key = ScopeProvider.instance.get();
|
||||
} else {
|
||||
key = SecurityTokenProvider.instance.get();
|
||||
}
|
||||
|
||||
ResourceRegistryClient client = clients.get(key);
|
||||
|
||||
if(client==null){
|
||||
|
||||
List<String> addresses = getAddresses();
|
||||
|
||||
if(addresses==null || addresses.isEmpty()){
|
||||
String error = String.format("No %s:%s found in the current context", Constants.SERVICE_CLASS, Constants.SERVICE_NAME);
|
||||
String error = String.format("No %s:%s found in the current context %s", Constants.SERVICE_CLASS, Constants.SERVICE_NAME, getCurrentContextFullName());
|
||||
throw new RuntimeException(error);
|
||||
}
|
||||
|
||||
Random random = new Random();
|
||||
int index = random.nextInt(addresses.size());
|
||||
|
||||
client = new ResourceRegistryClientImpl(addresses.get(index));
|
||||
|
||||
address = addresses.get(index);
|
||||
}
|
||||
|
||||
return client;
|
||||
return new ResourceRegistryClientImpl(address);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ import org.gcube.informationsystem.model.reference.entities.Resource;
|
|||
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
||||
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
|
||||
import org.gcube.informationsystem.model.reference.relations.Relation;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
|
@ -60,8 +62,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
|||
gxHTTPStringRequest.queryParams(queryParams);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Context> getAllContext() throws ContextNotFoundException, ResourceRegistryException {
|
||||
public List<Context> getAllContextFromServer() throws ResourceRegistryException {
|
||||
try {
|
||||
logger.info("Going to read all {}s", Context.NAME);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
||||
|
@ -75,6 +76,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
|||
|
||||
logger.debug("Got Contexts are {}", ret);
|
||||
return ElementMapper.unmarshalList(Context.class, ret);
|
||||
|
||||
} catch(ResourceRegistryException e) {
|
||||
// logger.trace("Error while getting {} schema for {}", polymorphic ?
|
||||
// AccessPath.POLYMORPHIC_PARAM + " " : "",
|
||||
|
@ -88,8 +90,25 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
|||
}
|
||||
}
|
||||
|
||||
protected Context getContext(String id) throws ContextNotFoundException, ResourceRegistryException {
|
||||
@Override
|
||||
public List<Context> getAllContext() throws ResourceRegistryException {
|
||||
ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
|
||||
|
||||
@Override
|
||||
public List<Context> renew() throws ResourceRegistryException {
|
||||
return getAllContextFromServer();
|
||||
}
|
||||
|
||||
};
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
contextCache.setContextCacheRenewal(contextCacheRenewal);
|
||||
return contextCache.getContexts();
|
||||
}
|
||||
|
||||
protected Context getContextFromServer(String id) throws ContextNotFoundException, ResourceRegistryException {
|
||||
try {
|
||||
// TODO use cache
|
||||
|
||||
logger.info("Going to get current {} ", Context.NAME);
|
||||
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
||||
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
||||
|
@ -118,12 +137,29 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
|||
|
||||
@Override
|
||||
public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
|
||||
return getContext(uuid.toString());
|
||||
return ContextCache.getInstance().getContextByUUID(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
|
||||
return getContext(AccessPath.CURRENT_CONTEXT);
|
||||
String contextFullName = ResourceRegistryClientFactory.getCurrentContextFullName();
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
UUID uuid = contextCache.getUUIDByFullName(contextFullName);
|
||||
Context context = null;
|
||||
if(uuid == null) {
|
||||
context = getContextFromServer(AccessPath.CURRENT_CONTEXT);
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue