Search in sources :

Example 1 with ComponentModelScope

use of org.keycloak.component.ComponentModelScope 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 2 with ComponentModelScope

use of org.keycloak.component.ComponentModelScope in project keycloak by keycloak.

the class ConcurrentHashMapStorageProviderFactory method init.

@Override
public void init(Scope config) {
    if (config instanceof ComponentModelScope) {
        this.suffix = "-" + ((ComponentModelScope) config).getComponentId();
    } else {
        this.suffix = "";
    }
    final String keyType = config.get("keyType", "uuid");
    defaultKeyConvertor = getKeyConvertor(keyType);
    for (String name : getModelNames()) {
        keyConvertors.put(name, getKeyConvertor(config.get("keyType." + name, keyType)));
    }
    final String dir = config.get("dir");
    try {
        if (dir == null || dir.trim().isEmpty()) {
            LOG.warn("No directory set, created objects will not survive server restart");
            this.storageDirectory = null;
        } else {
            File f = new File(dir);
            Files.createDirectories(f.toPath());
            if (f.exists()) {
                this.storageDirectory = f;
            } else {
                LOG.warnf("Directory cannot be used, created objects will not survive server restart: %s", dir);
                this.storageDirectory = null;
            }
        }
    } catch (IOException ex) {
        LOG.warnf("Directory cannot be used, created objects will not survive server restart: %s", dir);
        this.storageDirectory = null;
    }
}
Also used : ComponentModelScope(org.keycloak.component.ComponentModelScope) IOException(java.io.IOException) File(java.io.File)

Aggregations

ComponentModelScope (org.keycloak.component.ComponentModelScope)2 File (java.io.File)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Objects (java.util.Objects)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Function (java.util.function.Function)1 Stream (java.util.stream.Stream)1 Logger (org.jboss.logging.Logger)1 Config (org.keycloak.Config)1 Scope (org.keycloak.Config.Scope)1 ClusterProvider (org.keycloak.cluster.ClusterProvider)1 StackUtil (org.keycloak.common.util.StackUtil)1 ComponentFactoryProviderFactory (org.keycloak.component.ComponentFactoryProviderFactory)1 ComponentModel (org.keycloak.component.ComponentModel)1 KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)1