-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
ReactiveMongoClientFactory.java
209 lines (180 loc) · 7.29 KB
/
ReactiveMongoClientFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.mongo;
import java.util.Collections;
import java.util.List;
import com.mongodb.ConnectionString;
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;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
/**
* A factory for a reactive {@link MongoClient} that applies {@link MongoProperties}.
*
* @author Mark Paluch
* @author Stephane Nicoll
* @since 2.0.0
*/
public class ReactiveMongoClientFactory {
private final MongoProperties properties;
private final Environment environment;
private final List<MongoClientSettingsBuilderCustomizer> builderCustomizers;
public ReactiveMongoClientFactory(MongoProperties properties, Environment environment,
List<MongoClientSettingsBuilderCustomizer> builderCustomizers) {
this.properties = properties;
this.environment = environment;
this.builderCustomizers = (builderCustomizers != null) ? builderCustomizers
: Collections.emptyList();
}
/**
* Creates a {@link MongoClient} using the given {@code settings}. If the environment
* contains a {@code local.mongo.port} property, it is used to configure a client to
* an embedded MongoDB instance.
* @param settings the settings
* @return the Mongo client
*/
public MongoClient createMongoClient(MongoClientSettings settings) {
Integer embeddedPort = getEmbeddedPort();
if (embeddedPort != null) {
return createEmbeddedMongoClient(settings, embeddedPort);
}
return createNetworkMongoClient(settings);
}
private Integer getEmbeddedPort() {
if (this.environment != null) {
String localPort = this.environment.getProperty("local.mongo.port");
if (localPort != null) {
return Integer.valueOf(localPort);
}
}
return null;
}
private MongoClient createEmbeddedMongoClient(MongoClientSettings settings,
int port) {
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);
return createMongoClient(builder);
}
private MongoClient createNetworkMongoClient(MongoClientSettings settings) {
if (hasCustomAddress() || hasCustomCredentials()) {
return createCredentialNetworkMongoClient(settings);
}
ConnectionString connectionString = new ConnectionString(
this.properties.determineUri());
return createMongoClient(createBuilder(settings, connectionString));
}
private MongoClient createCredentialNetworkMongoClient(MongoClientSettings settings) {
Assert.state(this.properties.getUri() == null, "Invalid mongo configuration, "
+ "either uri or host/port/credentials must be specified");
Builder builder = builder(settings);
if (hasCustomCredentials()) {
applyCredentials(builder);
}
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());
return createMongoClient(builder);
}
private void applyCredentials(Builder builder) {
String database = (this.properties.getAuthenticationDatabase() != null)
? this.properties.getAuthenticationDatabase()
: this.properties.getMongoClientDatabase();
builder.credential((MongoCredential.createCredential(
this.properties.getUsername(), database, this.properties.getPassword())));
}
private <T> T getOrDefault(T value, T defaultValue) {
return (value != null) ? value : defaultValue;
}
private MongoClient createMongoClient(Builder builder) {
customize(builder);
return MongoClients.create(builder.build());
}
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();
}
private void customize(MongoClientSettings.Builder builder) {
for (MongoClientSettingsBuilderCustomizer customizer : this.builderCustomizers) {
customizer.customize(builder);
}
}
private boolean hasCustomAddress() {
return this.properties.getHost() != null || this.properties.getPort() != null;
}
private boolean hasCustomCredentials() {
return this.properties.getUsername() != null
&& this.properties.getPassword() != null;
}
private Builder builder(MongoClientSettings settings) {
if (settings == null) {
return MongoClientSettings.builder();
}
return MongoClientSettings.builder(settings);
}
}