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());
}
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());
}
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;
}
Aggregations