Lucio Lelii 2016-09-23 10:23:59 +00:00
parent 2f2620b800
commit 69994e0bca
1 changed files with 23 additions and 5 deletions

View File

@ -1,7 +1,10 @@
package org.gcube.common.authorization.library.enpoints; package org.gcube.common.authorization.library.enpoints;
import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
@ -10,6 +13,7 @@ import org.gcube.common.scan.ClasspathScanner;
import org.gcube.common.scan.ClasspathScannerFactory; import org.gcube.common.scan.ClasspathScannerFactory;
import org.gcube.common.scan.matchers.NameMatcher; import org.gcube.common.scan.matchers.NameMatcher;
import org.gcube.common.scan.resources.ClasspathResource; import org.gcube.common.scan.resources.ClasspathResource;
import org.gcube.common.scope.api.ServiceMap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -30,6 +34,7 @@ public class AuthorizationEndpointScanner {
public static synchronized EndpointsContainer endpoints() { public static synchronized EndpointsContainer endpoints() {
if (endpoints==null || endpoints.getEndpoints().size()==0){ if (endpoints==null || endpoints.getEndpoints().size()==0){
log.trace("starting authorization endpoint retrieving");
Map<Integer, AuthorizationEndpoint> endpointsMap = new HashMap<Integer, AuthorizationEndpoint>(); Map<Integer, AuthorizationEndpoint> endpointsMap = new HashMap<Integer, AuthorizationEndpoint>();
try { try {
@ -40,16 +45,19 @@ public class AuthorizationEndpointScanner {
String defaultInfrastructure = null; String defaultInfrastructure = null;
int defaultInfraPriority= Integer.MAX_VALUE; int defaultInfraPriority= Integer.MAX_VALUE;
ClasspathScanner scanner = ClasspathScannerFactory.scanner(); for (String r :getEnpointResourceNames()){
for (ClasspathResource r : scanner.scan(new NameMatcher(configurationPattern))){
AuthorizationEndpoint endpoint = (AuthorizationEndpoint)um.unmarshal(r.stream()); URL url = Thread.currentThread().getContextClassLoader().getResource(r);
AuthorizationEndpoint endpoint = (AuthorizationEndpoint)um.unmarshal(url);
if (defaultInfrastructure==null) if (defaultInfrastructure==null)
defaultInfrastructure = endpoint.getInfrastructure(); defaultInfrastructure = endpoint.getInfrastructure();
if (!endpointsMap.containsKey(endpoint.getInfrastructure()) if (!endpointsMap.containsKey(endpoint.getInfrastructure())
|| endpointsMap.get(endpoint.getInfrastructure()).getPriority()> endpoint.getPriority()){ || endpointsMap.get(endpoint.getInfrastructure()).getPriority()> endpoint.getPriority()){
if ((r.name()).startsWith("default") && endpoint.getPriority()<defaultInfraPriority ){ if (r.startsWith("default") && endpoint.getPriority()<defaultInfraPriority ){
defaultInfrastructure = endpoint.getInfrastructure(); defaultInfrastructure = endpoint.getInfrastructure();
defaultInfraPriority = endpoint.getPriority(); defaultInfraPriority = endpoint.getPriority();
} }
@ -62,6 +70,7 @@ public class AuthorizationEndpointScanner {
if (endpointsMap.size()==0) if (endpointsMap.size()==0)
throw new Exception("no endpoints retreived"); throw new Exception("no endpoints retreived");
endpoints = new EndpointsContainer(endpointsMap, defaultInfrastructure); endpoints = new EndpointsContainer(endpointsMap, defaultInfrastructure);
log.trace("authorization endpoint retrieving finished");
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("could not load authorization endpoints", e); throw new RuntimeException("could not load authorization endpoints", e);
} }
@ -70,4 +79,13 @@ public class AuthorizationEndpointScanner {
return endpoints; return endpoints;
} }
private static Set<String> getEnpointResourceNames() {
ClasspathScanner scanner = ClasspathScannerFactory.scanner();
Set<String> names = new HashSet<String>();
for (ClasspathResource r : scanner.scan(new NameMatcher(configurationPattern)))
names.add(r.name());
return names;
}
} }