28
28
import java .util .ArrayList ;
29
29
import java .util .LinkedHashSet ;
30
30
import java .util .List ;
31
+ import java .util .Objects ;
31
32
import java .util .Set ;
32
33
import java .util .concurrent .TimeUnit ;
33
34
import java .util .stream .Collectors ;
49
50
public final class ClusterSettings {
50
51
private final String srvHost ;
51
52
private final Integer srvMaxHosts ;
53
+ private final String srvServiceName ;
52
54
private final List <ServerAddress > hosts ;
53
55
private final ClusterConnectionMode mode ;
54
56
private final ClusterType requiredClusterType ;
@@ -86,6 +88,7 @@ public static final class Builder {
86
88
private static final List <ServerAddress > DEFAULT_HOSTS = singletonList (new ServerAddress ());
87
89
private String srvHost ;
88
90
private Integer srvMaxHosts ;
91
+ private String srvServiceName = "mongodb" ;
89
92
private List <ServerAddress > hosts = DEFAULT_HOSTS ;
90
93
private ClusterConnectionMode mode ;
91
94
private ClusterType requiredClusterType = ClusterType .UNKNOWN ;
@@ -110,6 +113,8 @@ private Builder() {
110
113
public Builder applySettings (final ClusterSettings clusterSettings ) {
111
114
notNull ("clusterSettings" , clusterSettings );
112
115
srvHost = clusterSettings .srvHost ;
116
+ srvServiceName = clusterSettings .srvServiceName ;
117
+ srvMaxHosts = clusterSettings .srvMaxHosts ;
113
118
hosts = clusterSettings .hosts ;
114
119
mode = clusterSettings .mode ;
115
120
requiredReplicaSetName = clusterSettings .requiredReplicaSetName ;
@@ -156,6 +161,28 @@ public Builder srvMaxHosts(final Integer srvMaxHosts) {
156
161
return this ;
157
162
}
158
163
164
+ /**
165
+ * Sets the SRV service name.
166
+ *
167
+ * <p>
168
+ * The SRV resource record (<a href="https://www.rfc-editor.org/rfc/rfc2782">RFC 2782</a>)
169
+ * service name, which is limited to 15 characters
170
+ * (<a href="https://www.rfc-editor.org/rfc/rfc6335#section-5.1">RFC 6335 section 5.1</a>).
171
+ * If specified, it is combined with the single host name specified by
172
+ * {@link #getHosts()} as follows: {@code _srvServiceName._tcp.hostName}. The combined string is an SRV resource record
173
+ * name (<a href="https://www.rfc-editor.org/rfc/rfc1035#section-2.3.1">RFC 1035 section 2.3.1</a>), which is limited to 255
174
+ * characters (<a href="https://www.rfc-editor.org/rfc/rfc1035#section-2.3.4">RFC 1035 section 2.3.4</a>).
175
+ * </p>
176
+ *
177
+ * @param srvServiceName the SRV service name
178
+ * @return this
179
+ * @since 4.5
180
+ */
181
+ public Builder srvServiceName (final String srvServiceName ) {
182
+ this .srvServiceName = notNull ("srvServiceName" , srvServiceName );
183
+ return this ;
184
+ }
185
+
159
186
/**
160
187
* Sets the hosts for the cluster. Any duplicate server addresses are removed from the list.
161
188
*
@@ -302,6 +329,9 @@ public Builder applyConnectionString(final ConnectionString connectionString) {
302
329
mode (ClusterConnectionMode .MULTIPLE );
303
330
srvHost (connectionString .getHosts ().get (0 ));
304
331
srvMaxHosts (connectionString .getSrvMaxHosts ());
332
+ if (connectionString .getSrvServiceName () != null ) {
333
+ srvServiceName (connectionString .getSrvServiceName ());
334
+ }
305
335
} else if ((directConnection != null && directConnection )
306
336
|| (directConnection == null && connectionString .getHosts ().size () == 1
307
337
&& connectionString .getRequiredReplicaSetName () == null )) {
@@ -358,6 +388,26 @@ public Integer getSrvMaxHosts() {
358
388
return srvMaxHosts ;
359
389
}
360
390
391
+ /**
392
+ * Gets the SRV service name.
393
+ *
394
+ * <p>
395
+ * The SRV resource record (<a href="https://www.rfc-editor.org/rfc/rfc2782">RFC 2782</a>)
396
+ * service name, which is limited to 15 characters
397
+ * (<a href="https://www.rfc-editor.org/rfc/rfc6335#section-5.1">RFC 6335 section 5.1</a>).
398
+ * If specified, it is combined with the single host name specified by
399
+ * {@link #getHosts()} as follows: {@code _srvServiceName._tcp.hostName}. The combined string is an SRV resource record
400
+ * name (<a href="https://www.rfc-editor.org/rfc/rfc1035#section-2.3.1">RFC 1035 section 2.3.1</a>), which is limited to 255
401
+ * characters (<a href="https://www.rfc-editor.org/rfc/rfc1035#section-2.3.4">RFC 1035 section 2.3.4</a>).
402
+ * </p>
403
+ *
404
+ * @return the SRV service name, which defaults to {@code "mongodb"}
405
+ * @since 4.5
406
+ */
407
+ public String getSrvServiceName () {
408
+ return srvServiceName ;
409
+ }
410
+
361
411
/**
362
412
* Gets the seed list of hosts for the cluster.
363
413
*
@@ -477,60 +527,33 @@ public boolean equals(final Object o) {
477
527
if (o == null || getClass () != o .getClass ()) {
478
528
return false ;
479
529
}
480
-
481
530
ClusterSettings that = (ClusterSettings ) o ;
482
-
483
- if (serverSelectionTimeoutMS != that .serverSelectionTimeoutMS ) {
484
- return false ;
485
- }
486
- if (localThresholdMS != that .localThresholdMS ) {
487
- return false ;
488
- }
489
- if (srvHost != null ? !srvHost .equals (that .srvHost ) : that .srvHost != null ) {
490
- return false ;
491
- }
492
- if (!hosts .equals (that .hosts )) {
493
- return false ;
494
- }
495
- if (mode != that .mode ) {
496
- return false ;
497
- }
498
- if (requiredClusterType != that .requiredClusterType ) {
499
- return false ;
500
- }
501
- if (requiredReplicaSetName != null ? !requiredReplicaSetName .equals (that .requiredReplicaSetName )
502
- : that .requiredReplicaSetName != null ) {
503
- return false ;
504
- }
505
- if (serverSelector != null ? !serverSelector .equals (that .serverSelector ) : that .serverSelector != null ) {
506
- return false ;
507
- }
508
- if (!clusterListeners .equals (that .clusterListeners )) {
509
- return false ;
510
- }
511
-
512
- return true ;
531
+ return localThresholdMS == that .localThresholdMS
532
+ && serverSelectionTimeoutMS == that .serverSelectionTimeoutMS
533
+ && Objects .equals (srvHost , that .srvHost )
534
+ && Objects .equals (srvMaxHosts , that .srvMaxHosts )
535
+ && srvServiceName .equals (that .srvServiceName )
536
+ && hosts .equals (that .hosts )
537
+ && mode == that .mode
538
+ && requiredClusterType == that .requiredClusterType
539
+ && Objects .equals (requiredReplicaSetName , that .requiredReplicaSetName )
540
+ && Objects .equals (serverSelector , that .serverSelector )
541
+ && clusterListeners .equals (that .clusterListeners );
513
542
}
514
543
515
544
@ Override
516
545
public int hashCode () {
517
- int result = hosts .hashCode ();
518
- result = 31 * result + (srvHost != null ? srvHost .hashCode () : 0 );
519
- result = 31 * result + mode .hashCode ();
520
- result = 31 * result + requiredClusterType .hashCode ();
521
- result = 31 * result + (requiredReplicaSetName != null ? requiredReplicaSetName .hashCode () : 0 );
522
- result = 31 * result + (serverSelector != null ? serverSelector .hashCode () : 0 );
523
- result = 31 * result + (int ) (serverSelectionTimeoutMS ^ (serverSelectionTimeoutMS >>> 32 ));
524
- result = 31 * result + (int ) (localThresholdMS ^ (localThresholdMS >>> 32 ));
525
- result = 31 * result + clusterListeners .hashCode ();
526
- return result ;
546
+ return Objects .hash (srvHost , srvMaxHosts , srvServiceName , hosts , mode , requiredClusterType , requiredReplicaSetName , serverSelector ,
547
+ localThresholdMS , serverSelectionTimeoutMS , clusterListeners );
527
548
}
528
549
529
550
@ Override
530
551
public String toString () {
531
552
return "{"
532
553
+ (hosts .isEmpty () ? "" : "hosts=" + hosts )
533
554
+ (srvHost == null ? "" : ", srvHost=" + srvHost )
555
+ + (srvServiceName == null ? "" : ", srvServiceName=" + srvServiceName )
556
+ + (srvMaxHosts == null ? "" : ", srvMaxHosts=" + srvMaxHosts )
534
557
+ ", mode=" + mode
535
558
+ ", requiredClusterType=" + requiredClusterType
536
559
+ ", requiredReplicaSetName='" + requiredReplicaSetName + '\''
@@ -588,6 +611,7 @@ private ClusterSettings(final Builder builder) {
588
611
589
612
srvHost = builder .srvHost ;
590
613
srvMaxHosts = builder .srvMaxHosts ;
614
+ srvServiceName = builder .srvServiceName ;
591
615
hosts = builder .hosts ;
592
616
if (srvHost != null ) {
593
617
if (builder .mode == ClusterConnectionMode .SINGLE ) {
0 commit comments