2020import com .datastax .oss .driver .api .core .auth .AuthProvider ;
2121import com .datastax .oss .driver .api .core .auth .PlainTextAuthProviderBase ;
2222import com .datastax .oss .driver .api .core .config .DefaultDriverOption ;
23+ import com .datastax .oss .driver .api .core .config .DriverConfig ;
2324import com .datastax .oss .driver .api .core .config .DriverConfigLoader ;
2425import com .datastax .oss .driver .api .core .config .DriverExecutionProfile ;
2526import com .datastax .oss .driver .api .core .context .DriverContext ;
6566import java .util .function .Predicate ;
6667import javax .net .ssl .SSLContext ;
6768import net .jcip .annotations .NotThreadSafe ;
69+ import org .slf4j .Logger ;
70+ import org .slf4j .LoggerFactory ;
6871
6972/**
7073 * Base implementation to build session instances.
7780@ NotThreadSafe
7881public abstract class SessionBuilder <SelfT extends SessionBuilder , SessionT > {
7982
83+ private static final Logger LOG = LoggerFactory .getLogger (SessionBuilder .class );
84+
8085 @ SuppressWarnings ("unchecked" )
8186 protected final SelfT self = (SelfT ) this ;
8287
@@ -87,7 +92,8 @@ public abstract class SessionBuilder<SelfT extends SessionBuilder, SessionT> {
8792
8893 protected ProgrammaticArguments .Builder programmaticArgumentsBuilder =
8994 ProgrammaticArguments .builder ();
90- private boolean sslConfigured = false ;
95+ private boolean programmaticSslFactory = false ;
96+ private boolean programmaticLocalDatacenter = false ;
9197
9298 /**
9399 * Sets the configuration loader to use.
@@ -314,7 +320,7 @@ public SelfT withAuthCredentials(
314320 */
315321 @ NonNull
316322 public SelfT withSslEngineFactory (@ Nullable SslEngineFactory sslEngineFactory ) {
317- this .sslConfigured = true ;
323+ this .programmaticSslFactory = true ;
318324 this .programmaticArgumentsBuilder .withSslEngineFactory (sslEngineFactory );
319325 return self ;
320326 }
@@ -352,6 +358,7 @@ public SelfT withSslContext(@Nullable SSLContext sslContext) {
352358 * if you use a third-party implementation, refer to their documentation.
353359 */
354360 public SelfT withLocalDatacenter (@ NonNull String profileName , @ NonNull String localDatacenter ) {
361+ this .programmaticLocalDatacenter = true ;
355362 this .programmaticArgumentsBuilder .withLocalDatacenter (profileName , localDatacenter );
356363 return self ;
357364 }
@@ -671,18 +678,29 @@ protected final CompletionStage<CqlSession> buildDefaultSessionAsync() {
671678 defaultConfig .getStringList (DefaultDriverOption .CONTACT_POINTS , Collections .emptyList ());
672679 if (cloudConfigInputStream != null ) {
673680 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 <>();
676686 }
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 ." );
682692 }
683693 CloudConfig cloudConfig =
684694 new CloudConfigFactory ().createCloudConfig (cloudConfigInputStream .call ());
685695 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+ }
686704 withLocalDatacenter (cloudConfig .getLocalDatacenter ());
687705 withSslEngineFactory (cloudConfig .getSslEngineFactory ());
688706 withCloudProxyAddress (cloudConfig .getProxyAddress ());
@@ -715,6 +733,15 @@ protected final CompletionStage<CqlSession> buildDefaultSessionAsync() {
715733 }
716734 }
717735
736+ private boolean anyProfileHasDatacenterDefined (DriverConfig driverConfig ) {
737+ for (DriverExecutionProfile driverExecutionProfile : driverConfig .getProfiles ().values ()) {
738+ if (driverExecutionProfile .isDefined (DefaultDriverOption .LOAD_BALANCING_LOCAL_DATACENTER )) {
739+ return true ;
740+ }
741+ }
742+ return false ;
743+ }
744+
718745 /**
719746 * Returns URL based on the configUrl setting. If the configUrl has no protocol provided, the
720747 * method will fallback to file:// protocol and return URL that has file protocol specified.
0 commit comments