20
20
import com .datastax .oss .driver .api .core .auth .AuthProvider ;
21
21
import com .datastax .oss .driver .api .core .auth .PlainTextAuthProviderBase ;
22
22
import com .datastax .oss .driver .api .core .config .DefaultDriverOption ;
23
+ import com .datastax .oss .driver .api .core .config .DriverConfig ;
23
24
import com .datastax .oss .driver .api .core .config .DriverConfigLoader ;
24
25
import com .datastax .oss .driver .api .core .config .DriverExecutionProfile ;
25
26
import com .datastax .oss .driver .api .core .context .DriverContext ;
65
66
import java .util .function .Predicate ;
66
67
import javax .net .ssl .SSLContext ;
67
68
import net .jcip .annotations .NotThreadSafe ;
69
+ import org .slf4j .Logger ;
70
+ import org .slf4j .LoggerFactory ;
68
71
69
72
/**
70
73
* Base implementation to build session instances.
77
80
@ NotThreadSafe
78
81
public abstract class SessionBuilder <SelfT extends SessionBuilder , SessionT > {
79
82
83
+ private static final Logger LOG = LoggerFactory .getLogger (SessionBuilder .class );
84
+
80
85
@ SuppressWarnings ("unchecked" )
81
86
protected final SelfT self = (SelfT ) this ;
82
87
@@ -87,7 +92,8 @@ public abstract class SessionBuilder<SelfT extends SessionBuilder, SessionT> {
87
92
88
93
protected ProgrammaticArguments .Builder programmaticArgumentsBuilder =
89
94
ProgrammaticArguments .builder ();
90
- private boolean sslConfigured = false ;
95
+ private boolean programmaticSslFactory = false ;
96
+ private boolean programmaticLocalDatacenter = false ;
91
97
92
98
/**
93
99
* Sets the configuration loader to use.
@@ -314,7 +320,7 @@ public SelfT withAuthCredentials(
314
320
*/
315
321
@ NonNull
316
322
public SelfT withSslEngineFactory (@ Nullable SslEngineFactory sslEngineFactory ) {
317
- this .sslConfigured = true ;
323
+ this .programmaticSslFactory = true ;
318
324
this .programmaticArgumentsBuilder .withSslEngineFactory (sslEngineFactory );
319
325
return self ;
320
326
}
@@ -352,6 +358,7 @@ public SelfT withSslContext(@Nullable SSLContext sslContext) {
352
358
* if you use a third-party implementation, refer to their documentation.
353
359
*/
354
360
public SelfT withLocalDatacenter (@ NonNull String profileName , @ NonNull String localDatacenter ) {
361
+ this .programmaticLocalDatacenter = true ;
355
362
this .programmaticArgumentsBuilder .withLocalDatacenter (profileName , localDatacenter );
356
363
return self ;
357
364
}
@@ -671,24 +678,32 @@ protected final CompletionStage<CqlSession> buildDefaultSessionAsync() {
671
678
defaultConfig .getStringList (DefaultDriverOption .CONTACT_POINTS , Collections .emptyList ());
672
679
if (cloudConfigInputStream != null ) {
673
680
if (!programmaticContactPoints .isEmpty () || !configContactPoints .isEmpty ()) {
674
- throw new IllegalStateException (
675
- "Can't use withCloudSecureConnectBundle and addContactPoint(s). They are mutually exclusive." );
681
+ LOG .info (
682
+ "Both a secure connect bundle and contact points were provided. These are mutually exclusive. The contact points from the secure bundle will have priority." );
683
+ // clear the contact points provided in the setting file and via addContactPoints
684
+ configContactPoints = Collections .emptyList ();
685
+ programmaticContactPoints = new HashSet <>();
676
686
}
677
- String configuredSSLFactory =
678
- defaultConfig . getString ( DefaultDriverOption . SSL_ENGINE_FACTORY_CLASS , null );
679
- if ( sslConfigured || configuredSSLFactory != null ) {
680
- throw new IllegalStateException (
681
- "Can't use withCloudSecureConnectBundle and explicitly specify ssl configuration . They are mutually exclusive." );
687
+
688
+ if ( programmaticSslFactory
689
+ || defaultConfig . isDefined ( DefaultDriverOption . SSL_ENGINE_FACTORY_CLASS ) ) {
690
+ LOG . info (
691
+ "Both a secure connect bundle and SSL options were provided . They are mutually exclusive. The SSL options from the secure bundle will have priority ." );
682
692
}
683
693
CloudConfig cloudConfig =
684
694
new CloudConfigFactory ().createCloudConfig (cloudConfigInputStream .call ());
685
695
addContactEndPoints (cloudConfig .getEndPoints ());
696
+
697
+ boolean localDataCenterDefined =
698
+ anyProfileHasDatacenterDefined (configLoader .getInitialConfig ());
699
+ if (programmaticLocalDatacenter || localDataCenterDefined ) {
700
+ LOG .info (
701
+ "Both a secure connect bundle and a local datacenter were provided. They are mutually exclusive. The local datacenter from the secure bundle will have priority." );
702
+ programmaticArgumentsBuilder .clearDatacenters ();
703
+ }
686
704
withLocalDatacenter (cloudConfig .getLocalDatacenter ());
687
705
withSslEngineFactory (cloudConfig .getSslEngineFactory ());
688
706
withCloudProxyAddress (cloudConfig .getProxyAddress ());
689
- if (cloudConfig .getAuthProvider ().isPresent ()) {
690
- withAuthProvider (cloudConfig .getAuthProvider ().get ());
691
- }
692
707
programmaticArguments = programmaticArgumentsBuilder .build ();
693
708
}
694
709
@@ -715,6 +730,15 @@ protected final CompletionStage<CqlSession> buildDefaultSessionAsync() {
715
730
}
716
731
}
717
732
733
+ private boolean anyProfileHasDatacenterDefined (DriverConfig driverConfig ) {
734
+ for (DriverExecutionProfile driverExecutionProfile : driverConfig .getProfiles ().values ()) {
735
+ if (driverExecutionProfile .isDefined (DefaultDriverOption .LOAD_BALANCING_LOCAL_DATACENTER )) {
736
+ return true ;
737
+ }
738
+ }
739
+ return false ;
740
+ }
741
+
718
742
/**
719
743
* Returns URL based on the configUrl setting. If the configUrl has no protocol provided, the
720
744
* method will fallback to file:// protocol and return URL that has file protocol specified.
0 commit comments