From 55730a8021eb865f3644c3867a9d648d3a3306e3 Mon Sep 17 00:00:00 2001 From: Alex Morel Date: Fri, 21 Jun 2024 11:58:56 +0200 Subject: [PATCH] Remove static ScimDispatcher map --- .../java/sh/libre/scim/core/ScimDispatcher.java | 13 +------------ .../libre/scim/event/ScimEventListenerProvider.java | 2 +- ...EndpointConfigurationStorageProviderFactory.java | 6 ++++-- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/main/java/sh/libre/scim/core/ScimDispatcher.java b/src/main/java/sh/libre/scim/core/ScimDispatcher.java index c46740df9c..bf06dbcf30 100644 --- a/src/main/java/sh/libre/scim/core/ScimDispatcher.java +++ b/src/main/java/sh/libre/scim/core/ScimDispatcher.java @@ -6,11 +6,8 @@ import org.keycloak.models.KeycloakSession; import sh.libre.scim.storage.ScimEndpointConfigurationStorageProviderFactory; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; /** @@ -20,20 +17,13 @@ public class ScimDispatcher { private static final Logger logger = Logger.getLogger(ScimDispatcher.class); - private static final Map sessionToScimDispatcher = new ConcurrentHashMap<>(); private final KeycloakSession session; private boolean clientsInitialized = false; private final List userScimServices = new ArrayList<>(); private final List groupScimServices = new ArrayList<>(); - public static ScimDispatcher createForSession(KeycloakSession session) { - // Only create a scim dispatcher if there is none already created for session - sessionToScimDispatcher.computeIfAbsent(session, ScimDispatcher::new); - return sessionToScimDispatcher.get(session); - } - - private ScimDispatcher(KeycloakSession session) { + public ScimDispatcher(KeycloakSession session) { this.session = session; } @@ -117,7 +107,6 @@ public class ScimDispatcher { } public void close() { - sessionToScimDispatcher.remove(session); for (GroupScimService c : groupScimServices) { c.close(); } diff --git a/src/main/java/sh/libre/scim/event/ScimEventListenerProvider.java b/src/main/java/sh/libre/scim/event/ScimEventListenerProvider.java index 1fe84ad2d7..e9635d455a 100644 --- a/src/main/java/sh/libre/scim/event/ScimEventListenerProvider.java +++ b/src/main/java/sh/libre/scim/event/ScimEventListenerProvider.java @@ -46,7 +46,7 @@ public class ScimEventListenerProvider implements EventListenerProvider { public ScimEventListenerProvider(KeycloakSession session) { this.session = session; this.keycloackDao = new KeycloakDao(session); - this.dispatcher = ScimDispatcher.createForSession(session); + this.dispatcher = new ScimDispatcher(session); } @Override diff --git a/src/main/java/sh/libre/scim/storage/ScimEndpointConfigurationStorageProviderFactory.java b/src/main/java/sh/libre/scim/storage/ScimEndpointConfigurationStorageProviderFactory.java index bac304e3ae..748e18b5ca 100644 --- a/src/main/java/sh/libre/scim/storage/ScimEndpointConfigurationStorageProviderFactory.java +++ b/src/main/java/sh/libre/scim/storage/ScimEndpointConfigurationStorageProviderFactory.java @@ -48,13 +48,14 @@ public class ScimEndpointConfigurationStorageProviderFactory KeycloakModelUtils.runJobInTransaction(sessionFactory, session -> { RealmModel realm = session.realms().getRealm(realmId); session.getContext().setRealm(realm); - ScimDispatcher dispatcher = ScimDispatcher.createForSession(session); + ScimDispatcher dispatcher = new ScimDispatcher(session); if (BooleanUtils.TRUE.equals(model.get("propagation-user"))) { dispatcher.dispatchUserModificationToOne(model, client -> client.sync(result)); } if (BooleanUtils.TRUE.equals(model.get("propagation-group"))) { dispatcher.dispatchGroupModificationToOne(model, client -> client.sync(result)); } + dispatcher.close(); }); return result; } @@ -74,13 +75,14 @@ public class ScimEndpointConfigurationStorageProviderFactory for (RealmModel realm : taskSession.realms().getRealmsStream().toList()) { KeycloakModelUtils.runJobInTransaction(factory, session -> { session.getContext().setRealm(realm); - ScimDispatcher dispatcher = ScimDispatcher.createForSession(session); + ScimDispatcher dispatcher = new ScimDispatcher(session); for (GroupModel group : session.groups().getGroupsStream(realm) .filter(x -> BooleanUtils.TRUE.equals(x.getFirstAttribute("scim-dirty"))).toList()) { logger.infof("[SCIM] Dirty group : %s", group.getName()); dispatcher.dispatchGroupModificationToAll(client -> client.replace(group)); group.removeAttribute("scim-dirty"); } + dispatcher.close(); }); } }, Duration.ofSeconds(30).toMillis(), "scim-background");