1 | /* |
2 | * Copyright 2012 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 | public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupport { |
36 | |
37 | private final Map<String, Bean<ElasticsearchOperations>> elasticsearchOperationsMap = new HashMap<String, Bean<ElasticsearchOperations>>(); |
38 | |
39 | @SuppressWarnings("unchecked") |
40 | <T> void processBean(@Observes ProcessBean<T> processBean) { |
41 | Bean<T> bean = processBean.getBean(); |
42 | for (Type type : bean.getTypes()) { |
43 | if (type instanceof Class<?> && ElasticsearchOperations.class.isAssignableFrom((Class<?>) type)) { |
44 | elasticsearchOperationsMap.put(bean.getQualifiers().toString(), ((Bean<ElasticsearchOperations>) bean)); |
45 | } |
46 | } |
47 | } |
48 | |
49 | void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { |
50 | for (Entry<Class<?>, Set<Annotation>> entry : getRepositoryTypes()) { |
51 | |
52 | Class<?> repositoryType = entry.getKey(); |
53 | Set<Annotation> qualifiers = entry.getValue(); |
54 | |
55 | Bean<?> repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager); |
56 | afterBeanDiscovery.addBean(repositoryBean); |
57 | } |
58 | } |
59 | |
60 | private <T> Bean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers, BeanManager beanManager) { |
61 | Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers.toString()); |
62 | |
63 | if (elasticsearchOperationsBean == null) { |
64 | throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", |
65 | ElasticsearchOperations.class.getName(), qualifiers)); |
66 | } |
67 | return new ElasticsearchRepositoryBean<T>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager); |
68 | } |
69 | |
70 | } |