org.xbib.oai.server.OAIServiceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oai-server Show documentation
Show all versions of oai-server Show documentation
Open Archive Initiative library for Java
package org.xbib.oai.server;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
*/
public class OAIServiceFactory {
private static final Logger logger = Logger.getLogger(OAIServiceFactory.class.getName());
private static final Map services = new HashMap<>();
private static final OAIServiceFactory instance = new OAIServiceFactory();
private OAIServiceFactory() {
ServiceLoader loader = ServiceLoader.load(OAIServer.class);
for (OAIServer service : loader) {
try {
URI uri = service.getURL().toURI();
if (!services.containsKey(uri)) {
services.put(uri, service);
}
} catch (URISyntaxException e) {
logger.log(Level.FINE, e.getMessage(), e);
}
}
}
public static OAIServiceFactory getInstance() {
return instance;
}
public static OAIServer getDefaultService() {
return services.isEmpty() ? null : services.entrySet().iterator().next().getValue();
}
public static OAIServer getService(URI uri) {
if (services.containsKey(uri)) {
return services.get(uri);
}
throw new IllegalArgumentException("OAI service " + uri + " not found in " + services);
}
public static OAIServer getService(String name) {
Properties properties = new Properties();
InputStream in = instance.getClass().getResourceAsStream("/org/xbib/oai/service/" + name + ".properties");
if (in != null) {
try {
properties.load(in);
} catch (IOException e) {
logger.log(Level.FINE, e.getMessage(), e);
}
} else {
throw new IllegalArgumentException("service " + name + " not found");
}
return new PropertiesOAIServer(properties);
}
}