From e1fdc3ff2b000bc2409830635924c25a8049fd55 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 22 Jul 2015 16:17:54 +0100 Subject: [PATCH] Use MongoOptions and MongoClientURI to build MongoClient The way that Spring Data Mongo conctructs a MongoDb has changed in Spring Data 1.8.0 to align with the recommended way from 10gen. This involves leaving the user credentials embedded in the URI if they are there, which they always are in Spring Cloud Connectors. Fixes gh-129 --- .../document/MongoDbFactoryCreator.java | 51 +++++-------------- .../MongoServiceConnectorCreatorTest.java | 25 ++++----- 2 files changed, 26 insertions(+), 50 deletions(-) diff --git a/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/document/MongoDbFactoryCreator.java b/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/document/MongoDbFactoryCreator.java index 565aee6..fe4e784 100644 --- a/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/document/MongoDbFactoryCreator.java +++ b/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/document/MongoDbFactoryCreator.java @@ -4,28 +4,23 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; - -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.MongoClientURI; -import com.mongodb.MongoClientOptions.Builder; -import com.mongodb.MongoCredential; -import com.mongodb.MongoException; -import com.mongodb.ServerAddress; -import com.mongodb.WriteConcern; import org.springframework.cloud.service.AbstractServiceConnectorCreator; import org.springframework.cloud.service.ServiceConnectorConfig; import org.springframework.cloud.service.ServiceConnectorCreationException; import org.springframework.cloud.service.common.MongoServiceInfo; -import org.springframework.data.authentication.UserCredentials; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoClientOptions.Builder; +import com.mongodb.MongoClientURI; +import com.mongodb.MongoException; +import com.mongodb.WriteConcern; + /** * Simplified access to creating MongoDB service objects. * @@ -37,7 +32,7 @@ public class MongoDbFactoryCreator extends AbstractServiceConnectorCreator serverAddressList = getServerAddresses(mongoClientURI); - - MongoClient mongo = new MongoClient(serverAddressList, mongoOptionsToUse); - MongoCredential mongoCredential = mongoClientURI.getCredentials(); - - if (mongoCredential.getUserName() != null && mongoCredential.getPassword() != null) { - UserCredentials credentials = new UserCredentials(mongoCredential.getUserName(), new String(mongoCredential.getPassword())); - return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase(), credentials, mongoCredential.getSource()); - } - + private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException { + MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri(), mongoOptionsToUse); + MongoClient mongo = new MongoClient(mongoClientURI); return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase()); } - private List getServerAddresses(MongoClientURI mongoClientURI) throws UnknownHostException { - List servers = mongoClientURI.getHosts(); - List serverAddressList = new ArrayList(); - - for(String server : servers) { - serverAddressList.add(new ServerAddress(server)); - } - - return serverAddressList; - } - - private MongoClientOptions getMongoOptions(MongoDbFactoryConfig config) { + private MongoClientOptions.Builder getMongoOptions(MongoDbFactoryConfig config) { MongoClientOptions.Builder builder; Method builderMethod = ClassUtils.getMethodIfAvailable(MongoClientOptions.class, "builder"); @@ -108,7 +83,7 @@ public class MongoDbFactoryCreator extends AbstractServiceConnectorCreator addresses = mongo.getAllAddress(); assertEquals(1, addresses.size()); @@ -55,8 +56,8 @@ public class MongoServiceConnectorCreatorTest { assertEquals(serviceInfo.getHost(), address.getHost()); assertEquals(serviceInfo.getPort(), address.getPort()); - assertEquals(serviceInfo.getUserName(), ReflectionTestUtils.getField(credentials, "username")); - assertEquals(serviceInfo.getPassword(), ReflectionTestUtils.getField(credentials, "password")); + assertEquals(serviceInfo.getUserName(), credentials.getUserName()); + assertNotNull(credentials.getPassword()); // Don't do connector.getDatabase().getName() as that will try to initiate the connection assertEquals(serviceInfo.getDatabase(), ReflectionTestUtils.getField(mongoDbFactory, "databaseName")); @@ -73,15 +74,15 @@ public class MongoServiceConnectorCreatorTest { assertNotNull(mongoDbFactory); - Mongo mongo = (Mongo) ReflectionTestUtils.getField(mongoDbFactory, "mongo"); - UserCredentials credentials = (UserCredentials) ReflectionTestUtils.getField(mongoDbFactory, "credentials"); + MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo"); assertNotNull(mongo); List addresses = mongo.getAllAddress(); assertEquals(3, addresses.size()); - assertEquals(TEST_USERNAME, ReflectionTestUtils.getField(credentials, "username")); - assertEquals(TEST_PASSWORD, ReflectionTestUtils.getField(credentials, "password")); + MongoCredential credentials = mongo.getCredentialsList().get(0); + assertEquals(TEST_USERNAME, credentials.getUserName()); + assertNotNull(credentials.getPassword()); // Don't do connector.getDatabase().getName() as that will try to initiate the connection assertEquals(TEST_DB, ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));