From 2e9a474eac49fa29cdf94d14dbe78ab39f2836d9 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Tue, 12 Nov 2013 11:49:03 +0000 Subject: [PATCH] Use service loader to load model provider --- .../org/keycloak/models/ModelProvider.java | 2 + .../keycloak/models/jpa/JpaModelProvider.java | 6 ++ .../org.keycloak.models.ModelProvider | 1 + .../models/mongo/PropertiesManager.java | 58 +++++++++++++ .../mongo/keycloak/MongoModelProvider.java | 20 ++++- .../org.keycloak.models.ModelProvider | 1 + .../picketlink/PicketlinkModelProvider.java | 5 ++ .../org.keycloak.models.ModelProvider | 1 + services/pom.xml | 7 +- .../resources/KeycloakApplication.java | 86 ++++++++----------- .../keycloak/test/ApplicationModelTest.java | 2 +- .../java/org/keycloak/test/ModelTest.java | 2 +- .../java/org/keycloak/test/UserModelTest.java | 2 +- .../test/common/AbstractKeycloakTest.java | 2 +- testsuite/integration/pom.xml | 5 ++ .../keycloak/testutils/KeycloakServer.java | 2 +- .../BaseJMeterPerformanceTest.java | 2 +- 17 files changed, 142 insertions(+), 62 deletions(-) create mode 100644 model/jpa/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider create mode 100755 model/mongo/src/main/java/org/keycloak/models/mongo/PropertiesManager.java create mode 100644 model/mongo/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider create mode 100644 model/picketlink/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider diff --git a/model/api/src/main/java/org/keycloak/models/ModelProvider.java b/model/api/src/main/java/org/keycloak/models/ModelProvider.java index 6e8e31e937..1e166acbd1 100755 --- a/model/api/src/main/java/org/keycloak/models/ModelProvider.java +++ b/model/api/src/main/java/org/keycloak/models/ModelProvider.java @@ -5,5 +5,7 @@ package org.keycloak.models; * @version $Revision: 1 $ */ public interface ModelProvider { + String getId(); + KeycloakSessionFactory createFactory(); } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaModelProvider.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaModelProvider.java index 1c5f743f56..629f7959c9 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaModelProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaModelProvider.java @@ -11,6 +11,12 @@ import javax.persistence.Persistence; * @version $Revision: 1 $ */ public class JpaModelProvider implements ModelProvider { + + @Override + public String getId() { + return "jpa"; + } + @Override public KeycloakSessionFactory createFactory() { EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa-keycloak-identity-store"); diff --git a/model/jpa/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider b/model/jpa/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider new file mode 100644 index 0000000000..199922e3f1 --- /dev/null +++ b/model/jpa/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider @@ -0,0 +1 @@ +org.keycloak.models.jpa.JpaModelProvider \ No newline at end of file diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/PropertiesManager.java b/model/mongo/src/main/java/org/keycloak/models/mongo/PropertiesManager.java new file mode 100755 index 0000000000..9e897ae238 --- /dev/null +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/PropertiesManager.java @@ -0,0 +1,58 @@ +package org.keycloak.models.mongo; + +/** + * @author Marek Posolda + */ +public class PropertiesManager { + + private static final String MONGO_HOST = "keycloak.mongodb.host"; + private static final String MONGO_PORT = "keycloak.mongodb.port"; + private static final String MONGO_DB_NAME = "keycloak.mongodb.databaseName"; + private static final String MONGO_DROP_DB_ON_STARTUP = "keycloak.mongodb.dropDatabaseOnStartup"; + private static final String BOOTSTRAP_EMBEDDED_MONGO_AT_CONTEXT_INIT = "keycloak.mongodb.bootstrapEmbeddedMongoAtContextInit"; + + // Port where embedded MongoDB will be started during keycloak bootstrap. Same port will be used by KeycloakApplication then + private static final int MONGO_DEFAULT_PORT_KEYCLOAK_WAR_EMBEDDED = 37017; + + // Port where MongoDB instance is normally started on linux. This port should be used if we're not starting embedded instance (keycloak.mongodb.bootstrapEmbeddedMongoAtContextInit is false) + private static final int MONGO_DEFAULT_PORT_KEYCLOAK_WAR = 27017; + + // Port where unit tests will start embedded MongoDB instance + public static final int MONGO_DEFAULT_PORT_UNIT_TESTS = 27777; + + public static String getMongoHost() { + return System.getProperty(MONGO_HOST, "localhost"); + } + + public static void setMongoHost(String mongoHost) { + System.setProperty(MONGO_HOST, mongoHost); + } + + public static int getMongoPort() { + return Integer.parseInt(System.getProperty(MONGO_PORT, String.valueOf(MONGO_DEFAULT_PORT_KEYCLOAK_WAR_EMBEDDED))); + } + + public static void setMongoPort(int mongoPort) { + System.setProperty(MONGO_PORT, String.valueOf(mongoPort)); + } + + public static String getMongoDbName() { + return System.getProperty(MONGO_DB_NAME, "keycloak"); + } + + public static void setMongoDbName(String mongoMongoDbName) { + System.setProperty(MONGO_DB_NAME, mongoMongoDbName); + } + + public static boolean dropDatabaseOnStartup() { + return Boolean.parseBoolean(System.getProperty(MONGO_DROP_DB_ON_STARTUP, "true")); + } + + public static void setDropDatabaseOnStartup(boolean dropDatabaseOnStartup) { + System.setProperty(MONGO_DROP_DB_ON_STARTUP, String.valueOf(dropDatabaseOnStartup)); + } + + public static boolean bootstrapEmbeddedMongoAtContextInit() { + return isMongoSessionFactory() && Boolean.parseBoolean(System.getProperty(BOOTSTRAP_EMBEDDED_MONGO_AT_CONTEXT_INIT, "true")); + } +} diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/MongoModelProvider.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/MongoModelProvider.java index 87c62af751..c035b5655d 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/MongoModelProvider.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/MongoModelProvider.java @@ -3,13 +3,31 @@ package org.keycloak.models.mongo.keycloak; import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.ModelProvider; +import java.lang.Override; + /** * @author Bill Burke * @version $Revision: 1 $ */ public class MongoModelProvider implements ModelProvider { + + @Override + public String getId() { + return "mongo"; + } + @Override public KeycloakSessionFactory createFactory() { - return null; //To change body of implemented methods use File | Settings | File Templates. + String host = PropertiesManager.getMongoHost(); + int port = PropertiesManager.getMongoPort(); + String dbName = PropertiesManager.getMongoDbName(); + boolean dropDatabaseOnStartup = PropertiesManager.dropDatabaseOnStartup(); + + // Create MongoDBSessionFactory via reflection now + try { + return new MongoDBSessionFactory(host, port, dbName, dropDatabaseOnStartup); + } catch (Exception e) { + throw new RuntimeException(e); + } } } diff --git a/model/mongo/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider b/model/mongo/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider new file mode 100644 index 0000000000..c9e753986d --- /dev/null +++ b/model/mongo/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider @@ -0,0 +1 @@ +org.keycloak.models.mongo.keycloak.MongoModelProvider \ No newline at end of file diff --git a/model/picketlink/src/main/java/org/keycloak/models/picketlink/PicketlinkModelProvider.java b/model/picketlink/src/main/java/org/keycloak/models/picketlink/PicketlinkModelProvider.java index 67f4d7a15f..3a87fa3111 100755 --- a/model/picketlink/src/main/java/org/keycloak/models/picketlink/PicketlinkModelProvider.java +++ b/model/picketlink/src/main/java/org/keycloak/models/picketlink/PicketlinkModelProvider.java @@ -37,6 +37,11 @@ public class PicketlinkModelProvider implements ModelProvider { return new PicketlinkKeycloakSessionFactory(emf, buildPartitionManager()); } + @Override + public String getId() { + return "picketlink"; + } + public static PartitionManager buildPartitionManager() { IdentityConfigurationBuilder builder = new IdentityConfigurationBuilder(); diff --git a/model/picketlink/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider b/model/picketlink/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider new file mode 100644 index 0000000000..225bcaa8fb --- /dev/null +++ b/model/picketlink/src/main/resources/META-INF/services/org.keycloak.models.ModelProvider @@ -0,0 +1 @@ +org.keycloak.models.picketlink.PicketlinkModelProvider \ No newline at end of file diff --git a/services/pom.xml b/services/pom.xml index 1df957c261..56b23b12fe 100755 --- a/services/pom.xml +++ b/services/pom.xml @@ -31,14 +31,15 @@ org.keycloak - keycloak-model-picketlink + keycloak-model-jpa ${project.version} - provided + test org.keycloak - keycloak-model-jpa + keycloak-model-picketlink ${project.version} + test