package org.keycloak.provider; import org.jboss.logging.Logger; import java.util.HashMap; import java.util.IdentityHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.ServiceLoader; /** * @author Stian Thorgersen */ public class ProviderManager { private static final Logger log = Logger.getLogger(ProviderManager.class); private List loaders = new LinkedList(); private Map> cache = new HashMap>(); public ProviderManager(ClassLoader baseClassLoader, String... resources) { List factories = new LinkedList(); for (ProviderLoaderFactory f : ServiceLoader.load(ProviderLoaderFactory.class, getClass().getClassLoader())) { 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 load(Spi spi) { List factories = cache.get(spi.getName()); if (factories == null) { factories = new LinkedList(); IdentityHashMap factoryClasses = new IdentityHashMap(); for (ProviderLoader loader : loaders) { List f = loader.load(spi); if (f != null) { for (ProviderFactory pf: f) { // make sure there are no duplicates if (!factoryClasses.containsKey(pf.getClass())) { factories.add(pf); factoryClasses.put(pf.getClass(), pf); } } } } } return factories; } public synchronized ProviderFactory load(Spi spi, String providerId) { for (ProviderFactory f : load(spi)) { if (f.getId().equals(providerId)) { return f; } } return null; } }