From 6741dffe38c9c8d9fd8ca1e92cb15762666a607a Mon Sep 17 00:00:00 2001 From: Carsten Saathoff Date: Thu, 8 Oct 2015 10:02:36 +0200 Subject: [PATCH] Added configuration via mongodb:// uri to mongo provider factory. --- ...DefaultMongoConnectionFactoryProvider.java | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java index 90e8c988ef..743eaa0450 100755 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java @@ -3,6 +3,7 @@ package org.keycloak.connections.mongo; import com.mongodb.DB; import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; +import com.mongodb.MongoClientURI; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; @@ -147,31 +148,38 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro * @throws UnknownHostException */ protected MongoClient createMongoClient() throws UnknownHostException { - String host = config.get("host", ServerAddress.defaultHost()); - int port = config.getInt("port", ServerAddress.defaultPort()); - String dbName = config.get("db", "keycloak"); - - String user = config.get("user"); - String password = config.get("password"); - - MongoClientOptions clientOptions = getClientOptions(); - - MongoClient client; - if (user != null && password != null) { - MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); - client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), clientOptions); + String uriString = config.get("uri"); + if (uriString != null) { + MongoClientURI uri = new MongoClientURI(uriString); + MongoClient client = new MongoClient(uri); + return client; } else { - client = new MongoClient(new ServerAddress(host, port), clientOptions); + String host = config.get("host", ServerAddress.defaultHost()); + int port = config.getInt("port", ServerAddress.defaultPort()); + String dbName = config.get("db", "keycloak"); + + String user = config.get("user"); + String password = config.get("password"); + + MongoClientOptions clientOptions = getClientOptions(); + + MongoClient client; + if (user != null && password != null) { + MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); + client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), clientOptions); + } else { + client = new MongoClient(new ServerAddress(host, port), clientOptions); + } + + operationalInfo = new LinkedHashMap<>(); + operationalInfo.put("mongoServerAddress", client.getAddress().toString()); + operationalInfo.put("mongoDatabaseName", dbName); + operationalInfo.put("mongoUser", user); + operationalInfo.put("mongoDriverVersion", client.getVersion()); + + logger.debugv("Initialized mongo model. host: %s, port: %d, db: %s", host, port, dbName); + return client; } - - operationalInfo = new LinkedHashMap<>(); - operationalInfo.put("mongoServerAddress", client.getAddress().toString()); - operationalInfo.put("mongoDatabaseName", dbName); - operationalInfo.put("mongoUser", user); - operationalInfo.put("mongoDriverVersion", client.getVersion()); - - logger.debugv("Initialized mongo model. host: %s, port: %d, db: %s", host, port, dbName); - return client; } protected MongoClientOptions getClientOptions() {