Search in sources :

Example 1 with BaseMongoDbProperties

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());
}
Also used : lombok.val(lombok.val) SSLContext(javax.net.ssl.SSLContext) Arrays(java.util.Arrays) SneakyThrows(lombok.SneakyThrows) Beans(org.apereo.cas.configuration.support.Beans) StringUtils(org.apache.commons.lang3.StringUtils) Document(org.springframework.data.mongodb.core.mapping.Document) LoggingUtils(org.apereo.cas.util.LoggingUtils) SocketSettings(com.mongodb.connection.SocketSettings) SSLContexts(org.apache.http.ssl.SSLContexts) ReadConcern(com.mongodb.ReadConcern) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) ClassUtils(org.springframework.util.ClassUtils) PropertyNameFieldNamingStrategy(org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy) MongoClients(com.mongodb.client.MongoClients) Collection(java.util.Collection) Set(java.util.Set) CodecRegistries(org.bson.codecs.configuration.CodecRegistries) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Stream(java.util.stream.Stream) JodaTimeConverters(org.springframework.data.convert.JodaTimeConverters) MongoClientSettings(com.mongodb.MongoClientSettings) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCredential(com.mongodb.MongoCredential) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) FieldNamingStrategy(org.springframework.data.mapping.model.FieldNamingStrategy) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) ServerSettings(com.mongodb.connection.ServerSettings) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BeanCreationException(org.springframework.beans.factory.BeanCreationException) CollectionUtils(org.apereo.cas.util.CollectionUtils) Jsr310Converters(org.springframework.data.convert.Jsr310Converters) ServerAddress(com.mongodb.ServerAddress) SslSettings(com.mongodb.connection.SslSettings) Converter(org.springframework.core.convert.converter.Converter) Persistent(org.springframework.data.annotation.Persistent) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) lombok.val(lombok.val) TimeUnit(java.util.concurrent.TimeUnit) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) ConnectionString(com.mongodb.ConnectionString) ReadConcernLevel(com.mongodb.ReadConcernLevel) BaseMongoDbProperties(org.apereo.cas.configuration.model.support.mongo.BaseMongoDbProperties) MongoOperations(org.springframework.data.mongodb.core.MongoOperations) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) WriteConcern(com.mongodb.WriteConcern) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) BeanCreationException(org.springframework.beans.factory.BeanCreationException) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ServerAddress(com.mongodb.ServerAddress) ConnectionString(com.mongodb.ConnectionString) ReadConcern(com.mongodb.ReadConcern)

Aggregations

ConnectionString (com.mongodb.ConnectionString)1 MongoClientSettings (com.mongodb.MongoClientSettings)1 MongoCredential (com.mongodb.MongoCredential)1 ReadConcern (com.mongodb.ReadConcern)1 ReadConcernLevel (com.mongodb.ReadConcernLevel)1 ServerAddress (com.mongodb.ServerAddress)1 WriteConcern (com.mongodb.WriteConcern)1 MongoClient (com.mongodb.client.MongoClient)1 MongoClients (com.mongodb.client.MongoClients)1 MongoCollection (com.mongodb.client.MongoCollection)1 ConnectionPoolSettings (com.mongodb.connection.ConnectionPoolSettings)1 ServerSettings (com.mongodb.connection.ServerSettings)1 SocketSettings (com.mongodb.connection.SocketSettings)1 SslSettings (com.mongodb.connection.SslSettings)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1