Search in sources :

Example 71 with ServerAddress

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

the class ServerAddressPropertyEditor method setAsText.

/*
	 * (non-Javadoc)
	 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
	 */
@Override
public void setAsText(@Nullable String replicaSetString) {
    if (!StringUtils.hasText(replicaSetString)) {
        setValue(null);
        return;
    }
    String[] replicaSetStringArray = StringUtils.commaDelimitedListToStringArray(replicaSetString);
    Set<ServerAddress> serverAddresses = new HashSet<ServerAddress>(replicaSetStringArray.length);
    for (String element : replicaSetStringArray) {
        ServerAddress address = parseServerAddress(element);
        if (address != null) {
            serverAddresses.add(address);
        }
    }
    if (serverAddresses.isEmpty()) {
        throw new IllegalArgumentException("Could not resolve at least one server of the replica set configuration! Validate your config!");
    }
    setValue(serverAddresses.toArray(new ServerAddress[serverAddresses.size()]));
}
Also used : ServerAddress(com.mongodb.ServerAddress) HashSet(java.util.HashSet)

Example 72 with ServerAddress

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

the class ServerAddressPropertyEditor method parseServerAddress.

/**
 * Parses the given source into a {@link ServerAddress}.
 *
 * @param source
 * @return the
 */
@Nullable
private ServerAddress parseServerAddress(String source) {
    if (!StringUtils.hasText(source)) {
        LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source);
        return null;
    }
    String[] hostAndPort = extractHostAddressAndPort(source.trim());
    if (hostAndPort.length > 2) {
        LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source);
        return null;
    }
    try {
        InetAddress hostAddress = InetAddress.getByName(hostAndPort[0]);
        Integer port = hostAndPort.length == 1 ? null : Integer.parseInt(hostAndPort[1]);
        return port == null ? new ServerAddress(hostAddress) : new ServerAddress(hostAddress, port);
    } catch (UnknownHostException e) {
        LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "host", hostAndPort[0]);
    } catch (NumberFormatException e) {
        LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "port", hostAndPort[1]);
    }
    return null;
}
Also used : UnknownHostException(java.net.UnknownHostException) ServerAddress(com.mongodb.ServerAddress) InetAddress(java.net.InetAddress) Nullable(org.springframework.lang.Nullable)

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