Search in sources :

Example 1 with UuidRepresentation

use of org.bson.UuidRepresentation in project mongo-java-driver by mongodb.

the class MongoClientURI method getOptions.

/**
 * Gets the options
 *
 * @return the MongoClientOptions based on this URI.
 */
public MongoClientOptions getOptions() {
    ReadPreference readPreference = proxied.getReadPreference();
    if (readPreference != null) {
        builder.readPreference(readPreference);
    }
    ReadConcern readConcern = proxied.getReadConcern();
    if (readConcern != null) {
        builder.readConcern(readConcern);
    }
    WriteConcern writeConcern = proxied.getWriteConcern();
    if (writeConcern != null) {
        builder.writeConcern(writeConcern);
    }
    Boolean retryWritesValue = proxied.getRetryWritesValue();
    if (retryWritesValue != null) {
        builder.retryWrites(retryWritesValue);
    }
    Boolean retryReads = proxied.getRetryReads();
    if (retryReads != null) {
        builder.retryReads(retryReads);
    }
    Integer maxConnectionPoolSize = proxied.getMaxConnectionPoolSize();
    if (maxConnectionPoolSize != null) {
        builder.connectionsPerHost(maxConnectionPoolSize);
    }
    Integer integer = proxied.getMinConnectionPoolSize();
    if (integer != null) {
        builder.minConnectionsPerHost(integer);
    }
    Integer maxWaitTime = proxied.getMaxWaitTime();
    if (maxWaitTime != null) {
        builder.maxWaitTime(maxWaitTime);
    }
    Integer maxConnectionIdleTime = proxied.getMaxConnectionIdleTime();
    if (maxConnectionIdleTime != null) {
        builder.maxConnectionIdleTime(maxConnectionIdleTime);
    }
    Integer maxConnectionLifeTime = proxied.getMaxConnectionLifeTime();
    if (maxConnectionLifeTime != null) {
        builder.maxConnectionLifeTime(maxConnectionLifeTime);
    }
    Integer maxConnecting = proxied.getMaxConnecting();
    if (maxConnecting != null) {
        builder.maxConnecting(maxConnecting);
    }
    Integer socketTimeout = proxied.getSocketTimeout();
    if (socketTimeout != null) {
        builder.socketTimeout(socketTimeout);
    }
    Integer connectTimeout = proxied.getConnectTimeout();
    if (connectTimeout != null) {
        builder.connectTimeout(connectTimeout);
    }
    String requiredReplicaSetName = proxied.getRequiredReplicaSetName();
    if (requiredReplicaSetName != null) {
        builder.requiredReplicaSetName(requiredReplicaSetName);
    }
    Boolean sslEnabled = proxied.getSslEnabled();
    if (sslEnabled != null) {
        builder.sslEnabled(sslEnabled);
    }
    Boolean sslInvalidHostnameAllowed = proxied.getSslInvalidHostnameAllowed();
    if (sslInvalidHostnameAllowed != null) {
        builder.sslInvalidHostNameAllowed(sslInvalidHostnameAllowed);
    }
    Integer serverSelectionTimeout = proxied.getServerSelectionTimeout();
    if (serverSelectionTimeout != null) {
        builder.serverSelectionTimeout(serverSelectionTimeout);
    }
    Integer localThreshold = proxied.getLocalThreshold();
    if (localThreshold != null) {
        builder.localThreshold(localThreshold);
    }
    Integer heartbeatFrequency = proxied.getHeartbeatFrequency();
    if (heartbeatFrequency != null) {
        builder.heartbeatFrequency(heartbeatFrequency);
    }
    String applicationName = proxied.getApplicationName();
    if (applicationName != null) {
        builder.applicationName(applicationName);
    }
    if (!proxied.getCompressorList().isEmpty()) {
        builder.compressorList(proxied.getCompressorList());
    }
    UuidRepresentation uuidRepresentation = proxied.getUuidRepresentation();
    if (uuidRepresentation != null) {
        builder.uuidRepresentation(uuidRepresentation);
    }
    Integer srvMaxHosts = proxied.getSrvMaxHosts();
    if (srvMaxHosts != null) {
        builder.srvMaxHosts(srvMaxHosts);
    }
    String srvServiceName = proxied.getSrvServiceName();
    if (srvServiceName != null) {
        builder.srvServiceName(srvServiceName);
    }
    return builder.build();
}
Also used : UuidRepresentation(org.bson.UuidRepresentation)

