29
29
import com .mongodb .connection .StreamFactoryFactory ;
30
30
import com .mongodb .event .CommandListener ;
31
31
import com .mongodb .lang .Nullable ;
32
+ import com .mongodb .spi .dns .DnsClient ;
33
+ import com .mongodb .spi .dns .InetAddressResolver ;
32
34
import org .bson .UuidRepresentation ;
33
35
import org .bson .codecs .BsonCodecProvider ;
34
36
import org .bson .codecs .BsonValueCodecProvider ;
@@ -107,6 +109,8 @@ public final class MongoClientSettings {
107
109
private final boolean heartbeatConnectTimeoutSetExplicitly ;
108
110
109
111
private final ContextProvider contextProvider ;
112
+ private final DnsClient dnsClient ;
113
+ private final InetAddressResolver inetAddressResolver ;
110
114
111
115
/**
112
116
* Gets the default codec registry. It includes the following providers:
@@ -160,6 +164,39 @@ public static Builder builder(final MongoClientSettings settings) {
160
164
return new Builder (settings );
161
165
}
162
166
167
+ /**
168
+ * Gets the {@link DnsClient} to use for resolving DNS queries.
169
+ *
170
+ * <p>If set, it will be used to resolve SRV and TXT records for mongodb+srv connections. Otherwise,
171
+ * implementations of {@link com.mongodb.spi.dns.DnsClientProvider} will be discovered via {@link java.util.ServiceLoader}.
172
+ * If no implementations are discovered, then {@code com.sun.jndi.dns.DnsContextFactory} will be used to resolve these records.
173
+ *
174
+ * <p>If applying a connection string to these settings, care must be taken to also pass the same {@link DnsClient} as an argument to
175
+ * the {@link ConnectionString} constructor.
176
+ *
177
+ * @return the DNS client
178
+ * @since 4.10
179
+ * @see ConnectionString#ConnectionString(String, DnsClient)
180
+ */
181
+ @ Nullable
182
+ public DnsClient getDnsClient () {
183
+ return dnsClient ;
184
+ }
185
+
186
+ /**
187
+ * Gets the {@link InetAddressResolver} to use for looking up the {@link java.net.InetAddress} instances for each host.
188
+ *
189
+ * <p>If set, it will be used to look up the {@link java.net.InetAddress} for each host, via
190
+ * {@link InetAddressResolver#lookupByName(String)}. Otherwise, {@link java.net.InetAddress#getAllByName(String)} will be used.
191
+ *
192
+ * @return the {@link java.net.InetAddress} resolver
193
+ * @since 4.10
194
+ */
195
+ @ Nullable
196
+ public InetAddressResolver getInetAddressResolver () {
197
+ return inetAddressResolver ;
198
+ }
199
+
163
200
/**
164
201
* A builder for {@code MongoClientSettings} so that {@code MongoClientSettings} can be immutable, and to support easier construction
165
202
* through chaining.
@@ -193,6 +230,8 @@ public static final class Builder {
193
230
private int heartbeatSocketTimeoutMS ;
194
231
195
232
private ContextProvider contextProvider ;
233
+ private DnsClient dnsClient ;
234
+ private InetAddressResolver inetAddressResolver ;
196
235
197
236
private Builder () {
198
237
}
@@ -211,6 +250,8 @@ private Builder(final MongoClientSettings settings) {
211
250
credential = settings .getCredential ();
212
251
uuidRepresentation = settings .getUuidRepresentation ();
213
252
serverApi = settings .getServerApi ();
253
+ dnsClient = settings .getDnsClient ();
254
+ inetAddressResolver = settings .getInetAddressResolver ();
214
255
streamFactoryFactory = settings .getStreamFactoryFactory ();
215
256
autoEncryptionSettings = settings .getAutoEncryptionSettings ();
216
257
contextProvider = settings .getContextProvider ();
@@ -220,6 +261,7 @@ private Builder(final MongoClientSettings settings) {
220
261
socketSettingsBuilder .applySettings (settings .getSocketSettings ());
221
262
connectionPoolSettingsBuilder .applySettings (settings .getConnectionPoolSettings ());
222
263
sslSettingsBuilder .applySettings (settings .getSslSettings ());
264
+
223
265
if (settings .heartbeatConnectTimeoutSetExplicitly ) {
224
266
heartbeatConnectTimeoutMS = settings .heartbeatSocketSettings .getConnectTimeout (MILLISECONDS );
225
267
}
@@ -273,7 +315,7 @@ public Builder applyConnectionString(final ConnectionString connectionString) {
273
315
/**
274
316
* Applies the {@link LoggerSettings.Builder} block and then sets the loggerSettings.
275
317
*
276
- * @param block the block to apply to the LoggerSettins .
318
+ * @param block the block to apply to the LoggerSettings .
277
319
* @return this
278
320
* @see MongoClientSettings#getLoggerSettings()
279
321
* @since 4.9
@@ -580,6 +622,45 @@ public Builder contextProvider(@Nullable final ContextProvider contextProvider)
580
622
return this ;
581
623
}
582
624
625
+ /**
626
+ * Sets the {@link DnsClient} to use for resolving DNS queries.
627
+ *
628
+ * <p> If set, it will be used to resolve SRV and TXT records for mongodb+srv connections. Otherwise,
629
+ * implementation of {@link com.mongodb.spi.dns.DnsClientProvider} will be discovered via {@link java.util.ServiceLoader}
630
+ * and used to create an instance of {@link DnsClient}. If no implementation is discovered, then
631
+ * {@code com.sun.jndi.dns.DnsContextFactory} will be used to resolve these records.
632
+ *
633
+ * <p>If {@linkplain #applyConnectionString(ConnectionString) applying a connection string to these settings}, care must be
634
+ * taken to also pass the same {@link DnsClient} as an argument to the {@link ConnectionString} constructor.
635
+ *
636
+ * @param dnsClient the DNS client
637
+ * @return the DNS client
638
+ * @since 4.10
639
+ * @see ConnectionString#ConnectionString(String, DnsClient)
640
+ */
641
+ public Builder dnsClient (@ Nullable final DnsClient dnsClient ) {
642
+ this .dnsClient = dnsClient ;
643
+ return this ;
644
+ }
645
+
646
+ /**
647
+ * Sets the {@link InetAddressResolver} to use for looking up the {@link java.net.InetAddress} instances for each host.
648
+ *
649
+ * <p>If set, it will be used to look up the {@link java.net.InetAddress} for each host, via
650
+ * {@link InetAddressResolver#lookupByName(String)}. Otherwise,
651
+ * an implementation of {@link com.mongodb.spi.dns.InetAddressResolverProvider} will be discovered via
652
+ * {@link java.util.ServiceLoader} and used to create an instance of {@link InetAddressResolver}. If no implementation is
653
+ * discovered, {@link java.net.InetAddress#getAllByName(String)} will be used to lookup the {@link java.net.InetAddress}
654
+ * instances for a host.
655
+ *
656
+ * @param inetAddressResolver the InetAddress provider
657
+ * @return the {@link java.net.InetAddress} resolver
658
+ * @since 4.10
659
+ */
660
+ public Builder inetAddressResolver (@ Nullable final InetAddressResolver inetAddressResolver ) {
661
+ this .inetAddressResolver = inetAddressResolver ;
662
+ return this ;
663
+ }
583
664
584
665
// Package-private to provide interop with MongoClientOptions
585
666
Builder heartbeatConnectTimeoutMS (final int heartbeatConnectTimeoutMS ) {
@@ -905,6 +986,8 @@ public boolean equals(final Object o) {
905
986
&& uuidRepresentation == that .uuidRepresentation
906
987
&& Objects .equals (serverApi , that .serverApi )
907
988
&& Objects .equals (autoEncryptionSettings , that .autoEncryptionSettings )
989
+ && Objects .equals (dnsClient , that .dnsClient )
990
+ && Objects .equals (inetAddressResolver , that .inetAddressResolver )
908
991
&& Objects .equals (contextProvider , that .contextProvider );
909
992
}
910
993
@@ -913,7 +996,8 @@ public int hashCode() {
913
996
return Objects .hash (readPreference , writeConcern , retryWrites , retryReads , readConcern , credential , streamFactoryFactory ,
914
997
commandListeners , codecRegistry , loggerSettings , clusterSettings , socketSettings , heartbeatSocketSettings ,
915
998
connectionPoolSettings , serverSettings , sslSettings , applicationName , compressorList , uuidRepresentation , serverApi ,
916
- autoEncryptionSettings , heartbeatSocketTimeoutSetExplicitly , heartbeatConnectTimeoutSetExplicitly , contextProvider );
999
+ autoEncryptionSettings , heartbeatSocketTimeoutSetExplicitly , heartbeatConnectTimeoutSetExplicitly , dnsClient ,
1000
+ inetAddressResolver , contextProvider );
917
1001
}
918
1002
919
1003
@ Override
@@ -940,6 +1024,8 @@ public String toString() {
940
1024
+ ", uuidRepresentation=" + uuidRepresentation
941
1025
+ ", serverApi=" + serverApi
942
1026
+ ", autoEncryptionSettings=" + autoEncryptionSettings
1027
+ + ", dnsClient=" + dnsClient
1028
+ + ", inetAddressResolver=" + inetAddressResolver
943
1029
+ ", contextProvider=" + contextProvider
944
1030
+ '}' ;
945
1031
}
@@ -964,6 +1050,8 @@ private MongoClientSettings(final Builder builder) {
964
1050
compressorList = builder .compressorList ;
965
1051
uuidRepresentation = builder .uuidRepresentation ;
966
1052
serverApi = builder .serverApi ;
1053
+ dnsClient = builder .dnsClient ;
1054
+ inetAddressResolver = builder .inetAddressResolver ;
967
1055
autoEncryptionSettings = builder .autoEncryptionSettings ;
968
1056
heartbeatSocketSettings = SocketSettings .builder ()
969
1057
.readTimeout (builder .heartbeatSocketTimeoutMS == 0
0 commit comments