From cb3d14a305f5b48242f266c3ab06e4daa790d0cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 4 Nov 2016 20:27:09 -0500 Subject: [PATCH 1/2] Allow to customize Casssandra's Cluster This commit allows to configure special customizers to be used by Cluster. Closes gh-7312 --- .../cassandra/CassandraAutoConfiguration.java | 22 ++++++++++- .../cassandra/ClusterCustomizer.java | 36 +++++++++++++++++ .../CassandraAutoConfigurationTests.java | 39 +++++++++++++++++-- 3 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterCustomizer.java 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 00a13a7305..0699c0de3c 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 customizers; + + public CassandraAutoConfiguration(CassandraProperties properties, + ObjectProvider> customizers) { this.properties = properties; + this.customizers = customizers.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.customizers != null) { + for (ClusterCustomizer customizer : this.customizers) { + 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..8bbba2f0f5 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterCustomizer.java @@ -0,0 +1,36 @@ +/* + * 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 used to customize a {@link Cluster}. + * + * @author Eddú Meléndez + * @since 1.5.0 + * @see CassandraAutoConfiguration + */ +public interface ClusterCustomizer { + + /** + * Customize the 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..f3fb9e7a94 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,8 +23,11 @@ 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} @@ -45,6 +48,7 @@ public class CassandraAutoConfigurationTests { @Test public void createClusterWithDefault() { this.context = doLoad(); + this.context.refresh(); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); Cluster cluster = this.context.getBean(Cluster.class); assertThat(cluster.getClusterName()).startsWith("cluster"); @@ -53,18 +57,47 @@ public class CassandraAutoConfigurationTests { @Test public void createClusterWithOverrides() { this.context = doLoad("spring.data.cassandra.cluster-name=testcluster"); + this.context.refresh(); 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) { + @Test + public void createCustomizeCluster() { + this.context = doLoad(ClusterConfig.class); + this.context.refresh(); + assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); + assertThat(this.context.getBeanNamesForType(ClusterCustomizer.class).length).isEqualTo(1); + } + + private AnnotationConfigApplicationContext doLoad() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(applicationContext, environment); applicationContext.register(PropertyPlaceholderAutoConfiguration.class, CassandraAutoConfiguration.class); - applicationContext.refresh(); return applicationContext; } + private AnnotationConfigApplicationContext doLoad(Class clazz) { + AnnotationConfigApplicationContext applicationContext = doLoad(); + applicationContext.register(clazz); + return applicationContext; + } + + private AnnotationConfigApplicationContext doLoad(String... environment) { + AnnotationConfigApplicationContext applicationContext = doLoad(); + EnvironmentTestUtils.addEnvironment(applicationContext, environment); + return applicationContext; + } + + @Configuration + static class ClusterConfig { + + @Bean + public ClusterCustomizer customizer() { + return mock(ClusterCustomizer.class); + } + + } + } From 64e0899456737783b6503995cf4c66970ff0e3e2 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 23 Nov 2016 15:04:31 +0100 Subject: [PATCH 2/2] Polish customize Cassandra's Cluster Closes gh-7320 --- .../cassandra/CassandraAutoConfiguration.java | 10 ++--- .../cassandra/ClusterCustomizer.java | 7 ++-- .../CassandraAutoConfigurationTests.java | 38 +++++++++---------- 3 files changed, 26 insertions(+), 29 deletions(-) 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 0699c0de3c..c800ff49e6 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 @@ -49,12 +49,12 @@ public class CassandraAutoConfiguration { private final CassandraProperties properties; - private final List customizers; + private final List clusterCustomizers; public CassandraAutoConfiguration(CassandraProperties properties, - ObjectProvider> customizers) { + ObjectProvider> clusterCustomizersProvider) { this.properties = properties; - this.customizers = customizers.getIfAvailable(); + this.clusterCustomizers = clusterCustomizersProvider.getIfAvailable(); } @Bean @@ -96,8 +96,8 @@ public class CassandraAutoConfiguration { } private void customize(Cluster cluster) { - if (this.customizers != null) { - for (ClusterCustomizer customizer : this.customizers) { + if (this.clusterCustomizers != null) { + for (ClusterCustomizer customizer : this.clusterCustomizers) { customizer.customize(cluster); } } 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 index 8bbba2f0f5..2b02cf7987 100644 --- 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 @@ -19,16 +19,17 @@ package org.springframework.boot.autoconfigure.cassandra; import com.datastax.driver.core.Cluster; /** - * Callback interface that can be used to customize a {@link 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 - * @see CassandraAutoConfiguration */ public interface ClusterCustomizer { /** - * Customize the cluster. + * 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 f3fb9e7a94..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 @@ -33,6 +33,7 @@ import static org.mockito.Mockito.mock; * Tests for {@link CassandraAutoConfiguration} * * @author Eddú Meléndez + * @author Stephane Nicoll */ public class CassandraAutoConfigurationTests { @@ -47,8 +48,7 @@ public class CassandraAutoConfigurationTests { @Test public void createClusterWithDefault() { - this.context = doLoad(); - this.context.refresh(); + load(); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); Cluster cluster = this.context.getBean(Cluster.class); assertThat(cluster.getClusterName()).startsWith("cluster"); @@ -56,8 +56,7 @@ public class CassandraAutoConfigurationTests { @Test public void createClusterWithOverrides() { - this.context = doLoad("spring.data.cassandra.cluster-name=testcluster"); - this.context.refresh(); + 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"); @@ -65,30 +64,27 @@ public class CassandraAutoConfigurationTests { @Test public void createCustomizeCluster() { - this.context = doLoad(ClusterConfig.class); - this.context.refresh(); + load(ClusterConfig.class); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); assertThat(this.context.getBeanNamesForType(ClusterCustomizer.class).length).isEqualTo(1); } - private AnnotationConfigApplicationContext doLoad() { - AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); - applicationContext.register(PropertyPlaceholderAutoConfiguration.class, + 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); - return applicationContext; + EnvironmentTestUtils.addEnvironment(ctx, environment); + ctx.refresh(); + this.context = ctx; } - private AnnotationConfigApplicationContext doLoad(Class clazz) { - AnnotationConfigApplicationContext applicationContext = doLoad(); - applicationContext.register(clazz); - return applicationContext; - } - - private AnnotationConfigApplicationContext doLoad(String... environment) { - AnnotationConfigApplicationContext applicationContext = doLoad(); - EnvironmentTestUtils.addEnvironment(applicationContext, environment); - return applicationContext; - } @Configuration static class ClusterConfig {