Allow authSource in the Mongo URI to be passed along.

This commit is contained in:
Scott Frederick
2015-05-07 15:09:50 -04:00
parent 426875dcc3
commit 2e3129a46f
3 changed files with 36 additions and 30 deletions

View File

@@ -7,7 +7,7 @@ buildscript {
maven { url 'http://repo.springsource.org/plugins-release' }
}
dependencies {
classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.5'
classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.7'
classpath 'org.springframework.build.gradle:spring-io-plugin:0.0.3.RELEASE'
}
}
@@ -18,7 +18,7 @@ ext {
springAmqpVersion = "1.1.1.RELEASE"
springDataRedisVersion = "1.1.1.RELEASE"
springDataMongoVersion = "1.2.4.RELEASE"
springDataMongoVersion = "1.4.3.RELEASE"
jedisVersion = "2.1.0"
@@ -179,9 +179,10 @@ configure(rootProject) {
ext {
matrix = [
"mongo13" : [springDataMongoVersion: "1.3.5.RELEASE"],
"mongo14" : [springDataMongoVersion: "1.4.2.RELEASE"],
"mongo15" : [springDataMongoVersion: "1.5.0.RELEASE"],
"mongo14" : [springDataMongoVersion: "1.4.3.RELEASE"],
"mongo15" : [springDataMongoVersion: "1.5.5.RELEASE"],
"mongo16" : [springDataMongoVersion: "1.6.2.RELEASE"],
"mongo17" : [springDataMongoVersion: "1.7.0.RELEASE"],
"jedis22-redis11": [jedisVersion: "2.2.1", springDataRedisVersion: "1.1.1.RELEASE"],
"jedis23-redis12": [jedisVersion: "2.3.1", springDataRedisVersion: "1.2.1.RELEASE"],
"jedis23-redis13": [jedisVersion: "2.3.1", springDataRedisVersion: "1.3.0.RELEASE"],

View File

@@ -7,7 +7,15 @@ 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;
@@ -18,13 +26,6 @@ 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.MongoException;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;
/**
* Simplified access to creating MongoDB service objects.
*
@@ -38,18 +39,7 @@ public class MongoDbFactoryCreator extends AbstractServiceConnectorCreator<Mongo
try {
MongoClientOptions mongoOptionsToUse = getMongoOptions((MongoDbFactoryConfig) config);
MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri());
List<ServerAddress> serverAddressList = getServerAddresses(mongoClientURI);
MongoClient mongo = new MongoClient(serverAddressList, mongoOptionsToUse);
SimpleMongoDbFactory mongoDbFactory;
if (mongoClientURI.getUsername() != null && mongoClientURI.getPassword() != null) {
UserCredentials credentials = new UserCredentials(mongoClientURI.getUsername(), new String(mongoClientURI.getPassword()));
mongoDbFactory = new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase(), credentials);
} else {
mongoDbFactory = new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
}
SimpleMongoDbFactory mongoDbFactory = createMongoDbFactory(serviceInfo, mongoOptionsToUse);
return configure(mongoDbFactory, (MongoDbFactoryConfig) config);
} catch (UnknownHostException e) {
@@ -59,6 +49,21 @@ public class MongoDbFactoryCreator extends AbstractServiceConnectorCreator<Mongo
}
}
private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions mongoOptionsToUse) throws UnknownHostException {
MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri());
List<ServerAddress> 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());
}
return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
}
private List<ServerAddress> getServerAddresses(MongoClientURI mongoClientURI) throws UnknownHostException {
List<String> servers = mongoClientURI.getHosts();
List<ServerAddress> serverAddressList = new ArrayList<ServerAddress>();

View File

@@ -3,12 +3,12 @@ package org.springframework.cloud.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;
/**
*
* @author Ramnivas Laddad
@@ -31,9 +31,9 @@ public class MongoDbFactoryCloudConfigTestHelper {
assertEquals(writeConcernW.intValue(), writeConcern.getW());
}
Mongo mongo = (Mongo) ReflectionTestUtils.getField(connector, "mongo");
assertEquals(connectionsPerHost.intValue(), mongo.getMongoOptions().connectionsPerHost);
assertEquals(maxWaitTime.intValue(), mongo.getMongoOptions().maxWaitTime);
MongoClient mongoClient = (MongoClient) ReflectionTestUtils.getField(connector, "mongo");
assertEquals(connectionsPerHost.intValue(), mongoClient.getMongoClientOptions().getConnectionsPerHost());
assertEquals(maxWaitTime.intValue(), mongoClient.getMongoClientOptions().getMaxWaitTime());
}
}