-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
MongoClientSettings.java
1214 lines (1128 loc) · 50.1 KB
/
MongoClientSettings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import com.mongodb.annotations.Immutable;
import com.mongodb.annotations.NotThreadSafe;
import com.mongodb.client.gridfs.codecs.GridFSFileCodecProvider;
import com.mongodb.client.model.geojson.codecs.GeoJsonCodecProvider;
import com.mongodb.client.model.mql.ExpressionCodecProvider;
import com.mongodb.connection.ClusterSettings;
import com.mongodb.connection.ConnectionPoolSettings;
import com.mongodb.connection.ServerSettings;
import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.SslSettings;
import com.mongodb.connection.StreamFactoryFactory;
import com.mongodb.connection.TransportSettings;
import com.mongodb.connection.grpc.GrpcStreamFactoryFactory;
import com.mongodb.event.CommandListener;
import com.mongodb.internal.connection.grpc.SharingGrpcStreamFactoryFactory;
import com.mongodb.lang.Nullable;
import com.mongodb.spi.dns.DnsClient;
import com.mongodb.spi.dns.InetAddressResolver;
import org.bson.UuidRepresentation;
import org.bson.codecs.BsonCodecProvider;
import org.bson.codecs.BsonValueCodecProvider;
import org.bson.codecs.CollectionCodecProvider;
import org.bson.codecs.DocumentCodecProvider;
import org.bson.codecs.EnumCodecProvider;
import org.bson.codecs.IterableCodecProvider;
import org.bson.codecs.JsonObjectCodecProvider;
import org.bson.codecs.MapCodecProvider;
import org.bson.codecs.ValueCodecProvider;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.jsr310.Jsr310CodecProvider;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import static com.mongodb.assertions.Assertions.assertFalse;
import static com.mongodb.assertions.Assertions.isTrueArgument;
import static com.mongodb.assertions.Assertions.notNull;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
/**
* Various settings to control the behavior of a {@code MongoClient}.
*
* @since 3.7
*/
@SuppressWarnings("deprecation")
@Immutable
public final class MongoClientSettings {
private static final CodecRegistry DEFAULT_CODEC_REGISTRY =
fromProviders(asList(new ValueCodecProvider(),
new BsonValueCodecProvider(),
new DBRefCodecProvider(),
new DBObjectCodecProvider(),
new DocumentCodecProvider(new DocumentToDBRefTransformer()),
new CollectionCodecProvider(new DocumentToDBRefTransformer()),
new IterableCodecProvider(new DocumentToDBRefTransformer()),
new MapCodecProvider(new DocumentToDBRefTransformer()),
new GeoJsonCodecProvider(),
new GridFSFileCodecProvider(),
new Jsr310CodecProvider(),
new JsonObjectCodecProvider(),
new BsonCodecProvider(),
new EnumCodecProvider(),
new ExpressionCodecProvider(),
new Jep395RecordCodecProvider(),
new KotlinCodecProvider()));
private final ReadPreference readPreference;
private final WriteConcern writeConcern;
private final boolean retryWrites;
private final boolean retryReads;
private final ReadConcern readConcern;
private final MongoCredential credential;
private final TransportSettings transportSettings;
/**
* {@linkplain StreamFactoryFactoryWrapping#assertRequiresNoFurtherWrapping(StreamFactoryFactory) Requires no further wrapping}.
*/
private final StreamFactoryFactory streamFactoryFactory;
private final List<CommandListener> commandListeners;
private final CodecRegistry codecRegistry;
private final LoggerSettings loggerSettings;
private final ClusterSettings clusterSettings;
private final SocketSettings socketSettings;
private final SocketSettings heartbeatSocketSettings;
private final ConnectionPoolSettings connectionPoolSettings;
private final ServerSettings serverSettings;
private final SslSettings sslSettings;
private final String applicationName;
private final List<MongoCompressor> compressorList;
private final UuidRepresentation uuidRepresentation;
private final ServerApi serverApi;
private final AutoEncryptionSettings autoEncryptionSettings;
private final boolean heartbeatSocketTimeoutSetExplicitly;
private final boolean heartbeatConnectTimeoutSetExplicitly;
private final ContextProvider contextProvider;
private final DnsClient dnsClient;
private final InetAddressResolver inetAddressResolver;
/**
* Gets the default codec registry. It includes the following providers:
*
* <ul>
* <li>{@link org.bson.codecs.ValueCodecProvider}</li>
* <li>{@link org.bson.codecs.BsonValueCodecProvider}</li>
* <li>{@link com.mongodb.DBRefCodecProvider}</li>
* <li>{@link com.mongodb.DBObjectCodecProvider}</li>
* <li>{@link org.bson.codecs.DocumentCodecProvider}</li>
* <li>{@link org.bson.codecs.CollectionCodecProvider}</li>
* <li>{@link org.bson.codecs.IterableCodecProvider}</li>
* <li>{@link org.bson.codecs.MapCodecProvider}</li>
* <li>{@link com.mongodb.client.model.geojson.codecs.GeoJsonCodecProvider}</li>
* <li>{@link com.mongodb.client.gridfs.codecs.GridFSFileCodecProvider}</li>
* <li>{@link org.bson.codecs.jsr310.Jsr310CodecProvider}</li>
* <li>{@link org.bson.codecs.JsonObjectCodecProvider}</li>
* <li>{@link org.bson.codecs.BsonCodecProvider}</li>
* <li>{@link org.bson.codecs.EnumCodecProvider}</li>
* <li>{@link ExpressionCodecProvider}</li>
* <li>{@link com.mongodb.Jep395RecordCodecProvider}</li>
* <li>{@link com.mongodb.KotlinCodecProvider}</li>
* </ul>
*
* <p>
* Additional providers may be added in a future release.
* </p>
*
* @return the default codec registry
*/
public static CodecRegistry getDefaultCodecRegistry() {
return DEFAULT_CODEC_REGISTRY;
}
/**
* Convenience method to create a Builder.
*
* @return a builder
*/
public static Builder builder() {
return new Builder();
}
/**
* Convenience method to create a from an existing {@code MongoClientSettings}.
*
* @param settings create a builder from existing settings
* @return a builder
*/
public static Builder builder(final MongoClientSettings settings) {
return new Builder(settings);
}
/**
* Gets the {@link DnsClient} to use for resolving DNS queries.
*
* <p>If set, it will be used to resolve SRV and TXT records for mongodb+srv connections. Otherwise,
* implementations of {@link com.mongodb.spi.dns.DnsClientProvider} will be discovered via {@link java.util.ServiceLoader}.
* If no implementations are discovered, then {@code com.sun.jndi.dns.DnsContextFactory} will be used to resolve these records.
*
* <p>If applying a connection string to these settings, care must be taken to also pass the same {@link DnsClient} as an argument to
* the {@link ConnectionString} constructor.
*
* @return the DNS client
* @since 4.10
* @see ConnectionString#ConnectionString(String, DnsClient)
*/
@Nullable
public DnsClient getDnsClient() {
return dnsClient;
}
/**
* Gets the {@link InetAddressResolver} to use for looking up the {@link java.net.InetAddress} instances for each host.
*
* <p>If set, it will be used to look up the {@link java.net.InetAddress} for each host, via
* {@link InetAddressResolver#lookupByName(String)}. Otherwise, {@link java.net.InetAddress#getAllByName(String)} will be used.
*
* @return the {@link java.net.InetAddress} resolver
* @since 4.10
*/
@Nullable
public InetAddressResolver getInetAddressResolver() {
return inetAddressResolver;
}
/**
* A builder for {@code MongoClientSettings} so that {@code MongoClientSettings} can be immutable, and to support easier construction
* through chaining.
*/
@NotThreadSafe
public static final class Builder {
private ReadPreference readPreference = ReadPreference.primary();
private WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
private boolean retryWrites = true;
private boolean retryReads = true;
private ReadConcern readConcern = ReadConcern.DEFAULT;
private CodecRegistry codecRegistry = MongoClientSettings.getDefaultCodecRegistry();
private TransportSettings transportSettings;
/**
* {@linkplain StreamFactoryFactoryWrapping#wrap(StreamFactoryFactory, ConnectionPoolSettings) Requires wrapping}.
*/
private StreamFactoryFactory streamFactoryFactory;
private List<CommandListener> commandListeners = new ArrayList<>();
private final LoggerSettings.Builder loggerSettingsBuilder = LoggerSettings.builder();
private final ClusterSettings.Builder clusterSettingsBuilder = ClusterSettings.builder();
private final SocketSettings.Builder socketSettingsBuilder = SocketSettings.builder();
private final ConnectionPoolSettings.Builder connectionPoolSettingsBuilder = ConnectionPoolSettings.builder();
private final ServerSettings.Builder serverSettingsBuilder = ServerSettings.builder();
private final SslSettings.Builder sslSettingsBuilder = SslSettings.builder();
private MongoCredential credential;
private String applicationName;
private List<MongoCompressor> compressorList = Collections.emptyList();
private UuidRepresentation uuidRepresentation = UuidRepresentation.UNSPECIFIED;
private ServerApi serverApi;
private AutoEncryptionSettings autoEncryptionSettings;
private int heartbeatConnectTimeoutMS;
private int heartbeatSocketTimeoutMS;
private ContextProvider contextProvider;
private DnsClient dnsClient;
private InetAddressResolver inetAddressResolver;
private Builder() {
}
private Builder(final MongoClientSettings settings) {
notNull("settings", settings);
applicationName = settings.getApplicationName();
commandListeners = new ArrayList<>(settings.getCommandListeners());
compressorList = new ArrayList<>(settings.getCompressorList());
codecRegistry = settings.getCodecRegistry();
readPreference = settings.getReadPreference();
writeConcern = settings.getWriteConcern();
retryWrites = settings.getRetryWrites();
retryReads = settings.getRetryReads();
readConcern = settings.getReadConcern();
credential = settings.getCredential();
uuidRepresentation = settings.getUuidRepresentation();
serverApi = settings.getServerApi();
dnsClient = settings.getDnsClient();
inetAddressResolver = settings.getInetAddressResolver();
transportSettings = settings.getTransportSettings();
streamFactoryFactory = StreamFactoryFactoryWrapping.unwrap(settings.getStreamFactoryFactory());
autoEncryptionSettings = settings.getAutoEncryptionSettings();
contextProvider = settings.getContextProvider();
loggerSettingsBuilder.applySettings(settings.getLoggerSettings());
clusterSettingsBuilder.applySettings(settings.getClusterSettings());
serverSettingsBuilder.applySettings(settings.getServerSettings());
socketSettingsBuilder.applySettings(settings.getSocketSettings());
connectionPoolSettingsBuilder.applySettings(settings.getConnectionPoolSettings());
sslSettingsBuilder.applySettings(settings.getSslSettings());
if (settings.heartbeatConnectTimeoutSetExplicitly) {
heartbeatConnectTimeoutMS = settings.heartbeatSocketSettings.getConnectTimeout(MILLISECONDS);
}
if (settings.heartbeatSocketTimeoutSetExplicitly) {
heartbeatSocketTimeoutMS = settings.heartbeatSocketSettings.getReadTimeout(MILLISECONDS);
}
}
/**
* Takes the settings from the given {@code ConnectionString} and applies them to the builder
*
* @param connectionString the connection string containing details of how to connect to MongoDB
* @return this
*/
public Builder applyConnectionString(final ConnectionString connectionString) {
if (connectionString.getApplicationName() != null) {
applicationName = connectionString.getApplicationName();
}
clusterSettingsBuilder.applyConnectionString(connectionString);
if (!connectionString.getCompressorList().isEmpty()) {
compressorList = connectionString.getCompressorList();
}
connectionPoolSettingsBuilder.applyConnectionString(connectionString);
if (connectionString.getCredential() != null) {
credential = connectionString.getCredential();
}
if (connectionString.getReadConcern() != null) {
readConcern = connectionString.getReadConcern();
}
if (connectionString.getReadPreference() != null) {
readPreference = connectionString.getReadPreference();
}
Boolean retryWritesValue = connectionString.getRetryWritesValue();
if (retryWritesValue != null) {
retryWrites = retryWritesValue;
}
Boolean retryReadsValue = connectionString.getRetryReads();
if (retryReadsValue != null) {
retryReads = retryReadsValue;
}
if (connectionString.getUuidRepresentation() != null) {
uuidRepresentation = connectionString.getUuidRepresentation();
}
serverSettingsBuilder.applyConnectionString(connectionString);
socketSettingsBuilder.applyConnectionString(connectionString);
sslSettingsBuilder.applyConnectionString(connectionString);
if (connectionString.getWriteConcern() != null) {
writeConcern = connectionString.getWriteConcern();
}
Boolean grpc = connectionString.isGrpc();
if (grpc != null) {
if (streamFactoryFactory == null && grpc) {
streamFactoryFactory = GrpcStreamFactoryFactory.builder().build();
} else if (grpc(streamFactoryFactory) && !grpc) {
throw new IllegalArgumentException(
streamFactoryFactory.getClass().getSimpleName() + " can not be specified with gRPC=false");
} else if (!grpc(streamFactoryFactory) && grpc) {
throw new IllegalArgumentException(
streamFactoryFactory.getClass().getSimpleName() + " can not be specified with gRPC=true");
}
}
return this;
}
/**
* Applies the {@link LoggerSettings.Builder} block and then sets the loggerSettings.
*
* @param block the block to apply to the LoggerSettings.
* @return this
* @see MongoClientSettings#getLoggerSettings()
* @since 4.9
*/
public Builder applyToLoggerSettings(final Block<LoggerSettings.Builder> block) {
notNull("block", block).apply(loggerSettingsBuilder);
return this;
}
/**
* Applies the {@link ClusterSettings.Builder} block and then sets the clusterSettings.
*
* @param block the block to apply to the ClusterSettings.
* @return this
* @see MongoClientSettings#getClusterSettings()
*/
public Builder applyToClusterSettings(final Block<ClusterSettings.Builder> block) {
notNull("block", block).apply(clusterSettingsBuilder);
return this;
}
/**
* Applies the {@link SocketSettings.Builder} block and then sets the socketSettings.
*
* @param block the block to apply to the SocketSettings.
* @return this
* @see MongoClientSettings#getSocketSettings()
*/
public Builder applyToSocketSettings(final Block<SocketSettings.Builder> block) {
notNull("block", block).apply(socketSettingsBuilder);
return this;
}
/**
* Applies the {@link ConnectionPoolSettings.Builder} block and then sets the connectionPoolSettings.
* <p>
* Must not be changed if {@linkplain #isGrpc() gRPC is enabled}.</p>
*
* @param block the block to apply to the ConnectionPoolSettings.
* @return this
* @see MongoClientSettings#getConnectionPoolSettings()
*/
public Builder applyToConnectionPoolSettings(final Block<ConnectionPoolSettings.Builder> block) {
notNull("block", block).apply(connectionPoolSettingsBuilder);
return this;
}
/**
* Applies the {@link ServerSettings.Builder} block and then sets the serverSettings.
*
* @param block the block to apply to the ServerSettings.
* @return this
* @see MongoClientSettings#getServerSettings()
*/
public Builder applyToServerSettings(final Block<ServerSettings.Builder> block) {
notNull("block", block).apply(serverSettingsBuilder);
return this;
}
/**
* Applies the {@link SslSettings.Builder} block and then sets the sslSettings.
*
* @param block the block to apply to the SslSettings.
* @return this
* @see MongoClientSettings#getSslSettings()
*/
public Builder applyToSslSettings(final Block<SslSettings.Builder> block) {
notNull("block", block).apply(sslSettingsBuilder);
return this;
}
/**
* Sets the read preference.
*
* @param readPreference read preference
* @return this
* @see MongoClientSettings#getReadPreference()
*/
public Builder readPreference(final ReadPreference readPreference) {
this.readPreference = notNull("readPreference", readPreference);
return this;
}
/**
* Sets the write concern.
*
* @param writeConcern the write concern.
* <p>
* {@link WriteConcern#withWTimeout(long, TimeUnit) wtimeout} must not be specified if {@linkplain #isGrpc() gRPC is enabled}.</p>
* @return this
* @see MongoClientSettings#getWriteConcern()
*/
public Builder writeConcern(final WriteConcern writeConcern) {
this.writeConcern = notNull("writeConcern", writeConcern);
return this;
}
/**
* Sets whether writes should be retried if they fail due to a network error.
*
* <p>Starting with the 3.11.0 release, the default value is true</p>
*
* @param retryWrites sets if writes should be retried if they fail due to a network error.
* @return this
* @see #getRetryWrites()
* @mongodb.server.release 3.6
*/
public Builder retryWrites(final boolean retryWrites) {
this.retryWrites = retryWrites;
return this;
}
/**
* Sets whether reads should be retried if they fail due to a network error.
*
* @param retryReads sets if reads should be retried if they fail due to a network error.
* @return this
* @see #getRetryReads()
* @since 3.11
* @mongodb.server.release 3.6
*/
public Builder retryReads(final boolean retryReads) {
this.retryReads = retryReads;
return this;
}
/**
* Sets the read concern.
*
* @param readConcern the read concern
* @return this
* @mongodb.server.release 3.2
* @mongodb.driver.manual reference/readConcern/ Read Concern
*/
public Builder readConcern(final ReadConcern readConcern) {
this.readConcern = notNull("readConcern", readConcern);
return this;
}
/**
* Sets the credential.
*
* @param credential the credential
* @return this
*/
public Builder credential(final MongoCredential credential) {
this.credential = notNull("credential", credential);
return this;
}
/**
* Sets the codec registry
*
* <p>The {@link CodecRegistry} configured by this method is effectively treated by the driver as an instance of
* {@link org.bson.codecs.configuration.CodecProvider}, which {@link CodecRegistry} extends. So there is no benefit to defining
* a class that implements {@link CodecRegistry}. Rather, an application should always create {@link CodecRegistry} instances
* using the factory methods in {@link org.bson.codecs.configuration.CodecRegistries}.</p>
*
* @param codecRegistry the codec registry
* @return this
* @see MongoClientSettings#getCodecRegistry()
* @see org.bson.codecs.configuration.CodecRegistries
*/
public Builder codecRegistry(final CodecRegistry codecRegistry) {
this.codecRegistry = notNull("codecRegistry", codecRegistry);
return this;
}
/**
* Sets the factory to use to create a {@code StreamFactory}.
*
* @param streamFactoryFactory the stream factory factory
* @return this
* @see #getStreamFactoryFactory()
* @deprecated Prefer {@link #transportSettings(TransportSettings)}
*/
@Deprecated
public Builder streamFactoryFactory(final StreamFactoryFactory streamFactoryFactory) {
this.streamFactoryFactory = StreamFactoryFactoryWrapping.unwrap(notNull("streamFactoryFactory", streamFactoryFactory));
return this;
}
/**
* Sets the {@link TransportSettings} to apply.
*
* <p>
* If transport settings are applied, application of {@link #streamFactoryFactory} is ignored.
* </p>
*
* @param transportSettings the transport settings
* @return this
* @see #getTransportSettings()
*/
public Builder transportSettings(final TransportSettings transportSettings) {
this.transportSettings = notNull("transportSettings", transportSettings);
return this;
}
/**
* Adds the given command listener.
*
* @param commandListener the command listener
* @return this
*/
public Builder addCommandListener(final CommandListener commandListener) {
notNull("commandListener", commandListener);
commandListeners.add(commandListener);
return this;
}
/**
* Sets the command listeners
*
* @param commandListeners the list of command listeners
* @return this
*/
public Builder commandListenerList(final List<CommandListener> commandListeners) {
notNull("commandListeners", commandListeners);
this.commandListeners = new ArrayList<>(commandListeners);
return this;
}
/**
* Sets the logical name of the application using this MongoClient. The application name may be used by the client to identify
* the application to the server, for use in server logs, slow query logs, and profile collection.
*
* @param applicationName the logical name of the application using this MongoClient. It may be null.
* The UTF-8 encoding may not exceed 128 bytes.
* @return this
* @see #getApplicationName()
* @mongodb.server.release 3.4
*/
public Builder applicationName(@Nullable final String applicationName) {
if (applicationName != null) {
isTrueArgument("applicationName UTF-8 encoding length <= 128",
applicationName.getBytes(StandardCharsets.UTF_8).length <= 128);
}
this.applicationName = applicationName;
return this;
}
/**
* Sets the compressors to use for compressing messages to the server. The driver will use the first compressor in the list
* that the server is configured to support.
*
* @param compressorList the list of compressors to request
* @return this
* @see #getCompressorList()
* @mongodb.server.release 3.4
*/
public Builder compressorList(final List<MongoCompressor> compressorList) {
notNull("compressorList", compressorList);
this.compressorList = new ArrayList<>(compressorList);
return this;
}
/**
* Sets the UUID representation to use when encoding instances of {@link java.util.UUID} and when decoding BSON binary values with
* subtype of 3.
*
* <p>See {@link #getUuidRepresentation()} for recommendations on settings this value</p>
*
* @param uuidRepresentation the UUID representation, which may not be null
* @return this
* @since 3.12
*/
public Builder uuidRepresentation(final UuidRepresentation uuidRepresentation) {
this.uuidRepresentation = notNull("uuidRepresentation", uuidRepresentation);
return this;
}
/**
* Sets the server API to use when sending commands to the server.
* <p>
* This is required for some MongoDB deployments.
* </p>
*
* @param serverApi the server API, which may not be null
* @return this
* @since 4.3
*/
public Builder serverApi(final ServerApi serverApi) {
this.serverApi = notNull("serverApi", serverApi);
return this;
}
/**
* Sets the auto-encryption settings
* <p>
* A separate, internal {@code MongoClient} is created if any of the following are true:
*
* <ul>
* <li>{@code AutoEncryptionSettings.keyVaultClient} is not passed</li>
* <li>{@code AutoEncryptionSettings.bypassAutomaticEncryption} is {@code false}</li>
* </ul>
*
* If an internal {@code MongoClient} is created, it is configured with the same
* options as the parent {@code MongoClient} except {@code minPoolSize} is set to {@code 0}
* and {@code AutoEncryptionSettings} is omitted.
*
* @param autoEncryptionSettings the auto-encryption settings
* @return this
* @since 3.11
* @see #getAutoEncryptionSettings()
*/
public Builder autoEncryptionSettings(@Nullable final AutoEncryptionSettings autoEncryptionSettings) {
this.autoEncryptionSettings = autoEncryptionSettings;
return this;
}
/**
* Sets the context provider
*
* <p>
* When used with the synchronous driver, this must be an instance of {@code com.mongodb.client.SynchronousContextProvider}.
* When used with the reactive streams driver, this must be an instance of
* {@code com.mongodb.reactivestreams.client.ReactiveContextProvider}.
*
* </p>
*
* @param contextProvider the context provider
* @return this
* @since 4.4
*/
public Builder contextProvider(@Nullable final ContextProvider contextProvider) {
this.contextProvider = contextProvider;
return this;
}
/**
* Sets the {@link DnsClient} to use for resolving DNS queries.
*
* <p> If set, it will be used to resolve SRV and TXT records for mongodb+srv connections. Otherwise,
* implementation of {@link com.mongodb.spi.dns.DnsClientProvider} will be discovered via {@link java.util.ServiceLoader}
* and used to create an instance of {@link DnsClient}. If no implementation is discovered, then
* {@code com.sun.jndi.dns.DnsContextFactory} will be used to resolve these records.
*
* <p>If {@linkplain #applyConnectionString(ConnectionString) applying a connection string to these settings}, care must be
* taken to also pass the same {@link DnsClient} as an argument to the {@link ConnectionString} constructor.
*
* @param dnsClient the DNS client
* @return the DNS client
* @since 4.10
* @see ConnectionString#ConnectionString(String, DnsClient)
*/
public Builder dnsClient(@Nullable final DnsClient dnsClient) {
this.dnsClient = dnsClient;
return this;
}
/**
* Sets the {@link InetAddressResolver} to use for looking up the {@link java.net.InetAddress} instances for each host.
*
* <p>If set, it will be used to look up the {@link java.net.InetAddress} for each host, via
* {@link InetAddressResolver#lookupByName(String)}. Otherwise,
* an implementation of {@link com.mongodb.spi.dns.InetAddressResolverProvider} will be discovered via
* {@link java.util.ServiceLoader} and used to create an instance of {@link InetAddressResolver}. If no implementation is
* discovered, {@link java.net.InetAddress#getAllByName(String)} will be used to lookup the {@link java.net.InetAddress}
* instances for a host.
*
* @param inetAddressResolver the InetAddress provider
* @return the {@link java.net.InetAddress} resolver
* @since 4.10
*/
public Builder inetAddressResolver(@Nullable final InetAddressResolver inetAddressResolver) {
this.inetAddressResolver = inetAddressResolver;
return this;
}
// Package-private to provide interop with MongoClientOptions
Builder heartbeatConnectTimeoutMS(final int heartbeatConnectTimeoutMS) {
this.heartbeatConnectTimeoutMS = heartbeatConnectTimeoutMS;
return this;
}
// Package-private to provide interop with MongoClientOptions
Builder heartbeatSocketTimeoutMS(final int heartbeatSocketTimeoutMS) {
this.heartbeatSocketTimeoutMS = heartbeatSocketTimeoutMS;
return this;
}
/**
* Build an instance of {@code MongoClientSettings}.
*
* @return the settings from this builder
*/
public MongoClientSettings build() {
return new MongoClientSettings(this);
}
}
/**
* The read preference to use for queries, map-reduce, aggregation, and count.
*
* <p>Default is {@code ReadPreference.primary()}.</p>
*
* @return the read preference
* @see ReadPreference#primary()
*/
public ReadPreference getReadPreference() {
return readPreference;
}
/**
* Gets the credential.
*
* @return the credential, which may be null
*/
@Nullable
public MongoCredential getCredential() {
return credential;
}
/**
* The write concern to use.
*
* <p>Default is {@code WriteConcern.ACKNOWLEDGED}.</p>
*
* @return the write concern
* @see Builder#writeConcern(WriteConcern)
*/
public WriteConcern getWriteConcern() {
return writeConcern;
}
/**
* Returns true if writes should be retried if they fail due to a network error or other retryable error.
*
* <p>Starting with the 3.11.0 release, the default value is true</p>
*
* @return the retryWrites value
* @mongodb.server.release 3.6
*/
public boolean getRetryWrites() {
return retryWrites;
}
/**
* Returns true if reads should be retried if they fail due to a network error or other retryable error. The default value is true.
*
* @return the retryReads value
* @since 3.11
* @mongodb.server.release 3.6
*/
public boolean getRetryReads() {
return retryReads;
}
/**
* The read concern to use.
*
* @return the read concern
* @mongodb.server.release 3.2
* @mongodb.driver.manual reference/readConcern/ Read Concern
*/
public ReadConcern getReadConcern() {
return readConcern;
}
/**
* The codec registry to use, or null if not set.
*
* @return the codec registry
*/
public CodecRegistry getCodecRegistry() {
return codecRegistry;
}
/**
* Gets the factory to use to create a {@code StreamFactory}.
*
* @return the stream factory factory
* @see Builder#streamFactoryFactory(StreamFactoryFactory)
* @deprecated Prefer {@link #getTransportSettings()}
*/
@Deprecated
@Nullable
public StreamFactoryFactory getStreamFactoryFactory() {
StreamFactoryFactoryWrapping.assertRequiresNoFurtherWrapping(streamFactoryFactory);
return streamFactoryFactory;
}
/**
* Gets whether gRPC is enabled.
*
* @return {@code true} if gRPC is enabled, {@code false} if not.
* @see ConnectionString#isGrpc()
* @see MongoClientSettings.Builder#streamFactoryFactory(StreamFactoryFactory)
* @see GrpcStreamFactoryFactory
* @since VAKOTODO
* @mongodb.server.release VAKOTODO
*/
public boolean isGrpc() {
return grpc(streamFactoryFactory);
}
/**
* Gets the settings for the underlying transport implementation
*
* @return the settings for the underlying transport implementation
*
* @since 4.11
* @see Builder#transportSettings(TransportSettings)
*/
@Nullable
public TransportSettings getTransportSettings() {
return transportSettings;
}
/**
* Gets the list of added {@code CommandListener}.
*
* <p>The default is an empty list.</p>
*
* @return the unmodifiable list of command listeners
*/
public List<CommandListener> getCommandListeners() {
return Collections.unmodifiableList(commandListeners);
}
/**
* Gets the logical name of the application using this MongoClient. The application name may be used by the client to identify
* the application to the server, for use in server logs, slow query logs, and profile collection.
*
* <p>Default is null.</p>
*
* @return the application name, which may be null
* @see Builder#applicationName(String)
* @mongodb.server.release 3.4
*/
@Nullable
public String getApplicationName() {
return applicationName;
}
/**
* Gets the compressors to use for compressing messages to the server. The driver will use the first compressor in the list
* that the server is configured to support.
*
* <p>Default is the empty list.</p>
*
* @return the compressors
* @mongodb.server.release 3.4
*/
public List<MongoCompressor> getCompressorList() {
return Collections.unmodifiableList(compressorList);
}
/**
* Gets the UUID representation to use when encoding instances of {@link java.util.UUID} and when decoding BSON binary values with
* subtype of 3.
*
* <p>The default is {@link UuidRepresentation#UNSPECIFIED}, If your application stores UUID values in MongoDB, you must set this
* value to the desired representation. New applications should prefer {@link UuidRepresentation#STANDARD}, while existing Java
* applications should prefer {@link UuidRepresentation#JAVA_LEGACY}. Applications wishing to interoperate with existing Python or
* .NET applications should prefer {@link UuidRepresentation#PYTHON_LEGACY} or {@link UuidRepresentation#C_SHARP_LEGACY},
* respectively. Applications that do not store UUID values in MongoDB don't need to set this value.
* </p>
*
* @return the UUID representation, which may not be null
* @since 3.12
*/
public UuidRepresentation getUuidRepresentation() {
return uuidRepresentation;
}
/**
* Gets the server API to use when sending commands to the server.
*
* @return the server API, which may be null
* @since 4.3
*/
@Nullable
public ServerApi getServerApi() {
return serverApi;
}
/**
* Gets the auto-encryption settings.
* <p>
* In-use encryption enables an application to specify what fields in a collection must be
* encrypted, and the driver automatically encrypts commands and decrypts results.
* </p>
* <p>
* Automatic encryption is an enterprise only feature that only applies to operations on a collection. Automatic encryption is not
* supported for operations on a database or view and will result in error. To bypass automatic encryption,
* set bypassAutoEncryption=true in ClientSideEncryptionOptions.
* </p>
* <p>
* Explicit encryption/decryption and automatic decryption is a community feature, enabled with the new
* {@code com.mongodb.client.vault .ClientEncryption} type. A MongoClient configured with bypassAutoEncryption=true will still
* automatically decrypt.
* </p>
* <p>
* Automatic encryption requires the authenticated user to have the listCollections privilege action.
* </p>
* <p>
* Supplying an {@code encryptedFieldsMap} provides more security than relying on an encryptedFields obtained from the server.
* It protects against a malicious server advertising false encryptedFields.
* </p>
*
* @return the auto-encryption settings, which may be null
* @since 3.11
*/
@Nullable
public AutoEncryptionSettings getAutoEncryptionSettings() {
return autoEncryptionSettings;
}
/**
* Gets the logger settings.
*
* @return the logger settings
* @since 4.9
*/
public LoggerSettings getLoggerSettings() {
return loggerSettings;
}
/**
* Gets the cluster settings.
*
* @return the cluster settings
*/
public ClusterSettings getClusterSettings() {
return clusterSettings;
}
/**
* Gets the SSL settings.
*
* @return the SSL settings
*/
public SslSettings getSslSettings() {
return sslSettings;
}
/**
* Gets the connection-specific settings wrapped in a settings object. This settings object uses the values for connectTimeout
* and socketTimeout.
*
* @return a SocketSettings object populated with the connection settings from this {@code MongoClientSettings} instance.
* @see SocketSettings
*/
public SocketSettings getSocketSettings() {
return socketSettings;
}
/**
* Gets the connection settings for the heartbeat thread (the background task that checks the state of the cluster) wrapped in a
* settings object.