Search in sources :

Example 6 with KeycloakSessionTask

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

the class MultipleStepsExportProvider method exportModel.

@Override
public void exportModel(KeycloakSessionFactory factory) throws IOException {
    final RealmsHolder holder = new RealmsHolder();
    KeycloakModelUtils.runJobInTransaction(factory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            List<RealmModel> realms = session.realms().getRealmsStream().collect(Collectors.toList());
            holder.realms = realms;
        }
    });
    for (RealmModel realm : holder.realms) {
        exportRealmImpl(factory, realm.getName());
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) List(java.util.List)

Example 7 with KeycloakSessionTask

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

the class InfinispanUserLoginFailureProviderFactory method loadLoginFailuresFromRemoteCaches.

private void loadLoginFailuresFromRemoteCaches(final KeycloakSessionFactory sessionFactory, String cacheName, final int sessionsPerSegment, final int maxErrors) {
    log.debugf("Check pre-loading sessions from remote cache '%s'", cacheName);
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            InfinispanConnectionProvider connections = session.getProvider(InfinispanConnectionProvider.class);
            Cache<String, Serializable> workCache = connections.getCache(InfinispanConnectionProvider.WORK_CACHE_NAME);
            InfinispanCacheInitializer initializer = new InfinispanCacheInitializer(sessionFactory, workCache, new RemoteCacheSessionsLoader(cacheName, sessionsPerSegment), "remoteCacheLoad::" + cacheName, sessionsPerSegment, maxErrors);
            initializer.initCache();
            initializer.loadSessions();
        }
    });
    log.debugf("Pre-loading login failures from remote cache '%s' finished", cacheName);
}
Also used : KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) InfinispanCacheInitializer(org.keycloak.models.sessions.infinispan.initializer.InfinispanCacheInitializer) RemoteCacheSessionsLoader(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader) Cache(org.infinispan.Cache) RemoteCache(org.infinispan.client.hotrod.RemoteCache)

Example 8 with KeycloakSessionTask

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

the class InfinispanUserSessionProviderFactory method loadSessionsFromRemoteCache.

private void loadSessionsFromRemoteCache(final KeycloakSessionFactory sessionFactory, String cacheName, final int sessionsPerSegment, final int maxErrors) {
    log.debugf("Check pre-loading sessions from remote cache '%s'", cacheName);
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            InfinispanConnectionProvider connections = session.getProvider(InfinispanConnectionProvider.class);
            Cache<String, Serializable> workCache = connections.getCache(InfinispanConnectionProvider.WORK_CACHE_NAME);
            InfinispanCacheInitializer initializer = new InfinispanCacheInitializer(sessionFactory, workCache, new RemoteCacheSessionsLoader(cacheName, sessionsPerSegment), "remoteCacheLoad::" + cacheName, sessionsPerSegment, maxErrors);
            initializer.initCache();
            initializer.loadSessions();
        }
    });
    log.debugf("Pre-loading sessions from remote cache '%s' finished", cacheName);
}
Also used : KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) InfinispanCacheInitializer(org.keycloak.models.sessions.infinispan.initializer.InfinispanCacheInitializer) RemoteCacheSessionsLoader(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader) Cache(org.infinispan.Cache) RemoteCache(org.infinispan.client.hotrod.RemoteCache)

Example 9 with KeycloakSessionTask

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

the class KeycloakApplication method startup.

