Skip to content

Commit 023214a

Browse files
mp911demohsinh
authored andcommitted
DATAES-234 - Use Set to store qualifiers in CDI extension.
Qualifiers are now stored in their original set instead of using the toString representation.
1 parent 477e8e2 commit 023214a

File tree

7 files changed

+168
-16
lines changed

7 files changed

+168
-16
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 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.
@@ -33,7 +33,7 @@
3333
import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport;
3434

3535
/**
36-
* ElasticsearchRepositoryExtension
36+
* CDI extension to export Elasticsearch repositories.
3737
*
3838
* @author Rizwan Idrees
3939
* @author Mohsin Husen
@@ -42,14 +42,14 @@
4242
*/
4343
public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupport {
4444

45-
private final Map<String, Bean<ElasticsearchOperations>> elasticsearchOperationsMap = new HashMap<String, Bean<ElasticsearchOperations>>();
45+
private final Map<Set<Annotation>, Bean<ElasticsearchOperations>> elasticsearchOperationsMap = new HashMap<Set<Annotation>, Bean<ElasticsearchOperations>>();
4646

4747
@SuppressWarnings("unchecked")
4848
<T> void processBean(@Observes ProcessBean<T> processBean) {
4949
Bean<T> bean = processBean.getBean();
5050
for (Type type : bean.getTypes()) {
5151
if (type instanceof Class<?> && ElasticsearchOperations.class.isAssignableFrom((Class<?>) type)) {
52-
elasticsearchOperationsMap.put(bean.getQualifiers().toString(), ((Bean<ElasticsearchOperations>) bean));
52+
elasticsearchOperationsMap.put(bean.getQualifiers(), ((Bean<ElasticsearchOperations>) bean));
5353
}
5454
}
5555
}
@@ -69,8 +69,7 @@ void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanMan
6969
private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers,
7070
BeanManager beanManager) {
7171

72-
Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers
73-
.toString());
72+
Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers);
7473

