diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java index 1c3b150ea5..643936d367 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.cassandra; +import java.util.List; + import com.datastax.driver.core.Cluster; import com.datastax.driver.core.QueryOptions; import com.datastax.driver.core.SocketOptions; @@ -24,6 +26,7 @@ import com.datastax.driver.core.policies.ReconnectionPolicy; import com.datastax.driver.core.policies.RetryPolicy; import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -46,8 +49,12 @@ public class CassandraAutoConfiguration { private final CassandraProperties properties; - public CassandraAutoConfiguration(CassandraProperties properties) { + private final List clusterCustomizers; + + public CassandraAutoConfiguration(CassandraProperties properties, + ObjectProvider> clusterCustomizersProvider) { this.properties = properties; + this.clusterCustomizers = clusterCustomizersProvider.getIfAvailable(); } @Bean @@ -82,7 +89,18 @@ public class CassandraAutoConfiguration { } String points = properties.getContactPoints(); builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points)); - return builder.build(); + + Cluster cluster = builder.build(); + customize(cluster); + return cluster; + } + + private void customize(Cluster cluster) { + if (this.clusterCustomizers != null) { + for (ClusterCustomizer customizer : this.clusterCustomizers) { + customizer.customize(cluster); + } + } } public static T instantiate(Class type) { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterCustomizer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterCustomizer.java new file mode 100644 index 0000000000..2b02cf7987 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterCustomizer.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 org.springframework.boot.autoconfigure.cassandra; + +import com.datastax.driver.core.Cluster; + +/** + * Callback interface that can be implemented by beans wishing to customize the + * {@link Cluster} before it is fully initialized, in particular to tune its + * configuration. + * + * @author Eddú Meléndez + * @since 1.5.0 + */ +public interface ClusterCustomizer { + + /** + * Customize the {@link Cluster}. + * @param cluster the cluster to customize + */ + void customize(Cluster cluster); + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java index 96fb92b231..b94312b3a1 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java @@ -23,13 +23,17 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; /** * Tests for {@link CassandraAutoConfiguration} * * @author Eddú Meléndez + * @author Stephane Nicoll */ public class CassandraAutoConfigurationTests { @@ -44,7 +48,7 @@ public class CassandraAutoConfigurationTests { @Test public void createClusterWithDefault() { - this.context = doLoad(); + load(); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); Cluster cluster = this.context.getBean(Cluster.class); assertThat(cluster.getClusterName()).startsWith("cluster"); @@ -52,19 +56,44 @@ public class CassandraAutoConfigurationTests { @Test public void createClusterWithOverrides() { - this.context = doLoad("spring.data.cassandra.cluster-name=testcluster"); + load("spring.data.cassandra.cluster-name=testcluster"); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); Cluster cluster = this.context.getBean(Cluster.class); assertThat(cluster.getClusterName()).isEqualTo("testcluster"); } - private AnnotationConfigApplicationContext doLoad(String... environment) { - AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(applicationContext, environment); - applicationContext.register(PropertyPlaceholderAutoConfiguration.class, + @Test + public void createCustomizeCluster() { + load(ClusterConfig.class); + assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); + assertThat(this.context.getBeanNamesForType(ClusterCustomizer.class).length).isEqualTo(1); + } + + private void load(String... environment) { + load(null, environment); + } + + private void load(Class config, String... environment) { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + if (config != null) { + ctx.register(config); + } + ctx.register(PropertyPlaceholderAutoConfiguration.class, CassandraAutoConfiguration.class); - applicationContext.refresh(); - return applicationContext; + EnvironmentTestUtils.addEnvironment(ctx, environment); + ctx.refresh(); + this.context = ctx; + } + + + @Configuration + static class ClusterConfig { + + @Bean + public ClusterCustomizer customizer() { + return mock(ClusterCustomizer.class); + } + } }