From 5daf310bf46aa82851471df018f9ad3f716051fb Mon Sep 17 00:00:00 2001 From: Steffen Folman Qvistgaard Date: Thu, 2 May 2019 09:08:50 +0200 Subject: [PATCH 1/2] Provide control over how a Cassandra Cluster is created See gh-16702 --- .../cassandra/CassandraAutoConfiguration.java | 13 +++++-- .../cassandra/CassandraClusterFactory.java | 34 ++++++++++++++++++ .../CassandraClusterFactoryImpl.java | 35 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java index 0798071967..be58c02228 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java @@ -40,6 +40,7 @@ import org.springframework.util.StringUtils; * @author Phillip Webb * @author EddĂș MelĂ©ndez * @author Stephane Nicoll + * @auther Steffen F. Qvistgaard * @since 1.3.0 */ @Configuration(proxyBeanMethods = false) @@ -47,10 +48,17 @@ import org.springframework.util.StringUtils; @EnableConfigurationProperties(CassandraProperties.class) public class CassandraAutoConfiguration { + @Bean + @ConditionalOnMissingBean + public CassandraClusterFactory cassandraClusterFactory() { + return new CassandraClusterFactoryImpl(); + } + @Bean @ConditionalOnMissingBean public Cluster cassandraCluster(CassandraProperties properties, - ObjectProvider builderCustomizers) { + ObjectProvider builderCustomizers, + CassandraClusterFactory clusterFactory) { PropertyMapper map = PropertyMapper.get(); Cluster.Builder builder = Cluster.builder() .withClusterName(properties.getClusterName()) @@ -71,7 +79,8 @@ public class CassandraAutoConfiguration { .toCall(builder::withoutJMXReporting); builderCustomizers.orderedStream() .forEach((customizer) -> customizer.customize(builder)); - return builder.build(); + + return clusterFactory.build(builder); } private QueryOptions getQueryOptions(CassandraProperties properties) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java new file mode 100644 index 0000000000..a07c55edb9 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2019 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 + * + * https://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; +import com.datastax.driver.core.Cluster.Initializer; + +/** + * Cassandra Cluster Factory Interface. + * + * This interface allows the default cassandra cluster builder to be overwritten + * + * @auther Steffen F. Qvistgaard + * @since 2.2.0 + */ +public interface CassandraClusterFactory { + + Cluster build(Initializer initializer); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java new file mode 100644 index 0000000000..2b858f9f92 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2019 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 + * + * https://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; +import com.datastax.driver.core.Cluster.Initializer; + +/** + * Default Cassandra Cluster Factory Implementation. + * + * @auther Steffen F. Qvistgaard + * @since 2.2.0 + */ +public class CassandraClusterFactoryImpl implements CassandraClusterFactory { + + @Override + public Cluster build(final Initializer initializer) { + return Cluster.buildFrom(initializer); + } + +} From ece87cc4cc0772e334d7e9d185bc7d8639e59d38 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 9 May 2019 12:27:21 +0100 Subject: [PATCH 2/2] Polish "Provide control over how a Cassandra Cluster is created" See gh-16702 --- .../cassandra/CassandraAutoConfiguration.java | 11 ++---- .../CassandraClusterFactoryImpl.java | 35 ------------------- ...lusterFactory.java => ClusterFactory.java} | 15 +++++--- .../CassandraAutoConfigurationTests.java | 31 ++++++++++++++++ 4 files changed, 43 insertions(+), 49 deletions(-) delete mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java rename spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/{CassandraClusterFactory.java => ClusterFactory.java} (69%) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java index be58c02228..35f91e454d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java @@ -48,17 +48,11 @@ import org.springframework.util.StringUtils; @EnableConfigurationProperties(CassandraProperties.class) public class CassandraAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public CassandraClusterFactory cassandraClusterFactory() { - return new CassandraClusterFactoryImpl(); - } - @Bean @ConditionalOnMissingBean public Cluster cassandraCluster(CassandraProperties properties, ObjectProvider builderCustomizers, - CassandraClusterFactory clusterFactory) { + ObjectProvider clusterFactory) { PropertyMapper map = PropertyMapper.get(); Cluster.Builder builder = Cluster.builder() .withClusterName(properties.getClusterName()) @@ -79,8 +73,7 @@ public class CassandraAutoConfiguration { .toCall(builder::withoutJMXReporting); builderCustomizers.orderedStream() .forEach((customizer) -> customizer.customize(builder)); - - return clusterFactory.build(builder); + return clusterFactory.getIfAvailable(() -> Cluster::buildFrom).create(builder); } private QueryOptions getQueryOptions(CassandraProperties properties) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java deleted file mode 100644 index 2b858f9f92..0000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2012-2019 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 - * - * https://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; -import com.datastax.driver.core.Cluster.Initializer; - -/** - * Default Cassandra Cluster Factory Implementation. - * - * @auther Steffen F. Qvistgaard - * @since 2.2.0 - */ -public class CassandraClusterFactoryImpl implements CassandraClusterFactory { - - @Override - public Cluster build(final Initializer initializer) { - return Cluster.buildFrom(initializer); - } - -} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterFactory.java similarity index 69% rename from spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java rename to spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterFactory.java index a07c55edb9..73d8684c1e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterFactory.java @@ -20,15 +20,20 @@ import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster.Initializer; /** - * Cassandra Cluster Factory Interface. - * - * This interface allows the default cassandra cluster builder to be overwritten + * {@code CassandraClusterFactory} provides control over the creation of a {@Cluster} from + * an {@link Initializer}. * * @auther Steffen F. Qvistgaard * @since 2.2.0 */ -public interface CassandraClusterFactory { +@FunctionalInterface +public interface ClusterFactory { - Cluster build(Initializer initializer); + /** + * Creates a {@link Cluster} from the given {@link Initializer}. + * @param initializer the {@Code Initializer} + * @return the {@code Cluster} + */ + Cluster create(Initializer initializer); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java index e9e9fc9496..a7f0c85fbb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.cassandra; import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Cluster.Initializer; import com.datastax.driver.core.PoolingOptions; import org.junit.jupiter.api.Test; @@ -115,6 +116,14 @@ public class CassandraAutoConfigurationTests { }); } + @Test + public void clusterFactoryIsCalledToCreateCluster() { + this.contextRunner.withUserConfiguration(ClusterFactoryConfig.class) + .run((context) -> assertThat( + context.getBean(TestClusterFactory.class).initializer) + .isNotNull()); + } + @Configuration(proxyBeanMethods = false) static class MockCustomizerConfig { @@ -135,4 +144,26 @@ public class CassandraAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class ClusterFactoryConfig { + + @Bean + public TestClusterFactory clusterFactory() { + return new TestClusterFactory(); + } + + } + + static class TestClusterFactory implements ClusterFactory { + + private Initializer initializer = null; + + @Override + public Cluster create(Initializer initializer) { + this.initializer = initializer; + return Cluster.buildFrom(initializer); + } + + } + }