Skip to content

Commit b77b8ba

Browse files
mp911demohsinh
authored andcommitted
DATAES-113 Add support for custom implementations in CDI repositories
1 parent 1f99dcb commit b77b8ba

File tree

7 files changed

+124
-6
lines changed

7 files changed

+124
-6
lines changed

src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryBean.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,24 +31,35 @@
3131
*
3232
* @author Rizwan Idrees
3333
* @author Mohsin Husen
34+
* @author Mark Paluch
3435
*/
3536
public class ElasticsearchRepositoryBean<T> extends CdiRepositoryBean<T> {
3637

3738
private final Bean<ElasticsearchOperations> elasticsearchOperationsBean;
3839

40+
/**
41+
* Creates a new {@link ElasticsearchRepositoryBean}.
42+
*
43+
* @param operations must not be {@literal null}.
44+
* @param qualifiers must not be {@literal null}.
45+
* @param repositoryType must not be {@literal null}.
46+
* @param beanManager must not be {@literal null}.
47+
* @param customImplementationBean the bean for the custom implementation of the
48+
* {@link org.springframework.data.repository.Repository}, can be {@literal null}.
49+
*/
3950
public ElasticsearchRepositoryBean(Bean<ElasticsearchOperations> operations, Set<Annotation> qualifiers,
40-
Class<T> repositoryType, BeanManager beanManager) {
41-
super(qualifiers, repositoryType, beanManager);
51+
Class<T> repositoryType, BeanManager beanManager, Bean<?> customImplementationBean) {
52+
super(qualifiers, repositoryType, beanManager, customImplementationBean);
4253

4354
Assert.notNull(operations, "Cannot create repository with 'null' for ElasticsearchOperations.");
4455
this.elasticsearchOperationsBean = operations;
4556
}
4657

4758
@Override
48-
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
59+
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType, Object customImplementation) {
4960
ElasticsearchOperations elasticsearchOperations = getDependencyInstance(elasticsearchOperationsBean,
5061
ElasticsearchOperations.class);
51-
return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType);
62+
return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType, customImplementation);
5263
}
5364

5465
@Override

src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryExtension.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author Rizwan Idrees
3939
* @author Mohsin Husen
4040
* @author Oliver Gierke
41+
* @author Mark Paluch
4142
*/
4243
public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupport {
4344

@@ -74,7 +75,8 @@ private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, S
7475
throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",
7576
ElasticsearchOperations.class.getName(), qualifiers));
7677
}
78+
Bean<?> customImplementationBean = getCustomImplementationBean(repositoryType, beanManager, qualifiers);
7779

78-
return new ElasticsearchRepositoryBean<T>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager);
80+
return new ElasticsearchRepositoryBean<T>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager, customImplementationBean);
7981
}
8082
}

src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
class CdiRepositoryClient {
2525

2626
private CdiProductRepository repository;
27+
private SamplePersonRepository samplePersonRepository;
28+
2729

2830
public CdiProductRepository getRepository() {
2931
return repository;
@@ -33,4 +35,13 @@ public CdiProductRepository getRepository() {
3335
public void setRepository(CdiProductRepository repository) {
3436
this.repository = repository;
3537
}
38+
39+
public SamplePersonRepository getSamplePersonRepository() {
40+
return samplePersonRepository;
41+
}
42+
43+
@Inject
44+
public void setSamplePersonRepository(SamplePersonRepository samplePersonRepository) {
45+
this.samplePersonRepository = samplePersonRepository;
46+
}
3647
}

src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repositories.cdi;
1717

18+
import static org.hamcrest.CoreMatchers.is;
1819
import static org.junit.Assert.*;
1920

2021
import org.apache.webbeans.cditest.CdiTestContainer;
@@ -33,6 +34,7 @@ public class CdiRepositoryTests {
3334

3435
private static CdiTestContainer cdiContainer;
3536
private CdiProductRepository repository;
37+
private SamplePersonRepository personRepository;
3638

3739
@BeforeClass
3840
public static void init() throws Exception {
@@ -51,6 +53,7 @@ public static void shutdown() throws Exception {
5153
public void setUp() {
5254
CdiRepositoryClient client = cdiContainer.getInstance(CdiRepositoryClient.class);
5355
repository = client.getRepository();
56+
personRepository = client.getSamplePersonRepository();
5457
}
5558

5659
@Test
@@ -80,4 +83,13 @@ public void testCdiRepository() {
8083
retrieved = repository.findOne(bean.getId());
8184
assertNull(retrieved);
8285
}
86+
87+
/**
88+
* @see DATAES-113
89+
*/
90+
@Test
91+
public void returnOneFromCustomImpl() {
92+
93+
assertThat(personRepository.returnOne(), is(1));
94+
}
8395
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.repositories.cdi;
17+
18+
import org.springframework.data.elasticsearch.entities.Person;
19+
import org.springframework.data.repository.Repository;
20+
21+
/**
22+
* @author Mark Paluch
23+
* @see DATAES-113
24+
*/
25+
public interface SamplePersonRepository extends Repository<Person, Long>, SamplePersonRepositoryCustom {
26+
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.elasticsearch.repositories.cdi;
18+
19+
/**
20+
* @see DATAES-113
21+
* @author Mark Paluch
22+
*/
23+
interface SamplePersonRepositoryCustom {
24+
25+
int returnOne();
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.elasticsearch.repositories.cdi;
18+
19+
/**
20+
* @see DATAES-113
21+
* @author Mark Paluch
22+
*/
23+
class SamplePersonRepositoryImpl implements SamplePersonRepositoryCustom {
24+
25+
@Override
26+
public int returnOne() {
27+
return 1;
28+
}
29+
}

0 commit comments

Comments
 (0)