Merge pull request #797 from mposolda/dynamic-adapter-registration

KEYCLOAK-778 InfinispanUserCacheProvider not working on EAP6 / AS7
This commit is contained in:
Marek Posolda 2014-10-23 11:38:43 +02:00
commit 1e68f63715

View file

@ -27,6 +27,9 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact
protected final RealmLookup emailLookup = new RealmLookup(); 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 @Override
public CacheUserProvider create(KeycloakSession session) { public CacheUserProvider create(KeycloakSession session) {
lazyInit(session); lazyInit(session);
@ -37,6 +40,7 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact
if (userCache == null) { if (userCache == null) {
synchronized (this) { synchronized (this) {
if (userCache == null) { if (userCache == null) {
checkIspnVersion();
Cache<String, CachedUser> cache = session.getProvider(InfinispanConnectionProvider.class).getCache("users"); Cache<String, CachedUser> cache = session.getProvider(InfinispanConnectionProvider.class).getCache("users");
cache.addListener(new CacheListener()); cache.addListener(new CacheListener());
userCache = new InfinispanUserCache(cache, usernameLookup, emailLookup); 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 @Override
public void init(Config.Scope config) { public void init(Config.Scope config) {
} }
@ -63,8 +76,19 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact
@CacheEntryCreated @CacheEntryCreated
public void userCreated(CacheEntryCreatedEvent<String, CachedUser> event) { public void userCreated(CacheEntryCreatedEvent<String, CachedUser> event) {
if (!event.isPre() && event.getValue() != null) { if (!event.isPre()) {
CachedUser cachedUser = event.getValue();
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(); String realm = cachedUser.getRealm();
usernameLookup.put(realm, cachedUser.getUsername(), cachedUser.getId()); usernameLookup.put(realm, cachedUser.getUsername(), cachedUser.getId());
if (cachedUser.getEmail() != null) { if (cachedUser.getEmail() != null) {
@ -72,6 +96,7 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact
} }
} }
} }
}
@CacheEntryRemoved @CacheEntryRemoved
public void userRemoved(CacheEntryRemovedEvent<String, CachedUser> event) { public void userRemoved(CacheEntryRemovedEvent<String, CachedUser> event) {