Search in sources :

Example 11 with MongoCredential

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"));
}
Also used : MongoClient(com.mongodb.MongoClient) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) Test(org.junit.Test)

Example 12 with MongoCredential

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;
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress)

Example 13 with MongoCredential

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());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) MongoClientURI(com.mongodb.MongoClientURI) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList)

Aggregations

MongoCredential (com.mongodb.MongoCredential)13 ServerAddress (com.mongodb.ServerAddress)9 MongoClient (com.mongodb.MongoClient)8 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 ConnectionString (com.mongodb.ConnectionString)2 MongoClientURI (com.mongodb.MongoClientURI)2 MongoSecurityException (com.mongodb.MongoSecurityException)2 SaslException (javax.security.sasl.SaslException)2 MongoServiceInfo (org.springframework.cloud.service.common.MongoServiceInfo)2 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)2 AuthenticationMechanism (com.mongodb.AuthenticationMechanism)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBCollection (com.mongodb.DBCollection)1 Builder (com.mongodb.async.client.MongoClientSettings.Builder)1 ClusterSettings (com.mongodb.connection.ClusterSettings)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Callback (javax.security.auth.callback.Callback)1