|
| 1 | +/* |
| 2 | + * Copyright 2011 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.mongodb.repository.cdi; |
| 17 | + |
| 18 | +import java.lang.annotation.Annotation; |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Map.Entry; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import javax.enterprise.event.Observes; |
| 27 | +import javax.enterprise.inject.UnsatisfiedResolutionException; |
| 28 | +import javax.enterprise.inject.spi.AfterBeanDiscovery; |
| 29 | +import javax.enterprise.inject.spi.Bean; |
| 30 | +import javax.enterprise.inject.spi.BeanManager; |
| 31 | +import javax.enterprise.inject.spi.ProcessBean; |
| 32 | + |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 36 | +import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; |
| 37 | + |
| 38 | +/** |
| 39 | + * CDI extension to export Mongo repositories. |
| 40 | + * |
| 41 | + * @author Oliver Gierke |
| 42 | + */ |
| 43 | +public class MongoRepositoryExtension extends CdiRepositoryExtensionSupport { |
| 44 | + |
| 45 | + private static final Logger LOG = LoggerFactory.getLogger(MongoRepositoryExtension.class); |
| 46 | + |
| 47 | + private final Map<Set<Annotation>, Bean<MongoOperations>> mongoOperations = new HashMap<Set<Annotation>, Bean<MongoOperations>>(); |
| 48 | + |
| 49 | + public MongoRepositoryExtension() { |
| 50 | + LOG.info("Activating CDI extension for Spring Data MongoDB repositories."); |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("unchecked") |
| 54 | + <X> void processBean(@Observes ProcessBean<X> processBean) { |
| 55 | + |
| 56 | + Bean<X> bean = processBean.getBean(); |
| 57 | + |
| 58 | + for (Type type : bean.getTypes()) { |
| 59 | + if (type instanceof Class<?> && MongoOperations.class.isAssignableFrom((Class<?>) type)) { |
| 60 | + if (LOG.isDebugEnabled()) { |
| 61 | + LOG.debug(String.format("Discovered %s with qualifiers %s.", MongoOperations.class.getName(), |
| 62 | + bean.getQualifiers())); |
| 63 | + } |
| 64 | + |
| 65 | + // Store the EntityManager bean using its qualifiers. |
| 66 | + mongoOperations.put(new HashSet<Annotation>(bean.getQualifiers()), (Bean<MongoOperations>) bean); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { |
| 72 | + |
| 73 | + for (Entry<Class<?>, Set<Annotation>> entry : getRepositoryTypes()) { |
| 74 | + |
| 75 | + Class<?> repositoryType = entry.getKey(); |
| 76 | + Set<Annotation> qualifiers = entry.getValue(); |
| 77 | + |
| 78 | + // Create the bean representing the repository. |
| 79 | + Bean<?> repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager); |
| 80 | + |
| 81 | + if (LOG.isInfoEnabled()) { |
| 82 | + LOG.info(String.format("Registering bean for %s with qualifiers %s.", repositoryType.getName(), qualifiers)); |
| 83 | + } |
| 84 | + |
| 85 | + // Register the bean to the container. |
| 86 | + afterBeanDiscovery.addBean(repositoryBean); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Creates a {@link Bean}. |
| 92 | + * |
| 93 | + * @param <T> The type of the repository. |
| 94 | + * @param repositoryType The class representing the repository. |
| 95 | + * @param beanManager The BeanManager instance. |
| 96 | + * @return The bean. |
| 97 | + */ |
| 98 | + private <T> Bean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers, BeanManager beanManager) { |
| 99 | + |
| 100 | + // Determine the MongoOperations bean which matches the qualifiers of the repository. |
| 101 | + Bean<MongoOperations> mongoOperations = this.mongoOperations.get(qualifiers); |
| 102 | + |
| 103 | + if (mongoOperations == null) { |
| 104 | + throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", |
| 105 | + MongoOperations.class.getName(), qualifiers)); |
| 106 | + } |
| 107 | + |
| 108 | + // Construct and return the repository bean. |
| 109 | + return new MongoRepositoryBean<T>(mongoOperations, qualifiers, repositoryType, beanManager); |
| 110 | + } |
| 111 | +} |
0 commit comments