Search in sources :

Example 61 with ServerAddress

use of com.mongodb.ServerAddress 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)

Example 62 with ServerAddress

use of com.mongodb.ServerAddress in project qi4j-sdk by Qi4j.

the class MongoMapEntityStoreMixin method loadConfiguration.

private void loadConfiguration() throws UnknownHostException {
    configuration.refresh();
    MongoEntityStoreConfiguration config = configuration.get();
    // Combine hostname, port and nodes configuration properties
    // If no configuration, use 127.0.0.1:27017
    serverAddresses = new ArrayList<>();
    int port = config.port().get() == null ? 27017 : config.port().get();
    if (config.nodes().get().isEmpty()) {
        String hostname = config.hostname().get() == null ? "127.0.0.1" : config.hostname().get();
        serverAddresses.add(new ServerAddress(hostname, port));
    } else {
        if (config.hostname().get() != null && !config.hostname().get().isEmpty()) {
            serverAddresses.add(new ServerAddress(config.hostname().get(), port));
        }
        serverAddresses.addAll(config.nodes().get());
    }
    // If database name not configured, set it to qi4j:entitystore
    databaseName = config.database().get();
    if (databaseName == null) {
        databaseName = DEFAULT_DATABASE_NAME;
    }
    // If collection name not configured, set it to qi4j:entitystore:entities
    collectionName = config.collection().get();
    if (collectionName == null) {
        collectionName = DEFAULT_COLLECTION_NAME;
    }
    // If write concern not configured, set it to normal
    switch(config.writeConcern().get()) {
        case FSYNC_SAFE:
            writeConcern = WriteConcern.FSYNC_SAFE;
            break;
        case JOURNAL_SAFE:
            writeConcern = WriteConcern.JOURNAL_SAFE;
            break;
        case MAJORITY:
            writeConcern = WriteConcern.MAJORITY;
            break;
        case REPLICAS_SAFE:
            writeConcern = WriteConcern.REPLICAS_SAFE;
            break;
        case SAFE:
            writeConcern = WriteConcern.SAFE;
            break;
        case NORMAL:
        default:
            writeConcern = WriteConcern.NORMAL;
    }
    // Username and password are defaulted to empty strings
    username = config.username().get();
    password = config.password().get().toCharArray();
}
Also used : ServerAddress(com.mongodb.ServerAddress)

Example 63 with ServerAddress

use of com.mongodb.ServerAddress in project leopard by tanhaichao.

the class DfsGridImpl method loadGridFS.

@SuppressWarnings("deprecation")
private synchronized GridFS loadGridFS() {
    if (fs != null) {
        return fs;
    }
    String[] list = server.split(":");
    String host = list[0];
    int port = Integer.parseInt(list[1]);
    String username = null;
    String password = null;
    if (list.length >= 4) {
        username = list[2];
        password = list[3];
    }
    int connectTimeout = 1000 * 60;
    MongoClientOptions options = new MongoClientOptions.Builder().connectTimeout(connectTimeout).build();
    client = new MongoClient(new ServerAddress(host, port), options);
    // @SuppressWarnings("deprecation")
    DB db = client.getDB("dfs");
    if (username != null && username.length() > 0) {
        if (password != null && password.length() > 0) {
            db.addUser(username, password.toCharArray());
        }
    }
    return new GridFS(db, "fs");
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientOptions(com.mongodb.MongoClientOptions) ServerAddress(com.mongodb.ServerAddress) GridFS(com.mongodb.gridfs.GridFS) DB(com.mongodb.DB)

Example 64 with ServerAddress

use of com.mongodb.ServerAddress in project leopard by tanhaichao.

the class MongoImpl method init.

@SuppressWarnings("deprecation")
public void init() {
    String[] list = server.split(":");
    String host = list[0];
    int port = Integer.parseInt(list[1]);
    int connectTimeout = 1000 * 60;
    MongoClientOptions options = new MongoClientOptions.Builder().connectTimeout(connectTimeout).build();
    client = new MongoClient(new ServerAddress(host, port), options);
    this.db = client.getDB(this.database);
    if (username != null && username.length() > 0) {
        if (password != null && password.length() > 0) {
            db.addUser(username, password.toCharArray());
        }
    }
    this.collection = db.getCollection(collectionName);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientOptions(com.mongodb.MongoClientOptions) ServerAddress(com.mongodb.ServerAddress)

Example 65 with ServerAddress

use of com.mongodb.ServerAddress in project spring-data-mongodb by spring-projects.

the class MongoNamespaceReplicaSetTests method testParsingMongoWithReplicaSets.

@Test
@SuppressWarnings("unchecked")
public void testParsingMongoWithReplicaSets() throws Exception {
    assertTrue(ctx.containsBean("replicaSetMongo"));
    MongoClientFactoryBean mfb = (MongoClientFactoryBean) ctx.getBean("&replicaSetMongo");
    List<ServerAddress> replicaSetSeeds = (List<ServerAddress>) ReflectionTestUtils.getField(mfb, "replicaSetSeeds");
    assertThat(replicaSetSeeds, is(notNullValue()));
    assertThat(replicaSetSeeds, hasItems(new ServerAddress(InetAddress.getByName("127.0.0.1"), 10001), new ServerAddress(InetAddress.getByName("localhost"), 10002)));
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

ServerAddress (com.mongodb.ServerAddress)72 Test (org.junit.Test)27 MongoClient (com.mongodb.MongoClient)21 ArrayList (java.util.ArrayList)13 MongoCredential (com.mongodb.MongoCredential)10 Before (org.junit.Before)9 Document (org.bson.Document)5 TagSet (com.mongodb.TagSet)4 ClusterDescription (com.mongodb.connection.ClusterDescription)4 ClusterSettings (com.mongodb.connection.ClusterSettings)4 MongoClient (com.mongodb.reactivestreams.client.MongoClient)4 UnknownHostException (java.net.UnknownHostException)4 List (java.util.List)4 Tag (com.mongodb.Tag)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 ServerDescription (com.mongodb.connection.ServerDescription)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 ChunkInfo (org.apache.drill.exec.store.mongo.common.ChunkInfo)3 BsonDocument (org.bson.BsonDocument)3