Aligned factory to other clients

This commit is contained in:
Luca Frosini 2020-11-04 16:33:33 +01:00
parent 3683b87f71
commit ec5e42a6e9
1 changed files with 26 additions and 32 deletions

View File

@ -1,11 +1,10 @@
package org.gcube.informationsystem.resourceregistry.schema;
import java.util.ArrayList;
import java.util.HashMap;
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;
@ -23,10 +22,10 @@ public class ResourceRegistrySchemaClientFactory {
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistrySchemaClientFactory.class);
protected static Map<String, ResourceRegistrySchemaClient> clients;
protected static List<String> addresses;
static {
clients = new HashMap<>();
addresses = new ArrayList<>();
}
private static String FORCED_URL = null;
@ -45,7 +44,16 @@ public class ResourceRegistrySchemaClientFactory {
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)
@ -86,42 +94,28 @@ public class ResourceRegistrySchemaClientFactory {
return addresses;
}
public static ResourceRegistrySchemaClient create(){
public static ResourceRegistrySchemaClient create() {
String address = null;
if(FORCED_URL!=null){
return new ResourceRegistrySchemaClientImpl(FORCED_URL);
}
address = FORCED_URL;
}else {
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.");
if(addresses==null) {
addresses = getAddresses();
}
key = ScopeProvider.instance.get();
} else {
key = SecurityTokenProvider.instance.get();
}
ResourceRegistrySchemaClient 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 ResourceRegistrySchemaClientImpl(addresses.get(index));
address = addresses.get(index);
}
return client;
return new ResourceRegistrySchemaClientImpl(address);
}
}