7574
if (elasticsearchOperationsBean == null) {
7675
throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 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.
@@ -20,12 +20,13 @@
2020
/**
2121
* @author Mohsin Husen
2222
* @author Oliver Gierke
23+
* @author Mark Paluch
2324
*/
2425
class CdiRepositoryClient {
2526

2627
private CdiProductRepository repository;
2728
private SamplePersonRepository samplePersonRepository;
28-
29+
private QualifiedProductRepository qualifiedProductRepository;
2930

3031
public CdiProductRepository getRepository() {
3132
return repository;
@@ -44,4 +45,14 @@ public SamplePersonRepository getSamplePersonRepository() {
4445
public void setSamplePersonRepository(SamplePersonRepository samplePersonRepository) {
4546
this.samplePersonRepository = samplePersonRepository;
4647
}
48+
49+
public QualifiedProductRepository getQualifiedProductRepository() {
50+
return qualifiedProductRepository;
51+
}
52+
53+
@Inject
54+
public void setQualifiedProductRepository(
55+
@PersonDB @OtherQualifier QualifiedProductRepository qualifiedProductRepository) {
56+
this.qualifiedProductRepository = qualifiedProductRepository;
57+
}
4758
}

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 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.
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repositories.cdi;
1717

18-
import static org.hamcrest.CoreMatchers.is;
18+
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020

2121
import org.apache.webbeans.cditest.CdiTestContainer;
@@ -28,13 +28,14 @@
2828

2929
/**
3030
* @author Mohsin Husen
31+
* @author Mark Paluch
3132
*/
32-
3333
public class CdiRepositoryTests {
3434

3535
private static CdiTestContainer cdiContainer;
3636
private CdiProductRepository repository;
3737
private SamplePersonRepository personRepository;
38+
private QualifiedProductRepository qualifiedProductRepository;
3839

3940
@BeforeClass
4041
public static void init() throws Exception {
@@ -54,6 +55,7 @@ public void setUp() {
5455
CdiRepositoryClient client = cdiContainer.getInstance(CdiRepositoryClient.class);
5556
repository = client.getRepository();
5657
personRepository = client.getSamplePersonRepository();
58+
qualifiedProductRepository = client.getQualifiedProductRepository();
5759
}
5860

5961
@Test
@@ -84,6 +86,37 @@ public void testCdiRepository() {
8486
assertNull(retrieved);
8587
}
8688

89+
/**
90+
* @see DATAES-234
91+
*/
92+
@Test
93+
public void testQualifiedCdiRepository() {
94+
assertNotNull(qualifiedProductRepository);
95+
96+
Product bean = new Product();
97+
bean.setId("id-1");
98+
bean.setName("cidContainerTest-1");
99+
100+
qualifiedProductRepository.save(bean);
101+
102+
assertTrue(qualifiedProductRepository.exists(bean.getId()));
103+
104+
Product retrieved = qualifiedProductRepository.findOne(bean.getId());
105+
assertNotNull(retrieved);
106+
assertEquals(bean.getId(), retrieved.getId());
107+
assertEquals(bean.getName(), retrieved.getName());
108+
109+
assertEquals(1, qualifiedProductRepository.count());
110+
111+
assertTrue(qualifiedProductRepository.exists(bean.getId()));
112+
113+
qualifiedProductRepository.delete(bean);
114+
115+
assertEquals(0, qualifiedProductRepository.count());
116+
retrieved = qualifiedProductRepository.findOne(bean.getId());
117+
assertNull(retrieved);
118+
}
119+
87120
/**
88121
* @see DATAES-113
89122
*/

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
import javax.annotation.PreDestroy;
1919
import javax.enterprise.context.ApplicationScoped;
2020
import javax.enterprise.inject.Produces;
21-
import javax.xml.parsers.ParserConfigurationException;
22-
import java.io.IOException;
2321

22+
import org.elasticsearch.client.node.NodeClient;
2423
import org.springframework.data.elasticsearch.Utils;
2524
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
2625
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
27-
import org.xml.sax.SAXException;
2826

2927
/**
3028
* @author Mohsin Husen
@@ -33,8 +31,20 @@
3331
class ElasticsearchTemplateProducer {
3432

3533
@Produces
36-
public ElasticsearchOperations createElasticsearchTemplate() throws IOException, ParserConfigurationException, SAXException {
37-
return new ElasticsearchTemplate(Utils.getNodeClient());
34+
public NodeClient createNodeClient() {
35+
return Utils.getNodeClient();
36+
}
37+
38+
@Produces
39+
public ElasticsearchOperations createElasticsearchTemplate(NodeClient nodeClient) {
40+
return new ElasticsearchTemplate(nodeClient);
41+
}
42+
43+
@Produces
44+
@OtherQualifier
45+
@PersonDB
46+
public ElasticsearchOperations createQualifiedElasticsearchTemplate(NodeClient nodeClient) {
47+
return new ElasticsearchTemplate(nodeClient);
3848
}
3949

4050
@PreDestroy
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016 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+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
import javax.inject.Qualifier;
25+
26+
/**
27+
* @author Mark Paluch
28+
* @see DATAES-234
29+
*/
30+
@Qualifier
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
33+
@interface OtherQualifier {
34+
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016 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+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
import javax.inject.Qualifier;
25+
26+
/**
27+
* @author Mark Paluch
28+
* @see DATAES-234
29+
*/
30+
@Qualifier
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
33+
@interface PersonDB {
34+
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2016 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.Product;
19+
import org.springframework.data.repository.CrudRepository;
20+
21+
/**
22+
* @author Mark Paluch
23+
* @see DATAES-234
24+
*/
25+
@PersonDB
26+
@OtherQualifier
27+
public interface QualifiedProductRepository extends CrudRepository<Product, String> {
28+
29+
}

0 commit comments

Comments
 (0)