Search in sources :

Example 16 with ProviderFactory

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;
}
Also used : Scope(org.keycloak.Config.Scope) InvalidableObjectType(org.keycloak.provider.InvalidationHandler.InvalidableObjectType) Arrays(java.util.Arrays) ComponentModelScope(org.keycloak.component.ComponentModelScope) KeycloakModelUtils(org.keycloak.models.utils.KeycloakModelUtils) Collection(java.util.Collection) Logger(org.jboss.logging.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Config(org.keycloak.Config) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ConcurrentMap(java.util.concurrent.ConcurrentMap) Objects(java.util.Objects) ClusterProvider(org.keycloak.cluster.ClusterProvider) InvalidationHandler(org.keycloak.provider.InvalidationHandler) Provider(org.keycloak.provider.Provider) Stream(java.util.stream.Stream) StackUtil(org.keycloak.common.util.StackUtil) ComponentFactoryProviderFactory(org.keycloak.component.ComponentFactoryProviderFactory) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) ObjectType(org.keycloak.provider.InvalidationHandler.ObjectType) ComponentModel(org.keycloak.component.ComponentModel) ProviderFactory(org.keycloak.provider.ProviderFactory) Scope(org.keycloak.Config.Scope) ComponentModelScope(org.keycloak.component.ComponentModelScope) ComponentModelScope(org.keycloak.component.ComponentModelScope) ComponentFactoryProviderFactory(org.keycloak.component.ComponentFactoryProviderFactory) ProviderFactory(org.keycloak.provider.ProviderFactory) ComponentModel(org.keycloak.component.ComponentModel)

Example 17 with 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();
    }
}
Also used : EnvironmentDependentProviderFactory(org.keycloak.provider.EnvironmentDependentProviderFactory) ComponentFactoryProviderFactory(org.keycloak.component.ComponentFactoryProviderFactory) ProviderFactory(org.keycloak.provider.ProviderFactory) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) LinkedList(java.util.LinkedList) ComponentFactoryProvider(org.keycloak.component.ComponentFactoryProvider) Provider(org.keycloak.provider.Provider)

Example 18 with ProviderFactory

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;
}
Also used : HashMap(java.util.HashMap) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) Config(org.keycloak.Config) EnvironmentDependentProviderFactory(org.keycloak.provider.EnvironmentDependentProviderFactory) ComponentFactoryProviderFactory(org.keycloak.component.ComponentFactoryProviderFactory) ProviderFactory(org.keycloak.provider.ProviderFactory) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) Spi(org.keycloak.provider.Spi) ComponentFactoryProvider(org.keycloak.component.ComponentFactoryProvider) Provider(org.keycloak.provider.Provider)

Example 19 with ProviderFactory

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();
    }
}
Also used : EnvironmentDependentProviderFactory(org.keycloak.provider.EnvironmentDependentProviderFactory) ComponentFactoryProviderFactory(org.keycloak.component.ComponentFactoryProviderFactory) ProviderFactory(org.keycloak.provider.ProviderFactory) LinkedList(java.util.LinkedList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) LinkedList(java.util.LinkedList) ComponentFactoryProvider(org.keycloak.component.ComponentFactoryProvider) Provider(org.keycloak.provider.Provider)

Example 20 with ProviderFactory

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);
}
Also used : ProviderManager(org.keycloak.provider.ProviderManager) EnvironmentDependentProviderFactory(org.keycloak.provider.EnvironmentDependentProviderFactory) ComponentFactoryProviderFactory(org.keycloak.component.ComponentFactoryProviderFactory) ProviderFactory(org.keycloak.provider.ProviderFactory) Spi(org.keycloak.provider.Spi) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) ComponentFactoryProvider(org.keycloak.component.ComponentFactoryProvider) Provider(org.keycloak.provider.Provider)

Aggregations

ProviderFactory (org.keycloak.provider.ProviderFactory)21 EnvironmentDependentProviderFactory (org.keycloak.provider.EnvironmentDependentProviderFactory)13 Spi (org.keycloak.provider.Spi)10 HashMap (java.util.HashMap)9 Map (java.util.Map)8 ComponentFactoryProviderFactory (org.keycloak.component.ComponentFactoryProviderFactory)8 Provider (org.keycloak.provider.Provider)7 KeycloakDeploymentInfo (org.keycloak.provider.KeycloakDeploymentInfo)6 ProviderManager (org.keycloak.provider.ProviderManager)6 List (java.util.List)4 Logger (org.jboss.logging.Logger)4 Config (org.keycloak.Config)4 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)4 ComponentFactoryProvider (org.keycloak.component.ComponentFactoryProvider)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3