Search in sources :

Example 31 with KeycloakSessionFactory

use of org.keycloak.models.KeycloakSessionFactory in project keycloak by keycloak.

the class DBLockTest method testTwoNestedLocksCurrentlyInternal.

private void testTwoNestedLocksCurrentlyInternal(KeycloakSession sessionLC, DBLockProvider.Namespace lockTop, DBLockProvider.Namespace lockInner) {
    final Semaphore semaphore = new Semaphore();
    final KeycloakSessionFactory sessionFactory = sessionLC.getKeycloakSessionFactory();
    List<Thread> threads = new LinkedList<>();
    // launch two threads and expect an error because the locks are different
    for (int i = 0; i < THREADS_COUNT_MEDIUM; i++) {
        final boolean nested = i % 2 == 0;
        Thread thread = new Thread(() -> {
            for (int j = 0; j < ITERATIONS_PER_THREAD_MEDIUM; j++) {
                try {
                    if (nested) {
                        // half the threads run two level lock top-inner
                        KeycloakModelUtils.runJobInTransaction(sessionFactory, session1 -> nestedTwoLevelLock(session1, lockTop, lockInner, semaphore));
                    } else {
                        // the other half only run a lock in the top namespace
                        KeycloakModelUtils.runJobInTransaction(sessionFactory, session1 -> lock(session1, lockTop, semaphore));
                    }
                } catch (RuntimeException e) {
                    semaphore.setException(e);
                }
            }
        });
        threads.add(thread);
    }
    for (Thread thread : threads) {
        thread.start();
    }
    for (Thread thread : threads) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    Assert.assertEquals(THREADS_COUNT_MEDIUM * ITERATIONS_PER_THREAD_MEDIUM, semaphore.getTotal());
    Assert.assertNull(semaphore.getException());
}
Also used : KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) LinkedList(java.util.LinkedList)

Example 32 with KeycloakSessionFactory

use of org.keycloak.models.KeycloakSessionFactory in project keycloak by keycloak.

the class DBLockTest method testTwoLocksCurrentlyInternal.

private void testTwoLocksCurrentlyInternal(KeycloakSession sessionLC, DBLockProvider.Namespace lock1, DBLockProvider.Namespace lock2) {
    final Semaphore semaphore = new Semaphore();
    final KeycloakSessionFactory sessionFactory = sessionLC.getKeycloakSessionFactory();
    List<Thread> threads = new LinkedList<>();
    // launch two threads and expect an error because the locks are different
    for (int i = 0; i < 2; i++) {
        final DBLockProvider.Namespace lock = (i % 2 == 0) ? lock1 : lock2;
        Thread thread = new Thread(() -> {
            for (int j = 0; j < ITERATIONS_PER_THREAD_LONG; j++) {
                try {
                    KeycloakModelUtils.runJobInTransaction(sessionFactory, session1 -> lock(session1, lock, semaphore));
                } catch (RuntimeException e) {
                    semaphore.setException(e);
                }
            }
        });
        threads.add(thread);
    }
    for (Thread thread : threads) {
        thread.start();
    }
    for (Thread thread : threads) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    // interference is needed because different namespaces can interfere
    Assert.assertNotNull(semaphore.getException());
}
Also used : KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) DBLockProvider(org.keycloak.models.dblock.DBLockProvider) LinkedList(java.util.LinkedList)

Example 33 with KeycloakSessionFactory

use of org.keycloak.models.KeycloakSessionFactory 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)

Aggregations

KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)33 KeycloakSession (org.keycloak.models.KeycloakSession)11 RealmModel (org.keycloak.models.RealmModel)11 Test (org.junit.Test)10 UserModel (org.keycloak.models.UserModel)9 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)9 UserStorageSyncManager (org.keycloak.services.managers.UserStorageSyncManager)8 ComponentModel (org.keycloak.component.ComponentModel)7 IdentityProviderMapper (org.keycloak.broker.provider.IdentityProviderMapper)5 ClusterProvider (org.keycloak.cluster.ClusterProvider)5 FederatedIdentityModel (org.keycloak.models.FederatedIdentityModel)5 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)5 HashMap (java.util.HashMap)4 Logger (org.jboss.logging.Logger)4 KeycloakModelUtils (org.keycloak.models.utils.KeycloakModelUtils)4 UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)4 Arrays (java.util.Arrays)3 LinkedList (java.util.LinkedList)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Function (java.util.function.Function)3