diff --git a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheUserProviderFactory.java b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheUserProviderFactory.java index 8fed69e6ee..c0ce985856 100755 --- a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheUserProviderFactory.java +++ b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheUserProviderFactory.java @@ -27,6 +27,9 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact protected final RealmLookup emailLookup = new RealmLookup(); + // Method CacheEntryCreatedEvent.getValue is available from ispn 6 (EAP6 and AS7 are on ispn 5) + private boolean isNewInfinispan; + @Override public CacheUserProvider create(KeycloakSession session) { lazyInit(session); @@ -37,6 +40,7 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact if (userCache == null) { synchronized (this) { if (userCache == null) { + checkIspnVersion(); Cache cache = session.getProvider(InfinispanConnectionProvider.class).getCache("users"); cache.addListener(new CacheListener()); userCache = new InfinispanUserCache(cache, usernameLookup, emailLookup); @@ -45,6 +49,15 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact } } + protected void checkIspnVersion() { + try { + CacheEntryCreatedEvent.class.getMethod("getValue"); + isNewInfinispan = true; + } catch (NoSuchMethodException nsme) { + isNewInfinispan = false; + } + } + @Override public void init(Config.Scope config) { } @@ -63,12 +76,24 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact @CacheEntryCreated public void userCreated(CacheEntryCreatedEvent event) { - if (!event.isPre() && event.getValue() != null) { - CachedUser cachedUser = event.getValue(); - String realm = cachedUser.getRealm(); - usernameLookup.put(realm, cachedUser.getUsername(), cachedUser.getId()); - if (cachedUser.getEmail() != null) { - emailLookup.put(realm, cachedUser.getEmail(), cachedUser.getId()); + if (!event.isPre()) { + + CachedUser cachedUser; + + // Try optimized version if available + if (isNewInfinispan) { + cachedUser = event.getValue(); + } else { + String userId = event.getKey(); + cachedUser = event.getCache().get(userId); + } + + if (cachedUser != null) { + String realm = cachedUser.getRealm(); + usernameLookup.put(realm, cachedUser.getUsername(), cachedUser.getId()); + if (cachedUser.getEmail() != null) { + emailLookup.put(realm, cachedUser.getEmail(), cachedUser.getId()); + } } } }