Skip to content

Commit

Permalink
Fix use of deprecated Mongo API
Browse files Browse the repository at this point in the history
We now use com.mongodb.MongoClientSettings to configure the reactive
MongoDB driver. This is a breaking change as
MongoClientSettingsBuilderCustomizer and user-provided
MongoClientSettings beans referenced the package the settings type
from com.mongodb.async.client.

MongoClient.getSettings() is deprecated and still in use within tests
until a replacement is available.

See spring-projectsgh-14318
  • Loading branch information
mp911de authored and snicoll committed Sep 6, 2018
1 parent f3ece97 commit b97892f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,11 +16,11 @@

package org.springframework.boot.autoconfigure.mongo;

import com.mongodb.async.client.MongoClientSettings.Builder;
import com.mongodb.MongoClientSettings.Builder;

/**
* Callback interface that can be implemented by beans wishing to customize the
* {@link com.mongodb.async.client.MongoClientSettings} via a {@link Builder
* {@link com.mongodb.MongoClientSettings} via a {@link Builder
* MongoClientSettings.Builder} whilst retaining default auto-configuration.
*
* @author Mark Paluch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import javax.annotation.PreDestroy;

import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.MongoClientSettings;
import com.mongodb.connection.netty.NettyStreamFactoryFactory;
import com.mongodb.reactivestreams.client.MongoClient;
import io.netty.channel.socket.SocketChannel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@
import java.util.List;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoClientSettings.Builder;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.async.client.MongoClientSettings.Builder;
import com.mongodb.connection.ClusterSettings;
import com.mongodb.connection.ConnectionPoolSettings;
import com.mongodb.connection.ServerSettings;
import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.SslSettings;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;

Expand Down Expand Up @@ -88,9 +83,8 @@ private MongoClient createEmbeddedMongoClient(MongoClientSettings settings,
Builder builder = builder(settings);
String host = (this.properties.getHost() != null) ? this.properties.getHost()
: "localhost";
ClusterSettings clusterSettings = ClusterSettings.builder()
.hosts(Collections.singletonList(new ServerAddress(host, port))).build();
builder.clusterSettings(clusterSettings);
builder.applyToClusterSettings(cluster -> cluster
.hosts(Collections.singletonList(new ServerAddress(host, port))));
return createMongoClient(builder);
}

Expand All @@ -113,8 +107,8 @@ private MongoClient createCredentialNetworkMongoClient(MongoClientSettings setti
String host = getOrDefault(this.properties.getHost(), "localhost");
int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT);
ServerAddress serverAddress = new ServerAddress(host, port);
builder.clusterSettings(ClusterSettings.builder()
.hosts(Collections.singletonList(serverAddress)).build());
builder.applyToClusterSettings(
cluster -> cluster.hosts(Collections.singletonList(serverAddress)));
return createMongoClient(builder);
}

Expand All @@ -124,7 +118,6 @@ private void applyCredentials(Builder builder) {
: this.properties.getMongoClientDatabase();
builder.credential((MongoCredential.createCredential(
this.properties.getUsername(), database, this.properties.getPassword())));

}

private <T> T getOrDefault(T value, T defaultValue) {
Expand All @@ -138,50 +131,7 @@ private MongoClient createMongoClient(Builder builder) {

private Builder createBuilder(MongoClientSettings settings,
ConnectionString connection) {
Builder builder = builder(settings);
builder.clusterSettings(getClusterSettings(connection));
builder.connectionPoolSettings(getConnectionPoolSettings(connection));
builder.serverSettings(getServerSettings(connection));
if (connection.getCredential() != null) {
builder.credential(connection.getCredential());
}
builder.sslSettings(getSslSettings(connection));
builder.socketSettings(getSocketSettings(connection));
if (connection.getReadPreference() != null) {
builder.readPreference(connection.getReadPreference());
}
if (connection.getReadConcern() != null) {
builder.readConcern(connection.getReadConcern());
}
if (connection.getWriteConcern() != null) {
builder.writeConcern(connection.getWriteConcern());
}
if (connection.getApplicationName() != null) {
builder.applicationName(connection.getApplicationName());
}
builder.retryWrites(connection.getRetryWrites());
return builder;
}

private ClusterSettings getClusterSettings(ConnectionString connection) {
return ClusterSettings.builder().applyConnectionString(connection).build();
}

private ConnectionPoolSettings getConnectionPoolSettings(
ConnectionString connection) {
return ConnectionPoolSettings.builder().applyConnectionString(connection).build();
}

private ServerSettings getServerSettings(ConnectionString connection) {
return ServerSettings.builder().applyConnectionString(connection).build();
}

private SslSettings getSslSettings(ConnectionString connection) {
return SslSettings.builder().applyConnectionString(connection).build();
}

private SocketSettings getSocketSettings(ConnectionString connection) {
return SocketSettings.builder().applyConnectionString(connection).build();
return builder(settings).applyConnectionString(connection);
}

private void customize(MongoClientSettings.Builder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import java.util.concurrent.TimeUnit;

import com.mongodb.MongoClientSettings;
import com.mongodb.ReadPreference;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.connection.AsynchronousSocketChannelStreamFactoryFactory;
import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.StreamFactory;
import com.mongodb.connection.StreamFactoryFactory;
import com.mongodb.connection.netty.NettyStreamFactoryFactory;
Expand Down Expand Up @@ -83,7 +82,8 @@ public void optionsSslConfig() {
.withUserConfiguration(SslOptionsConfig.class).run((context) -> {
assertThat(context).hasSingleBean(MongoClient.class);
MongoClient mongo = context.getBean(MongoClient.class);
MongoClientSettings settings = mongo.getSettings();
com.mongodb.async.client.MongoClientSettings settings = mongo
.getSettings();
assertThat(settings.getApplicationName()).isEqualTo("test-config");
assertThat(settings.getStreamFactoryFactory())
.isSameAs(context.getBean("myStreamFactoryFactory"));
Expand Down Expand Up @@ -120,8 +120,8 @@ static class OptionsConfig {
@Bean
public MongoClientSettings mongoClientSettings() {
return MongoClientSettings.builder().readPreference(ReadPreference.nearest())
.socketSettings(SocketSettings.builder()
.readTimeout(300, TimeUnit.SECONDS).build())
.applyToSocketSettings(
socket -> socket.readTimeout(300, TimeUnit.SECONDS))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.MongoClientSettings;
import com.mongodb.connection.ClusterSettings;
import com.mongodb.reactivestreams.client.MongoClient;
import org.junit.Rule;
Expand Down Expand Up @@ -196,13 +196,13 @@ private MongoClient createMongoClient(MongoProperties properties,
}

private List<ServerAddress> extractServerAddresses(MongoClient client) {
MongoClientSettings settings = client.getSettings();
com.mongodb.async.client.MongoClientSettings settings = client.getSettings();
ClusterSettings clusterSettings = settings.getClusterSettings();
return clusterSettings.getHosts();
}

private MongoCredential extractMongoCredentials(MongoClient client) {
MongoClientSettings settings = client.getSettings();
com.mongodb.async.client.MongoClientSettings settings = client.getSettings();
return settings.getCredential();
}

Expand Down

0 comments on commit b97892f

Please sign in to comment.