Skip to content

Commit aa8a99c

Browse files
committed
JAVA-2656: Deprecate MongoClient constructors that allow more than one credential to be used, and add otherwise equivalent constructors that take a single credential
1 parent 94d9d5c commit aa8a99c

File tree

2 files changed

+85
-12
lines changed

2 files changed

+85
-12
lines changed

driver/src/main/com/mongodb/MongoClient.java

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import static com.mongodb.assertions.Assertions.notNull;
3838
import static java.util.Arrays.asList;
39+
import static java.util.Collections.singletonList;
3940
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
4041

4142
/**
@@ -47,7 +48,7 @@
4748
* MongoClient mongoClient1 = new MongoClient("localhost");
4849
* MongoClient mongoClient2 = new MongoClient("localhost", 27017);
4950
* MongoClient mongoClient4 = new MongoClient(new ServerAddress("localhost"));
50-
* MongoClient mongoClient5 = new MongoClient(new ServerAddress("localhost"), new MongoClientOptions.Builder().build());
51+
* MongoClient mongoClient5 = new MongoClient(new ServerAddress("localhost"), MongoClientOptions.builder().build());
5152
* </pre>
5253
* <p>You can connect to a <a href="http://www.mongodb.org/display/DOCS/Replica+Sets">replica set</a> using the Java driver by passing a
5354
* ServerAddress list to the MongoClient constructor. For example:</p>
@@ -171,9 +172,11 @@ public MongoClient(final ServerAddress addr) {
171172
* @param credentialsList the list of credentials used to authenticate all connections
172173
* @see com.mongodb.ServerAddress
173174
* @since 2.11.0
175+
* @deprecated Prefer {@link #MongoClient(ServerAddress, MongoCredential, MongoClientOptions)}
174176
*/
177+
@Deprecated
175178
public MongoClient(final ServerAddress addr, final List<MongoCredential> credentialsList) {
176-
this(addr, credentialsList, new MongoClientOptions.Builder().build());
179+
this(addr, credentialsList, MongoClientOptions.builder().build());
177180
}
178181

179182
/**
@@ -195,11 +198,26 @@ public MongoClient(final ServerAddress addr, final MongoClientOptions options) {
195198
* @param options default options
196199
* @see com.mongodb.ServerAddress
197200
* @since 2.11.0
201+
* @deprecated Prefer {@link #MongoClient(ServerAddress, MongoCredential, MongoClientOptions)}
198202
*/
203+
@Deprecated
199204
public MongoClient(final ServerAddress addr, final List<MongoCredential> credentialsList, final MongoClientOptions options) {
200205
super(addr, credentialsList, options);
201206
}
202207

