Merge branch '1.5.x'

pull/6613/merge
Stephane Nicoll 8 years ago
commit 6d0888ecf4

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

@ -20,18 +20,17 @@ import com.datastax.driver.core.Cluster;
/** /**
* Callback interface that can be implemented by beans wishing to customize the * Callback interface that can be implemented by beans wishing to customize the
* {@link Cluster} before it is fully initialized, in particular to tune its * {@link Cluster} via {@link Cluster.Builder} retaining its default auto-configuration.
* configuration.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @since 1.5.0 * @since 1.5.0
*/ */
public interface ClusterCustomizer { public interface ClusterBuilderCustomizer {
/** /**
* Customize the {@link Cluster}. * Customize the {@link Cluster.Builder}.
* @param cluster the cluster to customize * @param clusterBuilder the builder to customize
*/ */
void customize(Cluster cluster); void customize(Cluster.Builder clusterBuilder);
} }

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

Loading…
Cancel
Save