Search in sources :

Example 31 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 32 with ServerAddress

use of com.mongodb.ServerAddress in project DataX by alibaba.

the class MongoUtil method parseServerAddress.

/**
     * 转换为mongo地址协议
     * @param rawAddressList
     * @return
     */
private static List<ServerAddress> parseServerAddress(List<Object> rawAddressList) throws UnknownHostException {
    List<ServerAddress> addressList = new ArrayList<ServerAddress>();
    for (Object address : rawAddressList) {
        String[] tempAddress = ((String) address).split(":");
        try {
            ServerAddress sa = new ServerAddress(tempAddress[0], Integer.valueOf(tempAddress[1]));
            addressList.add(sa);
        } catch (Exception e) {
            throw new UnknownHostException();
        }
    }
    return addressList;
}
Also used : UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) ServerAddress(com.mongodb.ServerAddress) DataXException(com.alibaba.datax.common.exception.DataXException) UnknownHostException(java.net.UnknownHostException)

Example 33 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class AbstractServerDiscoveryAndMonitoringTest method applyResponse.

protected void applyResponse(final BsonArray response) {
    ServerAddress serverAddress = new ServerAddress(response.get(0).asString().getValue());
    BsonDocument isMasterResult = response.get(1).asDocument();
    ServerDescription serverDescription;
    if (isMasterResult.isEmpty()) {
        serverDescription = ServerDescription.builder().type(ServerType.UNKNOWN).state(CONNECTING).address(serverAddress).build();
    } else {
        serverDescription = createServerDescription(serverAddress, isMasterResult, getVersion(new BsonDocument("versionArray", new BsonArray(asList(new BsonInt32(2), new BsonInt32(6), new BsonInt32(0))))), 5000000);
    }
    factory.sendNotification(serverAddress, serverDescription);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) DescriptionHelper.createServerDescription(com.mongodb.connection.DescriptionHelper.createServerDescription) BsonArray(org.bson.BsonArray) ServerAddress(com.mongodb.ServerAddress)

Example 34 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class ClusterDescriptionTest method testObjectOverrides.

@Test
public void testObjectOverrides() throws UnknownHostException {
    ClusterDescription description = new ClusterDescription(MULTIPLE, UNKNOWN, asList(builder().state(CONNECTING).address(new ServerAddress("loc:27019")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27018")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27017")).lastUpdateTimeNanos(42L).build()));
    ClusterDescription descriptionTwo = new ClusterDescription(MULTIPLE, UNKNOWN, asList(builder().state(CONNECTING).address(new ServerAddress("loc:27019")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27018")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27017")).lastUpdateTimeNanos(42L).build()));
    assertEquals(description, descriptionTwo);
    assertEquals(description.hashCode(), descriptionTwo.hashCode());
    assertTrue(description.toString().startsWith("ClusterDescription"));
}
Also used : ServerAddress(com.mongodb.ServerAddress) Test(org.junit.Test)

Example 35 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class PlainAuthenticatorUnitTest method before.

@Before
public void before() {
    connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
    credential = MongoCredential.createPlainCredential("user", "$external", "pencil".toCharArray());
    subject = new PlainAuthenticator(this.credential);
}
Also used : ServerAddress(com.mongodb.ServerAddress) Before(org.junit.Before)

Aggregations

ServerAddress (com.mongodb.ServerAddress)58 Test (org.junit.Test)22 MongoClient (com.mongodb.MongoClient)13 MongoCredential (com.mongodb.MongoCredential)9 ArrayList (java.util.ArrayList)9 Before (org.junit.Before)9 TagSet (com.mongodb.TagSet)4 ClusterDescription (com.mongodb.connection.ClusterDescription)4 ClusterSettings (com.mongodb.connection.ClusterSettings)4 MongoClient (com.mongodb.reactivestreams.client.MongoClient)4 Tag (com.mongodb.Tag)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 ServerDescription (com.mongodb.connection.ServerDescription)3 UnknownHostException (java.net.UnknownHostException)3 Date (java.util.Date)3 ChunkInfo (org.apache.drill.exec.store.mongo.common.ChunkInfo)3 BsonDocument (org.bson.BsonDocument)3 DataXException (com.alibaba.datax.common.exception.DataXException)2 ConnectionString (com.mongodb.ConnectionString)2 MongoClientURI (com.mongodb.MongoClientURI)2