1 | /* |
2 | * Copyright 2013 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.repository.cdi; |
17 | |
18 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
19 | import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; |
20 | |
21 | import javax.enterprise.event.Observes; |
22 | import javax.enterprise.inject.UnsatisfiedResolutionException; |
23 | import javax.enterprise.inject.spi.AfterBeanDiscovery; |
24 | import javax.enterprise.inject.spi.Bean; |
25 | import javax.enterprise.inject.spi.BeanManager; |
26 | import javax.enterprise.inject.spi.ProcessBean; |
27 | import java.lang.annotation.Annotation; |
28 | import java.lang.reflect.Type; |
29 | import java.util.HashMap; |
30 | import java.util.Map; |
31 | import java.util.Map.Entry; |
32 | import java.util.Set; |
33 | |
34 | /** |
35 | * ElasticsearchRepositoryExtension |
36 | * |
37 | * @author Rizwan Idrees |
38 | * @author Mohsin Husen |
39 | */ |
40 | |
41 | public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupport { |
42 | |
43 | private final Map<String, Bean<ElasticsearchOperations>> elasticsearchOperationsMap = new HashMap<String, Bean<ElasticsearchOperations>>(); |
44 | |
45 | @SuppressWarnings("unchecked") |
46 | <T> void processBean(@Observes ProcessBean<T> processBean) { |
47 | Bean<T> bean = processBean.getBean(); |
48 | for (Type type : bean.getTypes()) { |
49 | if (type instanceof Class<?> && ElasticsearchOperations.class.isAssignableFrom((Class<?>) type)) { |
50 | elasticsearchOperationsMap.put(bean.getQualifiers().toString(), ((Bean<ElasticsearchOperations>) bean)); |
51 | } |
52 | } |
53 | } |
54 | |
55 | void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { |
56 | for (Entry<Class<?>, Set<Annotation>> entry : getRepositoryTypes()) { |
57 | |
58 | Class<?> repositoryType = entry.getKey(); |
59 | Set<Annotation> qualifiers = entry.getValue(); |
60 | |
61 | Bean<?> repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager); |
62 | afterBeanDiscovery.addBean(repositoryBean); |
63 | } |
64 | } |
65 | |
66 | private <T> Bean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers, BeanManager beanManager) { |
67 | Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers.toString()); |
68 | |
69 | if (elasticsearchOperationsBean == null) { |
70 | throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", |
71 | ElasticsearchOperations.class.getName(), qualifiers)); |
72 | } |
73 | return new ElasticsearchRepositoryBean<T>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager); |
74 | } |
75 | |
76 | } |