Example usage for com.mongodb ServerAddress getPort

List of usage examples for com.mongodb ServerAddress getPort

Introduction

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

Prototype

public int getPort() 

Source Link

Document

Gets the port number

Usage

From source file:com.meltmedia.dropwizard.mongo.MongoBundle.java

License:Apache License

MongoClient buildClient(MongoConfiguration configuration) {
    try {/*from  ww w . j av a2  s.  com*/
        // build the seed server list.
        List<ServerAddress> servers = new ArrayList<>();
        for (Server seed : configuration.getSeeds()) {
            servers.add(new ServerAddress(seed.getHost(), seed.getPort()));
        }

        log.info("Found {} mongo seed servers", servers.size());
        for (ServerAddress server : servers) {
            log.info("Found mongo seed server {}:{}", server.getHost(), server.getPort());
        }

        // build the credentials
        Credentials credentialConfig = configuration.getCredentials();
        List<MongoCredential> credentials = credentialConfig == null ? Collections.<MongoCredential>emptyList()
                : Collections.singletonList(MongoCredential.createCredential(credentialConfig.getUserName(),
                        configuration.getDatabase(), credentialConfig.getPassword().toCharArray()));

        if (credentials.isEmpty()) {
            log.info("Found {} mongo credentials.", credentials.size());
        } else {
            for (MongoCredential credential : credentials) {
                log.info("Found mongo credential for {} on database {}.", credential.getUserName(),
                        credential.getSource());
            }
        }

        // build the options.
        MongoClientOptions options = new MongoClientOptions.Builder()
                .writeConcern(writeConcern(configuration.getWriteConcern())).build();

        log.info("Mongo database is {}", configuration.getDatabase());

        return new MongoClient(servers, credentials, options);
    } catch (Exception e) {
        throw new RuntimeException("Could not configure MongoDB client.", e);
    }
}

From source file:com.navercorp.pinpoint.plugin.mongo.interceptor.MongoDriverConnectInterceptor3_0.java

License:Apache License

private List<String> getHostList(Object arg) {
    if (!(arg instanceof Cluster)) {
        return Collections.emptyList();
    }/*from  w  ww. j  a  v a 2  s.  co m*/

    final Cluster cluster = (Cluster) arg;

    final List<String> hostList = new ArrayList<String>();

    Collection<ServerDescription> serverDescriptions;// = cluster.getDescription().getAll();//.getServerDescriptions();

    try {
        ClusterDescription.class.getDeclaredMethod("getServerDescriptions");
        serverDescriptions = cluster.getDescription().getServerDescriptions();
    } catch (NoSuchMethodException e) {
        serverDescriptions = cluster.getDescription().getAll();
    }

    for (ServerDescription serverDescription : serverDescriptions) {

        ServerAddress serverAddress = serverDescription.getAddress();
        final String hostAddress = HostAndPort.toHostAndPortString(serverAddress.getHost(),
                serverAddress.getPort());
        hostList.add(hostAddress);
    }

    return hostList;
}

From source file:com.navercorp.pinpoint.plugin.mongo.interceptor.MongoDriverConnectInterceptor3_7.java

License:Apache License

private List<String> getHostList(Object arg) {
    if (!(arg instanceof MongoClientSettings)) {
        return Collections.emptyList();
    }//from  ww  w .j a  va 2s .  c  om

    final MongoClientSettings mongoClientSettings = (MongoClientSettings) arg;

    List<ServerAddress> lists = mongoClientSettings.getClusterSettings().getHosts();

    final List<String> hostList = new ArrayList<String>();
    for (ServerAddress sa : lists) {
        final String hostAddress = HostAndPort.toHostAndPortString(sa.getHost(), sa.getPort());
        hostList.add(hostAddress);
    }

    return hostList;
}

From source file:com.nec.strudel.tkvs.store.mongodb.MongodbStore.java

License:Apache License

@Override
public MongoDbServer createDbServer(String dbName, Properties props) {
    ServerAddress addr = chooseOne(getServerAddressList(props));
    LOGGER.info("using mongos@" + addr.getHost() + ":" + addr.getPort());
    try {//from  w w w. j a va  2s .c om
        return new MongoDbServer(dbName, addr, new BackoffPolicy(props));
    } catch (IOException e) {
        throw new TkvStoreException("MongodbStore failed to create (IOException)", e);
    }
}

