Example usage for com.mongodb ServerAddress defaultHost

List of usage examples for com.mongodb ServerAddress defaultHost

Introduction

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

Prototype

public static String defaultHost() 

Source Link

Document

Returns the default database host: "127.0.0.1"

Usage

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVDatabase.java

License:Open Source License

@Override
public void connect(String name, int port) throws VException {
    connect(name, ServerAddress.defaultHost(), port);
}

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

License:Apache License

/**
 * Parse the comma-separated list of server addresses. The format of the supplied string is one of the following:
 * //from  w  w w. j a va  2s .c o  m
 * <pre>
 * replicaSetName/host:port
 * replicaSetName/host:port,host2:port2
 * replicaSetName/host:port,host2:port2,host3:port3
 * host:port
 * host:port,host2:port2
 * host:port,host2:port2,host3:port3
 * </pre>
 * 
 * where {@code replicaSetName} is the name of the replica set, {@code host} contains the resolvable hostname or IP address of
 * the server, and {@code port} is the integral port number. If the port is not provided, the
 * {@link ServerAddress#defaultPort() default port} is used. If neither the host or port are provided (or
 * {@code addressString} is {@code null}), then an address will use the {@link ServerAddress#defaultHost() default host} and
 * {@link ServerAddress#defaultPort() default port}.
 * <p>
 * The IP address can be either an IPv4 address, or an IPv6 address surrounded by square brackets.
 * <p>
 * This method does not use the replica set name.
 * 
 * @param addressStr the string containing a comma-separated list of host and port pairs, optionally preceded by a
 *            replica set name
 * @return the list of server addresses; never null, but possibly empty
 */
protected static List<ServerAddress> parseAddresses(String addressStr) {
    List<ServerAddress> addresses = new ArrayList<>();
    if (addressStr != null) {
        addressStr = addressStr.trim();
        for (String address : addressStr.split(ADDRESS_DELIMITER)) {
            String hostAndPort = null;
            if (address.startsWith("[")) {
                // Definitely an IPv6 address without a replica set name ...
                hostAndPort = address;
            } else {
                // May start with replica set name ...
                int index = address.indexOf("/[");
                if (index >= 0) {
                    if ((index + 2) < address.length()) {
                        // replica set name with IPv6, so use just the IPv6 address ...
                        hostAndPort = address.substring(index + 1);
                    } else {
                        // replica set name with just opening bracket; this is invalid, so we'll ignore ...
                        continue;
                    }
                } else {
                    // possible replica set name with IPv4 only
                    index = address.indexOf("/");
                    if (index >= 0) {
                        if ((index + 1) < address.length()) {
                            // replica set name with IPv4, so use just the IPv4 address ...
                            hostAndPort = address.substring(index + 1);
                        } else {
                            // replica set name with no address ...
                            hostAndPort = ServerAddress.defaultHost();
                        }
                    } else {
                        // No replica set name with IPv4, so use the whole address ...
                        hostAndPort = address;
                    }
                }
            }
            ServerAddress newAddress = parseAddress(hostAndPort);
            if (newAddress != null)
                addresses.add(newAddress);
        }
    }
    return addresses;
}

From source file:org.axonframework.eventsourcing.eventstore.mongo.MongoFactory.java

License:Apache License

/**
 * Creates a mongo instance based on the provided configuration. Read javadoc of the class to learn about the
 * configuration options. A new Mongo instance is created each time this method is called.
 *
 * @return a new Mongo instance each time this method is called.
 *///from  www  .  j a v  a  2 s  .c  o m
public Mongo createMongo() {
    Mongo mongo;
    if (mongoAddresses.isEmpty()) {
        try {
            mongo = new Mongo(new ServerAddress(), mongoOptions);
        } catch (UnknownHostException e) {
            throw new IllegalStateException(
                    String.format("No addresses were provided, but could not find IP for default host: %s",
                            ServerAddress.defaultHost()),
                    e);
        }
    } else {
        mongo = new Mongo(mongoAddresses, mongoOptions);
    }
    mongo.setWriteConcern(defaultWriteConcern());

    return mongo;
}

From source file:org.sourcepit.akka.poc.mongodb.MongoClientActor.java

License:Apache License

private static ServerAddress toServerAddresse(BSONObject server) throws UnknownHostException {
    final Object oHost = server.get("host");
    final String host = oHost == null ? ServerAddress.defaultHost() : (String) oHost;
    final Object oPort = server.get("port");
    final int port = oPort == null ? ServerAddress.defaultPort() : ((Integer) oPort).intValue();
    return new ServerAddress(host, port);
}

From source file:org.springframework.integration.mongodb.rules.MongoDbAvailableRule.java

License:Apache License

@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
    return new Statement() {
        @Override//from   www .  j ava 2s. c  om
        public void evaluate() throws Throwable {
            MongoDbAvailable mongoAvailable = method.getAnnotation(MongoDbAvailable.class);
            if (mongoAvailable != null) {
                try {
                    MongoClientOptions options = new MongoClientOptions.Builder().connectTimeout(100).build();
                    Mongo mongo = new MongoClient(ServerAddress.defaultHost(), options);
                    mongo.getDatabaseNames();
                } catch (Exception e) {
                    logger.warn("MongoDb is not available. Skipping the test: "
                            + target.getClass().getSimpleName() + "." + method.getName() + "()");
                    return;
                }
            }
            base.evaluate();
        }
    };
}

From source file:uk.ac.ebi.eva.dbmigration.mongodb.MongobeeHelper.java

License:Apache License

private static List<ServerAddress> getServers(DatabaseParameters databaseParameters) {
    List<ServerAddress> addresses = new ArrayList<>();

    int port = ServerAddress.defaultPort();
    if (hasText(databaseParameters.getDbPort())) {
        port = Integer.parseInt(databaseParameters.getDbPort());
    }//from ww  w  . j av  a2 s  .com

    String hosts = databaseParameters.getDbHosts();
    if (!hasText(hosts)) {
        hosts = ServerAddress.defaultHost();
    }

    for (String host : hosts.split(",")) {
        addresses.add(new ServerAddress(host, port));
    }

    return addresses;
}

From source file:xbdd.webapp.factory.ServletContextMongoClientFactory.java

License:Apache License

public ServletContextMongoClientFactory(@Context final ServletContext context) {
    this.host = Objects.toString(context.getInitParameter(XBDD_MONGO_HOSTNAME_INIT_PARAMETER),
            ServerAddress.defaultHost());
    this.username = context.getInitParameter(XBDD_MONGO_USERNAME_INIT_PARAMETER);
    this.password = context.getInitParameter(XBDD_MONGO_PASSWORD_INIT_PARAMETER) != null
            ? context.getInitParameter(XBDD_MONGO_PASSWORD_INIT_PARAMETER).toCharArray()
            : null;/*from  w w  w  .  j a  va2  s .  c o m*/
    this.port = NumberUtils.isNumber(context.getInitParameter(XBDD_MONGO_PORT_INIT_PARAMETER))
            ? Integer.parseInt(context.getInitParameter(XBDD_MONGO_PORT_INIT_PARAMETER))
            : ServerAddress.defaultPort();
}