From b77b8ba0f26f626d42c2bd4c44ff036a745ef07e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 7 Aug 2014 20:43:53 +0200 Subject: [PATCH] DATAES-113 Add support for custom implementations in CDI repositories --- .../cdi/ElasticsearchRepositoryBean.java | 21 ++++++++++---- .../cdi/ElasticsearchRepositoryExtension.java | 4 ++- .../repositories/cdi/CdiRepositoryClient.java | 11 +++++++ .../repositories/cdi/CdiRepositoryTests.java | 12 ++++++++ .../cdi/SamplePersonRepository.java | 27 +++++++++++++++++ .../cdi/SamplePersonRepositoryCustom.java | 26 +++++++++++++++++ .../cdi/SamplePersonRepositoryImpl.java | 29 +++++++++++++++++++ 7 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/springframework/data/elasticsearch/repositories/cdi/SamplePersonRepository.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/repositories/cdi/SamplePersonRepositoryCustom.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/repositories/cdi/SamplePersonRepositoryImpl.java diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryBean.java b/src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryBean.java index 3128935e1..78bf02b56 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryBean.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-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. @@ -31,24 +31,35 @@ import org.springframework.util.Assert; * * @author Rizwan Idrees * @author Mohsin Husen + * @author Mark Paluch */ public class ElasticsearchRepositoryBean extends CdiRepositoryBean { private final Bean elasticsearchOperationsBean; + /** + * Creates a new {@link ElasticsearchRepositoryBean}. + * + * @param operations must not be {@literal null}. + * @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 ElasticsearchRepositoryBean(Bean operations, Set qualifiers, - Class repositoryType, BeanManager beanManager) { - super(qualifiers, repositoryType, beanManager); + Class repositoryType, BeanManager beanManager, Bean customImplementationBean) { + super(qualifiers, repositoryType, beanManager, customImplementationBean); Assert.notNull(operations, "Cannot create repository with 'null' for ElasticsearchOperations."); this.elasticsearchOperationsBean = operations; } @Override - protected T create(CreationalContext creationalContext, Class repositoryType) { + protected T create(CreationalContext creationalContext, Class repositoryType, Object customImplementation) { ElasticsearchOperations elasticsearchOperations = getDependencyInstance(elasticsearchOperationsBean, ElasticsearchOperations.class); - return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType); + return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType, customImplementation); } @Override diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryExtension.java b/src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryExtension.java index 12e838fb3..0253201af 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryExtension.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryExtension.java @@ -38,6 +38,7 @@ import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; * @author Rizwan Idrees * @author Mohsin Husen * @author Oliver Gierke + * @author Mark Paluch */ public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupport { @@ -74,7 +75,8 @@ public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupp throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", ElasticsearchOperations.class.getName(), qualifiers)); } + Bean customImplementationBean = getCustomImplementationBean(repositoryType, beanManager, qualifiers); - return new ElasticsearchRepositoryBean(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager); + return new ElasticsearchRepositoryBean(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager, customImplementationBean); } } diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryClient.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryClient.java index ec3b3214a..15b1088ee 100644 --- a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryClient.java +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryClient.java @@ -24,6 +24,8 @@ import javax.inject.Inject; class CdiRepositoryClient { private CdiProductRepository repository; + private SamplePersonRepository samplePersonRepository; + public CdiProductRepository getRepository() { return repository; @@ -33,4 +35,13 @@ class CdiRepositoryClient { public void setRepository(CdiProductRepository repository) { this.repository = repository; } + + public SamplePersonRepository getSamplePersonRepository() { + return samplePersonRepository; + } + + @Inject + public void setSamplePersonRepository(SamplePersonRepository samplePersonRepository) { + this.samplePersonRepository = samplePersonRepository; + } } diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryTests.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryTests.java index cc56e4b6c..8aebdc01d 100644 --- a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryTests.java @@ -15,6 +15,7 @@ */ package org.springframework.data.elasticsearch.repositories.cdi; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; import org.apache.webbeans.cditest.CdiTestContainer; @@ -33,6 +34,7 @@ public class CdiRepositoryTests { private static CdiTestContainer cdiContainer; private CdiProductRepository repository; + private SamplePersonRepository personRepository; @BeforeClass public static void init() throws Exception { @@ -51,6 +53,7 @@ public class CdiRepositoryTests { public void setUp() { CdiRepositoryClient client = cdiContainer.getInstance(CdiRepositoryClient.class); repository = client.getRepository(); + personRepository = client.getSamplePersonRepository(); } @Test @@ -80,4 +83,13 @@ public class CdiRepositoryTests { retrieved = repository.findOne(bean.getId()); assertNull(retrieved); } + + /** + * @see DATAES-113 + */ + @Test + public void returnOneFromCustomImpl() { + + assertThat(personRepository.returnOne(), is(1)); + } } diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/SamplePersonRepository.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/SamplePersonRepository.java new file mode 100644 index 000000000..0d9c67b7b --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/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.elasticsearch.repositories.cdi; + +import org.springframework.data.elasticsearch.entities.Person; +import org.springframework.data.repository.Repository; + +/** + * @author Mark Paluch + * @see DATAES-113 + */ +public interface SamplePersonRepository extends Repository, SamplePersonRepositoryCustom { + +} diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/SamplePersonRepositoryCustom.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/SamplePersonRepositoryCustom.java new file mode 100644 index 000000000..8c8058203 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/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.elasticsearch.repositories.cdi; + +/** + * @see DATAES-113 + * @author Mark Paluch + */ +interface SamplePersonRepositoryCustom { + + int returnOne(); +} diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/SamplePersonRepositoryImpl.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/SamplePersonRepositoryImpl.java new file mode 100644 index 000000000..a2ae819c9 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/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.elasticsearch.repositories.cdi; + +/** + * @see DATAES-113 + * @author Mark Paluch + */ +class SamplePersonRepositoryImpl implements SamplePersonRepositoryCustom { + + @Override + public int returnOne() { + return 1; + } +}