use of org.keycloak.provider.Provider in project keycloak by keycloak.
the class KeycloakModelTest method createKeycloakSessionFactory.
/**
* Creates a fresh initialized {@link KeycloakSessionFactory}. The returned factory uses configuration
* local to the thread that calls this method, allowing for per-thread customization. This in turn allows
* testing of several parallel session factories which can be used to simulate several servers
* running in parallel.
* @return
*/
public static KeycloakSessionFactory createKeycloakSessionFactory() {
int factoryIndex = FACTORY_COUNT.incrementAndGet();
String threadName = Thread.currentThread().getName();
CONFIG.reset();
CONFIG.spi(ComponentFactorySpi.NAME).provider(DefaultComponentFactoryProviderFactory.PROVIDER_ID).config("cachingForced", "true");
MODEL_PARAMETERS.forEach(m -> m.updateConfig(CONFIG));
LOG.debugf("Creating factory %d in %s using the following configuration:\n %s", factoryIndex, threadName, CONFIG);
DefaultKeycloakSessionFactory res = new DefaultKeycloakSessionFactory() {
@Override
protected boolean isEnabled(ProviderFactory factory, Scope scope) {
return super.isEnabled(factory, scope) && isFactoryAllowed(factory);
}
@Override
protected Map<Class<? extends Provider>, Map<String, ProviderFactory>> loadFactories(ProviderManager pm) {
spis.removeIf(s -> !isSpiAllowed(s));
return super.loadFactories(pm);
}
private boolean isSpiAllowed(Spi s) {
return MODEL_PARAMETERS.stream().anyMatch(p -> p.isSpiAllowed(s));
}
private boolean isFactoryAllowed(ProviderFactory factory) {
return MODEL_PARAMETERS.stream().anyMatch(p -> p.isFactoryAllowed(factory));
}
@Override
public String toString() {
return "KeycloakSessionFactory " + factoryIndex + " (from " + threadName + " thread)";
}
};
res.init();
res.publish(new PostMigrationEvent());
return res;
}
use of org.keycloak.provider.Provider in project keycloak by keycloak.
the class KeycloakProcessor method loadFactories.
private Map<Spi, Map<Class<? extends Provider>, Map<String, ProviderFactory>>> loadFactories(Map<String, ProviderFactory> preConfiguredProviders) {
Config.init(new MicroProfileConfigProvider());
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
KeycloakDeploymentInfo keycloakDeploymentInfo = KeycloakDeploymentInfo.create().name("classpath").services().themeResources();
ProviderManager pm = new ProviderManager(keycloakDeploymentInfo, classLoader);
Map<Spi, Map<Class<? extends Provider>, Map<String, ProviderFactory>>> factories = new HashMap<>();
for (Spi spi : pm.loadSpis()) {
Map<Class<? extends Provider>, Map<String, ProviderFactory>> providers = new HashMap<>();
List<ProviderFactory> loadedFactories = new ArrayList<>(pm.load(spi));
Map<String, ProviderFactory> deployedScriptProviders = loadDeployedScriptProviders(classLoader, spi);
loadedFactories.addAll(deployedScriptProviders.values());
preConfiguredProviders.putAll(deployedScriptProviders);
for (ProviderFactory factory : loadedFactories) {
if (IGNORED_PROVIDER_FACTORY.contains(factory.getClass())) {
continue;
}
Config.Scope scope = Config.scope(spi.getName(), factory.getId());
if (isEnabled(factory, scope)) {
if (spi.isInternal() && !isInternal(factory)) {
ServicesLogger.LOGGER.spiMayChange(factory.getId(), factory.getClass().getName(), spi.getName());
}
providers.computeIfAbsent(spi.getProviderClass(), aClass -> new HashMap<>()).put(factory.getId(), factory);
} else {
logger.debugv("SPI {0} provider {1} disabled", spi.getName(), factory.getId());
}
}
factories.put(spi, providers);
}
return factories;
}
use of org.keycloak.provider.Provider in project keycloak by keycloak.
the class KeycloakProcessor method configureProviders.
/**
* <p>Load the built-in provider factories during build time so we don't spend time looking up them at runtime. By loading
* providers at this stage we are also able to perform a more dynamic configuration based on the default providers.
*
* <p>User-defined providers are going to be loaded at startup</p>
*
* @param recorder
*/
@Consume(RuntimeConfigSetupCompleteBuildItem.class)
@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
KeycloakSessionFactoryPreInitBuildItem configureProviders(KeycloakRecorder recorder) {
Profile.setInstance(new QuarkusProfile());
Map<Spi, Map<Class<? extends Provider>, Map<String, Class<? extends ProviderFactory>>>> factories = new HashMap<>();
Map<Class<? extends Provider>, String> defaultProviders = new HashMap<>();
Map<String, ProviderFactory> preConfiguredProviders = new HashMap<>();
for (Entry<Spi, Map<Class<? extends Provider>, Map<String, ProviderFactory>>> entry : loadFactories(preConfiguredProviders).entrySet()) {
checkProviders(entry.getKey(), entry.getValue(), defaultProviders);
for (Entry<Class<? extends Provider>, Map<String, ProviderFactory>> value : entry.getValue().entrySet()) {
for (ProviderFactory factory : value.getValue().values()) {
factories.computeIfAbsent(entry.getKey(), key -> new HashMap<>()).computeIfAbsent(entry.getKey().getProviderClass(), aClass -> new HashMap<>()).put(factory.getId(), factory.getClass());
}
}
}
recorder.configSessionFactory(factories, defaultProviders, preConfiguredProviders, Environment.isRebuild());
return new KeycloakSessionFactoryPreInitBuildItem();
}
use of org.keycloak.provider.Provider 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.Provider 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();
}
}
Aggregations