use of com.mongodb.MongoCredential in project spring-cloud-connectors by spring-cloud.
the class MongoServiceConnectorCreatorTest method cloudMongoCreationNoConfig.
@Test
public void cloudMongoCreationNoConfig() throws Exception {
MongoServiceInfo serviceInfo = new MongoServiceInfo("id", TEST_HOST, TEST_PORT, TEST_USERNAME, TEST_PASSWORD, TEST_DB);
MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null);
assertNotNull(mongoDbFactory);
MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
assertNotNull(mongo);
MongoCredential credentials = mongo.getCredentialsList().get(0);
List<ServerAddress> addresses = extractServerAddresses(mongo);
assertEquals(1, addresses.size());
ServerAddress address = addresses.get(0);
assertEquals(serviceInfo.getHost(), address.getHost());
assertEquals(serviceInfo.getPort(), address.getPort());
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"));
}
use of com.mongodb.MongoCredential in project drill by apache.
the class MongoStoragePlugin method getClient.
public synchronized MongoClient getClient(List<ServerAddress> addresses) {
// Take the first replica from the replicated servers
final ServerAddress serverAddress = addresses.get(0);
final MongoCredential credential = clientURI.getCredentials();
String userName = credential == null ? null : credential.getUserName();
MongoCnxnKey key = new MongoCnxnKey(serverAddress, userName);
MongoClient client = addressClientMap.getIfPresent(key);
if (client == null) {
if (credential != null) {
List<MongoCredential> credentialList = Arrays.asList(credential);
client = new MongoClient(addresses, credentialList, clientURI.getOptions());
} else {
client = new MongoClient(addresses, clientURI.getOptions());
}
addressClientMap.put(key, client);
logger.debug("Created connection to {}.", key.toString());
logger.debug("Number of open connections {}.", addressClientMap.size());
}
return client;
}
use of com.mongodb.MongoCredential in project jackrabbit-oak by apache.
the class NodeCollectionProvider method prepareClientForHostname.
private MongoClient prepareClientForHostname(String hostname) throws UnknownHostException {
ServerAddress address;
if (hostname.contains(":")) {
String[] hostSplit = hostname.split(":");
if (hostSplit.length != 2) {
throw new IllegalArgumentException("Not a valid hostname: " + hostname);
}
address = new ServerAddress(hostSplit[0], Integer.parseInt(hostSplit[1]));
} else {
address = new ServerAddress(hostname);
}
MongoClientURI originalUri = new MongoClientURI(originalMongoUri);
List<MongoCredential> credentialList = new ArrayList<MongoCredential>(1);
if (originalUri.getCredentials() != null) {
credentialList.add(originalUri.getCredentials());
}
return new MongoClient(address, credentialList, originalUri.getOptions());
}
Aggregations