diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java index 8c589d127..657cf634d 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractSpringDataCassandraConfiguration.java @@ -21,7 +21,6 @@ import java.util.Set; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.cassandra.config.java.AbstractCassandraConfiguration; -import org.springframework.cassandra.core.CassandraTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.Configuration; @@ -31,7 +30,6 @@ import org.springframework.data.cassandra.convert.CassandraConverter; import org.springframework.data.cassandra.convert.MappingCassandraConverter; import org.springframework.data.cassandra.core.CassandraAdminOperations; import org.springframework.data.cassandra.core.CassandraAdminTemplate; -import org.springframework.data.cassandra.core.SpringDataKeyspace; import org.springframework.data.cassandra.mapping.CassandraMappingContext; import org.springframework.data.cassandra.mapping.CassandraPersistentEntity; import org.springframework.data.cassandra.mapping.CassandraPersistentProperty; @@ -40,8 +38,6 @@ import org.springframework.data.mapping.context.MappingContext; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; -import com.datastax.driver.core.Session; - /** * Base class for Spring Data Cassandra configuration using JavaConfig. * @@ -54,20 +50,6 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC private ClassLoader beanClassLoader; - /** - * 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()}. - * - * @see #cluster() - * @see #Keyspace() - * @return - * @throws Exception - */ - @Bean - public SpringDataKeyspace keyspace() throws Exception { - return new SpringDataKeyspace(getKeyspaceName(), session(), converter()); - } - /** * 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 @@ -89,7 +71,7 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC */ @Bean public CassandraAdminOperations adminTemplate() throws Exception { - return new CassandraAdminTemplate(keyspace()); + return new CassandraAdminTemplate(session()); } /** diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceFactoryBean.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceFactoryBean.java deleted file mode 100644 index 2a691b88f..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceFactoryBean.java +++ /dev/null @@ -1,340 +0,0 @@ -/* - * Copyright 2011-2013 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.data.cassandra.config; - -import java.util.List; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.BeanClassLoaderAware; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.FactoryBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.cassandra.support.CassandraExceptionTranslator; -import org.springframework.dao.DataAccessException; -import org.springframework.dao.InvalidDataAccessApiUsageException; -import org.springframework.dao.support.PersistenceExceptionTranslator; -import org.springframework.data.cassandra.convert.CassandraConverter; -import org.springframework.data.cassandra.convert.MappingCassandraConverter; -import org.springframework.data.cassandra.core.SpringDataKeyspace; -import org.springframework.data.cassandra.mapping.CassandraMappingContext; -import org.springframework.data.cassandra.mapping.CassandraPersistentEntity; -import org.springframework.data.cassandra.mapping.CassandraPersistentProperty; -import org.springframework.data.cassandra.util.CqlUtils; -import org.springframework.data.mapping.context.MappingContext; -import org.springframework.util.ClassUtils; -import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; - -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.KeyspaceMetadata; -import com.datastax.driver.core.Session; -import com.datastax.driver.core.TableMetadata; -import com.datastax.driver.core.exceptions.NoHostAvailableException; - -/** - * Convenient factory for configuring a Cassandra Session. Session is a thread safe singleton and created per a - * keyspace. So, it is enough to have one session per application. - * - * @author Alex Shvid - */ - -public class CassandraKeyspaceFactoryBean implements FactoryBean, InitializingBean, DisposableBean, - BeanClassLoaderAware, PersistenceExceptionTranslator { - - private static final Logger log = LoggerFactory.getLogger(CassandraKeyspaceFactoryBean.class); - - public static final String DEFAULT_REPLICATION_STRATEGY = "SimpleStrategy"; - public static final int DEFAULT_REPLICATION_FACTOR = 1; - - private ClassLoader beanClassLoader; - - private Cluster cluster; - private Session session; - private String keyspace; - - private CassandraConverter converter; - private MappingContext, CassandraPersistentProperty> mappingContext; - - private SpringDataKeyspace keyspaceBean; - - private KeyspaceAttributes keyspaceAttributes; - - private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator(); - - public void setBeanClassLoader(ClassLoader classLoader) { - this.beanClassLoader = classLoader; - } - - public SpringDataKeyspace getObject() { - return keyspaceBean; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#getObjectType() - */ - public Class getObjectType() { - return Session.class; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#isSingleton() - */ - public boolean isSingleton() { - return true; - } - - /* - * (non-Javadoc) - * @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException) - */ - public DataAccessException translateExceptionIfPossible(RuntimeException ex) { - return exceptionTranslator.translateExceptionIfPossible(ex); - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ - public void afterPropertiesSet() throws Exception { - - if (this.converter == null) { - this.converter = getDefaultCassandraConverter(); - } - this.mappingContext = this.converter.getMappingContext(); - - if (cluster == null) { - throw new IllegalArgumentException("at least one cluster is required"); - } - - Session session = null; - session = cluster.connect(); - - if (StringUtils.hasText(keyspace)) { - - KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace.toLowerCase()); - boolean keyspaceExists = keyspaceMetadata != null; - boolean keyspaceCreated = false; - - if (keyspaceExists) { - log.info("keyspace exists " + keyspaceMetadata.asCQLQuery()); - } - - if (keyspaceAttributes == null) { - keyspaceAttributes = new KeyspaceAttributes(); - } - - // drop the old keyspace if needed - if (keyspaceExists && (keyspaceAttributes.isCreate() || keyspaceAttributes.isCreateDrop())) { - log.info("Drop keyspace " + keyspace + " on afterPropertiesSet"); - session.execute("DROP KEYSPACE " + keyspace + ";"); - keyspaceExists = false; - } - - // create the new keyspace if needed - if (!keyspaceExists - && (keyspaceAttributes.isCreate() || keyspaceAttributes.isCreateDrop() || keyspaceAttributes.isUpdate())) { - - String query = String - .format( - "CREATE KEYSPACE %1$s WITH replication = { 'class' : '%2$s', 'replication_factor' : %3$d } AND DURABLE_WRITES = %4$b", - keyspace, keyspaceAttributes.getReplicationStrategy(), keyspaceAttributes.getReplicationFactor(), - keyspaceAttributes.isDurableWrites()); - - log.info("Create keyspace " + keyspace + " on afterPropertiesSet " + query); - - session.execute(query); - keyspaceCreated = true; - } - - // update keyspace if needed - if (keyspaceAttributes.isUpdate() && !keyspaceCreated) { - - if (compareKeyspaceAttributes(keyspaceAttributes, keyspaceMetadata) != null) { - - String query = String - .format( - "ALTER KEYSPACE %1$s WITH replication = { 'class' : '%2$s', 'replication_factor' : %3$d } AND DURABLE_WRITES = %4$b", - keyspace, keyspaceAttributes.getReplicationStrategy(), keyspaceAttributes.getReplicationFactor(), - keyspaceAttributes.isDurableWrites()); - - log.info("Update keyspace " + keyspace + " on afterPropertiesSet " + query); - session.execute(query); - } - - } - - // validate keyspace if needed - if (keyspaceAttributes.isValidate()) { - - if (!keyspaceExists) { - throw new InvalidDataAccessApiUsageException("keyspace '" + keyspace + "' not found in the Cassandra"); - } - - String errorField = compareKeyspaceAttributes(keyspaceAttributes, keyspaceMetadata); - if (errorField != null) { - throw new InvalidDataAccessApiUsageException(errorField + " attribute is not much in the keyspace '" - + keyspace + "'"); - } - - } - - session.execute("USE " + keyspace); - - if (!CollectionUtils.isEmpty(keyspaceAttributes.getTables())) { - - for (TableAttributes tableAttributes : keyspaceAttributes.getTables()) { - - String entityClassName = tableAttributes.getEntity(); - Class entityClass = ClassUtils.forName(entityClassName, this.beanClassLoader); - CassandraPersistentEntity entity = determineEntity(entityClass); - String useTableName = tableAttributes.getName() != null ? tableAttributes.getName() : entity.getTableName(); - - if (keyspaceCreated) { - createNewTable(session, useTableName, entity); - } else if (keyspaceAttributes.isUpdate()) { - TableMetadata table = keyspaceMetadata.getTable(useTableName.toLowerCase()); - if (table == null) { - createNewTable(session, useTableName, entity); - } else { - // alter table columns - for (String cql : CqlUtils.alterTable(useTableName, entity, table)) { - log.info("Execute on keyspace " + keyspace + " CQL " + cql); - session.execute(cql); - } - } - } else if (keyspaceAttributes.isValidate()) { - TableMetadata table = keyspaceMetadata.getTable(useTableName.toLowerCase()); - if (table == null) { - throw new InvalidDataAccessApiUsageException("not found table " + useTableName + " for entity " - + entityClassName); - } - // validate columns - List alter = CqlUtils.alterTable(useTableName, entity, table); - if (!alter.isEmpty()) { - throw new InvalidDataAccessApiUsageException("invalid table " + useTableName + " for entity " - + entityClassName + ". modify it by " + alter); - } - } - - // System.out.println("tableAttributes, entityClass=" + entityClass + ", table = " + entity.getTable()); - - } - } - - } - - // initialize property - this.session = session; - - this.keyspaceBean = new SpringDataKeyspace(keyspace, session, converter); - } - - private void createNewTable(Session session, String useTableName, CassandraPersistentEntity entity) - throws NoHostAvailableException { - String cql = CqlUtils.createTable(useTableName, entity, converter); - log.info("Execute on keyspace " + keyspace + " CQL " + cql); - session.execute(cql); - for (String indexCQL : CqlUtils.createIndexes(useTableName, entity)) { - log.info("Execute on keyspace " + keyspace + " CQL " + indexCQL); - session.execute(indexCQL); - } - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ - public void destroy() throws Exception { - - if (StringUtils.hasText(keyspace) && keyspaceAttributes != null && keyspaceAttributes.isCreateDrop()) { - log.info("Drop keyspace " + keyspace + " on destroy"); - session.execute("USE system"); - session.execute("DROP KEYSPACE " + keyspace); - } - this.session.shutdown(); - } - - public void setKeyspace(String keyspace) { - this.keyspace = keyspace; - } - - public void setCluster(Cluster cluster) { - this.cluster = cluster; - } - - public void setKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes) { - this.keyspaceAttributes = keyspaceAttributes; - } - - public void setConverter(CassandraConverter converter) { - this.converter = converter; - } - - private static String compareKeyspaceAttributes(KeyspaceAttributes keyspaceAttributes, - KeyspaceMetadata keyspaceMetadata) { - if (keyspaceAttributes.isDurableWrites() != keyspaceMetadata.isDurableWrites()) { - return "durableWrites"; - } - Map replication = keyspaceMetadata.getReplication(); - String replicationFactorStr = replication.get("replication_factor"); - if (replicationFactorStr == null) { - return "replication_factor"; - } - try { - int replicationFactor = Integer.parseInt(replicationFactorStr); - if (keyspaceAttributes.getReplicationFactor() != replicationFactor) { - return "replication_factor"; - } - } catch (NumberFormatException e) { - return "replication_factor"; - } - - String attributesStrategy = keyspaceAttributes.getReplicationStrategy(); - if (attributesStrategy.indexOf('.') == -1) { - attributesStrategy = "org.apache.cassandra.locator." + attributesStrategy; - } - String replicationStrategy = replication.get("class"); - if (!attributesStrategy.equals(replicationStrategy)) { - return "replication_class"; - } - return null; - } - - CassandraPersistentEntity determineEntity(Class entityClass) { - - if (entityClass == null) { - throw new InvalidDataAccessApiUsageException( - "No class parameter provided, entity table name can't be determined!"); - } - - CassandraPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); - if (entity == null) { - throw new InvalidDataAccessApiUsageException("No Persitent Entity information found for the class " - + entityClass.getName()); - } - return entity; - } - - private static final CassandraConverter getDefaultCassandraConverter() { - MappingCassandraConverter converter = new MappingCassandraConverter(new CassandraMappingContext()); - converter.afterPropertiesSet(); - return converter; - } -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceParser.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceParser.java deleted file mode 100644 index 3b3c99e09..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraKeyspaceParser.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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.data.cassandra.config; - -import java.util.List; - -import org.springframework.beans.factory.BeanDefinitionStoreException; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.support.AbstractBeanDefinition; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.ManagedList; -import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; -import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.cassandra.config.KeyspaceAttributes; -import org.springframework.cassandra.config.xml.BeanNames; -import org.springframework.data.config.ParsingUtils; -import org.springframework.util.StringUtils; -import org.springframework.util.xml.DomUtils; -import org.w3c.dom.Element; - -/** - * Parser for <keyspace;gt; definitions. - * - * @author Alex Shvid - */ - -public class CassandraKeyspaceParser extends AbstractSimpleBeanDefinitionParser { - - @Override - protected Class getBeanClass(Element element) { - return CassandraKeyspaceFactoryBean.class; - } - - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#resolveId(org.w3c.dom.Element, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.factory.xml.ParserContext) - */ - @Override - protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) - throws BeanDefinitionStoreException { - - String id = super.resolveId(element, definition, parserContext); - return StringUtils.hasText(id) ? id : BeanNames.CASSANDRA_KEYSPACE; - } - - @Override - protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - - String name = element.getAttribute("name"); - if (StringUtils.hasText(name)) { - builder.addPropertyValue("keyspace", name); - } - - String clusterRef = element.getAttribute("cassandra-cluster-ref"); - if (!StringUtils.hasText(clusterRef)) { - clusterRef = BeanNames.CASSANDRA_CLUSTER; - } - builder.addPropertyReference("cluster", clusterRef); - - String converterRef = element.getAttribute("cassandra-converter-ref"); - if (StringUtils.hasText(converterRef)) { - builder.addPropertyReference("converter", converterRef); - } - - postProcess(builder, element); - } - - @Override - protected void postProcess(BeanDefinitionBuilder builder, Element element) { - List subElements = DomUtils.getChildElements(element); - - // parse nested elements - for (Element subElement : subElements) { - String name = subElement.getLocalName(); - - if ("keyspace-attributes".equals(name)) { - builder.addPropertyValue("keyspaceAttributes", parseKeyspaceAttributes(subElement)); - } - } - - } - - private BeanDefinition parseKeyspaceAttributes(Element element) { - BeanDefinitionBuilder defBuilder = BeanDefinitionBuilder.genericBeanDefinition(KeyspaceAttributes.class); - ParsingUtils.setPropertyValue(defBuilder, element, "auto", "auto"); - ParsingUtils.setPropertyValue(defBuilder, element, "replication-strategy", "replicationStrategy"); - ParsingUtils.setPropertyValue(defBuilder, element, "replication-factor", "replicationFactor"); - ParsingUtils.setPropertyValue(defBuilder, element, "durable-writes", "durableWrites"); - - List subElements = DomUtils.getChildElements(element); - ManagedList tables = new ManagedList(subElements.size()); - - // parse nested elements - for (Element subElement : subElements) { - String name = subElement.getLocalName(); - - if ("table".equals(name)) { - tables.add(parseTable(subElement)); - } - } - if (!tables.isEmpty()) { - defBuilder.addPropertyValue("tables", tables); - } - - return defBuilder.getBeanDefinition(); - } - - private BeanDefinition parseTable(Element element) { - BeanDefinitionBuilder defBuilder = BeanDefinitionBuilder.genericBeanDefinition(TableAttributes.class); - ParsingUtils.setPropertyValue(defBuilder, element, "entity", "entity"); - ParsingUtils.setPropertyValue(defBuilder, element, "name", "name"); - return defBuilder.getBeanDefinition(); - } - -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraNamespaceHandler.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraNamespaceHandler.java index ace033ece..310bb5a7b 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraNamespaceHandler.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraNamespaceHandler.java @@ -27,6 +27,6 @@ public class CassandraNamespaceHandler extends NamespaceHandlerSupport { public void init() { - registerBeanDefinitionParser("keyspace", new CassandraKeyspaceParser()); + // registerBeanDefinitionParser("keyspace", new CassandraKeyspaceParser()); } } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java index d8cb5b64a..d41d08ea4 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java @@ -27,13 +27,6 @@ import com.datastax.driver.core.TableMetadata; */ public interface CassandraAdminOperations { - /** - * Get the given table's metadata. - * - * @param tableName The name of the table. - */ - TableMetadata getTableMetadata(String tableName); - /** * Create a table with the name given and fields corresponding to the given class. If the table already exists and * parameter ifNotExists is {@literal true}, this is a no-op and {@literal false} is returned. If the @@ -76,4 +69,11 @@ public interface CassandraAdminOperations { * @param tableName The name of the table. */ void dropTable(String tableName); + + /** + * @param keyspace + * @param tableName + * @return + */ + TableMetadata getTableMetadata(String keyspace, String tableName); } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java index 9e8268b85..f5d888b95 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java @@ -5,8 +5,8 @@ import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.data.cassandra.core.SpringDataKeyspace; import org.springframework.cassandra.core.SessionCallback; +import org.springframework.cassandra.support.CassandraAccessor; import org.springframework.cassandra.support.CassandraExceptionTranslator; import org.springframework.cassandra.support.exception.CassandraTableExistsException; import org.springframework.dao.DataAccessException; @@ -26,11 +26,10 @@ import com.datastax.driver.core.TableMetadata; /** * Default implementation of {@link CassandraAdminOperations}. */ -public class CassandraAdminTemplate implements CassandraAdminOperations { +public class CassandraAdminTemplate extends CassandraAccessor implements CassandraAdminOperations { private static final Logger log = LoggerFactory.getLogger(CassandraAdminTemplate.class); - private SpringDataKeyspace keyspace; private Session session; private CassandraConverter converter; private MappingContext, CassandraPersistentProperty> mappingContext; @@ -42,19 +41,8 @@ public class CassandraAdminTemplate implements CassandraAdminOperations { * * @param keyspace must not be {@literal null}. */ - public CassandraAdminTemplate(SpringDataKeyspace keyspace) { - setKeyspace(keyspace); - } - - protected CassandraAdminTemplate setKeyspace(SpringDataKeyspace keyspace) { - Assert.notNull(keyspace); - this.keyspace = keyspace; - return setSession(keyspace.getSession()).setCassandraConverter(keyspace.getCassandraConverter()); - } - - protected CassandraAdminTemplate setSession(Session session) { - Assert.notNull(session); - return this; + public CassandraAdminTemplate(Session session) { + setSession(session); } protected CassandraAdminTemplate setCassandraConverter(CassandraConverter converter) { @@ -120,13 +108,13 @@ public class CassandraAdminTemplate implements CassandraAdminOperations { * @param entityClass * @param tableName */ - protected void doAlterTable(Class entityClass, String tableName) { + protected void doAlterTable(Class entityClass, String keyspace, String tableName) { CassandraPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); Assert.notNull(entity); - final TableMetadata tableMetadata = getTableMetadata(tableName); + final TableMetadata tableMetadata = getTableMetadata(keyspace, tableName); final List queryList = CqlUtils.alterTable(tableName, entity, tableMetadata); @@ -185,7 +173,7 @@ public class CassandraAdminTemplate implements CassandraAdminOperations { * @see org.springframework.data.cassandra.core.CassandraOperations#getTableMetadata(java.lang.Class) */ @Override - public TableMetadata getTableMetadata(final String tableName) { + public TableMetadata getTableMetadata(final String keyspace, final String tableName) { Assert.notNull(tableName); @@ -193,9 +181,7 @@ public class CassandraAdminTemplate implements CassandraAdminOperations { public TableMetadata doInSession(Session s) throws DataAccessException { - log.info("Keyspace => " + keyspace.getKeyspace()); - - return s.getCluster().getMetadata().getKeyspace(keyspace.getKeyspace()).getTable(tableName); + return s.getCluster().getMetadata().getKeyspace(keyspace).getTable(tableName); } }); } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataTemplate.java index 7e4c63e12..6e94b48d1 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraDataTemplate.java @@ -54,11 +54,6 @@ import com.datastax.driver.core.querybuilder.Select; */ public class CassandraDataTemplate extends CassandraTemplate implements CassandraDataOperations { - /* - * Default Keyspace if none is passed in. - */ - private static final String KEYSPACE_DEFAULT = "system"; - /* * List of iterable classes when testing POJOs for specific operations. */ @@ -115,7 +110,7 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr */ public CassandraDataTemplate(Session session, CassandraConverter converter, String keyspace) { setSession(session); - this.keyspace = keyspace == null ? KEYSPACE_DEFAULT : keyspace; + this.keyspace = keyspace; this.cassandraConverter = converter; this.mappingContext = this.cassandraConverter.getMappingContext(); } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/Keyspace.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/Keyspace.java deleted file mode 100644 index 7135a475b..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/Keyspace.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2011-2013 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.data.cassandra.core; - -import com.datastax.driver.core.Session; - -/** - * Simple Cassandra Keyspace object - * - * @author Alex Shvid - * @deprecated This needs more thought. - */ -@Deprecated -public class Keyspace { - - private final String keyspace; - private final Session session; - - /** - * Constructor used for a basic keyspace configuration - * - * @param keyspace, system if {@literal null}. - * @param session must not be {@literal null}. - * @param cassandraConverter must not be {@literal null}. - */ - public Keyspace(String keyspace, Session session) { - this.keyspace = keyspace; - this.session = session; - } - - public String getKeyspace() { - return keyspace; - } - - public Session getSession() { - return session; - } -} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/SpringDataKeyspace.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/SpringDataKeyspace.java deleted file mode 100644 index 9e1aaa0e3..000000000 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/SpringDataKeyspace.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.springframework.data.cassandra.core; - -import org.springframework.data.cassandra.convert.CassandraConverter; -import org.springframework.util.Assert; - -import com.datastax.driver.core.Session; - -/** - * @deprecated This needs more thought. - */ -@Deprecated -public class SpringDataKeyspace extends Keyspace { - - private CassandraConverter converter; - - public SpringDataKeyspace(String keyspace, Session session, CassandraConverter converter) { - super(keyspace, session); - setCassandraConverter(converter); - } - - public CassandraConverter getCassandraConverter() { - return converter; - } - - private void setCassandraConverter(CassandraConverter converter) { - Assert.notNull(converter); - this.converter = converter; - } -} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java index 2e2bdc0d7..e27eda166 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java @@ -10,10 +10,6 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; -import org.springframework.data.cassandra.core.SpringDataKeyspace; -import org.springframework.util.Assert; - -import com.datastax.driver.core.Cluster; // @RunWith(SpringJUnit4ClassRunner.class) // @ContextConfiguration @@ -28,18 +24,6 @@ public class CassandraNamespaceTests { EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml"); } - // @Test - public void testSingleton() throws Exception { - Object cluster = ctx.getBean("cassandra-cluster"); - Assert.notNull(cluster); - Assert.isInstanceOf(Cluster.class, cluster); - Object ks = ctx.getBean("cassandra-keyspace"); - Assert.notNull(ks); - Assert.isInstanceOf(SpringDataKeyspace.class, ks); - - Cluster c = (Cluster) cluster; - } - @After public void clearCassandra() { EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); 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 79cf57d42..1960db90d 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 @@ -6,9 +6,11 @@ import org.springframework.cassandra.core.CassandraTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.cassandra.config.AbstractSpringDataCassandraConfiguration; -import org.springframework.data.cassandra.config.CassandraKeyspaceFactoryBean; +import org.springframework.data.cassandra.convert.CassandraConverter; +import org.springframework.data.cassandra.convert.MappingCassandraConverter; import org.springframework.data.cassandra.core.CassandraDataOperations; import org.springframework.data.cassandra.core.CassandraDataTemplate; +import org.springframework.data.cassandra.mapping.CassandraMappingContext; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster.Builder; @@ -38,21 +40,12 @@ public class TestConfig extends AbstractSpringDataCassandraConfiguration { return builder.build(); } - @Bean - public CassandraKeyspaceFactoryBean keyspaceFactoryBean() { - - CassandraKeyspaceFactoryBean bean = new CassandraKeyspaceFactoryBean(); - bean.setCluster(cluster()); - bean.setKeyspace("test"); - - return bean; - } - @Bean public CassandraSessionFactoryBean sessionFactoryBean() { CassandraSessionFactoryBean bean = new CassandraSessionFactoryBean(); bean.setCluster(cluster()); + bean.setKeyspaceName(getKeyspaceName()); return bean; } @@ -63,11 +56,20 @@ public class TestConfig extends AbstractSpringDataCassandraConfiguration { return template; } + @Bean + public CassandraConverter cassandraConverter() { + + CassandraConverter converter = new MappingCassandraConverter(new CassandraMappingContext()); + + return converter; + + } + @Bean public CassandraDataOperations cassandraDataTemplate() { - CassandraDataOperations template = new CassandraDataTemplate(keyspaceFactoryBean().getObject().getSession(), - keyspaceFactoryBean().getObject().getCassandraConverter(), keyspaceFactoryBean().getObject().getKeyspace()); + CassandraDataOperations template = new CassandraDataTemplate(sessionFactoryBean().getObject(), converter(), + keyspaceName); return template;