Polish
This commit is contained in:
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.boot.autoconfigure.mongo;
|
package org.springframework.boot.autoconfigure.mongo;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -89,39 +88,48 @@ public class MongoClientFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private MongoClient createNetworkMongoClient(MongoClientOptions options) {
|
private MongoClient createNetworkMongoClient(MongoClientOptions options) {
|
||||||
if (this.properties.getUri() != null) {
|
MongoProperties properties = this.properties;
|
||||||
return new MongoClient(
|
if (properties.getUri() != null) {
|
||||||
new MongoClientURI(this.properties.getUri(), builder(options)));
|
return createMongoClient(properties.getUri(), options);
|
||||||
}
|
}
|
||||||
if (hasCustomAddress() || hasCustomCredentials()) {
|
if (hasCustomAddress() || hasCustomCredentials()) {
|
||||||
if (options == null) {
|
if (options == null) {
|
||||||
options = MongoClientOptions.builder().build();
|
options = MongoClientOptions.builder().build();
|
||||||
}
|
}
|
||||||
List<MongoCredential> credentials = new ArrayList<>();
|
List<MongoCredential> credentials = getCredentials(properties);
|
||||||
if (hasCustomCredentials()) {
|
String host = getValue(properties.getHost(), "localhost");
|
||||||
String database = this.properties.getAuthenticationDatabase() == null
|
int port = getValue(properties.getPort(), MongoProperties.DEFAULT_PORT);
|
||||||
? this.properties.getMongoClientDatabase()
|
List<ServerAddress> seeds = Collections
|
||||||
: this.properties.getAuthenticationDatabase();
|
.singletonList(new ServerAddress(host, port));
|
||||||
credentials.add(
|
return new MongoClient(seeds, credentials, options);
|
||||||
MongoCredential.createCredential(this.properties.getUsername(),
|
|
||||||
database, this.properties.getPassword()));
|
|
||||||
}
|
|
||||||
String host = this.properties.getHost() == null ? "localhost"
|
|
||||||
: this.properties.getHost();
|
|
||||||
int port = this.properties.getPort() != null ? this.properties.getPort()
|
|
||||||
: MongoProperties.DEFAULT_PORT;
|
|
||||||
return new MongoClient(
|
|
||||||
Collections.singletonList(new ServerAddress(host, port)), credentials,
|
|
||||||
options);
|
|
||||||
}
|
}
|
||||||
return new MongoClient(
|
return createMongoClient(MongoProperties.DEFAULT_URI, options);
|
||||||
new MongoClientURI(MongoProperties.DEFAULT_URI, builder(options)));
|
}
|
||||||
|
|
||||||
|
private MongoClient createMongoClient(String uri, MongoClientOptions options) {
|
||||||
|
return new MongoClient(new MongoClientURI(uri, builder(options)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T getValue(T value, T fallback) {
|
||||||
|
return (value == null ? fallback : value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasCustomAddress() {
|
private boolean hasCustomAddress() {
|
||||||
return this.properties.getHost() != null || this.properties.getPort() != null;
|
return this.properties.getHost() != null || this.properties.getPort() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<MongoCredential> getCredentials(MongoProperties properties) {
|
||||||
|
if (!hasCustomCredentials()) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
String username = properties.getUsername();
|
||||||
|
String database = getValue(properties.getAuthenticationDatabase(),
|
||||||
|
properties.getMongoClientDatabase());
|
||||||
|
char[] password = properties.getPassword();
|
||||||
|
return Collections.singletonList(
|
||||||
|
MongoCredential.createCredential(username, database, password));
|
||||||
|
}
|
||||||
|
|
||||||
private boolean hasCustomCredentials() {
|
private boolean hasCustomCredentials() {
|
||||||
return this.properties.getUsername() != null
|
return this.properties.getUsername() != null
|
||||||
&& this.properties.getPassword() != null;
|
&& this.properties.getPassword() != null;
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ public class JsonbAutoConfigurationTests {
|
|||||||
|
|
||||||
public class DataObject {
|
public class DataObject {
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
private String data = "hello";
|
private String data = "hello";
|
||||||
|
|
||||||
public String getData() {
|
public String getData() {
|
||||||
|
|||||||
@@ -50,7 +50,6 @@
|
|||||||
<addTestClassPath>true</addTestClassPath>
|
<addTestClassPath>true</addTestClassPath>
|
||||||
<skipInvocation>${skipTests}</skipInvocation>
|
<skipInvocation>${skipTests}</skipInvocation>
|
||||||
<streamLogs>true</streamLogs>
|
<streamLogs>true</streamLogs>
|
||||||
<!-- <properties> <revision>${revision}</revision> </properties> -->
|
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
|
|||||||
Reference in New Issue
Block a user