55 * License, v. 2.0. If a copy of the MPL was not distributed with this
66 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
77 */
8- package org .seedstack .mongodb .morphia . internal ;
8+ package org .seedstack .mongodb .internal ;
99
10+ import com .google .common .base .Preconditions ;
1011import com .google .inject .Module ;
1112import com .mongodb .AuthenticationMechanism ;
1213import com .mongodb .MongoCredential ;
1314import com .mongodb .ServerAddress ;
14- import org .apache .commons .configuration .Configuration ;
15+ import org .seedstack .coffig .Coffig ;
16+ import org .seedstack .mongodb .MongoDbConfig ;
1517import org .seedstack .seed .SeedException ;
1618import org .slf4j .Logger ;
1719import org .slf4j .LoggerFactory ;
2628abstract class AbstractMongoDbManager <C , D > implements MongoDbManager {
2729 private static final Logger LOGGER = LoggerFactory .getLogger (AbstractMongoDbManager .class );
2830
29- private final Map <String , C > mongoClients = new HashMap <String , C >();
30- private final Map <String , D > mongoDatabases = new HashMap <String , D >();
31+ private final Map <String , C > mongoClients = new HashMap <>();
32+ private final Map <String , D > mongoDatabases = new HashMap <>();
3133
3234 @ Override
33- public void registerClient (String clientName , Configuration clientConfiguration ) {
35+ public void registerClient (String clientName , MongoDbConfig . ClientConfig clientConfig , Coffig coffig ) {
3436 LOGGER .info ("Creating MongoDB client {}" , clientName );
35- mongoClients .put (clientName , doCreateClient (clientConfiguration ));
37+ mongoClients .put (clientName , doCreateClient (clientName , clientConfig , coffig ));
3638 }
3739
3840 @ Override
3941 public void registerDatabase (String clientName , String dbName , String alias ) {
4042 C mongoClient = mongoClients .get (clientName );
41-
42- if (mongoClient == null ) {
43- throw SeedException .createNew (MongoDbErrorCodes .UNKNOWN_CLIENT_SPECIFIED ).put ("clientName" , clientName ).put ("dbName" , dbName );
44- }
45-
43+ Preconditions .checkNotNull (mongoClient , "Mongo client " + clientName + " is not registered" );
4644 mongoDatabases .put (alias , doCreateDatabase (mongoClient , dbName ));
4745 }
4846
@@ -67,19 +65,11 @@ public void shutdown() {
6765 @ SuppressWarnings ("unchecked" )
6866 public Module getModule () {
6967 Type [] actualTypeArguments = ((ParameterizedType ) getClass ().getGenericSuperclass ()).getActualTypeArguments ();
70- return new MongoDbModule <C , D >((Class <C >) actualTypeArguments [0 ], (Class <D >) actualTypeArguments [1 ], mongoClients , mongoDatabases );
71- }
72-
73- protected <T > T instanceFromClassName (Class <T > clazz , String className ) {
74- try {
75- return Class .forName (className ).asSubclass (clazz ).newInstance ();
76- } catch (Exception e ) {
77- throw SeedException .wrap (e , MongoDbErrorCodes .UNABLE_TO_INSTANTIATE_CLASS ).put ("className" , className );
78- }
68+ return new MongoDbModule <>((Class <C >) actualTypeArguments [0 ], (Class <D >) actualTypeArguments [1 ], mongoClients , mongoDatabases );
7969 }
8070
81- public List <ServerAddress > buildServerAddresses (String [] addresses ) {
82- List <ServerAddress > serverAddresses = new ArrayList <ServerAddress >();
71+ List <ServerAddress > buildServerAddresses (String clientName , List < String > addresses ) {
72+ List <ServerAddress > serverAddresses = new ArrayList <>();
8373
8474 if (addresses != null ) {
8575 for (String address : addresses ) {
@@ -89,39 +79,45 @@ public List<ServerAddress> buildServerAddresses(String[] addresses) {
8979 } else if (split .length == 2 ) {
9080 serverAddresses .add (new ServerAddress (split [0 ], Integer .parseInt (split [1 ])));
9181 } else {
92- throw SeedException .createNew (MongoDbErrorCodes .UNABLE_TO_PARSE_SERVER_ADDRESS ).put ("address" , address );
82+ throw SeedException .createNew (MongoDbErrorCode .INVALID_SERVER_ADDRESS )
83+ .put ("clientName" , clientName )
84+ .put ("address" , address );
9385 }
9486 }
9587 }
9688
9789 return serverAddresses ;
9890 }
9991
100- protected List <MongoCredential > buildMongoCredentials (String [] credentials ) {
101- List <MongoCredential > mongoCredentials = new ArrayList <MongoCredential >();
92+ List <MongoCredential > buildMongoCredentials (String clientName , List < String > credentials ) {
93+ List <MongoCredential > mongoCredentials = new ArrayList <>();
10294
10395 if (credentials != null ) {
10496 for (String credential : credentials ) {
10597 String [] elements = credential .split (":" , 3 );
10698 if (elements .length == 3 ) {
10799 String [] sourceElements = elements [0 ].split ("/" , 2 );
108100 if (sourceElements .length == 2 ) {
109- mongoCredentials .add (buildMongoCredential (elements [1 ], elements [2 ], sourceElements [1 ], sourceElements [0 ]));
101+ mongoCredentials .add (buildMongoCredential (clientName , elements [1 ], elements [2 ], sourceElements [1 ], sourceElements [0 ]));
110102 } else if (sourceElements .length == 1 ) {
111- mongoCredentials .add (buildMongoCredential (elements [1 ], elements [2 ], sourceElements [0 ], null ));
103+ mongoCredentials .add (buildMongoCredential (clientName , elements [1 ], elements [2 ], sourceElements [0 ], null ));
112104 } else {
113- throw SeedException .createNew (MongoDbErrorCodes .INVALID_CREDENTIAL_SYNTAX ).put ("credential" , credential );
105+ throw SeedException .createNew (MongoDbErrorCode .INVALID_CREDENTIAL_SYNTAX )
106+ .put ("credential" , credential )
107+ .put ("clientName" , clientName );
114108 }
115109 } else {
116- throw SeedException .createNew (MongoDbErrorCodes .INVALID_CREDENTIAL_SYNTAX ).put ("credential" , credential );
110+ throw SeedException .createNew (MongoDbErrorCode .INVALID_CREDENTIAL_SYNTAX )
111+ .put ("credential" , credential )
112+ .put ("clientName" , clientName );
117113 }
118114 }
119115 }
120116
121117 return mongoCredentials ;
122118 }
123119
124- protected MongoCredential buildMongoCredential (String user , String password , String source , String mechanism ) {
120+ MongoCredential buildMongoCredential (String clientName , String user , String password , String source , String mechanism ) {
125121 if (mechanism != null ) {
126122 AuthenticationMechanism authenticationMechanism = AuthenticationMechanism .fromMechanismName (mechanism );
127123 switch (authenticationMechanism ) {
@@ -136,7 +132,9 @@ protected MongoCredential buildMongoCredential(String user, String password, Str
136132 case GSSAPI :
137133 return MongoCredential .createGSSAPICredential (user );
138134 default :
139- throw SeedException .createNew (MongoDbErrorCodes .UNSUPPORTED_AUTHENTICATION_MECHANISM ).put ("mechanism" , authenticationMechanism .getMechanismName ());
135+ throw SeedException .createNew (MongoDbErrorCode .UNSUPPORTED_AUTHENTICATION_MECHANISM )
136+ .put ("clientName" , clientName )
137+ .put ("mechanism" , authenticationMechanism .getMechanismName ());
140138 }
141139 } else {
142140 return MongoCredential .createCredential (
@@ -147,7 +145,7 @@ protected MongoCredential buildMongoCredential(String user, String password, Str
147145 }
148146 }
149147
150- protected abstract C doCreateClient (Configuration clientConfiguration );
148+ protected abstract C doCreateClient (String clientName , MongoDbConfig . ClientConfig clientConfiguration , Coffig coffig );
151149
152150 protected abstract D doCreateDatabase (C client , String dbName );
153151
0 commit comments