@@ -202,59 +202,69 @@ public String getMongoClientDatabase() {
202
202
public MongoClient createMongoClient (MongoClientOptions options ,
203
203
Environment environment ) throws UnknownHostException {
204
204
try {
205
- if (hasCustomAddress () || hasCustomCredentials ()) {
206
- if (this .uri != null ) {
207
- throw new IllegalStateException ("Invalid mongo configuration, "
208
- + "either uri or host/port/credentials must be specified" );
209
- }
210
- if (options == null ) {
211
- options = MongoClientOptions .builder ().build ();
212
- }
213
- List <MongoCredential > credentials = new ArrayList <MongoCredential >();
214
- if (hasCustomCredentials ()) {
215
- String database = this .authenticationDatabase == null
216
- ? getMongoClientDatabase () : this .authenticationDatabase ;
217
- credentials .add (MongoCredential .createCredential (this .username ,
218
- database , this .password ));
219
- }
220
- String host = this .host == null ? "localhost" : this .host ;
221
- int port = determinePort (environment );
222
- return new MongoClient (
223
- Collections .singletonList (new ServerAddress (host , port )),
224
- credentials , options );
205
+ Integer embeddedPort = getEmbeddedPort (environment );
206
+ if (embeddedPort != null ) {
207
+ return createEmbeddedMongoClient (options , embeddedPort );
225
208
}
226
- // The options and credentials are in the URI
227
- return new MongoClient (new MongoClientURI (determineUri (), builder (options )));
209
+ return createNetworkMongoClient (options );
228
210
}
229
211
finally {
230
212
clearPassword ();
231
213
}
232
214
}
233
215
234
- private boolean hasCustomAddress () {
235
- return this .host != null || this .port != null ;
216
+ private Integer getEmbeddedPort (Environment environment ) {
217
+ if (environment != null ) {
218
+ String localPort = environment .getProperty ("local.mongo.port" );
219
+ if (localPort != null ) {
220
+ return Integer .valueOf (localPort );
221
+ }
222
+ }
223
+ return null ;
236
224
}
237
225
238
- private boolean hasCustomCredentials () {
239
- return this .username != null && this .password != null ;
226
+ private MongoClient createEmbeddedMongoClient (MongoClientOptions options , int port ) {
227
+ if (options == null ) {
228
+ options = MongoClientOptions .builder ().build ();
229
+ }
230
+ String host = this .host == null ? "localhost" : this .host ;
231
+ return new MongoClient (
232
+ Collections .singletonList (new ServerAddress (host , port )),
233
+ Collections .<MongoCredential >emptyList (), options );
240
234
}
241
235
242
- private int determinePort (Environment environment ) {
243
- if (this .port == null ) {
244
- return DEFAULT_PORT ;
245
- }
246
- if (this .port == 0 ) {
247
- if (environment != null ) {
248
- String localPort = environment .getProperty ("local.mongo.port" );
249
- if (localPort != null ) {
250
- return Integer .valueOf (localPort );
251
- }
236
+ private MongoClient createNetworkMongoClient (MongoClientOptions options ) {
237
+ if (hasCustomAddress () || hasCustomCredentials ()) {
238
+ if (this .uri != null ) {
239
+ throw new IllegalStateException ("Invalid mongo configuration, "
240
+ + "either uri or host/port/credentials must be specified" );
241
+ }
242
+ if (options == null ) {
243
+ options = MongoClientOptions .builder ().build ();
244
+ }
245
+ List <MongoCredential > credentials = new ArrayList <MongoCredential >();
246
+ if (hasCustomCredentials ()) {
247
+ String database = this .authenticationDatabase == null
248
+ ? getMongoClientDatabase () : this .authenticationDatabase ;
249
+ credentials .add (MongoCredential .createCredential (this .username ,
250
+ database , this .password ));
252
251
}
253
- throw new IllegalStateException (
254
- "spring.data.mongodb.port=0 and no local mongo port configuration "
255
- + "is available" );
252
+ String host = this .host == null ? "localhost" : this .host ;
253
+ int port = this .port != null ? this .port : DEFAULT_PORT ;
254
+ return new MongoClient (
255
+ Collections .singletonList (new ServerAddress (host , port )),
256
+ credentials , options );
256
257
}
257
- return this .port ;
258
+ // The options and credentials are in the URI
259
+ return new MongoClient (new MongoClientURI (determineUri (), builder (options )));
260
+ }
261
+
262
+ private boolean hasCustomAddress () {
263
+ return this .host != null || this .port != null ;
264
+ }
265
+
266
+ private boolean hasCustomCredentials () {
267
+ return this .username != null && this .password != null ;
258
268
}
259
269
260
270
private Builder builder (MongoClientOptions options ) {
0 commit comments