@@ -251,7 +251,7 @@ public class ConnectionString {
251
251
private static final String MONGODB_PREFIX = "mongodb://" ;
252
252
private static final String MONGODB_SRV_PREFIX = "mongodb+srv://" ;
253
253
private static final Set <String > ALLOWED_OPTIONS_IN_TXT_RECORD =
254
- new HashSet <String >(asList ("authsource" , "replicaset" , "loadbalanced" ));
254
+ new HashSet <>(asList ("authsource" , "replicaset" , "loadbalanced" ));
255
255
private static final Logger LOGGER = Loggers .getLogger ("uri" );
256
256
257
257
private final MongoCredential credential ;
@@ -395,9 +395,9 @@ public ConnectionString(final String connectionString) {
395
395
396
396
String txtRecordsQueryParameters = isSrvProtocol
397
397
? new DefaultDnsResolver ().resolveAdditionalQueryParametersFromTxtRecords (unresolvedHosts .get (0 )) : "" ;
398
- String connectionStringQueryParamenters = unprocessedConnectionString ;
398
+ String connectionStringQueryParameters = unprocessedConnectionString ;
399
399
400
- Map <String , List <String >> connectionStringOptionsMap = parseOptions (connectionStringQueryParamenters );
400
+ Map <String , List <String >> connectionStringOptionsMap = parseOptions (connectionStringQueryParameters );
401
401
Map <String , List <String >> txtRecordsOptionsMap = parseOptions (txtRecordsQueryParameters );
402
402
if (!ALLOWED_OPTIONS_IN_TXT_RECORD .containsAll (txtRecordsOptionsMap .keySet ())) {
403
403
throw new MongoConfigurationException (format ("A TXT record is only permitted to contain the keys %s, but the TXT record for "
@@ -448,12 +448,12 @@ public ConnectionString(final String connectionString) {
448
448
warnOnUnsupportedOptions (combinedOptionsMaps );
449
449
}
450
450
451
- private static final Set <String > GENERAL_OPTIONS_KEYS = new LinkedHashSet <String >();
452
- private static final Set <String > AUTH_KEYS = new HashSet <String >();
453
- private static final Set <String > READ_PREFERENCE_KEYS = new HashSet <String >();
454
- private static final Set <String > WRITE_CONCERN_KEYS = new HashSet <String >();
455
- private static final Set <String > COMPRESSOR_KEYS = new HashSet <String >();
456
- private static final Set <String > ALL_KEYS = new HashSet <String >();
451
+ private static final Set <String > GENERAL_OPTIONS_KEYS = new LinkedHashSet <>();
452
+ private static final Set <String > AUTH_KEYS = new HashSet <>();
453
+ private static final Set <String > READ_PREFERENCE_KEYS = new HashSet <>();
454
+ private static final Set <String > WRITE_CONCERN_KEYS = new HashSet <>();
455
+ private static final Set <String > COMPRESSOR_KEYS = new HashSet <>();
456
+ private static final Set <String > ALL_KEYS = new HashSet <>();
457
457
458
458
static {
459
459
GENERAL_OPTIONS_KEYS .add ("minpoolsize" );
@@ -525,10 +525,8 @@ public ConnectionString(final String connectionString) {
525
525
// even for options which multiple values, e.g. readPreferenceTags
526
526
private Map <String , List <String >> combineOptionsMaps (final Map <String , List <String >> txtRecordsOptionsMap ,
527
527
final Map <String , List <String >> connectionStringOptionsMap ) {
528
- Map <String , List <String >> combinedOptionsMaps = new HashMap <String , List <String >>(txtRecordsOptionsMap );
529
- for (Map .Entry <String , List <String >> entry : connectionStringOptionsMap .entrySet ()) {
530
- combinedOptionsMaps .put (entry .getKey (), entry .getValue ());
531
- }
528
+ Map <String , List <String >> combinedOptionsMaps = new HashMap <>(txtRecordsOptionsMap );
529
+ combinedOptionsMaps .putAll (connectionStringOptionsMap );
532
530
return combinedOptionsMaps ;
533
531
}
534
532
@@ -552,64 +550,93 @@ private void translateOptions(final Map<String, List<String>> optionsMap) {
552
550
if (value == null ) {
553
551
continue ;
554
552
}
555
- if (key .equals ("maxpoolsize" )) {
556
- maxConnectionPoolSize = parseInteger (value , "maxpoolsize" );
557
- } else if (key .equals ("minpoolsize" )) {
558
- minConnectionPoolSize = parseInteger (value , "minpoolsize" );
559
- } else if (key .equals ("maxidletimems" )) {
560
- maxConnectionIdleTime = parseInteger (value , "maxidletimems" );
561
- } else if (key .equals ("maxlifetimems" )) {
562
- maxConnectionLifeTime = parseInteger (value , "maxlifetimems" );
563
- } else if (key .equals ("maxconnecting" )) {
564
- maxConnecting = parseInteger (value , "maxConnecting" );
565
- } else if (key .equals ("waitqueuetimeoutms" )) {
566
- maxWaitTime = parseInteger (value , "waitqueuetimeoutms" );
567
- } else if (key .equals ("connecttimeoutms" )) {
568
- connectTimeout = parseInteger (value , "connecttimeoutms" );
569
- } else if (key .equals ("sockettimeoutms" )) {
570
- socketTimeout = parseInteger (value , "sockettimeoutms" );
571
- } else if (key .equals ("tlsallowinvalidhostnames" )) {
572
- sslInvalidHostnameAllowed = parseBoolean (value , "tlsAllowInvalidHostnames" );
573
- tlsAllowInvalidHostnamesSet = true ;
574
- } else if (key .equals ("sslinvalidhostnameallowed" )) {
575
- sslInvalidHostnameAllowed = parseBoolean (value , "sslinvalidhostnameallowed" );
576
- tlsAllowInvalidHostnamesSet = true ;
577
- } else if (key .equals ("tlsinsecure" )) {
578
- sslInvalidHostnameAllowed = parseBoolean (value , "tlsinsecure" );
579
- tlsInsecureSet = true ;
580
- } else if (key .equals ("ssl" )) {
581
- initializeSslEnabled ("ssl" , value );
582
- } else if (key .equals ("tls" )) {
583
- initializeSslEnabled ("tls" , value );
584
- } else if (key .equals ("replicaset" )) {
585
- requiredReplicaSetName = value ;
586
- } else if (key .equals ("readconcernlevel" )) {
587
- readConcern = new ReadConcern (ReadConcernLevel .fromString (value ));
588
- } else if (key .equals ("serverselectiontimeoutms" )) {
589
- serverSelectionTimeout = parseInteger (value , "serverselectiontimeoutms" );
590
- } else if (key .equals ("localthresholdms" )) {
591
- localThreshold = parseInteger (value , "localthresholdms" );
592
- } else if (key .equals ("heartbeatfrequencyms" )) {
593
- heartbeatFrequency = parseInteger (value , "heartbeatfrequencyms" );
594
- } else if (key .equals ("appname" )) {
595
- applicationName = value ;
596
- } else if (key .equals ("retrywrites" )) {
597
- retryWrites = parseBoolean (value , "retrywrites" );
598
- } else if (key .equals ("retryreads" )) {
599
- retryReads = parseBoolean (value , "retryreads" );
600
- } else if (key .equals ("uuidrepresentation" )) {
601
- uuidRepresentation = createUuidRepresentation (value );
602
- } else if (key .equals ("directconnection" )) {
603
- directConnection = parseBoolean (value , "directconnection" );
604
- } else if (key .equals ("loadbalanced" )) {
605
- loadBalanced = parseBoolean (value , "loadbalanced" );
606
- } else if (key .equals ("srvmaxhosts" )) {
607
- srvMaxHosts = parseInteger (value , "srvmaxhosts" );
608
- if (srvMaxHosts < 0 ) {
609
- throw new IllegalArgumentException ("srvMaxHosts must be >= 0" );
610
- }
611
- } else if (key .equals ("srvservicename" )) {
612
- srvServiceName = value ;
553
+ switch (key ) {
554
+ case "maxpoolsize" :
555
+ maxConnectionPoolSize = parseInteger (value , "maxpoolsize" );
556
+ break ;
557
+ case "minpoolsize" :
558
+ minConnectionPoolSize = parseInteger (value , "minpoolsize" );
559
+ break ;
560
+ case "maxidletimems" :
561
+ maxConnectionIdleTime = parseInteger (value , "maxidletimems" );
562
+ break ;
563
+ case "maxlifetimems" :
564
+ maxConnectionLifeTime = parseInteger (value , "maxlifetimems" );
565
+ break ;
566
+ case "maxconnecting" :
567
+ maxConnecting = parseInteger (value , "maxConnecting" );
568
+ break ;
569
+ case "waitqueuetimeoutms" :
570
+ maxWaitTime = parseInteger (value , "waitqueuetimeoutms" );
571
+ break ;
572
+ case "connecttimeoutms" :
573
+ connectTimeout = parseInteger (value , "connecttimeoutms" );
574
+ break ;
575
+ case "sockettimeoutms" :
576
+ socketTimeout = parseInteger (value , "sockettimeoutms" );
577
+ break ;
578
+ case "tlsallowinvalidhostnames" :
579
+ sslInvalidHostnameAllowed = parseBoolean (value , "tlsAllowInvalidHostnames" );
580
+ tlsAllowInvalidHostnamesSet = true ;
581
+ break ;
582
+ case "sslinvalidhostnameallowed" :
583
+ sslInvalidHostnameAllowed = parseBoolean (value , "sslinvalidhostnameallowed" );
584
+ tlsAllowInvalidHostnamesSet = true ;
585
+ break ;
586
+ case "tlsinsecure" :
587
+ sslInvalidHostnameAllowed = parseBoolean (value , "tlsinsecure" );
588
+ tlsInsecureSet = true ;
589
+ break ;
590
+ case "ssl" :
591
+ initializeSslEnabled ("ssl" , value );
592
+ break ;
593
+ case "tls" :
594
+ initializeSslEnabled ("tls" , value );
595
+ break ;
596
+ case "replicaset" :
597
+ requiredReplicaSetName = value ;
598
+ break ;
599
+ case "readconcernlevel" :
600
+ readConcern = new ReadConcern (ReadConcernLevel .fromString (value ));
601
+ break ;
602
+ case "serverselectiontimeoutms" :
603
+ serverSelectionTimeout = parseInteger (value , "serverselectiontimeoutms" );
604
+ break ;
605
+ case "localthresholdms" :
606
+ localThreshold = parseInteger (value , "localthresholdms" );
607
+ break ;
608
+ case "heartbeatfrequencyms" :
609
+ heartbeatFrequency = parseInteger (value , "heartbeatfrequencyms" );
610
+ break ;
611
+ case "appname" :
612
+ applicationName = value ;
613
+ break ;
614
+ case "retrywrites" :
615
+ retryWrites = parseBoolean (value , "retrywrites" );
616
+ break ;
617
+ case "retryreads" :
618
+ retryReads = parseBoolean (value , "retryreads" );
619
+ break ;
620
+ case "uuidrepresentation" :
621
+ uuidRepresentation = createUuidRepresentation (value );
622
+ break ;
623
+ case "directconnection" :
624
+ directConnection = parseBoolean (value , "directconnection" );
625
+ break ;
626
+ case "loadbalanced" :
627
+ loadBalanced = parseBoolean (value , "loadbalanced" );
628
+ break ;
629
+ case "srvmaxhosts" :
630
+ srvMaxHosts = parseInteger (value , "srvmaxhosts" );
631
+ if (srvMaxHosts < 0 ) {
632
+ throw new IllegalArgumentException ("srvMaxHosts must be >= 0" );
633
+ }
634
+ break ;
635
+ case "srvservicename" :
636
+ srvServiceName = value ;
637
+ break ;
638
+ default :
639
+ break ;
613
640
}
614
641
}
615
642
@@ -651,7 +678,7 @@ private List<MongoCompressor> createCompressors(final Map<String, List<String>>
651
678
}
652
679
653
680
private List <MongoCompressor > buildCompressors (final String compressors , @ Nullable final Integer zlibCompressionLevel ) {
654
- List <MongoCompressor > compressorsList = new ArrayList <MongoCompressor >();
681
+ List <MongoCompressor > compressorsList = new ArrayList <>();
655
682
656
683
for (String cur : compressors .split ("," )) {
657
684
if (cur .equals ("zlib" )) {
@@ -685,14 +712,21 @@ private WriteConcern createWriteConcern(final Map<String, List<String>> optionsM
685
712
continue ;
686
713
}
687
714
688
- if (key .equals ("safe" )) {
689
- safe = parseBoolean (value , "safe" );
690
- } else if (key .equals ("w" )) {
691
- w = value ;
692
- } else if (key .equals ("wtimeoutms" )) {
693
- wTimeout = Integer .parseInt (value );
694
- } else if (key .equals ("journal" )) {
695
- journal = parseBoolean (value , "journal" );
715
+ switch (key ) {
716
+ case "safe" :
717
+ safe = parseBoolean (value , "safe" );
718
+ break ;
719
+ case "w" :
720
+ w = value ;
721
+ break ;
722
+ case "wtimeoutms" :
723
+ wTimeout = Integer .parseInt (value );
724
+ break ;
725
+ case "journal" :
726
+ journal = parseBoolean (value , "journal" );
727
+ break ;
728
+ default :
729
+ break ;
696
730
}
697
731
}
698
732
return buildWriteConcern (safe , w , wTimeout , journal );
@@ -701,7 +735,7 @@ private WriteConcern createWriteConcern(final Map<String, List<String>> optionsM
701
735
@ Nullable
702
736
private ReadPreference createReadPreference (final Map <String , List <String >> optionsMap ) {
703
737
String readPreferenceType = null ;
704
- List <TagSet > tagSetList = new ArrayList <TagSet >();
738
+ List <TagSet > tagSetList = new ArrayList <>();
705
739
long maxStalenessSeconds = -1 ;
706
740
707
741
for (final String key : READ_PREFERENCE_KEYS ) {
@@ -710,15 +744,21 @@ private ReadPreference createReadPreference(final Map<String, List<String>> opti
710
744
continue ;
711
745
}
712
746
713
- if (key .equals ("readpreference" )) {
714
- readPreferenceType = value ;
715
- } else if (key .equals ("maxstalenessseconds" )) {
716
- maxStalenessSeconds = parseInteger (value , "maxstalenessseconds" );
717
- } else if (key .equals ("readpreferencetags" )) {
718
- for (final String cur : optionsMap .get (key )) {
719
- TagSet tagSet = getTags (cur .trim ());
720
- tagSetList .add (tagSet );
721
- }
747
+ switch (key ) {
748
+ case "readpreference" :
749
+ readPreferenceType = value ;
750
+ break ;
751
+ case "maxstalenessseconds" :
752
+ maxStalenessSeconds = parseInteger (value , "maxstalenessseconds" );
753
+ break ;
754
+ case "readpreferencetags" :
755
+ for (final String cur : optionsMap .get (key )) {
756
+ TagSet tagSet = getTags (cur .trim ());
757
+ tagSetList .add (tagSet );
758
+ }
759
+ break ;
760
+ default :
761
+ break ;
722
762
}
723
763
}
724
764
return buildReadPreference (readPreferenceType , tagSetList , maxStalenessSeconds );
@@ -758,24 +798,31 @@ private MongoCredential createCredentials(final Map<String, List<String>> option
758
798
continue ;
759
799
}
760
800
761
- if (key .equals ("authmechanism" )) {
762
- if (value .equals ("MONGODB-CR" )) {
763
- if (userName == null ) {
764
- throw new IllegalArgumentException ("username can not be null" );
801
+ switch (key ) {
802
+ case "authmechanism" :
803
+ if (value .equals ("MONGODB-CR" )) {
804
+ if (userName == null ) {
805
+ throw new IllegalArgumentException ("username can not be null" );
806
+ }
807
+ LOGGER .warn ("Deprecated MONGDOB-CR authentication mechanism used in connection string" );
808
+ } else {
809
+ mechanism = AuthenticationMechanism .fromMechanismName (value );
765
810
}
766
- LOGGER .warn ("Deprecated MONGDOB-CR authentication mechanism used in connection string" );
767
- } else {
768
- mechanism = AuthenticationMechanism .fromMechanismName (value );
769
- }
770
- } else if (key .equals ("authsource" )) {
771
- if (value .equals ("" )) {
772
- throw new IllegalArgumentException ("authSource can not be an empty string" );
773
- }
774
- authSource = value ;
775
- } else if (key .equals ("gssapiservicename" )) {
776
- gssapiServiceName = value ;
777
- } else if (key .equals ("authmechanismproperties" )) {
778
- authMechanismProperties = value ;
811
+ break ;
812
+ case "authsource" :
813
+ if (value .equals ("" )) {
814
+ throw new IllegalArgumentException ("authSource can not be an empty string" );
815
+ }
816
+ authSource = value ;
817
+ break ;
818
+ case "gssapiservicename" :
819
+ gssapiServiceName = value ;
820
+ break ;
821
+ case "authmechanismproperties" :
822
+ authMechanismProperties = value ;
823
+ break ;
824
+ default :
825
+ break ;
779
826
}
780
827
}
781
828
@@ -882,7 +929,7 @@ private String getLastValue(final Map<String, List<String>> optionsMap, final St
882
929
}
883
930
884
931
private Map <String , List <String >> parseOptions (final String optionsPart ) {
885
- Map <String , List <String >> optionsMap = new HashMap <String , List < String > >();
932
+ Map <String , List <String >> optionsMap = new HashMap <>();
886
933
if (optionsPart .length () == 0 ) {
887
934
return optionsMap ;
888
935
}
@@ -897,7 +944,7 @@ private Map<String, List<String>> parseOptions(final String optionsPart) {
897
944
String value = part .substring (idx + 1 );
898
945
List <String > valueList = optionsMap .get (key );
899
946
if (valueList == null ) {
900
- valueList = new ArrayList <String >(1 );
947
+ valueList = new ArrayList <>(1 );
901
948
}
902
949
valueList .add (urldecode (value ));
903
950
optionsMap .put (key , valueList );
@@ -987,7 +1034,7 @@ private WriteConcern buildWriteConcern(@Nullable final Boolean safe, @Nullable f
987
1034
}
988
1035
989
1036
private TagSet getTags (final String tagSetString ) {
990
- List <Tag > tagList = new ArrayList <Tag >();
1037
+ List <Tag > tagList = new ArrayList <>();
991
1038
if (tagSetString .length () > 0 ) {
992
1039
for (final String tag : tagSetString .split ("," )) {
993
1040
String [] tagKeyValuePair = tag .split (":" );
@@ -1001,8 +1048,8 @@ private TagSet getTags(final String tagSetString) {
1001
1048
return new TagSet (tagList );
1002
1049
}
1003
1050
1004
- private static final Set <String > TRUE_VALUES = new HashSet <String >(asList ("true" , "yes" , "1" ));
1005
- private static final Set <String > FALSE_VALUES = new HashSet <String >(asList ("false" , "no" , "0" ));
1051
+ private static final Set <String > TRUE_VALUES = new HashSet <>(asList ("true" , "yes" , "1" ));
1052
+ private static final Set <String > FALSE_VALUES = new HashSet <>(asList ("false" , "no" , "0" ));
1006
1053
1007
1054
@ Nullable
1008
1055
private Boolean parseBoolean (final String input , final String key ) {
@@ -1040,7 +1087,7 @@ private List<String> parseHosts(final List<String> rawHosts) {
1040
1087
if (rawHosts .size () == 0 ){
1041
1088
throw new IllegalArgumentException ("The connection string must contain at least one host" );
1042
1089
}
1043
- List <String > hosts = new ArrayList <String >();
1090
+ List <String > hosts = new ArrayList <>();
1044
1091
for (String host : rawHosts ) {
1045
1092
if (host .length () == 0 ) {
1046
1093
throw new IllegalArgumentException (format ("The connection string contains an empty host '%s'. " , rawHosts ));
@@ -1327,8 +1374,9 @@ public Integer getMaxConnectionIdleTime() {
1327
1374
}
1328
1375
1329
1376
/**
1330
- * Gets the maximum connection life time specified in the connection string.
1331
- * @return the maximum connection life time
1377
+ * Gets the maximum connection lifetime specified in the connection string.
1378
+ *
1379
+ * @return the maximum connection lifetime
1332
1380
*/
1333
1381
@ Nullable
1334
1382
public Integer getMaxConnectionLifeTime () {
0 commit comments