Example 2 with UuidRepresentation

use of org.bson.UuidRepresentation in project spring-data-mongodb by spring-projects.

the class MongoClientFactoryBean method computeClientSetting.

/**
 * Create {@link MongoClientSettings} based on configuration and priority (lower is better).
 * <ol>
 * <li>{@link MongoClientFactoryBean#mongoClientSettings}</li>
 * <li>{@link MongoClientFactoryBean#connectionString}</li>
 * <li>default {@link MongoClientSettings}</li>
 * </ol>
 *
 * @since 3.0
 */
protected MongoClientSettings computeClientSetting() {
    if (connectionString != null && (StringUtils.hasText(host) || port != null)) {
        throw new IllegalStateException("ConnectionString and host/port configuration exclude one another!");
    }
    ConnectionString connectionString = this.connectionString != null ? this.connectionString : new ConnectionString(String.format("mongodb://%s:%s", getOrDefault(host, ServerAddress.defaultHost()), getOrDefault(port, "" + ServerAddress.defaultPort())));
    Builder builder = MongoClientSettings.builder().applyConnectionString(connectionString);
    builder.uuidRepresentation(UuidRepresentation.JAVA_LEGACY);
    if (mongoClientSettings != null) {
        MongoClientSettings defaultSettings = MongoClientSettings.builder().build();
        SslSettings sslSettings = mongoClientSettings.getSslSettings();
        ClusterSettings clusterSettings = mongoClientSettings.getClusterSettings();
        ConnectionPoolSettings connectionPoolSettings = mongoClientSettings.getConnectionPoolSettings();
        SocketSettings socketSettings = mongoClientSettings.getSocketSettings();
        ServerSettings serverSettings = mongoClientSettings.getServerSettings();
        builder = // 
        builder.applicationName(computeSettingsValue(defaultSettings.getApplicationName(), mongoClientSettings.getApplicationName(), // 
        connectionString.getApplicationName())).applyToSslSettings(settings -> {
            applySettings(settings::enabled, computeSettingsValue(SslSettings::isEnabled, defaultSettings.getSslSettings(), sslSettings, connectionString.getSslEnabled()));
            applySettings(settings::invalidHostNameAllowed, (computeSettingsValue(SslSettings::isInvalidHostNameAllowed, defaultSettings.getSslSettings(), sslSettings, connectionString.getSslInvalidHostnameAllowed())));
            settings.context(sslSettings.getContext());
        }).applyToClusterSettings(settings -> {
            applySettings(settings::hosts, computeSettingsValue(ClusterSettings::getHosts, defaultSettings.getClusterSettings(), clusterSettings, connectionString.getHosts().stream().map(ServerAddress::new).collect(Collectors.toList())));
            applySettings(settings::requiredReplicaSetName, computeSettingsValue(ClusterSettings::getRequiredReplicaSetName, defaultSettings.getClusterSettings(), clusterSettings, connectionString.getRequiredReplicaSetName()));
            applySettings(settings::srvHost, computeSettingsValue(ClusterSettings::getSrvHost, defaultSettings.getClusterSettings(), clusterSettings, null));
            applySettings(settings::mode, computeSettingsValue(ClusterSettings::getMode, defaultSettings.getClusterSettings(), clusterSettings, null));
            applySettings(it -> settings.localThreshold(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ClusterSettings it) -> it.getLocalThreshold(TimeUnit.MILLISECONDS), defaultSettings.getClusterSettings(), clusterSettings, connectionString.getLocalThreshold()));
            applySettings(settings::requiredClusterType, computeSettingsValue(ClusterSettings::getRequiredClusterType, defaultSettings.getClusterSettings(), clusterSettings, null));
            applySettings(it -> settings.serverSelectionTimeout(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ClusterSettings it) -> it.getServerSelectionTimeout(TimeUnit.MILLISECONDS), defaultSettings.getClusterSettings(), clusterSettings, connectionString.getServerSelectionTimeout()));
            applySettings(settings::serverSelector, computeSettingsValue(ClusterSettings::getServerSelector, defaultSettings.getClusterSettings(), clusterSettings, null));
            List<ClusterListener> clusterListeners = computeSettingsValue(ClusterSettings::getClusterListeners, defaultSettings.getClusterSettings(), clusterSettings, null);
            if (clusterListeners != null) {
                clusterListeners.forEach(settings::addClusterListener);
            }
        }).applyToConnectionPoolSettings(settings -> {
            applySettings(it -> settings.maintenanceFrequency(it, TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaintenanceFrequency(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, null));
            applySettings(it -> settings.maxConnectionIdleTime(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaxConnectionIdleTime(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMaxConnectionIdleTime()));
            applySettings(it -> settings.maxConnectionLifeTime(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaxConnectionLifeTime(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMaxConnectionLifeTime()));
            applySettings(it -> settings.maxWaitTime(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaxWaitTime(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMaxWaitTime()));
            applySettings(it -> settings.maintenanceInitialDelay(it, TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaintenanceInitialDelay(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, null));
            applySettings(settings::minSize, computeSettingsValue(ConnectionPoolSettings::getMinSize, defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMinConnectionPoolSize()));
            applySettings(settings::maxSize, computeSettingsValue(ConnectionPoolSettings::getMaxSize, defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMaxConnectionPoolSize()));
        }).applyToSocketSettings(settings -> {
            applySettings(it -> settings.connectTimeout(it, TimeUnit.MILLISECONDS), computeSettingsValue((SocketSettings it) -> it.getConnectTimeout(TimeUnit.MILLISECONDS), defaultSettings.getSocketSettings(), socketSettings, connectionString.getConnectTimeout()));
            applySettings(it -> settings.readTimeout(it, TimeUnit.MILLISECONDS), computeSettingsValue((SocketSettings it) -> it.getReadTimeout(TimeUnit.MILLISECONDS), defaultSettings.getSocketSettings(), socketSettings, connectionString.getSocketTimeout()));
            applySettings(settings::receiveBufferSize, computeSettingsValue(SocketSettings::getReceiveBufferSize, defaultSettings.getSocketSettings(), socketSettings, null));
            applySettings(settings::sendBufferSize, computeSettingsValue(SocketSettings::getSendBufferSize, defaultSettings.getSocketSettings(), socketSettings, null));
        }).applyToServerSettings(settings -> {
            applySettings(it -> settings.minHeartbeatFrequency(it.intValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ServerSettings it) -> it.getMinHeartbeatFrequency(TimeUnit.MILLISECONDS), defaultSettings.getServerSettings(), serverSettings, null));
            applySettings(it -> settings.heartbeatFrequency(it.intValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ServerSettings it) -> it.getHeartbeatFrequency(TimeUnit.MILLISECONDS), defaultSettings.getServerSettings(), serverSettings, connectionString.getHeartbeatFrequency()));
            settings.applySettings(serverSettings);
        }).autoEncryptionSettings(// 
        mongoClientSettings.getAutoEncryptionSettings()).codecRegistry(// 
        mongoClientSettings.getCodecRegistry());
        applySettings(builder::readConcern, computeSettingsValue(defaultSettings.getReadConcern(), mongoClientSettings.getReadConcern(), connectionString.getReadConcern()));
        applySettings(builder::writeConcern, computeSettingsValue(defaultSettings.getWriteConcern(), mongoClientSettings.getWriteConcern(), connectionString.getWriteConcern()));
        applySettings(builder::readPreference, computeSettingsValue(defaultSettings.getReadPreference(), mongoClientSettings.getReadPreference(), connectionString.getReadPreference()));
        applySettings(builder::retryReads, computeSettingsValue(defaultSettings.getRetryReads(), mongoClientSettings.getRetryReads(), connectionString.getRetryReads()));
        applySettings(builder::retryWrites, computeSettingsValue(defaultSettings.getRetryWrites(), mongoClientSettings.getRetryWrites(), connectionString.getRetryWritesValue()));
        applySettings(builder::uuidRepresentation, computeSettingsValue(null, mongoClientSettings.getUuidRepresentation(), UuidRepresentation.JAVA_LEGACY));
    }
    if (!CollectionUtils.isEmpty(credential)) {
        builder = builder.credential(credential.iterator().next());
    }
    if (StringUtils.hasText(replicaSet)) {
        builder.applyToClusterSettings((settings) -> {
            settings.requiredReplicaSetName(replicaSet);
        });
    }
    return builder.build();
}
Also used : ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) Arrays(java.util.Arrays) DataAccessException(org.springframework.dao.DataAccessException) MongoCredential(com.mongodb.MongoCredential) MongoClient(com.mongodb.client.MongoClient) ClusterSettings(com.mongodb.connection.ClusterSettings) Function(java.util.function.Function) ServerSettings(com.mongodb.connection.ServerSettings) AbstractFactoryBean(org.springframework.beans.factory.config.AbstractFactoryBean) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) SocketSettings(com.mongodb.connection.SocketSettings) Nullable(org.springframework.lang.Nullable) ServerAddress(com.mongodb.ServerAddress) SslSettings(com.mongodb.connection.SslSettings) ObjectUtils(org.springframework.util.ObjectUtils) MongoClients(com.mongodb.client.MongoClients) ClusterListener(com.mongodb.event.ClusterListener) UuidRepresentation(org.bson.UuidRepresentation) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Builder(com.mongodb.MongoClientSettings.Builder) List(java.util.List) ConnectionString(com.mongodb.ConnectionString) CollectionUtils(org.springframework.util.CollectionUtils) MongoClientSettings(com.mongodb.MongoClientSettings) SpringDataMongoDB(org.springframework.data.mongodb.SpringDataMongoDB) StringUtils(org.springframework.util.StringUtils) ClusterSettings(com.mongodb.connection.ClusterSettings) Builder(com.mongodb.MongoClientSettings.Builder) MongoClientSettings(com.mongodb.MongoClientSettings) SocketSettings(com.mongodb.connection.SocketSettings) SslSettings(com.mongodb.connection.SslSettings) ServerSettings(com.mongodb.connection.ServerSettings) ClusterListener(com.mongodb.event.ClusterListener) ConnectionString(com.mongodb.ConnectionString) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings)

