diff --git a/pom.xml b/pom.xml
index 99a90cd930..c2a58bcd10 100755
--- a/pom.xml
+++ b/pom.xml
@@ -254,6 +254,11 @@
mongo-java-driver
2.11.2
+
+ de.flapdoodle.embed
+ de.flapdoodle.embed.mongo
+ 1.27
+
diff --git a/services/pom.xml b/services/pom.xml
index 7c38f45478..8696a5d1e0 100755
--- a/services/pom.xml
+++ b/services/pom.xml
@@ -152,6 +152,11 @@
org.mongodb
mongo-java-driver
+
+ de.flapdoodle.embed
+ de.flapdoodle.embed.mongo
+ test
+
junit
junit
diff --git a/services/src/main/java/org/keycloak/services/models/nosql/impl/MongoDBImpl.java b/services/src/main/java/org/keycloak/services/models/nosql/impl/MongoDBImpl.java
index 6862a0c86e..44a340a21a 100644
--- a/services/src/main/java/org/keycloak/services/models/nosql/impl/MongoDBImpl.java
+++ b/services/src/main/java/org/keycloak/services/models/nosql/impl/MongoDBImpl.java
@@ -48,7 +48,7 @@ public class MongoDBImpl implements NoSQL {
new ConcurrentHashMap, ObjectInfo>();
- public MongoDBImpl(DB database, boolean removeAllObjectsAtStartup, Class extends NoSQLObject>[] managedDataTypes) {
+ public MongoDBImpl(DB database, boolean dropDatabaseOnStartup, Class extends NoSQLObject>[] managedDataTypes) {
this.database = database;
typeConverter = new TypeConverter();
@@ -70,20 +70,9 @@ public class MongoDBImpl implements NoSQL {
typeConverter.addDBObjectConverter(new BasicDBObjectConverter(this, typeConverter, type));
}
- if (removeAllObjectsAtStartup) {
- for (Class extends NoSQLObject> type : managedDataTypes) {
- ObjectInfo objectInfo = getObjectInfo(type);
- String collectionName = objectInfo.getDbCollectionName();
- if (collectionName != null) {
- logger.debug("Dropping collection " + collectionName);
-
- DBCollection dbCollection = this.database.getCollection(collectionName);
- dbCollection.drop();
- } else {
- logger.debug("Skip removing objects of type " + type + " as it doesn't have it's own collection");
- }
- }
- logger.info("All objects successfully removed from MongoDB");
+ if (dropDatabaseOnStartup) {
+ this.database.dropDatabase();
+ logger.info("Database " + this.database.getName() + " dropped in MongoDB");
}
}
diff --git a/services/src/main/java/org/keycloak/services/models/nosql/keycloak/adapters/MongoDBSessionFactory.java b/services/src/main/java/org/keycloak/services/models/nosql/keycloak/adapters/MongoDBSessionFactory.java
index 114f7b74db..73ba4615b7 100644
--- a/services/src/main/java/org/keycloak/services/models/nosql/keycloak/adapters/MongoDBSessionFactory.java
+++ b/services/src/main/java/org/keycloak/services/models/nosql/keycloak/adapters/MongoDBSessionFactory.java
@@ -43,14 +43,14 @@ public class MongoDBSessionFactory implements KeycloakSessionFactory {
private final MongoClient mongoClient;
private final NoSQL mongoDB;
- public MongoDBSessionFactory(String host, int port, String dbName, boolean removeAllObjectsAtStartup) {
- logger.info(String.format("Going to use MongoDB database. host: %s, port: %d, databaseName: %s, removeAllObjectsAtStartup: %b", host, port, dbName, removeAllObjectsAtStartup));
+ public MongoDBSessionFactory(String host, int port, String dbName, boolean dropDatabaseOnStartup) {
+ logger.info(String.format("Going to use MongoDB database. host: %s, port: %d, databaseName: %s, removeAllObjectsAtStartup: %b", host, port, dbName, dropDatabaseOnStartup));
try {
// TODO: authentication support
mongoClient = new MongoClient(host, port);
DB db = mongoClient.getDB(dbName);
- mongoDB = new MongoDBImpl(db, removeAllObjectsAtStartup, MANAGED_DATA_TYPES);
+ mongoDB = new MongoDBImpl(db, dropDatabaseOnStartup, MANAGED_DATA_TYPES);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
diff --git a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java
index dabba4072a..fc82975224 100755
--- a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java
+++ b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java
@@ -37,6 +37,16 @@ import java.util.Set;
* @version $Revision: 1 $
*/
public class KeycloakApplication extends Application {
+
+ public static final String SESSION_FACTORY = "keycloak.sessionFactory";
+ public static final String SESSION_FACTORY_PICKETLINK = "picketlink";
+ public static final String SESSION_FACTORY_MONGO = "mongo";
+ public static final String MONGO_HOST = "keycloak.mongodb.host";
+ public static final String MONGO_PORT = "keycloak.mongodb.port";
+ public static final String MONGO_DB_NAME = "keycloak.mongodb.databaseName";
+ public static final String MONGO_DROP_DB_ON_STARTUP = "keycloak.mongodb.dropDatabaseOnStartup";
+
+
protected Set