70 lines
2.3 KiB
Java
70 lines
2.3 KiB
Java
package org.gcube.common.authorization.library.enpoints;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
import javax.xml.bind.Unmarshaller;
|
|
|
|
import org.gcube.common.scan.ClasspathScanner;
|
|
import org.gcube.common.scan.ClasspathScannerFactory;
|
|
import org.gcube.common.scan.matchers.NameMatcher;
|
|
import org.gcube.common.scan.resources.ClasspathResource;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class AuthorizationEndpointScanner {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(AuthorizationEndpointScanner.class);
|
|
|
|
private static EndpointsContainer endpoints;
|
|
|
|
/**
|
|
* The path used to find service map configuration files.
|
|
*/
|
|
static final String configurationPattern = ".*\\.authorization";
|
|
|
|
/**
|
|
* Scans the classpath for {@link ServiceMap}s.
|
|
*/
|
|
public static synchronized EndpointsContainer endpoints() {
|
|
|
|
if (endpoints==null || endpoints.getEndpoints().size()==0){
|
|
Map<Integer, AuthorizationEndpoint> endpointsMap = new HashMap<Integer, AuthorizationEndpoint>();
|
|
|
|
try {
|
|
|
|
JAXBContext context = JAXBContext.newInstance(AuthorizationEndpoint.class);
|
|
Unmarshaller um = context.createUnmarshaller();
|
|
|
|
String defaultInfrastructure = null;
|
|
|
|
ClasspathScanner scanner = ClasspathScannerFactory.scanner();
|
|
for (ClasspathResource r : scanner.scan(new NameMatcher(configurationPattern))){
|
|
AuthorizationEndpoint endpoint = (AuthorizationEndpoint)um.unmarshal(r.stream());
|
|
if (defaultInfrastructure==null)
|
|
defaultInfrastructure = endpoint.getInfrastructure();
|
|
|
|
if (!endpointsMap.containsKey(endpoint.getInfrastructure())
|
|
|| endpointsMap.get(endpoint.getInfrastructure()).getPriority()> endpoint.getPriority()){
|
|
if ((r.name()).startsWith("default"))
|
|
defaultInfrastructure = endpoint.getInfrastructure();
|
|
endpointsMap.put(endpoint.getInfrastructure().hashCode(), endpoint);
|
|
}
|
|
|
|
log.info("loaded endpoint {} ",endpoint.toString());
|
|
}
|
|
|
|
if (endpointsMap.size()==0)
|
|
throw new Exception("no endpoints retreived");
|
|
endpoints = new EndpointsContainer(endpointsMap, defaultInfrastructure);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("could not load authorization endpoints", e);
|
|
}
|
|
|
|
}
|
|
return endpoints;
|
|
}
|
|
|
|
}
|