2016-01-19 13:28:03 +00:00
|
|
|
/*
|
2016-02-03 10:20:22 +00:00
|
|
|
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
|
|
|
* and other contributors as indicated by the @author tags.
|
2016-01-19 13:28:03 +00:00
|
|
|
*
|
2016-02-03 10:20:22 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2016-01-19 13:28:03 +00:00
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2016-02-03 10:20:22 +00:00
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2016-01-19 13:28:03 +00:00
|
|
|
*/
|
2015-01-28 08:10:32 +00:00
|
|
|
package org.keycloak.provider;
|
|
|
|
|
2016-10-17 13:19:54 +00:00
|
|
|
import org.jboss.logging.Logger;
|
2016-08-07 15:41:52 +00:00
|
|
|
import org.keycloak.common.util.MultivaluedHashMap;
|
2015-01-28 08:10:32 +00:00
|
|
|
|
2016-08-07 15:41:52 +00:00
|
|
|
import java.util.Collections;
|
2015-01-28 08:10:32 +00:00
|
|
|
import java.util.HashMap;
|
2018-02-06 05:49:10 +00:00
|
|
|
import java.util.HashSet;
|
2016-01-05 22:06:26 +00:00
|
|
|
import java.util.IdentityHashMap;
|
2015-01-28 08:10:32 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.ServiceLoader;
|
2018-02-06 05:49:10 +00:00
|
|
|
import java.util.Set;
|
2015-01-28 08:10:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
|
|
|
*/
|
|
|
|
public class ProviderManager {
|
|
|
|
|
2016-10-17 13:19:54 +00:00
|
|
|
private static final Logger logger = Logger.getLogger(ProviderManager.class);
|
2015-01-28 08:10:32 +00:00
|
|
|
|
|
|
|
private List<ProviderLoader> loaders = new LinkedList<ProviderLoader>();
|
2016-08-07 15:41:52 +00:00
|
|
|
private MultivaluedHashMap<Class<? extends Provider>, ProviderFactory> cache = new MultivaluedHashMap<>();
|
|
|
|
|
2015-01-28 08:10:32 +00:00
|
|
|
|
2018-02-06 05:49:10 +00:00
|
|
|
public ProviderManager(KeycloakDeploymentInfo info, ClassLoader baseClassLoader, String... resources) {
|
2015-01-28 08:10:32 +00:00
|
|
|
List<ProviderLoaderFactory> factories = new LinkedList<ProviderLoaderFactory>();
|
2015-03-05 07:55:51 +00:00
|
|
|
for (ProviderLoaderFactory f : ServiceLoader.load(ProviderLoaderFactory.class, getClass().getClassLoader())) {
|
2015-01-28 08:10:32 +00:00
|
|
|
factories.add(f);
|
|
|
|
}
|
|
|
|
|
2016-01-19 13:28:03 +00:00
|
|
|
logger.debugv("Provider loaders {0}", factories);
|
2015-01-28 08:10:32 +00:00
|
|
|
|
2018-02-06 05:49:10 +00:00
|
|
|
loaders.add(new DefaultProviderLoader(info, baseClassLoader));
|
2015-01-28 08:10:32 +00:00
|
|
|
|
|
|
|
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)) {
|
2018-02-06 05:49:10 +00:00
|
|
|
KeycloakDeploymentInfo resourceInfo = KeycloakDeploymentInfo.create().services();
|
|
|
|
loaders.add(f.create(resourceInfo, baseClassLoader, resource));
|
2015-01-28 08:10:32 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
throw new RuntimeException("Provider loader for " + r + " not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 12:24:05 +00:00
|
|
|
public synchronized List<Spi> loadSpis() {
|
|
|
|
// Use a map to prevent duplicates, since the loaders may have overlapping classpaths.
|
|
|
|
Map<String, Spi> spiMap = new HashMap<>();
|
|
|
|
for (ProviderLoader loader : loaders) {
|
|
|
|
List<Spi> spis = loader.loadSpis();
|
|
|
|
if (spis != null) {
|
|
|
|
for (Spi spi : spis) {
|
|
|
|
spiMap.put(spi.getName(), spi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new LinkedList<>(spiMap.values());
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:10:32 +00:00
|
|
|
public synchronized List<ProviderFactory> load(Spi spi) {
|
2016-08-07 15:41:52 +00:00
|
|
|
if (!cache.containsKey(spi.getProviderClass())) {
|
2018-02-06 05:49:10 +00:00
|
|
|
|
|
|
|
Set<String> loaded = new HashSet<>();
|
2015-01-28 08:10:32 +00:00
|
|
|
for (ProviderLoader loader : loaders) {
|
|
|
|
List<ProviderFactory> f = loader.load(spi);
|
|
|
|
if (f != null) {
|
2016-01-05 22:06:26 +00:00
|
|
|
for (ProviderFactory pf: f) {
|
2018-02-06 05:49:10 +00:00
|
|
|
String uniqueId = spi.getName() + "-" + pf.getId();
|
|
|
|
if (!loaded.contains(uniqueId)) {
|
2016-08-07 15:41:52 +00:00
|
|
|
cache.add(spi.getProviderClass(), pf);
|
2018-02-06 05:49:10 +00:00
|
|
|
loaded.add(uniqueId);
|
2016-01-05 22:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-28 08:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 15:41:52 +00:00
|
|
|
List<ProviderFactory> rtn = cache.get(spi.getProviderClass());
|
|
|
|
return rtn == null ? Collections.EMPTY_LIST : rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a copy of internal factories.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public synchronized MultivaluedHashMap<Class<? extends Provider>, ProviderFactory> getLoadedFactories() {
|
|
|
|
MultivaluedHashMap<Class<? extends Provider>, ProviderFactory> copy = new MultivaluedHashMap<>();
|
|
|
|
copy.addAll(cache);
|
|
|
|
return copy;
|
2015-01-28 08:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized ProviderFactory load(Spi spi, String providerId) {
|
|
|
|
for (ProviderFactory f : load(spi)) {
|
|
|
|
if (f.getId().equals(providerId)) {
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|