KEYCLOAK-999 Load providers from file-system

This commit is contained in:
Stian Thorgersen 2015-01-28 09:10:32 +01:00
parent 15feb39ecc
commit 67ba1de56f
11 changed files with 223 additions and 81 deletions

View file

@ -1,70 +0,0 @@
package org.keycloak.util;
import java.util.Iterator;
import java.util.ServiceLoader;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class ProviderLoader<T> implements Iterable<T> {
private final ServiceLoader<T> serviceLoader;
public static <T> Iterable<T> load(Class<T> service) {
ServiceLoader<T> providers = ServiceLoader.load(service);
return new ProviderLoader(providers);
}
private ProviderLoader(ServiceLoader<T> serviceLoader) {
this.serviceLoader = serviceLoader;
}
@Override
public Iterator iterator() {
return new ProviderIterator(serviceLoader.iterator());
}
private static class ProviderIterator<T> implements Iterator<T> {
private final Iterator<T> itr;
private T next;
private ProviderIterator(Iterator<T> itr) {
this.itr = itr;
setNext();
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public T next() {
T n = next;
setNext();
return n;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
private void setNext() {
next = null;
while (itr.hasNext()) {
if (itr.hasNext()) {
T n = itr.next();
if (!System.getProperties().containsKey(n.getClass().getName() + ".disabled")) {
next = n;
return;
}
}
}
}
}
}

View file

@ -0,0 +1,12 @@
package org.keycloak.provider;
import java.util.List;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public interface ProviderLoader {
List<ProviderFactory> load(Spi spi);
}

View file

@ -0,0 +1,12 @@
package org.keycloak.provider;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public interface ProviderLoaderFactory {
boolean supports(String type);
ProviderLoader create(ClassLoader baseClassLoader, String resource);
}

View file

@ -1,4 +1,8 @@
{
"providers": [
"classpath:${jboss.server.config.dir}/providers/*"
],
"admin": {
"realm": "master"
},

View file

@ -0,0 +1,27 @@
package org.keycloak.provider;
import java.util.LinkedList;
import java.util.List;
import java.util.ServiceLoader;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class DefaultProviderLoader implements ProviderLoader {
private ClassLoader classLoader;
public DefaultProviderLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public List<ProviderFactory> load(Spi spi) {
LinkedList<ProviderFactory> list = new LinkedList<ProviderFactory>();
for (ProviderFactory f : ServiceLoader.load(spi.getProviderFactoryClass(), classLoader)) {
list.add(f);
}
return list;
}
}

View file

@ -0,0 +1,18 @@
package org.keycloak.provider;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class DefaultProviderLoaderFactory implements ProviderLoaderFactory {
@Override
public boolean supports(String type) {
return false;
}
@Override
public ProviderLoader create(ClassLoader baseClassLoader, String resource) {
return new DefaultProviderLoader(baseClassLoader);
}
}

View file

@ -0,0 +1,63 @@
package org.keycloak.provider;
import org.jboss.logging.Logger;
import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.LinkedList;
import java.util.List;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class FileSystemProviderLoaderFactory implements ProviderLoaderFactory {
private static final Logger log = Logger.getLogger(FileSystemProviderLoaderFactory.class);
@Override
public boolean supports(String type) {
return "classpath".equals(type);
}
@Override
public ProviderLoader create(ClassLoader baseClassLoader, String resource) {
return new DefaultProviderLoader(createClassLoader(baseClassLoader, resource.split(";")));
}
private static URLClassLoader createClassLoader(ClassLoader parent, String... files) {
try {
List<URL> urls = new LinkedList<URL>();
for (String f : files) {
if (f.endsWith("*")) {
File dir = new File(f.substring(0, f.length() - 1));
if (dir.isDirectory()) {
for (File file : dir.listFiles(new JarFilter())) {
urls.add(file.toURI().toURL());
}
}
} else {
urls.add(new File(f).toURI().toURL());
}
}
log.debug("Loading providers from " + urls.toString());
return new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static class JarFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jar");
}
}
}

View file

@ -0,0 +1,74 @@
package org.keycloak.provider;
import org.jboss.logging.Logger;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class ProviderManager {
private static final Logger log = Logger.getLogger(ProviderManager.class);
private List<ProviderLoader> loaders = new LinkedList<ProviderLoader>();
private Map<String, List<ProviderFactory>> cache = new HashMap<String, List<ProviderFactory>>();
public ProviderManager(ClassLoader baseClassLoader, String... resources) {
List<ProviderLoaderFactory> factories = new LinkedList<ProviderLoaderFactory>();
for (ProviderLoaderFactory f : ServiceLoader.load(ProviderLoaderFactory.class)) {
factories.add(f);
}
log.debugv("Provider loaders {0}", factories);
loaders.add(new DefaultProviderLoader(baseClassLoader));
if (resources != null) {
for (String r : resources) {
String type = r.substring(0, r.indexOf(':'));
String resource = r.substring(r.indexOf(':') + 1, r.length());
boolean found = false;
for (ProviderLoaderFactory f : factories) {
if (f.supports(type)) {
loaders.add(f.create(baseClassLoader, resource));
found = true;
break;
}
}
if (!found) {
throw new RuntimeException("Provider loader for " + r + " not found");
}
}
}
}
public synchronized List<ProviderFactory> load(Spi spi) {
List<ProviderFactory> factories = cache.get(spi.getName());
if (factories == null) {
factories = new LinkedList<ProviderFactory>();
for (ProviderLoader loader : loaders) {
List<ProviderFactory> f = loader.load(spi);
if (f != null) {
factories.addAll(f);
}
}
}
return factories;
}
public synchronized ProviderFactory load(Spi spi, String providerId) {
for (ProviderFactory f : load(spi)) {
if (f.getId().equals(providerId)) {
return f;
}
}
return null;
}
}

View file

@ -6,6 +6,7 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.provider.Provider;
import org.keycloak.provider.ProviderFactory;
import org.keycloak.provider.ProviderManager;
import org.keycloak.provider.Spi;
import java.util.HashMap;
@ -24,6 +25,8 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory {
private Map<Class<? extends Provider>, Map<String, ProviderFactory>> factoriesMap = new HashMap<Class<? extends Provider>, Map<String, ProviderFactory>>();
public void init() {
ProviderManager pm = new ProviderManager(getClass().getClassLoader(), Config.scope().getArray("providers"));
for (Spi spi : ServiceLoader.load(Spi.class)) {
Map<String, ProviderFactory> factories = new HashMap<String, ProviderFactory>();
factoriesMap.put(spi.getProviderClass(), factories);
@ -32,7 +35,11 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory {
if (provider != null) {
this.provider.put(spi.getProviderClass(), provider);
ProviderFactory factory = loadProviderFactory(spi, provider);
ProviderFactory factory = pm.load(spi, provider);
if (factory == null) {
throw new RuntimeException("Failed to find provider " + provider + " for " + spi.getName());
}
Config.Scope scope = Config.scope(spi.getName(), provider);
factory.init(scope);
@ -40,7 +47,7 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory {
log.debugv("Loaded SPI {0} (provider = {1})", spi.getName(), provider);
} else {
for (ProviderFactory factory : ServiceLoader.load(spi.getProviderFactoryClass())) {
for (ProviderFactory factory : pm.load(spi)) {
Config.Scope scope = Config.scope(spi.getName(), factory.getId());
factory.init(scope);
@ -59,15 +66,6 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory {
}
}
private ProviderFactory loadProviderFactory(Spi spi, String id) {
for (ProviderFactory factory : ServiceLoader.load(spi.getProviderFactoryClass())) {
if (factory.getId().equals(id)){
return factory;
}
}
throw new RuntimeException("Failed to find provider " + id + " for " + spi.getName());
}
public KeycloakSession create() {
return new DefaultKeycloakSession(this);
}

View file

@ -0,0 +1,2 @@
org.keycloak.provider.DefaultProviderLoaderFactory
org.keycloak.provider.FileSystemProviderLoaderFactory

View file

@ -8,6 +8,8 @@ log4j.logger.org.keycloak=info
# Enable to view loaded SPI and Providers
# log4j.logger.org.keycloak.services.DefaultKeycloakSessionFactory=debug
# log4j.logger.org.keycloak.provider.ProviderManager=debug
# log4j.logger.org.keycloak.provider.FileSystemProviderLoaderFactory=debug
# Enable to view database updates
# log4j.logger.org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider=debug