From 8a9b3d647138382faab604bb71d30741a856eba3 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 8 Aug 2014 08:05:48 +0200 Subject: [PATCH] DATAGRAPH-500 - Add support for custom implementations in CDI repositories. Original pull request: #198. --- .../cdi/Neo4jCdiRepositoryBean.java | 11 ++++--- .../cdi/Neo4jCdiRepositoryExtension.java | 5 +++- .../cdi/CdiExtensionIntegrationTests.java | 15 ++++++++++ .../repository/cdi/RepositoryClient.java | 1 + .../cdi/SamplePersonRepository.java | 27 +++++++++++++++++ .../cdi/SamplePersonRepositoryCustom.java | 26 +++++++++++++++++ .../cdi/SamplePersonRepositoryImpl.java | 29 +++++++++++++++++++ 7 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepository.java create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepositoryCustom.java create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepositoryImpl.java diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryBean.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryBean.java index b94bde52a..f5f8fe730 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryBean.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryBean.java @@ -32,6 +32,7 @@ import java.util.Set; * {@link org.springframework.data.repository.cdi.CdiRepositoryBean} to create Neo4j repository instances via CDI. * * @author Nicki Watt + * @author Mark Paluch */ public class Neo4jCdiRepositoryBean extends CdiRepositoryBean { @@ -44,11 +45,13 @@ public class Neo4jCdiRepositoryBean extends CdiRepositoryBean { * @param qualifiers must not be {@literal null}. * @param repositoryType must not be {@literal null}. * @param beanManager must not be {@literal null}. + * @param customImplementationBean the bean for the custom implementation of the + * {@link org.springframework.data.repository.Repository}, can be {@literal null}. */ public Neo4jCdiRepositoryBean(Bean graphDatabase, - Set qualifiers, Class repositoryType, BeanManager beanManager) { + Set qualifiers, Class repositoryType, BeanManager beanManager, Bean customImplementationBean) { - super(qualifiers, repositoryType, beanManager); + super(qualifiers, repositoryType, beanManager, customImplementationBean); this.graphDatabase = graphDatabase; } @@ -58,12 +61,12 @@ public class Neo4jCdiRepositoryBean extends CdiRepositoryBean { * @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class) */ @Override - protected T create(CreationalContext creationalContext, Class repositoryType) { + protected T create(CreationalContext creationalContext, Class repositoryType, Object customImplementation) { Neo4jMappingContext neo4jMapCtx = new Neo4jMappingContext(); Neo4jTemplate neo4jTemplate = new Neo4jTemplate(getDependencyInstance(graphDatabase, GraphDatabase.class)); GraphRepositoryFactory factory = new GraphRepositoryFactory(neo4jTemplate, neo4jMapCtx); - return factory.getRepository(repositoryType); + return factory.getRepository(repositoryType, customImplementation); } } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryExtension.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryExtension.java index 2e9da9cda..fe0ecca01 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryExtension.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryExtension.java @@ -42,6 +42,7 @@ import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; * * @author Nicki Watt * @author Oliver Gierke + * @author Mark Paluch */ public class Neo4jCdiRepositoryExtension extends CdiRepositoryExtensionSupport { @@ -108,7 +109,9 @@ public class Neo4jCdiRepositoryExtension extends CdiRepositoryExtensionSupport { Neo4jMappingContext.class.getName(), qualifiers)); } + Bean customImplementationBean = getCustomImplementationBean(repositoryType, beanManager, qualifiers); + return new Neo4jCdiRepositoryBean(graphDatabase, qualifiers, repositoryType, - beanManager); + beanManager, customImplementationBean); } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiExtensionIntegrationTests.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiExtensionIntegrationTests.java index ecd447f72..93b914d0a 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiExtensionIntegrationTests.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiExtensionIntegrationTests.java @@ -141,4 +141,19 @@ public class CdiExtensionIntegrationTests { } } + + /** + * @see DATAGRAPH-500 + */ + @Test + public void returnOneFromCustomImpl() { + + GraphDatabase database = container.getInstance(GraphDatabase.class); + + try (Transaction tx = database.beginTx()) { + + RepositoryClient client = container.getInstance(RepositoryClient.class); + assertThat(client.samplePersonRepository.returnOne(), is(1)); + } + } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/RepositoryClient.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/RepositoryClient.java index 5a56602ac..690bce531 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/RepositoryClient.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/RepositoryClient.java @@ -26,4 +26,5 @@ class RepositoryClient { @Inject CdiPersonRepository repository; @Inject CdiPersonRepository2 repository2; @Inject CdiPersonRepository3 repository3; + @Inject SamplePersonRepository samplePersonRepository; } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepository.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepository.java new file mode 100644 index 000000000..a27c47317 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepository.java @@ -0,0 +1,27 @@ +/* + * 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.neo4j.repository.cdi; + +import org.springframework.data.neo4j.model.Person; +import org.springframework.data.repository.Repository; + +/** + * @author Mark Paluch + * @see DATAGRAPH-500 + */ +public interface SamplePersonRepository extends Repository, SamplePersonRepositoryCustom { + +} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepositoryCustom.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepositoryCustom.java new file mode 100644 index 000000000..8f4ac8ce0 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/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.neo4j.repository.cdi; + +/** + * @see DATAGRAPH-500 + * @author Mark Paluch + */ +interface SamplePersonRepositoryCustom { + + int returnOne(); +} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepositoryImpl.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/SamplePersonRepositoryImpl.java new file mode 100644 index 000000000..9b3506735 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/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.neo4j.repository.cdi; + +/** + * @see DATAGRAPH-500 + * @author Mark Paluch + */ +class SamplePersonRepositoryImpl implements SamplePersonRepositoryCustom { + + @Override + public int returnOne() { + return 1; + } +}