Example 3 with UuidRepresentation

use of org.bson.UuidRepresentation in project spring-data-mongodb by spring-projects.

the class MongoClientSettingsFactoryBean method createInstance.

@Override
protected MongoClientSettings createInstance() {
    Builder builder = // 
    MongoClientSettings.builder().readPreference(// 
    readPreference).writeConcern(// 
    writeConcern).readConcern(// 
    readConcern).codecRegistry(// 
    codecRegistry).applicationName(// 
    applicationName).autoEncryptionSettings(// 
    autoEncryptionSettings).applyToClusterSettings((settings) -> {
        settings.serverSelectionTimeout(clusterServerSelectionTimeoutMS, TimeUnit.MILLISECONDS);
        if (clusterConnectionMode != null) {
            settings.mode(clusterConnectionMode);
        }
        settings.requiredReplicaSetName(clusterRequiredReplicaSetName);
        if (!CollectionUtils.isEmpty(clusterHosts)) {
            settings.hosts(clusterHosts);
        }
        settings.localThreshold(clusterLocalThresholdMS, TimeUnit.MILLISECONDS);
        // settings.maxWaitQueueSize(clusterMaxWaitQueueSize);
        settings.requiredClusterType(custerRequiredClusterType);
        if (StringUtils.hasText(clusterSrvHost)) {
            settings.srvHost(clusterSrvHost);
        }
    }).applyToConnectionPoolSettings((settings) -> {
        settings.minSize(poolMinSize);
        settings.maxSize(poolMaxSize);
        settings.maxConnectionIdleTime(poolMaxConnectionIdleTimeMS, TimeUnit.MILLISECONDS);
        settings.maxWaitTime(poolMaxWaitTimeMS, TimeUnit.MILLISECONDS);
        settings.maxConnectionLifeTime(poolMaxConnectionLifeTimeMS, TimeUnit.MILLISECONDS);
        // settings.maxWaitQueueSize(poolMaxWaitQueueSize);
        settings.maintenanceFrequency(poolMaintenanceFrequencyMS, TimeUnit.MILLISECONDS);
        settings.maintenanceInitialDelay(poolMaintenanceInitialDelayMS, TimeUnit.MILLISECONDS);
    }).applyToServerSettings((settings) -> {
        settings.minHeartbeatFrequency(serverMinHeartbeatFrequencyMS, TimeUnit.MILLISECONDS);
        settings.heartbeatFrequency(serverHeartbeatFrequencyMS, TimeUnit.MILLISECONDS);
    }).applyToSocketSettings((settings) -> {
        settings.connectTimeout(socketConnectTimeoutMS, TimeUnit.MILLISECONDS);
        settings.readTimeout(socketReadTimeoutMS, TimeUnit.MILLISECONDS);
        settings.receiveBufferSize(socketReceiveBufferSize);
        settings.sendBufferSize(socketSendBufferSize);
    }).applyToSslSettings((settings) -> {
        settings.enabled(sslEnabled);
        if (sslEnabled) {
            settings.invalidHostNameAllowed(sslInvalidHostNameAllowed);
            try {
                settings.context(StringUtils.hasText(sslProvider) ? SSLContext.getInstance(sslProvider) : SSLContext.getDefault());
            } catch (NoSuchAlgorithmException e) {
                throw new IllegalArgumentException(e.getMessage(), e);
            }
        }
    });
    if (streamFactoryFactory != null) {
        builder = builder.streamFactoryFactory(streamFactoryFactory);
    }
    if (retryReads != null) {
        builder = builder.retryReads(retryReads);
    }
    if (retryWrites != null) {
        builder = builder.retryWrites(retryWrites);
    }
    if (uUidRepresentation != null) {
        builder = builder.uuidRepresentation(uUidRepresentation);
    }
    if (serverApi != null) {
        builder = builder.serverApi(serverApi);
    }
    return builder.build();
}
Also used : ReadPreference(com.mongodb.ReadPreference) ServerAddress(com.mongodb.ServerAddress) Arrays(java.util.Arrays) SSLContext(javax.net.ssl.SSLContext) ServerApi(com.mongodb.ServerApi) ClusterType(com.mongodb.connection.ClusterType) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) UuidRepresentation(org.bson.UuidRepresentation) AbstractFactoryBean(org.springframework.beans.factory.config.AbstractFactoryBean) TimeUnit(java.util.concurrent.TimeUnit) Builder(com.mongodb.MongoClientSettings.Builder) StreamFactoryFactory(com.mongodb.connection.StreamFactoryFactory) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Nullable(org.springframework.lang.Nullable) MongoClientSettings(com.mongodb.MongoClientSettings) ReadConcern(com.mongodb.ReadConcern) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) Builder(com.mongodb.MongoClientSettings.Builder) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

UuidRepresentation (org.bson.UuidRepresentation)3 MongoClientSettings (com.mongodb.MongoClientSettings)2 Builder (com.mongodb.MongoClientSettings.Builder)2 ServerAddress (com.mongodb.ServerAddress)2 Arrays (java.util.Arrays)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 AbstractFactoryBean (org.springframework.beans.factory.config.AbstractFactoryBean)2 Nullable (org.springframework.lang.Nullable)2 CollectionUtils (org.springframework.util.CollectionUtils)2 StringUtils (org.springframework.util.StringUtils)2 AutoEncryptionSettings (com.mongodb.AutoEncryptionSettings)1 ConnectionString (com.mongodb.ConnectionString)1 MongoCredential (com.mongodb.MongoCredential)1 ReadConcern (com.mongodb.ReadConcern)1 ReadPreference (com.mongodb.ReadPreference)1 ServerApi (com.mongodb.ServerApi)1 WriteConcern (com.mongodb.WriteConcern)1 MongoClient (com.mongodb.client.MongoClient)1 MongoClients (com.mongodb.client.MongoClients)1