All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tenio.interstellar.mongo.config.parser.ClusterSettingsParser Maven / Gradle / Ivy

There is a newer version: 0.1.30
Show newest version
package org.tenio.interstellar.mongo.config.parser;

import com.mongodb.ConnectionString;
import com.mongodb.ServerAddress;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.connection.ClusterSettings;
import org.tenio.interstellar.mongo.config.MongoClientProperties;

import java.util.ArrayList;
import java.util.List;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

/**
 * TODO
 * 

* @author: Ban Tenio * @version: 1.0 */ public class ClusterSettingsParser { private final ClusterSettings settings; /** * * TODO * * @param connectionString TODO * @param config TODO */ public ClusterSettingsParser(ConnectionString connectionString, MongoClientProperties config) { ClusterSettings.Builder settings = ClusterSettings.builder(); MongoClientProperties.ClusterProperties clusterProperties = config.getCluster(); // ConnectionString takes precedence if (connectionString != null) { settings.applyConnectionString(connectionString); } else { // hosts List hosts = parseHosts(config, clusterProperties); settings.hosts(hosts); // replica set / mode String replicaSet = clusterProperties.getReplicaSet(); if (hosts.size() == 1 && replicaSet == null) { settings.mode(ClusterConnectionMode.SINGLE); } else { settings.mode(ClusterConnectionMode.MULTIPLE); } if (replicaSet != null) { settings.requiredReplicaSetName(replicaSet); } // serverSelectionTimeoutMS Long serverSelectionTimeoutMS = clusterProperties.getServerSelectionTimeoutMS(); if (serverSelectionTimeoutMS != null) { settings.serverSelectionTimeout(serverSelectionTimeoutMS, MILLISECONDS); } } this.settings = settings.build(); } /** * TODO * * @return TODO */ public ClusterSettings settings() { return settings; } private static List parseHosts(MongoClientProperties config, MongoClientProperties.ClusterProperties clusterProperties) { List hosts = new ArrayList<>(); List jsonHosts = clusterProperties.getServerAddressList(); if (jsonHosts != null) { jsonHosts.forEach(jsonHost -> { ServerAddress address = serverAddress(jsonHost); if (address != null) { hosts.add(address); } }); } else { // Support host / port properties and if not present use default ServerAddress (127.0.0.1:27017) ServerAddress address = serverAddress(new MongoClientProperties.ServerAddress() .setHost(config.getHost()) .setPort(config.getPort())); hosts.add(address == null ? new ServerAddress() : address); } return hosts; } private static ServerAddress serverAddress(MongoClientProperties.ServerAddress json) { if (json == null) return null; String host = json.getHost(); Integer port = json.getPort(); if (host == null) { return null; } else { if (port == null) { return new ServerAddress(host); } return new ServerAddress(host, port); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy