Search in sources :

Example 1 with CloudConfig

use of com.datastax.oss.driver.internal.core.config.cloud.CloudConfig in project java-driver by datastax.

the class SessionBuilder method buildDefaultSessionAsync.

@NonNull
protected final CompletionStage<CqlSession> buildDefaultSessionAsync() {
    try {
        ProgrammaticArguments programmaticArguments = programmaticArgumentsBuilder.build();
        DriverConfigLoader configLoader = this.configLoader != null ? this.configLoader : defaultConfigLoader(programmaticArguments.getClassLoader());
        DriverExecutionProfile defaultConfig = configLoader.getInitialConfig().getDefaultProfile();
        if (cloudConfigInputStream == null) {
            String configUrlString = defaultConfig.getString(DefaultDriverOption.CLOUD_SECURE_CONNECT_BUNDLE, null);
            if (configUrlString != null) {
                cloudConfigInputStream = () -> getURL(configUrlString).openStream();
            }
        }
        List<String> configContactPoints = defaultConfig.getStringList(DefaultDriverOption.CONTACT_POINTS, Collections.emptyList());
        if (cloudConfigInputStream != null) {
            if (!programmaticContactPoints.isEmpty() || !configContactPoints.isEmpty()) {
                LOG.info("Both a secure connect bundle and contact points were provided. These are mutually exclusive. The contact points from the secure bundle will have priority.");
                // clear the contact points provided in the setting file and via addContactPoints
                configContactPoints = Collections.emptyList();
                programmaticContactPoints = new HashSet<>();
            }
            if (programmaticSslFactory || defaultConfig.isDefined(DefaultDriverOption.SSL_ENGINE_FACTORY_CLASS)) {
                LOG.info("Both a secure connect bundle and SSL options were provided. They are mutually exclusive. The SSL options from the secure bundle will have priority.");
            }
            CloudConfig cloudConfig = new CloudConfigFactory().createCloudConfig(cloudConfigInputStream.call());
            addContactEndPoints(cloudConfig.getEndPoints());
            boolean localDataCenterDefined = anyProfileHasDatacenterDefined(configLoader.getInitialConfig());
            if (programmaticLocalDatacenter || localDataCenterDefined) {
                LOG.info("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.");
                programmaticArgumentsBuilder.clearDatacenters();
            }
            withLocalDatacenter(cloudConfig.getLocalDatacenter());
            withSslEngineFactory(cloudConfig.getSslEngineFactory());
            withCloudProxyAddress(cloudConfig.getProxyAddress());
            programmaticArguments = programmaticArgumentsBuilder.build();
        }
        boolean resolveAddresses = defaultConfig.getBoolean(DefaultDriverOption.RESOLVE_CONTACT_POINTS, true);
        Set<EndPoint> contactPoints = ContactPoints.merge(programmaticContactPoints, configContactPoints, resolveAddresses);
        if (keyspace == null && defaultConfig.isDefined(DefaultDriverOption.SESSION_KEYSPACE)) {
            keyspace = CqlIdentifier.fromCql(defaultConfig.getString(DefaultDriverOption.SESSION_KEYSPACE));
        }
        return DefaultSession.init((InternalDriverContext) buildContext(configLoader, programmaticArguments), contactPoints, keyspace);
    } catch (Throwable t) {
        // failed future if anything goes wrong. So wrap any error from that synchronous part.
        return CompletableFutures.failedFuture(t);
    }
}
Also used : DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) CloudConfig(com.datastax.oss.driver.internal.core.config.cloud.CloudConfig) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) DefaultDriverConfigLoader(com.datastax.oss.driver.internal.core.config.typesafe.DefaultDriverConfigLoader) CloudConfigFactory(com.datastax.oss.driver.internal.core.config.cloud.CloudConfigFactory) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) DefaultEndPoint(com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 CloudConfig (com.datastax.oss.driver.internal.core.config.cloud.CloudConfig)1 CloudConfigFactory (com.datastax.oss.driver.internal.core.config.cloud.CloudConfigFactory)1 DefaultDriverConfigLoader (com.datastax.oss.driver.internal.core.config.typesafe.DefaultDriverConfigLoader)1 DefaultEndPoint (com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1