Example usage for com.mongodb ServerAddress ServerAddress

List of usage examples for com.mongodb ServerAddress ServerAddress

Introduction

In this page you can find the example usage for com.mongodb ServerAddress ServerAddress.

Prototype

public ServerAddress(final InetSocketAddress inetSocketAddress) 

Source Link

Document

Creates a ServerAddress

Usage

From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java

License:Apache License

@SuppressWarnings("unchecked")
private Set<ServerAddress> getPreferredHosts(MongoClient client, List<String> hosts) {
    Set<ServerAddress> addressList = Sets.newHashSet();
    MongoDatabase db = client.getDatabase(scanSpec.getDbName());
    ReadPreference readPreference = client.getReadPreference();
    Document command = db.runCommand(new Document("isMaster", 1));

    final String primaryHost = command.getString("primary");
    final List<String> hostsList = (List<String>) command.get("hosts");

    switch (readPreference.getName().toUpperCase()) {
    case "PRIMARY":
    case "PRIMARYPREFERRED":
        if (primaryHost == null) {
            return null;
        }/* www  .j ava2 s .c  om*/
        addressList.add(new ServerAddress(primaryHost));
        return addressList;
    case "SECONDARY":
    case "SECONDARYPREFERRED":
        if (primaryHost == null || hostsList == null) {
            return null;
        }
        hostsList.remove(primaryHost);
        for (String host : hostsList) {
            addressList.add(new ServerAddress(host));
        }
        return addressList;
    case "NEAREST":
        if (hostsList == null) {
            return null;
        }
        for (String host : hostsList) {
            addressList.add(new ServerAddress(host));
        }
        return addressList;
    default:
        return null;
    }
}

From source file:org.apache.drill.exec.store.mongo.MongoRecordReader.java

License:Apache License

private void init(MongoSubScan.MongoSubScanSpec subScanSpec) {
    List<String> hosts = subScanSpec.getHosts();
    List<ServerAddress> addresses = Lists.newArrayList();
    for (String host : hosts) {
        addresses.add(new ServerAddress(host));
    }/*from   w  ww . j a  v a2  s .  c o  m*/
    MongoClient client = plugin.getClient(addresses);
    MongoDatabase db = client.getDatabase(subScanSpec.getDbName());
    this.unionEnabled = fragmentContext.getOptions().getOption(ExecConstants.ENABLE_UNION_TYPE);
    collection = db.getCollection(subScanSpec.getCollectionName(), BsonDocument.class);
}

From source file:org.apache.drill.exec.store.mongo.MongoStoragePlugin.java

License:Apache License

public MongoClient getClient(String host) {
    return getClient(Collections.singletonList(new ServerAddress(host)));
}

From source file:org.apache.drill.exec.store.mongo.MongoStoragePlugin.java

License:Apache License

public MongoClient getClient() {
    List<String> hosts = clientURI.getHosts();
    List<ServerAddress> addresses = Lists.newArrayList();
    for (String host : hosts) {
        addresses.add(new ServerAddress(host));
    }//from   ww w .  java  2s.co m
    return getClient(addresses);
}

From source file:org.apache.karaf.jaas.modules.mongo.internal.DefaultUserDetailService.java

License:Apache License

protected MongoClient createClient() throws NumberFormatException, UnknownHostException {

    List<ServerAddress> servers = new ArrayList<ServerAddress>();
    StringTokenizer st = new StringTokenizer(configuration.getDatasourceURL(), ",");
    while (st.hasMoreTokens()) {
        String serverURL = st.nextToken();
        if (serverURL.indexOf(':') == -1) {
            servers.add(new ServerAddress(serverURL));
        } else {//from  ww  w  .j  a  v a2s .co m
            String host = serverURL.substring(0, serverURL.indexOf(':'));
            String port = serverURL.substring(serverURL.indexOf(':') + 1);
            servers.add(new ServerAddress(host, Integer.parseInt(port)));
        }
    }
    return new MongoClient(servers);

}

From source file:org.apache.metamodel.DataContextFactory.java

License:Apache License

/**
 * Creates a new MongoDB datacontext./*ww w .j  a v  a  2  s  .co m*/
 * 
 * @param hostname
 *            The hostname of the MongoDB instance
 * @param port
 *            the port of the MongoDB instance, or null if the default port
 *            should be used.
 * @param databaseName
 *            the name of the database
 * @param username
 *            the username, or null if unauthenticated access should be used
 * @param password
 *            the password, or null if unathenticated access should be used
 * @param tableDefs
 *            an array of table definitions, or null if table definitions
 *            should be autodetected.
 * @return a DataContext object that matches the request
 */
public static UpdateableDataContext createMongoDbDataContext(String hostname, Integer port, String databaseName,
        String username, char[] password, SimpleTableDef[] tableDefs) {
    try {
        final ServerAddress serverAddress;
        if (port == null) {
            serverAddress = new ServerAddress(hostname);
        } else {
            serverAddress = new ServerAddress(hostname, port);
        }
        final MongoClient mongoClient;
        final MongoDatabase mongoDb;
        if (Strings.isNullOrEmpty(username)) {
            mongoClient = new MongoClient(serverAddress);
        } else {
            final MongoCredential credential = MongoCredential.createCredential(username, databaseName,
                    password);
            mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
        }
        mongoDb = mongoClient.getDatabase(databaseName);

        if (tableDefs == null || tableDefs.length == 0) {
            return new MongoDbDataContext(mongoDb);
        }
        return new MongoDbDataContext(mongoDb, tableDefs);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new IllegalStateException(e);
    }
}

From source file:org.apache.whirr.service.mongodb.integration.MongoDBServiceController.java

License:Apache License

private void getMongo(InetAddress mongoAddr) throws Exception {
    if (mongo == null || mongo.getAddress() != new ServerAddress(mongoAddr))
        mongo = new Mongo(new ServerAddress(mongoAddr));
    /**//w w  w.j a va  2  s  .co  m
     * Test the connection...
     */
    mongo.getDB("test").getCollection("whirr_conn_validation").save(new BasicDBObject("foo", "bar"),
            WriteConcern.SAFE);

    LOG.info("Connected to MongoDB Server. ");
}

From source file:org.eclipselabs.emongo.components.MongoClientProviderComponent.java

License:Open Source License

private ServerAddress createServerAddress(String uriProperty) throws URISyntaxException, UnknownHostException {
    URI uri = new URI(uriProperty);
    int port = uri.getPort();
    ServerAddress serverAddress = port == -1 ? new ServerAddress(uri.getHost())
            : new ServerAddress(uri.getHost(), uri.getPort());
    return serverAddress;
}

From source file:org.exoplatform.mongo.factory.MongoFactoryBean.java

License:Open Source License

private void attemptMongoConnection(String host, int port) throws Exception {
    try {//from www .  j a v  a  2 s  .  c o  m
        if (port == Integer.MIN_VALUE) {
            replicaSetSeeds.add(new ServerAddress(host));
        } else {
            replicaSetSeeds.add(new ServerAddress(host, port));
        }
    } catch (UnknownHostException unknownHost) {
        throw unknownHost;
    }
}

From source file:org.fixtrading.timpani.securitydef.datastore.MongoDBSecurityDefinitionStore.java

License:Apache License

@Override
public CompletableFuture<SecurityDefinitionStore> open() {
    List<ServerAddress> hostList = Arrays.stream(hosts).map(h -> new ServerAddress(h))
            .collect(Collectors.toList());
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(hostList).build();
    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).build();
    mongoClient = MongoClients.create(settings);

    database = mongoClient.getDatabase(DATABASE_NAME);
    collection = database.getCollection(SECDEF_COLLECTION_NAME);

    // In the case of MongoDB, open is synchronous because it doesn't
    // actually communicate with the server until a query is invoked.
    return CompletableFuture.completedFuture(this);
}