@@ -205,6 +205,79 @@ private MongoDb4Provider(
205205 final String collectionName ,
206206 final boolean isCapped ,
207207 final Long collectionSize ) {
208+ ConnectionString connectionString ;
209+ try {
210+ connectionString = new ConnectionString (connectionStringSource );
211+ } catch (final IllegalArgumentException e ) {
212+ LOGGER .error ("Invalid MongoDB connection string `{}`." , connectionStringSource , e );
213+ throw e ;
214+ }
215+
216+ String effectiveDatabaseName = databaseName != null ? databaseName : connectionString .getDatabase ();
217+ String effectiveCollectionName = collectionName != null ? collectionName : connectionString .getCollection ();
218+ // Validate the provided databaseName property
219+ try {
220+ MongoNamespace .checkDatabaseNameValidity (effectiveDatabaseName );
221+ } catch (final IllegalArgumentException e ) {
222+ LOGGER .error ("Invalid MongoDB database name `{}`." , effectiveDatabaseName , e );
223+ throw e ;
224+ }
225+ // Validate the provided collectionName property
226+ try {
227+ MongoNamespace .checkCollectionNameValidity (effectiveCollectionName );
228+ } catch (final IllegalArgumentException e ) {
229+ LOGGER .error ("Invalid MongoDB collection name `{}`." , effectiveCollectionName , e );
230+ throw e ;
231+ }
232+ LOGGER .debug ("Creating ConnectionString {}..." , connectionStringSource );
233+ this .connectionString = new ConnectionString (connectionStringSource );
234+ LOGGER .debug ("Created ConnectionString {}" , connectionString );
235+ LOGGER .debug ("Creating MongoClientSettings..." );
236+ // @formatter:off
237+ final MongoClientSettings settings = MongoClientSettings .builder ()
238+ .applyConnectionString (this .connectionString )
239+ .codecRegistry (CODEC_REGISTRIES )
240+ .build ();
241+ // @formatter:on
242+ LOGGER .debug ("Created MongoClientSettings {}" , settings );
243+ LOGGER .debug ("Creating MongoClient {}..." , settings );
244+ this .mongoClient = MongoClients .create (settings );
245+ LOGGER .debug ("Created MongoClient {}" , mongoClient );
246+ LOGGER .debug ("Getting MongoDatabase {}..." , effectiveDatabaseName );
247+ this .mongoDatabase = this .mongoClient .getDatabase (effectiveDatabaseName );
248+ LOGGER .debug ("Got MongoDatabase {}" , mongoDatabase );
249+ this .isCapped = isCapped ;
250+ this .collectionSize = collectionSize ;
251+ this .collectionName = effectiveCollectionName ;
252+ }
253+
254+ private MongoDb4Provider (final String connectionStringSource , final boolean isCapped , final Long collectionSize ) {
255+
256+ ConnectionString connectionString ;
257+ try {
258+ connectionString = new ConnectionString (connectionStringSource );
259+ } catch (final IllegalArgumentException e ) {
260+ LOGGER .error ("Invalid MongoDB connection string `{}`." , connectionStringSource , e );
261+ throw e ;
262+ }
263+
264+ String effectiveDatabaseName = connectionString .getDatabase ();
265+ String effectiveCollectionName = connectionString .getCollection ();
266+ // Validate the provided databaseName property
267+ try {
268+ MongoNamespace .checkDatabaseNameValidity (effectiveDatabaseName );
269+ } catch (final IllegalArgumentException e ) {
270+ LOGGER .error ("Invalid MongoDB database name `{}`." , effectiveDatabaseName , e );
271+ throw e ;
272+ }
273+ // Validate the provided collectionName property
274+ try {
275+ MongoNamespace .checkCollectionNameValidity (effectiveCollectionName );
276+ } catch (final IllegalArgumentException e ) {
277+ LOGGER .error ("Invalid MongoDB collection name `{}`." , effectiveCollectionName , e );
278+ throw e ;
279+ }
280+
208281 LOGGER .debug ("Creating ConnectionString {}..." , connectionStringSource );
209282 this .connectionString = new ConnectionString (connectionStringSource );
210283 LOGGER .debug ("Created ConnectionString {}" , connectionString );
@@ -219,12 +292,12 @@ private MongoDb4Provider(
219292 LOGGER .debug ("Creating MongoClient {}..." , settings );
220293 this .mongoClient = MongoClients .create (settings );
221294 LOGGER .debug ("Created MongoClient {}" , mongoClient );
222- LOGGER .debug ("Getting MongoDatabase {}..." , databaseName );
223- this .mongoDatabase = this .mongoClient .getDatabase (databaseName );
295+ LOGGER .debug ("Getting MongoDatabase {}..." , effectiveDatabaseName );
296+ this .mongoDatabase = this .mongoClient .getDatabase (effectiveCollectionName );
224297 LOGGER .debug ("Got MongoDatabase {}" , mongoDatabase );
225298 this .isCapped = isCapped ;
226299 this .collectionSize = collectionSize ;
227- this .collectionName = collectionName ;
300+ this .collectionName = effectiveCollectionName ;
228301 }
229302
230303 @ Override
0 commit comments