) bean));
+ }
+ }
+ }
+
+ /**
+ * Implementation of a an observer which registers beans to the CDI container for the detected Spring Data
+ * repositories.
+ *
+ * The repository beans are associated to the EntityManagers using their qualifiers.
+ *
+ * @param beanManager The BeanManager instance.
+ */
+ void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) {
+ for (Map.Entry, Set> entry : getRepositoryTypes()) {
+
+ Class> repositoryType = entry.getKey();
+ Set qualifiers = entry.getValue();
+
+ CdiRepositoryBean> repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager);
+ afterBeanDiscovery.addBean(repositoryBean);
+ registerBean(repositoryBean);
+ }
+ }
+
+ /**
+ * Creates a {@link Bean}.
+ *
+ * @param The type of the repository.
+ * @param repositoryType The class representing the repository.
+ * @param beanManager The BeanManager instance.
+ * @return The bean.
+ */
+ private CdiRepositoryBean createRepositoryBean(Class repositoryType, Set qualifiers,
+ BeanManager beanManager) {
+
+ Bean cassandraOperationsBean = this.cassandraOperationsMap.get(qualifiers.toString());
+
+ if (cassandraOperationsBean == null) {
+ throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",
+ CassandraOperations.class.getName(), qualifiers));
+ }
+
+ return new CassandraRepositoryBean(cassandraOperationsBean, qualifiers, repositoryType, beanManager,
+ getCustomImplementationDetector());
+ }
+
+}
diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/CassandraRepositoryFactory.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/CassandraRepositoryFactory.java
index 0c5ab8ed3..01f453f28 100644
--- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/CassandraRepositoryFactory.java
+++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/CassandraRepositoryFactory.java
@@ -19,7 +19,7 @@ import java.io.Serializable;
import java.lang.reflect.Method;
import org.springframework.dao.InvalidDataAccessApiUsageException;
-import org.springframework.data.cassandra.core.CassandraTemplate;
+import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository;
@@ -44,20 +44,20 @@ import org.springframework.util.Assert;
public class CassandraRepositoryFactory extends RepositoryFactorySupport {
- private final CassandraTemplate cassandraTemplate;
+ private final CassandraOperations cassandraTemplate;
private final CassandraMappingContext mappingContext;
/**
- * Creates a new {@link MongoRepositoryFactory} with the given {@link MongoOperations}.
+ * Creates a new {@link CassandraRepositoryFactory} with the given {@link CassandraOperations}.
*
- * @param mongoOperations must not be {@literal null}
+ * @param cassandraOperations must not be {@literal null}
*/
- public CassandraRepositoryFactory(CassandraTemplate cassandraTemplate) {
+ public CassandraRepositoryFactory(CassandraOperations cassandraOperations) {
- Assert.notNull(cassandraTemplate);
+ Assert.notNull(cassandraOperations);
- this.cassandraTemplate = cassandraTemplate;
- this.mappingContext = cassandraTemplate.getConverter().getMappingContext();
+ this.cassandraTemplate = cassandraOperations;
+ this.mappingContext = cassandraOperations.getConverter().getMappingContext();
// TODO: remove when supporting declarative query methods
setQueryLookupStrategyKey(QueryLookupStrategy.Key.USE_DECLARED_QUERY);
diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java
index 017b92a0d..2725ba3a5 100644
--- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java
+++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleCassandraRepository.java
@@ -35,7 +35,7 @@ import com.datastax.driver.core.querybuilder.Select;
*/
public class SimpleCassandraRepository implements TypedIdCassandraRepository {
- protected CassandraOperations template;
+ protected CassandraOperations operations;
protected CassandraEntityInformation entityInformation;
/**
@@ -43,45 +43,45 @@ public class SimpleCassandraRepository implements Ty
* {@link CassandraTemplate}.
*
* @param metadata must not be {@literal null}.
- * @param template must not be {@literal null}.
+ * @param operations must not be {@literal null}.
*/
- public SimpleCassandraRepository(CassandraEntityInformation metadata, CassandraTemplate template) {
+ public SimpleCassandraRepository(CassandraEntityInformation metadata, CassandraOperations operations) {
- Assert.notNull(template);
+ Assert.notNull(operations);
Assert.notNull(metadata);
this.entityInformation = metadata;
- this.template = template;
+ this.operations = operations;
}
@Override
public S save(S entity) {
- return template.insert(entity);
+ return operations.insert(entity);
}
@Override
public List save(Iterable entities) {
- return template.insert(CollectionUtils.toList(entities));
+ return operations.insert(CollectionUtils.toList(entities));
}
@Override
public T findOne(ID id) {
- return template.selectOneById(entityInformation.getJavaType(), id);
+ return operations.selectOneById(entityInformation.getJavaType(), id);
}
@Override
public boolean exists(ID id) {
- return template.exists(entityInformation.getJavaType(), id);
+ return operations.exists(entityInformation.getJavaType(), id);
}
@Override
public long count() {
- return template.count(entityInformation.getTableName());
+ return operations.count(entityInformation.getTableName());
}
@Override
public void delete(ID id) {
- template.deleteById(entityInformation.getJavaType(), id);
+ operations.deleteById(entityInformation.getJavaType(), id);
}
@Override
@@ -91,25 +91,25 @@ public class SimpleCassandraRepository implements Ty
@Override
public void delete(Iterable extends T> entities) {
- template.delete(CollectionUtils.toList(entities));
+ operations.delete(CollectionUtils.toList(entities));
}
@Override
public void deleteAll() {
- template.truncate(entityInformation.getTableName());
+ operations.truncate(entityInformation.getTableName());
}
@Override
public List findAll() {
- return template.selectAll(entityInformation.getJavaType());
+ return operations.selectAll(entityInformation.getJavaType());
}
@Override
public Iterable findAll(Iterable ids) {
- return template.selectBySimpleIds(entityInformation.getJavaType(), ids);
+ return operations.selectBySimpleIds(entityInformation.getJavaType(), ids);
}
protected List findAll(Select query) {
- return template.select(query, entityInformation.getJavaType());
+ return operations.select(query, entityInformation.getJavaType());
}
}
diff --git a/spring-data-cassandra/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/spring-data-cassandra/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
new file mode 100644
index 000000000..686d46819
--- /dev/null
+++ b/spring-data-cassandra/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
@@ -0,0 +1 @@
+org.springframework.data.cassandra.repository.cdi.CassandraRepositoryExtension
diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CassandraOperationsProducer.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CassandraOperationsProducer.java
new file mode 100644
index 000000000..e7fab7c0b
--- /dev/null
+++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CassandraOperationsProducer.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2014 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.test.integration.repository.cdi;
+
+import java.util.HashMap;
+import java.util.Set;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Disposes;
+import javax.enterprise.inject.Produces;
+
+import com.google.common.collect.Sets;
+import com.google.common.util.concurrent.Service;
+import org.springframework.cassandra.core.cql.CqlIdentifier;
+import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
+import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest;
+import org.springframework.data.cassandra.convert.MappingCassandraConverter;
+import org.springframework.data.cassandra.core.CassandraAdminTemplate;
+import org.springframework.data.cassandra.core.CassandraOperations;
+import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
+import org.springframework.data.cassandra.test.integration.repository.User;
+
+/**
+ * @author Mark Paluch
+ */
+@ApplicationScoped
+class CassandraOperationsProducer {
+
+ @Produces
+ public CassandraOperations createCassandraOperations() throws Exception {
+ String keySpace = AbstractEmbeddedCassandraIntegrationTest.randomKeyspaceName();
+
+ MappingCassandraConverter cassandraConverter = new MappingCassandraConverter();
+ CassandraAdminTemplate cassandraTemplate = new CassandraAdminTemplate(AbstractEmbeddedCassandraIntegrationTest
+ .cluster().connect(), cassandraConverter);
+
+ CreateKeyspaceSpecification createKeyspaceSpecification = new CreateKeyspaceSpecification(keySpace).ifNotExists();
+ cassandraTemplate.execute(createKeyspaceSpecification);
+ cassandraTemplate.execute("USE " + keySpace);
+
+ cassandraTemplate.createTable(true, CqlIdentifier.cqlId("users"), User.class, new HashMap());
+
+ for (CassandraPersistentEntity> entity : cassandraTemplate.getConverter().getMappingContext()
+ .getPersistentEntities()) {
+ cassandraTemplate.truncate(entity.getTableName());
+ }
+
+ return cassandraTemplate;
+ }
+
+ public void close(@Disposes CassandraOperations cassandraOperations) {
+ cassandraOperations.getSession().close();
+ }
+
+ @Produces
+ public Set producerToSatisfyGuavaDependenciesWhenTesting() {
+ return Sets.newHashSet();
+ }
+
+}
diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryClient.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryClient.java
new file mode 100644
index 000000000..fef23a0a7
--- /dev/null
+++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryClient.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2014 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.test.integration.repository.cdi;
+
+import javax.inject.Inject;
+
+/**
+ * @author Mohsin Husen
+ * @author Oliver Gierke
+ */
+class CdiRepositoryClient {
+
+ private CdiUserRepository repository;
+ private SamplePersonRepository samplePersonRepository;
+
+ public CdiUserRepository getRepository() {
+ return repository;
+ }
+
+ @Inject
+ public void setRepository(CdiUserRepository repository) {
+ this.repository = repository;
+ }
+
+ public SamplePersonRepository getSamplePersonRepository() {
+ return samplePersonRepository;
+ }
+
+ @Inject
+ public void setSamplePersonRepository(SamplePersonRepository samplePersonRepository) {
+ this.samplePersonRepository = samplePersonRepository;
+ }
+}
diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryTests.java
new file mode 100644
index 000000000..68ea58f5a
--- /dev/null
+++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryTests.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2014 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.test.integration.repository.cdi;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.apache.webbeans.cditest.CdiTestContainer;
+import org.apache.webbeans.cditest.CdiTestContainerLoader;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest;
+import org.springframework.data.cassandra.test.integration.repository.User;
+
+/**
+ * @author Mohsin Husen
+ * @author Mark Paluch
+ */
+
+public class CdiRepositoryTests extends AbstractEmbeddedCassandraIntegrationTest {
+
+ private static CdiTestContainer cdiContainer;
+ private CdiUserRepository repository;
+ private SamplePersonRepository personRepository;
+
+ @BeforeClass
+ public static void init() throws Exception {
+ startCassandra();
+ cdiContainer = CdiTestContainerLoader.getCdiContainer();
+ cdiContainer.startApplicationScope();
+ cdiContainer.bootContainer();
+ }
+
+ @AfterClass
+ public static void shutdown() throws Exception {
+ cdiContainer.stopContexts();
+ cdiContainer.shutdownContainer();
+ }
+
+ @Before
+ public void setUp() {
+ CdiRepositoryClient client = cdiContainer.getInstance(CdiRepositoryClient.class);
+ repository = client.getRepository();
+ personRepository = client.getSamplePersonRepository();
+ }
+
+ @Test
+ public void testCdiRepository() {
+ assertNotNull(repository);
+
+ User bean = new User();
+ bean.setUsername("username");
+ bean.setFirstName("first");
+ bean.setLastName("last");
+
+ repository.save(bean);
+
+ assertTrue(repository.exists(bean.getUsername()));
+
+ User retrieved = repository.findOne(bean.getUsername());
+ assertNotNull(retrieved);
+ assertEquals(bean.getUsername(), retrieved.getUsername());
+ assertEquals(bean.getFirstName(), retrieved.getFirstName());
+ assertEquals(bean.getLastName(), retrieved.getLastName());
+
+ assertEquals(1, repository.count());
+
+ assertTrue(repository.exists(bean.getUsername()));
+
+ repository.delete(bean);
+
+ assertEquals(0, repository.count());
+ retrieved = repository.findOne(bean.getUsername());
+ assertNull(retrieved);
+ }
+
+ /**
+ * @see DATACASS-149
+ */
+ @Test
+ public void returnOneFromCustomImpl() {
+
+ assertThat(personRepository.returnOne(), is(1));
+ }
+}
diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiUserRepository.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiUserRepository.java
new file mode 100644
index 000000000..8a3b5dac0
--- /dev/null
+++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiUserRepository.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2014 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.test.integration.repository.cdi;
+
+import org.springframework.data.cassandra.test.integration.repository.User;
+import org.springframework.data.repository.CrudRepository;
+
+/**
+ * @author Mohsin Husen
+ * @author Oliver Gierke
+ */
+public interface CdiUserRepository extends CrudRepository {
+
+ User findOne(String id);
+}
diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/SamplePersonRepository.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/SamplePersonRepository.java
new file mode 100644
index 000000000..bba7a5f7e
--- /dev/null
+++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/SamplePersonRepository.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2014 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.test.integration.repository.cdi;
+
+import org.springframework.data.cassandra.test.integration.querymethods.declared.Person;
+import org.springframework.data.repository.Repository;
+
+/**
+ * @author Mark Paluch
+ * @see DATACASS-149
+ */
+public interface SamplePersonRepository extends Repository, SamplePersonRepositoryCustom {
+
+}
diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/SamplePersonRepositoryCustom.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/SamplePersonRepositoryCustom.java
new file mode 100644
index 000000000..7bf719820
--- /dev/null
+++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/SamplePersonRepositoryCustom.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2014 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.test.integration.repository.cdi;
+
+/**
+ * @see DATACASS-149
+ * @author Mark Paluch
+ */
+interface SamplePersonRepositoryCustom {
+
+ int returnOne();
+}
diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/SamplePersonRepositoryImpl.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/SamplePersonRepositoryImpl.java
new file mode 100644
index 000000000..f5b5b94eb
--- /dev/null
+++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/SamplePersonRepositoryImpl.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2014 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.test.integration.repository.cdi;
+
+/**
+ * @see DATACASS-149
+ * @author Mark Paluch
+ */
+class SamplePersonRepositoryImpl implements SamplePersonRepositoryCustom {
+
+ @Override
+ public int returnOne() {
+ return 1;
+ }
+}
diff --git a/spring-data-cassandra/template.mf b/spring-data-cassandra/template.mf
index 053c579cc..8b6db04ba 100644
--- a/spring-data-cassandra/template.mf
+++ b/spring-data-cassandra/template.mf
@@ -5,6 +5,7 @@ Bundle-ManifestVersion: 2
Import-Package:
sun.reflect;version="0";resolution:=optional
Import-Template:
+ javax.enterprise.*;version="${cdi:[=.=.=,+1.0.0)}";resolution:=optional,
org.springframework.beans.*;version="[3.1.0, 4.0.0)",
org.springframework.cache.*;version="[3.1.0, 4.0.0)",
org.springframework.context.*;version="[3.1.0, 4.0.0)",