| 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.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 | * @author Rizwan Idrees |
| 33 | * @author Mohsin Husen |
| 34 | */ |
| 35 | public class ElasticsearchRepositoryBean<T> extends CdiRepositoryBean<T> { |
| 36 | |
| 37 | private final Bean<ElasticsearchOperations> elasticsearchOperationsBean; |
| 38 | |
| 39 | public ElasticsearchRepositoryBean(Bean<ElasticsearchOperations> operations, Set<Annotation> qualifiers, Class<T> repositoryType, |
| 40 | BeanManager beanManager) { |
| 41 | super(qualifiers, repositoryType, beanManager); |
| 42 | |
| 43 | Assert.notNull(operations, "Cannot create repository with 'null' for ElasticsearchOperations."); |
| 44 | this.elasticsearchOperationsBean = operations; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) { |
| 49 | ElasticsearchOperations elasticsearchOperations = getDependencyInstance(elasticsearchOperationsBean, ElasticsearchOperations.class); |
| 50 | return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Class<? extends Annotation> getScope() { |
| 55 | return elasticsearchOperationsBean.getScope(); |
| 56 | } |
| 57 | |
| 58 | } |