31
31
import java .util .LinkedHashSet ;
32
32
import java .util .List ;
33
33
import java .util .Map ;
34
+ import java .util .Objects ;
34
35
import java .util .Set ;
35
36
import java .util .concurrent .TimeUnit ;
36
37
226
227
* <li>{@code uuidRepresentation=unspecified|standard|javaLegacy|csharpLegacy|pythonLegacy}. See
227
228
* {@link MongoClientSettings#getUuidRepresentation()} for documentation of semantics of this parameter. Defaults to "javaLegacy", but
228
229
* will change to "unspecified" in the next major release.</li>
230
+ * <li>{@code directConnection=true|false}. If true the driver will set the connection to be a direct connection to the host.</li>
229
231
* </ul>
230
232
*
231
233
* @mongodb.driver.manual reference/connection-string Connection String Format
@@ -247,6 +249,7 @@ public class ConnectionString {
247
249
private final String collection ;
248
250
private final String connectionString ;
249
251
252
+ private Boolean directConnection ;
250
253
private ReadPreference readPreference ;
251
254
private WriteConcern writeConcern ;
252
255
private Boolean retryWrites ;
@@ -389,6 +392,15 @@ public ConnectionString(final String connectionString) {
389
392
combinedOptionsMaps .put ("ssl" , singletonList ("true" ));
390
393
}
391
394
translateOptions (combinedOptionsMaps );
395
+
396
+ if (directConnection != null && directConnection ) {
397
+ if (isSrvProtocol ) {
398
+ throw new IllegalArgumentException ("Direct connections are not supported when using mongodb+srv protocol" );
399
+ } else if (hosts .size () > 1 ) {
400
+ throw new IllegalArgumentException ("Direct connections are not supported when using multiple hosts" );
401
+ }
402
+ }
403
+
392
404
credential = createCredentials (combinedOptionsMaps , userName , password );
393
405
warnOnUnsupportedOptions (combinedOptionsMaps );
394
406
}
@@ -435,6 +447,8 @@ public ConnectionString(final String connectionString) {
435
447
436
448
GENERAL_OPTIONS_KEYS .add ("uuidrepresentation" );
437
449
450
+ GENERAL_OPTIONS_KEYS .add ("directconnection" );
451
+
438
452
COMPRESSOR_KEYS .add ("compressors" );
439
453
COMPRESSOR_KEYS .add ("zlibcompressionlevel" );
440
454
@@ -535,6 +549,8 @@ private void translateOptions(final Map<String, List<String>> optionsMap) {
535
549
retryReads = parseBoolean (value , "retryreads" );
536
550
} else if (key .equals ("uuidrepresentation" )) {
537
551
uuidRepresentation = createUuidRepresentation (value );
552
+ } else if (key .equals ("directconnection" )) {
553
+ directConnection = parseBoolean (value , "directconnection" );
538
554
}
539
555
}
540
556
@@ -1082,7 +1098,6 @@ public String getDatabase() {
1082
1098
return database ;
1083
1099
}
1084
1100
1085
-
1086
1101
/**
1087
1102
* Gets the collection name
1088
1103
*
@@ -1093,6 +1108,17 @@ public String getCollection() {
1093
1108
return collection ;
1094
1109
}
1095
1110
1111
+ /**
1112
+ * Indicates if the connection should be a direct connection
1113
+ *
1114
+ * @return true if a direct connection
1115
+ * @since 4.1
1116
+ */
1117
+ @ Nullable
1118
+ public Boolean isDirectConnection () {
1119
+ return directConnection ;
1120
+ }
1121
+
1096
1122
/**
1097
1123
* Get the unparsed connection string.
1098
1124
*
@@ -1337,91 +1363,45 @@ public boolean equals(final Object o) {
1337
1363
if (this == o ) {
1338
1364
return true ;
1339
1365
}
1340
- if (!( o instanceof ConnectionString )) {
1366
+ if (o == null || getClass () != o . getClass ( )) {
1341
1367
return false ;
1342
1368
}
1343
-
1344
- ConnectionString that = (ConnectionString ) o ;
1345
-
1346
- if (collection != null ? !collection .equals (that .collection ) : that .collection != null ) {
1347
- return false ;
1348
- }
1349
- if (connectTimeout != null ? !connectTimeout .equals (that .connectTimeout ) : that .connectTimeout != null ) {
1350
- return false ;
1351
- }
1352
- if (credential != null ? !credential .equals (that .credential ) : that .credential != null ) {
1353
- return false ;
1354
- }
1355
- if (database != null ? !database .equals (that .database ) : that .database != null ) {
1356
- return false ;
1357
- }
1358
- if (!hosts .equals (that .hosts )) {
1359
- return false ;
1360
- }
1361
- if (maxConnectionIdleTime != null ? !maxConnectionIdleTime .equals (that .maxConnectionIdleTime )
1362
- : that .maxConnectionIdleTime != null ) {
1363
- return false ;
1364
- }
1365
- if (maxConnectionLifeTime != null ? !maxConnectionLifeTime .equals (that .maxConnectionLifeTime )
1366
- : that .maxConnectionLifeTime != null ) {
1367
- return false ;
1368
- }
1369
- if (maxConnectionPoolSize != null ? !maxConnectionPoolSize .equals (that .maxConnectionPoolSize )
1370
- : that .maxConnectionPoolSize != null ) {
1371
- return false ;
1372
- }
1373
- if (maxWaitTime != null ? !maxWaitTime .equals (that .maxWaitTime ) : that .maxWaitTime != null ) {
1374
- return false ;
1375
- }
1376
- if (minConnectionPoolSize != null ? !minConnectionPoolSize .equals (that .minConnectionPoolSize )
1377
- : that .minConnectionPoolSize != null ) {
1378
- return false ;
1379
- }
1380
- if (readPreference != null ? !readPreference .equals (that .readPreference ) : that .readPreference != null ) {
1381
- return false ;
1382
- }
1383
- if (requiredReplicaSetName != null ? !requiredReplicaSetName .equals (that .requiredReplicaSetName )
1384
- : that .requiredReplicaSetName != null ) {
1385
- return false ;
1386
- }
1387
- if (socketTimeout != null ? !socketTimeout .equals (that .socketTimeout ) : that .socketTimeout != null ) {
1388
- return false ;
1389
- }
1390
- if (sslEnabled != null ? !sslEnabled .equals (that .sslEnabled ) : that .sslEnabled != null ) {
1391
- return false ;
1392
- }
1393
- if (writeConcern != null ? !writeConcern .equals (that .writeConcern ) : that .writeConcern != null ) {
1394
- return false ;
1395
- }
1396
- if (applicationName != null ? !applicationName .equals (that .applicationName ) : that .applicationName != null ) {
1397
- return false ;
1398
- }
1399
- if (!compressorList .equals (that .compressorList )) {
1400
- return false ;
1401
- }
1402
-
1403
- return true ;
1369
+ final ConnectionString that = (ConnectionString ) o ;
1370
+ return isSrvProtocol == that .isSrvProtocol
1371
+ && Objects .equals (directConnection , that .directConnection )
1372
+ && Objects .equals (credential , that .credential )
1373
+ && Objects .equals (hosts , that .hosts )
1374
+ && Objects .equals (database , that .database )
1375
+ && Objects .equals (collection , that .collection )
1376
+ && Objects .equals (readPreference , that .readPreference )
1377
+ && Objects .equals (writeConcern , that .writeConcern )
1378
+ && Objects .equals (retryWrites , that .retryWrites )
1379
+ && Objects .equals (retryReads , that .retryReads )
1380
+ && Objects .equals (readConcern , that .readConcern )
1381
+ && Objects .equals (minConnectionPoolSize , that .minConnectionPoolSize )
1382
+ && Objects .equals (maxConnectionPoolSize , that .maxConnectionPoolSize )
1383
+ && Objects .equals (maxWaitTime , that .maxWaitTime )
1384
+ && Objects .equals (maxConnectionIdleTime , that .maxConnectionIdleTime )
1385
+ && Objects .equals (maxConnectionLifeTime , that .maxConnectionLifeTime )
1386
+ && Objects .equals (connectTimeout , that .connectTimeout )
1387
+ && Objects .equals (socketTimeout , that .socketTimeout )
1388
+ && Objects .equals (sslEnabled , that .sslEnabled )
1389
+ && Objects .equals (sslInvalidHostnameAllowed , that .sslInvalidHostnameAllowed )
1390
+ && Objects .equals (requiredReplicaSetName , that .requiredReplicaSetName )
1391
+ && Objects .equals (serverSelectionTimeout , that .serverSelectionTimeout )
1392
+ && Objects .equals (localThreshold , that .localThreshold )
1393
+ && Objects .equals (heartbeatFrequency , that .heartbeatFrequency )
1394
+ && Objects .equals (applicationName , that .applicationName )
1395
+ && Objects .equals (compressorList , that .compressorList )
1396
+ && Objects .equals (uuidRepresentation , that .uuidRepresentation );
1404
1397
}
1405
1398
1406
1399
@ Override
1407
1400
public int hashCode () {
1408
- int result = credential != null ? credential .hashCode () : 0 ;
1409
- result = 31 * result + hosts .hashCode ();
1410
- result = 31 * result + (database != null ? database .hashCode () : 0 );
1411
- result = 31 * result + (collection != null ? collection .hashCode () : 0 );
1412
- result = 31 * result + (readPreference != null ? readPreference .hashCode () : 0 );
1413
- result = 31 * result + (writeConcern != null ? writeConcern .hashCode () : 0 );
1414
- result = 31 * result + (minConnectionPoolSize != null ? minConnectionPoolSize .hashCode () : 0 );
1415
- result = 31 * result + (maxConnectionPoolSize != null ? maxConnectionPoolSize .hashCode () : 0 );
1416
- result = 31 * result + (maxWaitTime != null ? maxWaitTime .hashCode () : 0 );
1417
- result = 31 * result + (maxConnectionIdleTime != null ? maxConnectionIdleTime .hashCode () : 0 );
1418
- result = 31 * result + (maxConnectionLifeTime != null ? maxConnectionLifeTime .hashCode () : 0 );
1419
- result = 31 * result + (connectTimeout != null ? connectTimeout .hashCode () : 0 );
1420
- result = 31 * result + (socketTimeout != null ? socketTimeout .hashCode () : 0 );
1421
- result = 31 * result + (sslEnabled != null ? sslEnabled .hashCode () : 0 );
1422
- result = 31 * result + (requiredReplicaSetName != null ? requiredReplicaSetName .hashCode () : 0 );
1423
- result = 31 * result + (applicationName != null ? applicationName .hashCode () : 0 );
1424
- result = 31 * result + compressorList .hashCode ();
1425
- return result ;
1401
+ return Objects .hash (credential , isSrvProtocol , hosts , database , collection , directConnection , readPreference ,
1402
+ writeConcern , retryWrites , retryReads , readConcern , minConnectionPoolSize , maxConnectionPoolSize , maxWaitTime ,
1403
+ maxConnectionIdleTime , maxConnectionLifeTime , connectTimeout , socketTimeout , sslEnabled , sslInvalidHostnameAllowed ,
1404
+ requiredReplicaSetName , serverSelectionTimeout , localThreshold , heartbeatFrequency , applicationName , compressorList ,
1405
+ uuidRepresentation );
1426
1406
}
1427
1407
}
0 commit comments