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.elasticsearch.repository.support.ElasticsearchRepositoryFactory; |
20 | import org.springframework.data.repository.cdi.CdiRepositoryBean; |
21 | import org.springframework.util.Assert; |
22 | |
23 | import javax.enterprise.context.spi.CreationalContext; |
24 | import javax.enterprise.inject.spi.Bean; |
25 | import javax.enterprise.inject.spi.BeanManager; |
26 | import java.lang.annotation.Annotation; |
27 | import java.util.Set; |
28 | |
29 | /** |
30 | * Uses CdiRepositoryBean to create ElasticsearchRepository instances. |
31 | * |
32 | */ |
33 | public class ElasticsearchRepositoryBean<T> extends CdiRepositoryBean<T> { |
34 | |
35 | private final Bean<ElasticsearchOperations> elasticsearchOperationsBean; |
36 | |
37 | public ElasticsearchRepositoryBean(Bean<ElasticsearchOperations> operations, Set<Annotation> qualifiers, Class<T> repositoryType, |
38 | BeanManager beanManager) { |
39 | super(qualifiers, repositoryType, beanManager); |
40 | |
41 | Assert.notNull(operations, "Cannot create repository with 'null' for ElasticsearchOperations."); |
42 | this.elasticsearchOperationsBean = operations; |
43 | } |
44 | |
45 | @Override |
46 | protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) { |
47 | ElasticsearchOperations elasticsearchOperations = getDependencyInstance(elasticsearchOperationsBean, ElasticsearchOperations.class); |
48 | return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType); |
49 | } |
50 | |
51 | @Override |
52 | public Class<? extends Annotation> getScope() { |
53 | return elasticsearchOperationsBean.getScope(); |
54 | } |
55 | |
56 | } |