protected void startup() {
    KeycloakApplication.sessionFactory = createSessionFactory();
    ExportImportManager[] exportImportManager = new ExportImportManager[1];
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            DBLockManager dbLockManager = new DBLockManager(session);
            dbLockManager.checkForcedUnlock();
            DBLockProvider dbLock = dbLockManager.getDBLock();
            dbLock.waitForLock(DBLockProvider.Namespace.KEYCLOAK_BOOT);
            try {
                exportImportManager[0] = bootstrap();
            } finally {
                dbLock.releaseLock();
            }
        }
    });
    if (exportImportManager[0].isRunExport()) {
        exportImportManager[0].runExport();
    }
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            boolean shouldBootstrapAdmin = new ApplianceBootstrap(session).isNoMasterUser();
            BOOTSTRAP_ADMIN_USER.set(shouldBootstrapAdmin);
        }
    });
    sessionFactory.publish(new PostMigrationEvent());
    setupScheduledTasks(sessionFactory);
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) ExportImportManager(org.keycloak.exportimport.ExportImportManager) DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Example 10 with KeycloakSessionTask

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

the class UserStorageSyncManager method syncChangedUsers.

public SynchronizationResult syncChangedUsers(final KeycloakSessionFactory sessionFactory, final String realmId, final UserStorageProviderModel provider) {
    UserStorageProviderFactory factory = (UserStorageProviderFactory) sessionFactory.getProviderFactory(UserStorageProvider.class, provider.getProviderId());
    if (!(factory instanceof ImportSynchronization) || !provider.isImportEnabled() || !provider.isEnabled()) {
        return SynchronizationResult.ignored();
    }
    final Holder holder = new Holder();
    // Ensure not executed concurrently on this or any other cluster node
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            ClusterProvider clusterProvider = session.getProvider(ClusterProvider.class);
            // shared key for "full" and "changed" . Improve if needed
            String taskKey = provider.getId() + "::sync";
            // 30 seconds minimal timeout for now
            int timeout = Math.max(30, provider.getChangedSyncPeriod());
            holder.result = clusterProvider.executeIfNotExecuted(taskKey, timeout, new Callable<SynchronizationResult>() {

                @Override
                public SynchronizationResult call() throws Exception {
                    // See when we did last sync.
                    int oldLastSync = provider.getLastSync();
                    updateLastSyncInterval(sessionFactory, provider, realmId);
                    return ((ImportSynchronization) factory).syncSince(Time.toDate(oldLastSync), sessionFactory, realmId, provider);
                }
            });
        }
    });
    if (holder.result == null || !holder.result.isExecuted()) {
        logger.debugf("syncChangedUsers for federation provider %s was ignored as it's already in progress", provider.getName());
        return SynchronizationResult.ignored();
    } else {
        return holder.result.getResult();
    }
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) ClusterProvider(org.keycloak.cluster.ClusterProvider) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) ImportSynchronization(org.keycloak.storage.user.ImportSynchronization) KeycloakSession(org.keycloak.models.KeycloakSession) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult)

Aggregations

KeycloakSession (org.keycloak.models.KeycloakSession)15 KeycloakSessionTask (org.keycloak.models.KeycloakSessionTask)15 RealmModel (org.keycloak.models.RealmModel)5 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)4 UserModel (org.keycloak.models.UserModel)3 UserStorageProvider (org.keycloak.storage.UserStorageProvider)3 UserStorageProviderFactory (org.keycloak.storage.UserStorageProviderFactory)3 ImportSynchronization (org.keycloak.storage.user.ImportSynchronization)3 List (java.util.List)2 Cache (org.infinispan.Cache)2 RemoteCache (org.infinispan.client.hotrod.RemoteCache)2 ClusterProvider (org.keycloak.cluster.ClusterProvider)2 InfinispanConnectionProvider (org.keycloak.connections.infinispan.InfinispanConnectionProvider)2 ExportImportManager (org.keycloak.exportimport.ExportImportManager)2 KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)2 UserSessionPersisterProvider (org.keycloak.models.session.UserSessionPersisterProvider)2 InfinispanCacheInitializer (org.keycloak.models.sessions.infinispan.initializer.InfinispanCacheInitializer)2 RemoteCacheSessionsLoader (org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader)2 LDAPQuery (org.keycloak.storage.ldap.idm.query.internal.LDAPQuery)2 Date (java.util.Date)1