208+
/**
209+
* Creates a Mongo instance based on a (single) mongo node using a given server address, credential, and options
210+
*
211+
* @param addr the database address
212+
* @param credential the credential used to authenticate all connections
213+
* @param options default options
214+
* @see com.mongodb.ServerAddress
215+
* @since 3.6.0
216+
*/
217+
public MongoClient(final ServerAddress addr, final MongoCredential credential, final MongoClientOptions options) {
218+
super(addr, singletonList(credential), options);
219+
}
220+
203221
/**
204222
* <p>Creates an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members.
205223
* For a list with a single seed, the driver will still discover all members of the replica set. For a direct
@@ -234,7 +252,9 @@ public MongoClient(final List<ServerAddress> seeds) {
234252
* @param credentialsList the list of credentials used to authenticate all connections
235253
* @see MongoClientOptions#getLocalThreshold()
236254
* @since 2.11.0
255+
* @deprecated Prefer {@link #MongoClient(List, MongoCredential, MongoClientOptions)}
237256
*/
257+
@Deprecated
238258
public MongoClient(final List<ServerAddress> seeds, final List<MongoCredential> credentialsList) {
239259
this(seeds, credentialsList, new MongoClientOptions.Builder().build());
240260
}
@@ -276,11 +296,35 @@ public MongoClient(final List<ServerAddress> seeds, final MongoClientOptions opt
276296
* @param options the options
277297
* @see MongoClientOptions#getLocalThreshold()
278298
* @since 2.11.0
299+
* @deprecated Prefer {@link #MongoClient(List, MongoCredential, MongoClientOptions)}
279300
*/
301+
@Deprecated
280302
public MongoClient(final List<ServerAddress> seeds, final List<MongoCredential> credentialsList, final MongoClientOptions options) {
281303
super(seeds, credentialsList, options);
282304
}
283305

306+
/**
307+
* <p>Creates an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members.
308+
* For a list with a single seed, the driver will still discover all members of the replica set. For a direct
309+
* connection to a replica set member, with no discovery, use the {@link #MongoClient(ServerAddress, List, MongoClientOptions)}
310+
* constructor instead.</p>
311+
*
312+
* <p>When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a
313+
* read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters.
314+
* The servers to randomly select from are further limited by the local threshold. See
315+
* {@link MongoClientOptions#getLocalThreshold()}</p>
316+
*
317+
* @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod
318+
* servers in the same replica set or a list of mongos servers in the same sharded cluster.
319+
* @param credential the credential used to authenticate all connections
320+
* @param options the options
321+
* @see MongoClientOptions#getLocalThreshold()
322+
* @since 3.6.0
323+
*/
324+
public MongoClient(final List<ServerAddress> seeds, final MongoCredential credential, final MongoClientOptions options) {
325+
super(seeds, singletonList(credential), options);
326+
}
327+
284328
/**
285329
* Creates a Mongo described by a URI. If only one address is used it will only connect to that node, otherwise it will discover all
286330
* nodes.
@@ -317,12 +361,31 @@ public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoD
317361
* @param mongoDriverInformation any driver information to associate with the MongoClient
318362
* @see com.mongodb.ServerAddress
319363
* @since 3.4
364+
* @deprecated Prefer {@link #MongoClient(ServerAddress, MongoCredential, MongoClientOptions, MongoDriverInformation)}
320365
*/
366+
@Deprecated
321367
public MongoClient(final ServerAddress addr, final List<MongoCredential> credentialsList, final MongoClientOptions options,
322368
final MongoDriverInformation mongoDriverInformation) {
323369
super(addr, credentialsList, options, mongoDriverInformation);
324370
}
325371

372+
/**
373+
* Creates a MongoClient to a single node using a given ServerAddress.
374+
*
375+
* <p>Note: Intended for driver and library authors to associate extra driver metadata with the connections.</p>
376+
*
377+
* @param addr the database address
378+
* @param credential the credential used to authenticate all connections
379+
* @param options default options
380+
* @param mongoDriverInformation any driver information to associate with the MongoClient
381+
* @see com.mongodb.ServerAddress
382+
* @since 3.6
383+
*/
384+
public MongoClient(final ServerAddress addr, final MongoCredential credential, final MongoClientOptions options,
385+
final MongoDriverInformation mongoDriverInformation) {
386+
super(addr, singletonList(credential), options, mongoDriverInformation);
387+
}
388+
326389
/**
327390
* Creates a MongoClient
328391
*
@@ -334,12 +397,31 @@ public MongoClient(final ServerAddress addr, final List<MongoCredential> credent
334397
* @param options the options
335398
* @param mongoDriverInformation any driver information to associate with the MongoClient
336399
* @since 3.4
400+
* @deprecated Prefer {@link #MongoClient(List, MongoCredential, MongoClientOptions, MongoDriverInformation)}
337401
*/
402+
@Deprecated
338403
public MongoClient(final List<ServerAddress> seeds, final List<MongoCredential> credentialsList, final MongoClientOptions options,
339404
final MongoDriverInformation mongoDriverInformation) {
340405
super(seeds, credentialsList, options, mongoDriverInformation);
341406
}
342407

408+
/**
409+
* Creates a MongoClient
410+
*
411+
* <p>Note: Intended for driver and library authors to associate extra driver metadata with the connections.</p>
412+
*
413+
* @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod
414+
* servers in the same replica set or a list of mongos servers in the same sharded cluster.
415+
* @param credential the credential used to authenticate all connections
416+
* @param options the options
417+
* @param mongoDriverInformation any driver information to associate with the MongoClient
418+
* @since 3.6
419+
*/
420+
public MongoClient(final List<ServerAddress> seeds, final MongoCredential credential, final MongoClientOptions options,
421+
final MongoDriverInformation mongoDriverInformation) {
422+
super(seeds, singletonList(credential), options, mongoDriverInformation);
423+
}
424+
343425
/**
344426
* Gets the options that this client uses to connect to server.
345427
*

driver/src/test/unit/com/mongodb/MongoConstructorsTest.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import java.net.UnknownHostException;
2323
import java.util.Arrays;
24-
import java.util.Collections;
2524
import java.util.List;
2625

2726
import static org.junit.Assert.assertEquals;
@@ -71,13 +70,6 @@ public void shouldGetSeedList() throws UnknownHostException {
7170
}
7271
}
7372

74-
@Test
75-
public void shouldUseGivenCredentials() throws UnknownHostException {
76-
Mongo mongo = new MongoClient(new ServerAddress(),
77-
Arrays.asList(MongoCredential.createMongoCRCredential("user", "admin", "pwd".toCharArray())));
78-
mongo.close();
79-
}
80-
8173
@Test
8274
public void shouldDefaultToPrimaryReadPreference() throws UnknownHostException {
8375
Mongo mongo = new MongoClient();
@@ -93,8 +85,7 @@ public void shouldAllowRequiredReplicaSetNameForSingleServerConstructors() throw
9385
Mongo mongo = new MongoClient("localhost", MongoClientOptions.builder().requiredReplicaSetName("test").build());
9486
mongo.close();
9587

96-
mongo = new MongoClient(new ServerAddress(), Collections.<MongoCredential>emptyList(),
97-
MongoClientOptions.builder().requiredReplicaSetName("test").build());
88+
mongo = new MongoClient(new ServerAddress(), MongoClientOptions.builder().requiredReplicaSetName("test").build());
9889
mongo.close();
9990

10091
mongo = new MongoClient(new MongoClientURI("mongodb://localhost/?setName=test"));

0 commit comments

Comments
 (0)