From 1dde3e1459f23161f8d21e55fadd485d0b6c0f89 Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Mon, 23 Feb 2015 11:39:24 -0500 Subject: [PATCH] provider events --- .../models/KeycloakSessionFactory.java | 3 +- .../keycloak/models/RealmListenerHelper.java | 31 ------------------- .../org/keycloak/models/RealmProvider.java | 7 ++--- .../org/keycloak/provider/ProviderEvent.java | 8 +++++ .../provider/ProviderEventListener.java | 9 ++++++ .../provider/ProviderEventManager.java | 16 ++++++++++ .../cache/DefaultCacheRealmProvider.java | 11 ------- .../models/cache/NoCacheRealmProvider.java | 10 ------ .../keycloak/models/jpa/JpaRealmProvider.java | 25 +++++---------- .../models/jpa/JpaRealmProviderFactory.java | 5 +-- .../keycloak/adapters/MongoRealmProvider.java | 26 +++++----------- .../adapters/MongoRealmProviderFactory.java | 4 +-- .../DefaultKeycloakSessionFactory.java | 23 ++++++++++++++ 13 files changed, 79 insertions(+), 99 deletions(-) delete mode 100755 model/api/src/main/java/org/keycloak/models/RealmListenerHelper.java create mode 100755 model/api/src/main/java/org/keycloak/provider/ProviderEvent.java create mode 100755 model/api/src/main/java/org/keycloak/provider/ProviderEventListener.java create mode 100755 model/api/src/main/java/org/keycloak/provider/ProviderEventManager.java diff --git a/model/api/src/main/java/org/keycloak/models/KeycloakSessionFactory.java b/model/api/src/main/java/org/keycloak/models/KeycloakSessionFactory.java index 2e94eed624..b17629fa40 100755 --- a/model/api/src/main/java/org/keycloak/models/KeycloakSessionFactory.java +++ b/model/api/src/main/java/org/keycloak/models/KeycloakSessionFactory.java @@ -1,6 +1,7 @@ package org.keycloak.models; import org.keycloak.provider.Provider; +import org.keycloak.provider.ProviderEventManager; import org.keycloak.provider.ProviderFactory; import java.util.List; @@ -9,7 +10,7 @@ import java.util.List; * @author Bill Burke * @version $Revision: 1 $ */ -public interface KeycloakSessionFactory { +public interface KeycloakSessionFactory extends ProviderEventManager { KeycloakSession create(); ProviderFactory getProviderFactory(Class clazz); diff --git a/model/api/src/main/java/org/keycloak/models/RealmListenerHelper.java b/model/api/src/main/java/org/keycloak/models/RealmListenerHelper.java deleted file mode 100755 index b7c074dd92..0000000000 --- a/model/api/src/main/java/org/keycloak/models/RealmListenerHelper.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.keycloak.models; - -import java.util.LinkedList; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class RealmListenerHelper { - protected LinkedList list = new LinkedList(); - - public void registerListener(RealmProvider.RealmCreationListener listener) { - synchronized (list) { - list.add(listener); - } - } - - public void unregisterListener(RealmProvider.RealmCreationListener listener) { - synchronized (list) { - list.remove(listener); - } - } - - public void executeCreationListeners(RealmModel realm) { - synchronized (list) { - for (RealmProvider.RealmCreationListener listener : list) { - listener.created(realm); - } - } - } -} diff --git a/model/api/src/main/java/org/keycloak/models/RealmProvider.java b/model/api/src/main/java/org/keycloak/models/RealmProvider.java index 2c04cc1627..f2454d41bd 100755 --- a/model/api/src/main/java/org/keycloak/models/RealmProvider.java +++ b/model/api/src/main/java/org/keycloak/models/RealmProvider.java @@ -1,6 +1,7 @@ package org.keycloak.models; import org.keycloak.provider.Provider; +import org.keycloak.provider.ProviderEvent; import java.util.List; @@ -9,16 +10,14 @@ import java.util.List; * @version $Revision: 1 $ */ public interface RealmProvider extends Provider { - public interface RealmCreationListener { - void created(RealmModel realm); + public interface RealmCreationEvent extends ProviderEvent { + RealmModel getCreatedRealm(); } // Note: The reason there are so many query methods here is for layering a cache on top of an persistent KeycloakSession RealmModel createRealm(String name); RealmModel createRealm(String id, String name); - void registerListener(RealmCreationListener listener); - void unregisterListener(RealmCreationListener listener); RealmModel getRealm(String id); RealmModel getRealmByName(String name); diff --git a/model/api/src/main/java/org/keycloak/provider/ProviderEvent.java b/model/api/src/main/java/org/keycloak/provider/ProviderEvent.java new file mode 100755 index 0000000000..403e1123c9 --- /dev/null +++ b/model/api/src/main/java/org/keycloak/provider/ProviderEvent.java @@ -0,0 +1,8 @@ +package org.keycloak.provider; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public interface ProviderEvent { +} diff --git a/model/api/src/main/java/org/keycloak/provider/ProviderEventListener.java b/model/api/src/main/java/org/keycloak/provider/ProviderEventListener.java new file mode 100755 index 0000000000..8f994620c3 --- /dev/null +++ b/model/api/src/main/java/org/keycloak/provider/ProviderEventListener.java @@ -0,0 +1,9 @@ +package org.keycloak.provider; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public interface ProviderEventListener { + void onEvent(ProviderEvent event); +} diff --git a/model/api/src/main/java/org/keycloak/provider/ProviderEventManager.java b/model/api/src/main/java/org/keycloak/provider/ProviderEventManager.java new file mode 100755 index 0000000000..efdfa826b5 --- /dev/null +++ b/model/api/src/main/java/org/keycloak/provider/ProviderEventManager.java @@ -0,0 +1,16 @@ +package org.keycloak.provider; + +import org.keycloak.provider.ProviderEvent; +import org.keycloak.provider.ProviderEventListener; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public interface ProviderEventManager { + void register(ProviderEventListener listener); + + void unregister(ProviderEventListener listener); + + void publish(ProviderEvent event); +} diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheRealmProvider.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheRealmProvider.java index 9419c91ee2..a1d232ecb3 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheRealmProvider.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheRealmProvider.java @@ -50,17 +50,6 @@ public class DefaultCacheRealmProvider implements CacheRealmProvider { session.getTransaction().enlistAfterCompletion(getTransaction()); } - @Override - public void registerListener(RealmCreationListener listener) { - getDelegate().registerListener(listener); - } - - @Override - public void unregisterListener(RealmCreationListener listener) { - getDelegate().unregisterListener(listener); - - } - @Override public boolean isEnabled() { return cache.isEnabled(); diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheRealmProvider.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheRealmProvider.java index 9896e51281..e7b1551c2d 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheRealmProvider.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheRealmProvider.java @@ -38,16 +38,6 @@ public class NoCacheRealmProvider implements CacheRealmProvider { return delegate; } - @Override - public void registerListener(RealmCreationListener listener) { - getDelegate().registerListener(listener); - } - - @Override - public void unregisterListener(RealmCreationListener listener) { - getDelegate().unregisterListener(listener); - - } @Override public void registerRealmInvalidation(String id) { } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProvider.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProvider.java index 5529b83749..371d0488b4 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProvider.java @@ -3,7 +3,6 @@ package org.keycloak.models.jpa; import org.keycloak.models.ApplicationModel; import org.keycloak.models.KeycloakSession; import org.keycloak.models.OAuthClientModel; -import org.keycloak.models.RealmListenerHelper; import org.keycloak.models.RealmModel; import org.keycloak.models.RealmProvider; import org.keycloak.models.RoleModel; @@ -26,24 +25,11 @@ import java.util.List; public class JpaRealmProvider implements RealmProvider { private final KeycloakSession session; protected EntityManager em; - protected RealmListenerHelper listeners; - public JpaRealmProvider(KeycloakSession session, EntityManager em, RealmListenerHelper listeners) { + public JpaRealmProvider(KeycloakSession session, EntityManager em) { this.session = session; this.em = em; - this.listeners = listeners; - } - - @Override - public void registerListener(RealmCreationListener listener) { - listeners.registerListener(listener); - } - - @Override - public void unregisterListener(RealmCreationListener listener) { - listeners.unregisterListener(listener); - } @Override @@ -58,8 +44,13 @@ public class JpaRealmProvider implements RealmProvider { realm.setId(id); em.persist(realm); em.flush(); - RealmModel model = new RealmAdapter(session, em, realm); - listeners.executeCreationListeners(model); + final RealmModel model = new RealmAdapter(session, em, realm); + session.getKeycloakSessionFactory().publish(new RealmCreationEvent() { + @Override + public RealmModel getCreatedRealm() { + return model; + } + }); return model; } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProviderFactory.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProviderFactory.java index 4869ca745c..7e090c2142 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProviderFactory.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProviderFactory.java @@ -3,7 +3,6 @@ package org.keycloak.models.jpa; import org.keycloak.Config; import org.keycloak.connections.jpa.JpaConnectionProvider; import org.keycloak.models.KeycloakSession; -import org.keycloak.models.RealmListenerHelper; import org.keycloak.models.RealmProvider; import org.keycloak.models.RealmProviderFactory; @@ -15,8 +14,6 @@ import javax.persistence.EntityManager; */ public class JpaRealmProviderFactory implements RealmProviderFactory { - protected RealmListenerHelper listeners = new RealmListenerHelper(); - @Override public void init(Config.Scope config) { } @@ -29,7 +26,7 @@ public class JpaRealmProviderFactory implements RealmProviderFactory { @Override public RealmProvider create(KeycloakSession session) { EntityManager em = session.getProvider(JpaConnectionProvider.class).getEntityManager(); - return new JpaRealmProvider(session, em, listeners); + return new JpaRealmProvider(session, em); } @Override diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoRealmProvider.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoRealmProvider.java index 3d6b735096..997025e888 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoRealmProvider.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoRealmProvider.java @@ -8,7 +8,6 @@ import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.ApplicationModel; import org.keycloak.models.KeycloakSession; import org.keycloak.models.OAuthClientModel; -import org.keycloak.models.RealmListenerHelper; import org.keycloak.models.RealmModel; import org.keycloak.models.RealmProvider; import org.keycloak.models.RoleModel; @@ -28,12 +27,10 @@ public class MongoRealmProvider implements RealmProvider { private final MongoStoreInvocationContext invocationContext; private final KeycloakSession session; - protected RealmListenerHelper listeners; - public MongoRealmProvider(KeycloakSession session, MongoStoreInvocationContext invocationContext, RealmListenerHelper listeners) { + public MongoRealmProvider(KeycloakSession session, MongoStoreInvocationContext invocationContext) { this.session = session; this.invocationContext = invocationContext; - this.listeners = listeners; } @Override @@ -41,18 +38,6 @@ public class MongoRealmProvider implements RealmProvider { // TODO } - @Override - public void registerListener(RealmCreationListener listener) { - listeners.registerListener(listener); - } - - @Override - public void unregisterListener(RealmCreationListener listener) { - listeners.unregisterListener(listener); - - } - - @Override public RealmModel createRealm(String name) { return createRealm(KeycloakModelUtils.generateId(), name); @@ -66,8 +51,13 @@ public class MongoRealmProvider implements RealmProvider { getMongoStore().insertEntity(newRealm, invocationContext); - RealmModel model = new RealmAdapter(session, newRealm, invocationContext); - listeners.executeCreationListeners(model); + final RealmModel model = new RealmAdapter(session, newRealm, invocationContext); + session.getKeycloakSessionFactory().publish(new RealmCreationEvent() { + @Override + public RealmModel getCreatedRealm() { + return model; + } + }); return model; } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoRealmProviderFactory.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoRealmProviderFactory.java index c8f2f1186a..dd62b788e0 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoRealmProviderFactory.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoRealmProviderFactory.java @@ -4,7 +4,6 @@ import org.jboss.logging.Logger; import org.keycloak.Config; import org.keycloak.connections.mongo.MongoConnectionProvider; import org.keycloak.models.KeycloakSession; -import org.keycloak.models.RealmListenerHelper; import org.keycloak.models.RealmProvider; import org.keycloak.models.RealmProviderFactory; @@ -16,7 +15,6 @@ import org.keycloak.models.RealmProviderFactory; public class MongoRealmProviderFactory implements RealmProviderFactory { protected static final Logger logger = Logger.getLogger(MongoRealmProviderFactory.class); - protected RealmListenerHelper listeners = new RealmListenerHelper(); @Override public String getId() { @@ -30,7 +28,7 @@ public class MongoRealmProviderFactory implements RealmProviderFactory { @Override public RealmProvider create(KeycloakSession session) { MongoConnectionProvider connection = session.getProvider(MongoConnectionProvider.class); - return new MongoRealmProvider(session, connection.getInvocationContext(), listeners); + return new MongoRealmProvider(session, connection.getInvocationContext()); } @Override diff --git a/services/src/main/java/org/keycloak/services/DefaultKeycloakSessionFactory.java b/services/src/main/java/org/keycloak/services/DefaultKeycloakSessionFactory.java index ef3b1766d7..361eb87aec 100755 --- a/services/src/main/java/org/keycloak/services/DefaultKeycloakSessionFactory.java +++ b/services/src/main/java/org/keycloak/services/DefaultKeycloakSessionFactory.java @@ -4,7 +4,11 @@ import org.jboss.logging.Logger; import org.keycloak.Config; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; +import org.keycloak.models.RealmModel; +import org.keycloak.models.RealmProvider; import org.keycloak.provider.Provider; +import org.keycloak.provider.ProviderEvent; +import org.keycloak.provider.ProviderEventListener; import org.keycloak.provider.ProviderFactory; import org.keycloak.provider.ProviderManager; import org.keycloak.provider.Spi; @@ -16,6 +20,7 @@ import java.util.List; import java.util.Map; import java.util.ServiceLoader; import java.util.Set; +import java.util.concurrent.CopyOnWriteArrayList; public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory { @@ -23,6 +28,24 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory { private Map, String> provider = new HashMap, String>(); private Map, Map> factoriesMap = new HashMap, Map>(); + protected CopyOnWriteArrayList listeners = new CopyOnWriteArrayList(); + + @Override + public void register(ProviderEventListener listener) { + listeners.add(listener); + } + + @Override + public void unregister(ProviderEventListener listener) { + listeners.remove(listener); + } + + @Override + public void publish(ProviderEvent event) { + for (ProviderEventListener listener : listeners) { + listener.onEvent(event); + } + } public void init() { ProviderManager pm = new ProviderManager(getClass().getClassLoader(), Config.scope().getArray("providers"));