|
|
@ -16,7 +16,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.autoconfigure.mongo;
|
|
|
|
package org.springframework.boot.autoconfigure.mongo;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
import com.mongodb.ConnectionString;
|
|
|
|
import com.mongodb.ConnectionString;
|
|
|
|
import com.mongodb.MongoClientSettings;
|
|
|
|
import com.mongodb.MongoClientSettings;
|
|
|
@ -30,6 +31,7 @@ import org.springframework.core.Ordered;
|
|
|
|
* {@link MongoProperties} to a {@link MongoClientSettings}.
|
|
|
|
* {@link MongoProperties} to a {@link MongoClientSettings}.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Scott Frederick
|
|
|
|
* @author Scott Frederick
|
|
|
|
|
|
|
|
* @author Safeer Ansari
|
|
|
|
* @since 2.4.0
|
|
|
|
* @since 2.4.0
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClientSettingsBuilderCustomizer, Ordered {
|
|
|
|
public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClientSettingsBuilderCustomizer, Ordered {
|
|
|
@ -63,12 +65,21 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie
|
|
|
|
String host = getOrDefault(this.properties.getHost(), "localhost");
|
|
|
|
String host = getOrDefault(this.properties.getHost(), "localhost");
|
|
|
|
int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT);
|
|
|
|
int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT);
|
|
|
|
ServerAddress serverAddress = new ServerAddress(host, port);
|
|
|
|
ServerAddress serverAddress = new ServerAddress(host, port);
|
|
|
|
settings.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress)));
|
|
|
|
List<ServerAddress> serverAddressList = new ArrayList<>(List.of(serverAddress));
|
|
|
|
|
|
|
|
applyAdditionalHosts(serverAddressList);
|
|
|
|
|
|
|
|
settings.applyToClusterSettings((cluster) -> cluster.hosts(serverAddressList));
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings.applyConnectionString(new ConnectionString(MongoProperties.DEFAULT_URI));
|
|
|
|
settings.applyConnectionString(new ConnectionString(MongoProperties.DEFAULT_URI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void applyAdditionalHosts(List<ServerAddress> serverAddressList) {
|
|
|
|
|
|
|
|
if (this.properties.getAdditionalHosts() != null && !this.properties.getAdditionalHosts().isEmpty()) {
|
|
|
|
|
|
|
|
this.properties.getAdditionalHosts()
|
|
|
|
|
|
|
|
.forEach((additionalHost) -> serverAddressList.add(new ServerAddress(additionalHost)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void applyCredentials(MongoClientSettings.Builder builder) {
|
|
|
|
private void applyCredentials(MongoClientSettings.Builder builder) {
|
|
|
|
if (this.properties.getUri() == null && this.properties.getUsername() != null
|
|
|
|
if (this.properties.getUri() == null && this.properties.getUsername() != null
|
|
|
|
&& this.properties.getPassword() != null) {
|
|
|
|
&& this.properties.getPassword() != null) {
|
|
|
|