77 */
88package org .seedstack .mongodb .internal ;
99
10- import com .google .common .base .Preconditions ;
11- import com .google .inject .Module ;
12- import com .mongodb .AuthenticationMechanism ;
13- import com .mongodb .MongoClient ;
14- import com .mongodb .MongoClientOptions ;
15- import com .mongodb .MongoClientURI ;
16- import com .mongodb .MongoCredential ;
17- import com .mongodb .ServerAddress ;
18- import com .mongodb .client .MongoDatabase ;
10+ import java .util .ArrayList ;
11+ import java .util .HashMap ;
12+ import java .util .List ;
13+ import java .util .Map ;
14+ import java .util .stream .Collectors ;
15+
1916import org .seedstack .coffig .BuilderSupplier ;
2017import org .seedstack .coffig .Coffig ;
2118import org .seedstack .mongodb .MongoDbConfig ;
2219import org .seedstack .seed .SeedException ;
2320import org .slf4j .Logger ;
2421import org .slf4j .LoggerFactory ;
2522
26- import java .util .ArrayList ;
27- import java .util .HashMap ;
28- import java .util .List ;
29- import java .util .Map ;
23+ import com .google .common .base .Preconditions ;
24+ import com .google .inject .Module ;
25+ import com .mongodb .ConnectionString ;
26+ import com .mongodb .MongoClientSettings ;
27+ import com .mongodb .ServerAddress ;
28+ import com .mongodb .client .MongoClient ;
29+ import com .mongodb .client .MongoClients ;
30+ import com .mongodb .client .MongoDatabase ;
3031
3132class MongoDbManager {
3233 private static final Logger LOGGER = LoggerFactory .getLogger (MongoDbManager .class );
3334 private final Map <String , MongoClient > mongoClients = new HashMap <>();
3435 private final Map <String , MongoDatabase > mongoDatabases = new HashMap <>();
3536
36- protected MongoClient doCreateClient (String clientName , MongoDbConfig .ClientConfig clientConfig , Coffig coffig ) {
37+ protected MongoClient doCreateClient (
38+ String clientName , MongoDbConfig .ClientConfig clientConfig , Coffig coffig ) {
3739 AllOptions allOptions = coffig .get (AllOptions .class , String .format ("mongoDb.clients.%s" , clientName ));
3840 if (clientConfig .isConfiguredByUri ()) {
39- return new MongoClient (new MongoClientURI (clientConfig .getUri (), allOptions .options .get ()));
41+ return MongoClients .create (
42+ allOptions .options
43+ .get ()
44+ .applyConnectionString (new ConnectionString (clientConfig .getUri ()))
45+ .build ());
4046 } else {
41- return createMongoClient (clientName , clientConfig , allOptions .options .get (). build () );
47+ return createMongoClient (clientName , clientConfig , allOptions .options .get ());
4248 }
4349 }
4450
@@ -50,32 +56,26 @@ protected void doClose(MongoClient client) {
5056 client .close ();
5157 }
5258
53- private MongoClient createMongoClient (String clientName , MongoDbConfig .ClientConfig clientConfig ,
54- MongoClientOptions mongoClientOptions ) {
59+ private MongoClient createMongoClient (
60+ String clientName ,
61+ MongoDbConfig .ClientConfig clientConfig ,
62+ MongoClientSettings .Builder mongoClientSettingsBuilder ) {
5563 List <ServerAddress > serverAddresses = buildServerAddresses (clientName , clientConfig .getHosts ());
5664
5765 if (serverAddresses .isEmpty ()) {
5866 throw SeedException .createNew (MongoDbErrorCode .MISSING_HOSTS_CONFIGURATION )
5967 .put ("clientName" , clientName );
6068 }
6169
62- MongoCredential mongoCredential = buildMongoCredential (clientName , clientConfig .getCredentials ());
63- if (mongoCredential == null ) {
64- if (serverAddresses .size () == 1 ) {
65- return new MongoClient (serverAddresses .get (0 ), mongoClientOptions );
66- } else {
67- return new MongoClient (serverAddresses , mongoClientOptions );
68- }
69- } else {
70- if (serverAddresses .size () == 1 ) {
71- return new MongoClient (serverAddresses .get (0 ), mongoCredential , mongoClientOptions );
72- } else {
73- return new MongoClient (serverAddresses , mongoCredential , mongoClientOptions );
74- }
75- }
70+ ConnectionString connectionString = buildConnectionString (clientName , serverAddresses ,
71+ clientConfig .getCredentials ());
72+
73+ return MongoClients .create (mongoClientSettingsBuilder .applyConnectionString (connectionString ).build ());
74+
7675 }
7776
78- public void registerClient (String clientName , MongoDbConfig .ClientConfig clientConfig , Coffig coffig ) {
77+ public void registerClient (
78+ String clientName , MongoDbConfig .ClientConfig clientConfig , Coffig coffig ) {
7979 LOGGER .info ("Creating MongoDB client {}" , clientName );
8080 mongoClients .put (clientName , doCreateClient (clientName , clientConfig , coffig ));
8181 }
@@ -93,7 +93,10 @@ public void shutdown() {
9393 try {
9494 doClose (mongoClientEntry .getValue ());
9595 } catch (Exception e ) {
96- LOGGER .error (String .format ("Unable to properly close MongoDB client %s" , mongoClientEntry .getKey ()), e );
96+ LOGGER .error (
97+ String .format (
98+ "Unable to properly close MongoDB client %s" , mongoClientEntry .getKey ()),
99+ e );
97100 }
98101 }
99102 } finally {
@@ -103,7 +106,29 @@ public void shutdown() {
103106 }
104107
105108 public Module getModule () {
106- return new MongoDbModule <>(com .mongodb .MongoClient .class , MongoDatabase .class , mongoClients , mongoDatabases );
109+ return new MongoDbModule <>(
110+ com .mongodb .client .MongoClient .class , MongoDatabase .class , mongoClients , mongoDatabases );
111+ }
112+
113+ ConnectionString buildConnectionString (String clientName , List <ServerAddress > serverAddresses ,
114+ String mongoCredential ) {
115+
116+ StringBuilder builder = new StringBuilder ();
117+ builder .append ("mongodb://" );
118+
119+ if (mongoCredential != null ) {
120+ builder .append (mongoCredential );
121+ builder .append ("@" );
122+ }
123+ if (serverAddresses .size () == 1 ) {
124+ builder .append (serverAddresses .get (0 ));
125+ } else {
126+ builder .append (serverAddresses .stream ()
127+ .map (x -> x .toString ())
128+ .collect (Collectors .joining ("," )));
129+ }
130+ LOGGER .info ("Connection string" , builder );
131+ return new ConnectionString (builder .toString ());
107132 }
108133
109134 List <ServerAddress > buildServerAddresses (String clientName , List <String > addresses ) {
@@ -127,61 +152,8 @@ List<ServerAddress> buildServerAddresses(String clientName, List<String> address
127152 return serverAddresses ;
128153 }
129154
130- MongoCredential buildMongoCredential (String clientName , String credential ) {
131- if (credential == null || credential .isEmpty ()) {
132- return null ;
133- } else {
134- String [] elements = credential .split (":" , 3 );
135- if (elements .length == 3 ) {
136- String [] sourceElements = elements [0 ].split ("/" , 2 );
137- if (sourceElements .length == 2 ) {
138- return buildMongoCredential (clientName , elements [1 ], elements [2 ], sourceElements [1 ], sourceElements [0 ]);
139- } else if (sourceElements .length == 1 ) {
140- return buildMongoCredential (clientName , elements [1 ], elements [2 ], sourceElements [0 ], null );
141- } else {
142- throw SeedException .createNew (MongoDbErrorCode .INVALID_CREDENTIAL_SYNTAX )
143- .put ("credential" , credential )
144- .put ("clientName" , clientName );
145- }
146- } else {
147- throw SeedException .createNew (MongoDbErrorCode .INVALID_CREDENTIAL_SYNTAX )
148- .put ("credential" , credential )
149- .put ("clientName" , clientName );
150- }
151- }
152- }
153-
154- MongoCredential buildMongoCredential (String clientName , String user , String password , String source , String mechanism ) {
155- if (mechanism != null ) {
156- AuthenticationMechanism authenticationMechanism = AuthenticationMechanism .fromMechanismName (mechanism );
157- switch (authenticationMechanism ) {
158- case PLAIN :
159- return MongoCredential .createPlainCredential (user , source , password .toCharArray ());
160- case SCRAM_SHA_1 :
161- return MongoCredential .createScramSha1Credential (user , source , password .toCharArray ());
162- case SCRAM_SHA_256 :
163- return MongoCredential .createScramSha256Credential (user , source , password .toCharArray ());
164- case MONGODB_AWS :
165- return MongoCredential .createAwsCredential (user , password .toCharArray ());
166- case MONGODB_X509 :
167- return MongoCredential .createMongoX509Credential (user );
168- case GSSAPI :
169- return MongoCredential .createGSSAPICredential (user );
170- default :
171- throw SeedException .createNew (MongoDbErrorCode .UNSUPPORTED_AUTHENTICATION_MECHANISM )
172- .put ("clientName" , clientName )
173- .put ("mechanism" , authenticationMechanism .getMechanismName ());
174- }
175- } else {
176- return MongoCredential .createCredential (
177- user ,
178- source ,
179- password .toCharArray ()
180- );
181- }
182- }
183-
184155 private static class AllOptions {
185- private BuilderSupplier <MongoClientOptions .Builder > options = BuilderSupplier .of (MongoClientOptions .builder ());
156+ private BuilderSupplier <MongoClientSettings .Builder > options = BuilderSupplier
157+ .of (MongoClientSettings .builder ());
186158 }
187159}
0 commit comments