use of org.apereo.cas.configuration.model.support.mongo.BaseMongoDbProperties in project cas by apereo.
the class MongoDbConnectionFactory method buildMongoDbClient.
/**
* Build mongo db client.
*
* @param mongo the mongo
* @return the mongo client
*/
public MongoClient buildMongoDbClient(final BaseMongoDbProperties mongo) {
val settingsBuilder = MongoClientSettings.builder();
if (StringUtils.isNotBlank(mongo.getClientUri())) {
LOGGER.debug("Using MongoDb client URI [{}] to connect to MongoDb instance", mongo.getClientUri());
settingsBuilder.applyConnectionString(new ConnectionString(mongo.getClientUri()));
} else {
val serverAddresses = mongo.getHost().split(",");
if (serverAddresses.length == 0) {
throw new BeanCreationException("Unable to build a MongoDb client without any hosts/servers defined");
}
val servers = new ArrayList<ServerAddress>(0);
if (serverAddresses.length > 1) {
LOGGER.debug("Multiple MongoDb server addresses are defined. Ignoring port [{}], " + "assuming ports are defined as part of the address", mongo.getPort());
Arrays.stream(serverAddresses).filter(StringUtils::isNotBlank).map(ServerAddress::new).forEach(servers::add);
} else {
val port = mongo.getPort() > 0 ? mongo.getPort() : DEFAULT_PORT;
LOGGER.debug("Found single MongoDb server address [{}] using port [{}]", mongo.getHost(), port);
val addr = new ServerAddress(mongo.getHost(), port);
servers.add(addr);
}
settingsBuilder.applyToClusterSettings(builder -> builder.hosts(servers));
val credential = buildMongoCredential(mongo);
settingsBuilder.credential(credential).writeConcern(WriteConcern.valueOf(mongo.getWriteConcern())).codecRegistry(CodecRegistries.fromRegistries(CodecRegistries.fromProviders(new BaseConverters.ZonedDateTimeCodecProvider()), MongoClientSettings.getDefaultCodecRegistry())).readConcern(new ReadConcern(ReadConcernLevel.valueOf(mongo.getReadConcern()))).applyToConnectionPoolSettings(builder -> {
val poolConfig = mongo.getPool();
val pool = ConnectionPoolSettings.builder().maxConnectionLifeTime(Beans.newDuration(poolConfig.getLifeTime()).toMillis(), TimeUnit.MILLISECONDS).maxConnectionIdleTime(Beans.newDuration(poolConfig.getIdleTime()).toMillis(), TimeUnit.MILLISECONDS).maxSize(poolConfig.getMaxSize()).minSize(poolConfig.getMinSize()).maxWaitTime(Beans.newDuration(poolConfig.getMaxWaitTime()).toMillis(), TimeUnit.MILLISECONDS).build();
builder.applySettings(pool);
}).applyToSocketSettings(builder -> {
val socket = SocketSettings.builder().connectTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis(), TimeUnit.MILLISECONDS).readTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis(), TimeUnit.MILLISECONDS).build();
builder.applySettings(socket);
}).applyToSslSettings(builder -> {
val ssl = SslSettings.builder().enabled(mongo.isSslEnabled()).context(this.sslContext).build();
builder.applySettings(ssl);
}).applyToServerSettings(builder -> {
val server = ServerSettings.builder().heartbeatFrequency((int) Beans.newDuration(mongo.getTimeout()).toMillis(), TimeUnit.MILLISECONDS).build();
builder.applySettings(server);
}).retryWrites(mongo.isRetryWrites());
}
return MongoClients.create(settingsBuilder.build());
}
Aggregations