From source file:com.sitewhere.mongodb.MongoDbClient.java

License:Open Source License

/**
 * Detects whether a replica set is configured and creates one if not.
 * //  w ww.jav  a 2s . co  m
 * @param addresses
 */
protected void doAutoConfigureReplication(List<ServerAddress> addresses) throws SiteWhereException {
    // Only connect to primary for sending commands.
    MongoClient primary = getPrimaryConnection(addresses);

    // Check for existing replica set configuration.
    getLogger().info("Checking for existing replica set...");
    try {
        Document result = primary.getDatabase("admin").runCommand(new BasicDBObject("replSetGetStatus", 1));
        if (result.getDouble("ok") == 1) {
            getLogger().warn("Replica set already configured. Skipping auto-configuration.");
            return;
        }
    } catch (MongoCommandException e) {
        getLogger().info("Replica set was not configured.");
    }

    // Create configuration for new replica set.
    try {
        getLogger().info("Configuring new replica set '" + getConfiguration().getReplicaSetName() + "'.");
        BasicDBObject config = new BasicDBObject("_id", getConfiguration().getReplicaSetName());
        List<BasicDBObject> servers = new ArrayList<BasicDBObject>();

        // Create list of members in replica set.
        int index = 0;
        for (final ServerAddress address : addresses) {
            BasicDBObject server = new BasicDBObject("_id", index);
            server.put("host", (address.getHost() + ":" + address.getPort()));

            // First server in list is preferred primary.
            if (index == 0) {
                server.put("priority", 10);
            }

            servers.add(server);
            index++;
        }
        config.put("members", servers);

        // Send command.
        Document result = primary.getDatabase("admin").runCommand(new BasicDBObject("replSetInitiate", config));
        if (result.getDouble("ok") != 1) {
            throw new SiteWhereException("Unable to auto-configure replica set.\n" + result.toJson());
        }
        getLogger().info(
                "Replica set '" + getConfiguration().getReplicaSetName() + "' creation command successful.");
    } finally {
        primary.close();
    }
}

From source file:com.torodb.testing.mongodb.docker.ServerAddressConverter.java

License:Apache License

public static HostAndPort convert(ServerAddress address) {
    return HostAndPort.fromParts(address.getHost(), address.getPort());
}

From source file:de.otto.mongodb.profiler.web.ConnectionsPageViewModel.java

License:Apache License

public String serversAsString(ProfiledConnection connection) {
    final StringBuilder sb = new StringBuilder();
    for (ServerAddress serverAddress : connection.getConfiguration().getServerAddresses()) {
        if (sb.length() > 0) {
            sb.append(", ");
        }/*from  w  w  w  .j ava2 s .c o  m*/
        sb.append(serverAddress.getHost());
        if (serverAddress.getPort() != 27017) {
            sb.append(":").append(serverAddress.getPort());
        }
    }
    return sb.toString();
}

From source file:dk.au.cs.karibu.backend.mongo.StandardMongoConfiguration.java

License:Apache License

/**
 * {@inheritDoc}//from ww  w.j a va2 s  .c o  m
 */
@Override
public String toString() {
    String adrlist = "";
    for (ServerAddress adr : getServerAddressList()) {
        if (adrlist.length() > 0) {
            adrlist += ",";
        }
        adrlist += adr.getHost() + ":" + adr.getPort();
    }
    return "StandardMongoConfiguration" + " (databaseName : " + getDatabaseName() + ", serverAddressList : ["
            + adrlist + "] User: " + getUsername() + ")";
}

From source file:io.debezium.connector.mongodb.MongoUtil.java

License:Apache License

protected static String toString(ServerAddress address) {
    String host = address.getHost();
    if (host.contains(":")) {
        // IPv6 address, so wrap with square brackets ...
        return "[" + host + "]:" + address.getPort();
    }// w  ww  .j  a  v  a2  s . co  m
    return host + ":" + address.getPort();
}

From source file:io.debezium.connector.mongodb.ReplicaSet.java

License:Apache License

protected static int compare(ServerAddress address1, ServerAddress address2) {
    int diff = address1.getHost().compareTo(address2.getHost());
    if (diff != 0)
        return diff;
    return address1.getPort() - address2.getPort();
}