use of org.keycloak.provider.ProviderFactory in project keycloak by keycloak.
the class DefaultComponentFactoryProviderFactory method getProviderFactory.
@Override
@SuppressWarnings("unchecked")
public <T extends Provider> ProviderFactory<T> getProviderFactory(Class<T> clazz, String realmId, String componentId, Function<KeycloakSessionFactory, ComponentModel> modelGetter) {
ProviderFactory res = componentsMap.get().get(componentId);
if (res != null) {
LOG.tracef("Found cached ProviderFactory for %s in (%s, %s)", clazz, realmId, componentId);
return res;
}
// Apply the expensive operation before putting it into the cache
final ComponentModel cm;
if (modelGetter == null) {
LOG.debugf("Getting component configuration for component (%s, %s) from realm configuration", clazz, realmId, componentId);
cm = KeycloakModelUtils.getComponentModel(factory, realmId, componentId);
} else {
LOG.debugf("Getting component configuration for component (%s, %s) via provided method", realmId, componentId);
cm = modelGetter.apply(factory);
}
if (cm == null) {
return null;
}
final String provider = cm.getProviderId();
ProviderFactory<T> pf = provider == null ? factory.getProviderFactory(clazz) : factory.getProviderFactory(clazz, provider);
if (pf == null) {
// Either not found or not enabled
LOG.debugf("ProviderFactory for %s in (%s, %s) not found", clazz, realmId, componentId);
return null;
}
final ProviderFactory newFactory;
try {
newFactory = pf.getClass().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
LOG.warn("Cannot instantiate factory", ex);
return null;
}
Scope scope = Config.scope(factory.getSpi(clazz).getName(), provider);
ComponentModelScope configScope = new ComponentModelScope(scope, cm);
ProviderFactory<T> providerFactory;
if (this.componentCachingAvailable) {
providerFactory = componentsMap.get().computeIfAbsent(componentId, cId -> initializeFactory(clazz, realmId, componentId, newFactory, configScope));
} else {
providerFactory = initializeFactory(clazz, realmId, componentId, newFactory, configScope);
}
return providerFactory;
}
use of org.keycloak.provider.ProviderFactory in project keycloak by keycloak.
the class DefaultKeycloakSessionFactory method deploy.
@Override
public void deploy(ProviderManager pm) {
Map<Class<? extends Provider>, Map<String, ProviderFactory>> copy = getFactoriesCopy();
Map<Class<? extends Provider>, Map<String, ProviderFactory>> newFactories = loadFactories(pm);
List<ProviderFactory> deployed = new LinkedList<>();
List<ProviderFactory> undeployed = new LinkedList<>();
for (Map.Entry<Class<? extends Provider>, Map<String, ProviderFactory>> entry : newFactories.entrySet()) {
Map<String, ProviderFactory> current = copy.get(entry.getKey());
if (current == null) {
copy.put(entry.getKey(), entry.getValue());
} else {
for (ProviderFactory f : entry.getValue().values()) {
deployed.add(f);
ProviderFactory old = current.remove(f.getId());
if (old != null)
undeployed.add(old);
}
current.putAll(entry.getValue());
}
}
factoriesMap = copy;
// need to update the default provider map
checkProvider();
boolean cfChanged = false;
for (ProviderFactory factory : undeployed) {
invalidate(ObjectType.PROVIDER_FACTORY, factory.getClass());
factory.close();
cfChanged |= (componentFactoryPF == factory);
}
// Component factory must be initialized first, so that postInit in other factories can use component factories
if (cfChanged) {
updateComponentFactoryProviderFactory();
if (componentFactoryPF != null) {
componentFactoryPF.postInit(this);
}
}
for (ProviderFactory factory : deployed) {
if (factory != componentFactoryPF) {
factory.postInit(this);
}
}
if (pm.getInfo().hasThemes() || pm.getInfo().hasThemeResources()) {
themeManagerFactory.clearCache();
}
}
use of org.keycloak.provider.ProviderFactory in project keycloak by keycloak.
the class DefaultKeycloakSessionFactory method loadFactories.
protected Map<Class<? extends Provider>, Map<String, ProviderFactory>> loadFactories(ProviderManager pm) {
Map<Class<? extends Provider>, Map<String, ProviderFactory>> factoryMap = new HashMap<>();
Set<Spi> spiList = spis;
for (Spi spi : spiList) {
Map<String, ProviderFactory> factories = new HashMap<String, ProviderFactory>();
factoryMap.put(spi.getProviderClass(), factories);
String provider = Config.getProvider(spi.getName());
if (provider != null) {
ProviderFactory factory = pm.load(spi, provider);
if (factory == null) {
continue;
}
Config.Scope scope = Config.scope(spi.getName(), provider);
if (isEnabled(factory, scope)) {
factory.init(scope);
if (spi.isInternal() && !isInternal(factory)) {
ServicesLogger.LOGGER.spiMayChange(factory.getId(), factory.getClass().getName(), spi.getName());
}
factories.put(factory.getId(), factory);
logger.debugv("Loaded SPI {0} (provider = {1})", spi.getName(), provider);
}
} else {
for (ProviderFactory factory : pm.load(spi)) {
Config.Scope scope = Config.scope(spi.getName(), factory.getId());
if (isEnabled(factory, scope)) {
factory.init(scope);
if (spi.isInternal() && !isInternal(factory)) {
ServicesLogger.LOGGER.spiMayChange(factory.getId(), factory.getClass().getName(), spi.getName());
}
factories.put(factory.getId(), factory);
} else {
logger.debugv("SPI {0} provider {1} disabled", spi.getName(), factory.getId());
}
}
}
}
return factoryMap;
}
use of org.keycloak.provider.ProviderFactory in project keycloak by keycloak.
the class DefaultKeycloakSessionFactory method undeploy.
@Override
public void undeploy(ProviderManager pm) {
logger.debug("undeploy");
// we make a copy to avoid concurrent access exceptions
Map<Class<? extends Provider>, Map<String, ProviderFactory>> copy = getFactoriesCopy();
MultivaluedHashMap<Class<? extends Provider>, ProviderFactory> factories = pm.getLoadedFactories();
List<ProviderFactory> undeployed = new LinkedList<>();
for (Map.Entry<Class<? extends Provider>, List<ProviderFactory>> entry : factories.entrySet()) {
Map<String, ProviderFactory> registered = copy.get(entry.getKey());
for (ProviderFactory factory : entry.getValue()) {
undeployed.add(factory);
logger.debugv("undeploying {0} of id {1}", factory.getClass().getName(), factory.getId());
if (registered != null) {
registered.remove(factory.getId());
}
}
}
factoriesMap = copy;
for (ProviderFactory factory : undeployed) {
factory.close();
}
}
use of org.keycloak.provider.ProviderFactory in project keycloak by keycloak.
the class DefaultKeycloakSessionFactory method init.
public void init() {
serverStartupTimestamp = System.currentTimeMillis();
ProviderManager pm = new ProviderManager(KeycloakDeploymentInfo.create().services(), getClass().getClassLoader(), Config.scope().getArray("providers"));
for (Spi spi : pm.loadSpis()) {
if (spi.isEnabled()) {
spis.add(spi);
}
}
factoriesMap = loadFactories(pm);
synchronized (ProviderManagerRegistry.SINGLETON) {
for (ProviderManager manager : ProviderManagerRegistry.SINGLETON.getPreBoot()) {
Map<Class<? extends Provider>, Map<String, ProviderFactory>> factoryMap = loadFactories(manager);
for (Map.Entry<Class<? extends Provider>, Map<String, ProviderFactory>> entry : factoryMap.entrySet()) {
Map<String, ProviderFactory> factories = factoriesMap.get(entry.getKey());
if (factories == null) {
factoriesMap.put(entry.getKey(), entry.getValue());
} else {
factories.putAll(entry.getValue());
}
}
}
checkProvider();
// Component factory must be initialized first, so that postInit in other factories can use component factories
updateComponentFactoryProviderFactory();
if (componentFactoryPF != null) {
componentFactoryPF.postInit(this);
}
for (Map<String, ProviderFactory> factories : factoriesMap.values()) {
for (ProviderFactory factory : factories.values()) {
if (factory != componentFactoryPF) {
factory.postInit(this);
}
}
}
// make the session factory ready for hot deployment
ProviderManagerRegistry.SINGLETON.setDeployer(this);
}
AdminPermissions.registerListener(this);
}
Aggregations