17
17
package com .mongodb .connection ;
18
18
19
19
import com .mongodb .ConnectionString ;
20
- import com .mongodb .MongoClientException ;
21
20
import com .mongodb .ServerAddress ;
22
21
import com .mongodb .annotations .Immutable ;
23
22
import com .mongodb .annotations .NotThreadSafe ;
@@ -519,14 +518,13 @@ public String getShortDescription() {
519
518
}
520
519
521
520
private ClusterSettings (final Builder builder ) {
522
- // TODO: Unit test this
523
521
if (builder .srvHost != null ) {
524
522
if (builder .srvHost .contains (":" )) {
525
523
throw new IllegalArgumentException ("The srvHost can not contain a host name that specifies a port" );
526
524
}
527
525
528
- if (builder .hosts . get ( 0 ). getHost () .split ("\\ ." ).length < 3 ) {
529
- throw new MongoClientException (format ("An SRV host name '%s' was provided that does not contain at least three parts. "
526
+ if (builder .srvHost .split ("\\ ." ).length < 3 ) {
527
+ throw new IllegalArgumentException (format ("An SRV host name '%s' was provided that does not contain at least three parts. "
530
528
+ "It must contain a hostname, domain name and a top level domain." , builder .hosts .get (0 ).getHost ()));
531
529
}
532
530
}
@@ -535,10 +533,6 @@ private ClusterSettings(final Builder builder) {
535
533
throw new IllegalArgumentException ("Multiple hosts cannot be specified when using ClusterType.STANDALONE." );
536
534
}
537
535
538
- if (builder .mode != null && builder .mode == ClusterConnectionMode .SINGLE && builder .hosts .size () > 1 ) {
539
- throw new IllegalArgumentException ("Can not directly connect to more than one server" );
540
- }
541
-
542
536
if (builder .requiredReplicaSetName != null ) {
543
537
if (builder .requiredClusterType == ClusterType .UNKNOWN ) {
544
538
builder .requiredClusterType = ClusterType .REPLICA_SET ;
@@ -550,7 +544,18 @@ private ClusterSettings(final Builder builder) {
550
544
551
545
srvHost = builder .srvHost ;
552
546
hosts = builder .hosts ;
553
- mode = builder .mode != null ? builder .mode : hosts .size () == 1 ? ClusterConnectionMode .SINGLE : ClusterConnectionMode .MULTIPLE ;
547
+ if (srvHost != null ) {
548
+ if (builder .mode == ClusterConnectionMode .SINGLE ) {
549
+ throw new IllegalArgumentException ("An SRV host name was provided but the connection mode is not MULTIPLE" );
550
+ }
551
+ mode = ClusterConnectionMode .MULTIPLE ;
552
+ } else {
553
+ if (builder .mode == ClusterConnectionMode .SINGLE && builder .hosts .size () > 1 ) {
554
+ throw new IllegalArgumentException ("Can not directly connect to more than one server" );
555
+ }
556
+
557
+ mode = builder .mode != null ? builder .mode : hosts .size () == 1 ? ClusterConnectionMode .SINGLE : ClusterConnectionMode .MULTIPLE ;
558
+ }
554
559
requiredReplicaSetName = builder .requiredReplicaSetName ;
555
560
requiredClusterType = builder .requiredClusterType ;
556
561
localThresholdMS = builder .localThresholdMS ;
0 commit comments