Browse Source

Allow to customize Cluster.Builder rather than Cluster

Rework cb3d14a so that the customizer operates on the Builder rather than
the Cluster. The former exposes many more options.

See gh-7320
Closes gh-7669
pull/7763/head
Stephane Nicoll 9 years ago
parent
commit
eb294956d8
  1. 22
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java
  2. 11
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterBuilderCustomizer.java
  3. 34
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java

22
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java

@ -40,6 +40,8 @@ import org.springframework.util.StringUtils; @@ -40,6 +40,8 @@ import org.springframework.util.StringUtils;
*
* @author Julien Dubois
* @author Phillip Webb
* @author Eddú Meléndez
* @author Stephane Nicoll
* @since 1.3.0
*/
@Configuration
@ -49,12 +51,13 @@ public class CassandraAutoConfiguration { @@ -49,12 +51,13 @@ public class CassandraAutoConfiguration {
private final CassandraProperties properties;
private final List<ClusterCustomizer> clusterCustomizers;
private final List<ClusterBuilderCustomizer> builderCustomizers;
public CassandraAutoConfiguration(CassandraProperties properties,
ObjectProvider<List<ClusterCustomizer>> clusterCustomizersProvider) {
ObjectProvider<List<ClusterBuilderCustomizer>> builderCustomizersProvider) {
this.properties = properties;
this.clusterCustomizers = clusterCustomizersProvider.getIfAvailable();
this.builderCustomizers = builderCustomizersProvider
.getIfAvailable();
}
@Bean
@ -90,15 +93,14 @@ public class CassandraAutoConfiguration { @@ -90,15 +93,14 @@ public class CassandraAutoConfiguration {
String points = properties.getContactPoints();
builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points));
Cluster cluster = builder.build();
customize(cluster);
return cluster;
customize(builder);
return builder.build();
}
private void customize(Cluster cluster) {
if (this.clusterCustomizers != null) {
for (ClusterCustomizer customizer : this.clusterCustomizers) {
customizer.customize(cluster);
private void customize(Cluster.Builder builder) {
if (this.builderCustomizers != null) {
for (ClusterBuilderCustomizer customizer : this.builderCustomizers) {
customizer.customize(builder);
}
}
}

11
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterCustomizer.java → spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterBuilderCustomizer.java

@ -20,18 +20,17 @@ import com.datastax.driver.core.Cluster; @@ -20,18 +20,17 @@ 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.
* {@link Cluster} via {@link Cluster.Builder} retaining its default auto-configuration.
*
* @author Eddú Meléndez
* @since 1.5.0
*/
public interface ClusterCustomizer {
public interface ClusterBuilderCustomizer {
/**
* Customize the {@link Cluster}.
* @param cluster the cluster to customize
* Customize the {@link Cluster.Builder}.
* @param clusterBuilder the builder to customize
*/
void customize(Cluster cluster);
void customize(Cluster.Builder clusterBuilder);
}

34
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java

@ -64,12 +64,21 @@ public class CassandraAutoConfigurationTests { @@ -64,12 +64,21 @@ public class CassandraAutoConfigurationTests {
@Test
public void createCustomizeCluster() {
load(ClusterConfig.class);
load(MockCustomizerConfig.class);
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
assertThat(this.context.getBeanNamesForType(ClusterCustomizer.class).length)
assertThat(this.context.getBeanNamesForType(ClusterBuilderCustomizer.class).length)
.isEqualTo(1);
}
@Test
public void customizerOverridesAutoConfig() {
load(SimpleCustomizerConfig.class,
"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("overridden-name");
}
private void load(String... environment) {
load(null, environment);
}
@ -87,11 +96,26 @@ public class CassandraAutoConfigurationTests { @@ -87,11 +96,26 @@ public class CassandraAutoConfigurationTests {
}
@Configuration
static class ClusterConfig {
static class MockCustomizerConfig {
@Bean
public ClusterBuilderCustomizer customizer() {
return mock(ClusterBuilderCustomizer.class);
}
}
@Configuration
static class SimpleCustomizerConfig {
@Bean
public ClusterCustomizer customizer() {
return mock(ClusterCustomizer.class);
public ClusterBuilderCustomizer customizer() {
return new ClusterBuilderCustomizer() {
@Override
public void customize(Cluster.Builder clusterBuilder) {
clusterBuilder.withClusterName("overridden-name");
}
};
}
}

Loading…
Cancel
Save