From a177562b277c9ea2a9ccf4e1fcb7128dc9583bca Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Mon, 9 Dec 2013 13:16:16 -0600 Subject: [PATCH] wip: moved base c* config to spring-cassandra --- .../AbstractCassandraConfiguration.java | 69 +++++++++++++++++++ ...ractSpringDataCassandraConfiguration.java} | 61 ++-------------- .../test/integration/config/TestConfig.java | 4 +- 3 files changed, 78 insertions(+), 56 deletions(-) create mode 100644 spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java rename spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/{AbstractCassandraConfiguration.java => AbstractSpringDataCassandraConfiguration.java} (79%) diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java b/spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java new file mode 100644 index 000000000..3c3465bd7 --- /dev/null +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/config/AbstractCassandraConfiguration.java @@ -0,0 +1,69 @@ +/* + * Copyright 2011-2012 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.cassandra.config; + +import org.springframework.cassandra.core.CassandraOperations; +import org.springframework.cassandra.core.CassandraTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.StringUtils; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; + +/** + * Base class for Spring Cassandra configuration using JavaConfig. + * + * @author Alex Shvid + * @author Matthew T. Adams + */ +@Configuration +public abstract class AbstractCassandraConfiguration { + + /** + * The name of the keyspace to connect to. If {@literal null} or empty, then the system keyspace will be used. + */ + protected abstract String getKeyspaceName(); + + /** + * The {@link Cluster} instance to connect to. Must not be null. + */ + @Bean + public abstract Cluster cluster(); + + /** + * Creates a {@link Session} using the {@link Cluster} instance configured in {@link #cluster()}. + * + * @see #cluster() + */ + @Bean + public Session session() { + String keyspace = getKeyspaceName(); + if (StringUtils.hasText(keyspace)) { + return cluster().connect(keyspace); + } else { + return cluster().connect(); + } + } + + /** + * A {@link CassandraTemplate} created from the {@link Session} returned by {@link #session()}. + */ + @Bean + public CassandraOperations cassandraTemplate() { + return new CassandraTemplate(session()); + } +} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractCassandraConfiguration.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java similarity index 79% rename from spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractCassandraConfiguration.java rename to spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java index c8cc1b262..51ba42aab 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractCassandraConfiguration.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java @@ -20,6 +20,7 @@ import java.util.Set; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.cassandra.config.AbstractCassandraConfiguration; import org.springframework.cassandra.core.CassandraOperations; import org.springframework.cassandra.core.CassandraTemplate; import org.springframework.context.annotation.Bean; @@ -47,51 +48,14 @@ import com.datastax.driver.core.Session; * Base class for Spring Data Cassandra configuration using JavaConfig. * * @author Alex Shvid + * @author Matthew T. Adams */ @Configuration -public abstract class AbstractCassandraConfiguration implements BeanClassLoaderAware { - - /** - * Used by CassandraTemplate and CassandraAdminTemplate - */ +public abstract class AbstractSpringDataCassandraConfiguration extends AbstractCassandraConfiguration implements + BeanClassLoaderAware { private ClassLoader beanClassLoader; - /** - * Return the name of the keyspace to connect to. - * - * @return must not be {@literal null}. - */ - protected abstract String getKeyspaceName(); - - /** - * Return the {@link Cluster} instance to connect to. - * - * @return - * @throws Exception - */ - @Bean - public abstract Cluster cluster() throws Exception; - - /** - * Creates a {@link Session} to be used by the {@link SpringDataKeyspace}. Will use the {@link Cluster} instance - * configured in {@link #cluster()}. - * - * @see #cluster() - * @see #Keyspace() - * @return - * @throws Exception - */ - @Bean - public Session session() throws Exception { - String keyspace = getKeyspaceName(); - if (StringUtils.hasText(keyspace)) { - return cluster().connect(keyspace); - } else { - return cluster().connect(); - } - } - /** * Creates a {@link SpringDataKeyspace} to be used by the {@link CassandraTemplate}. Will use the {@link Session} * instance configured in {@link #session()} and {@link CassandraConverter} configured in {@link #converter()}. @@ -109,8 +73,8 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA /** * Return the base package to scan for mapped {@link Table}s. Will return the package name of the configuration class' * (the concrete class, not this one here) by default. So if you have a {@code com.acme.AppConfig} extending - * {@link AbstractCassandraConfiguration} the base package will be considered {@code com.acme} unless the method is - * overriden to implement alternate behaviour. + * {@link AbstractSpringDataCassandraConfiguration} the base package will be considered {@code com.acme} unless the + * method is overriden to implement alternate behaviour. * * @return the base package to scan for mapped {@link Table} classes or {@literal null} to not enable scanning for * entities. @@ -119,17 +83,6 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA return getClass().getPackage().getName(); } - /** - * Creates a {@link CassandraTemplate}. - * - * @return - * @throws Exception - */ - @Bean - public CassandraOperations cassandraTemplate() throws Exception { - return new CassandraTemplate(session()); - } - /** * Creates a {@link CassandraAdminTemplate}. * @@ -185,7 +138,7 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) { initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), - AbstractCassandraConfiguration.class.getClassLoader())); + AbstractSpringDataCassandraConfiguration.class.getClassLoader())); } } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java index 415f5393e..aec114209 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java @@ -5,7 +5,7 @@ import org.springframework.cassandra.core.CassandraTemplate; import org.springframework.cassandra.core.SessionFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.cassandra.config.AbstractCassandraConfiguration; +import org.springframework.data.cassandra.config.AbstractSpringDataCassandraConfiguration; import org.springframework.data.cassandra.core.CassandraDataOperations; import org.springframework.data.cassandra.core.CassandraDataTemplate; import org.springframework.data.cassandra.core.CassandraKeyspaceFactoryBean; @@ -20,7 +20,7 @@ import com.datastax.driver.core.Cluster.Builder; * */ @Configuration -public class TestConfig extends AbstractCassandraConfiguration { +public class TestConfig extends AbstractSpringDataCassandraConfiguration { public static final String keyspace = "test";