|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 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 | + * https://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 | + |
| 17 | +package org.springframework.boot.autoconfigure.interfaceclients; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.text.Normalizer; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.apache.commons.text.CaseUtils; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.ListableBeanFactory; |
| 28 | +import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; |
| 29 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 30 | +import org.springframework.beans.factory.config.BeanDefinitionHolder; |
| 31 | +import org.springframework.beans.factory.support.AbstractBeanDefinition; |
| 32 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 33 | +import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; |
| 34 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 35 | +import org.springframework.beans.factory.support.BeanNameGenerator; |
| 36 | +import org.springframework.boot.autoconfigure.AutoConfigurationPackages; |
| 37 | +import org.springframework.context.EnvironmentAware; |
| 38 | +import org.springframework.context.ResourceLoaderAware; |
| 39 | +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; |
| 40 | +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
| 41 | +import org.springframework.core.annotation.MergedAnnotation; |
| 42 | +import org.springframework.core.env.Environment; |
| 43 | +import org.springframework.core.io.ResourceLoader; |
| 44 | +import org.springframework.core.type.AnnotationMetadata; |
| 45 | +import org.springframework.core.type.filter.AnnotationTypeFilter; |
| 46 | +import org.springframework.util.Assert; |
| 47 | +import org.springframework.util.ObjectUtils; |
| 48 | + |
| 49 | +/** |
| 50 | + * @author Josh Long |
| 51 | + * @author Olga Maciaszek-Sharma |
| 52 | + */ |
| 53 | +// TODO: Handle AOT |
| 54 | +public abstract class AbstractInterfaceClientsImportRegistrar |
| 55 | + implements ImportBeanDefinitionRegistrar, EnvironmentAware, ResourceLoaderAware { |
| 56 | + |
| 57 | + private static final String INTERFACE_CLIENT_SUFFIX = "InterfaceClient"; |
| 58 | + |
| 59 | + private static final String BEAN_NAME_ATTRIBUTE_NAME = "beanName"; |
| 60 | + |
| 61 | + private static final String CLIENT_ID_ATTRIBUTE_NAME = "clientId"; |
| 62 | + |
| 63 | + private static final String BEAN_CLASS_ATTRIBUTE_NAME = "type"; |
| 64 | + |
| 65 | + private Environment environment; |
| 66 | + |
| 67 | + private ResourceLoader resourceLoader; |
| 68 | + |
| 69 | + @Override |
| 70 | + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, |
| 71 | + BeanNameGenerator importBeanNameGenerator) { |
| 72 | + Assert.isInstanceOf(ListableBeanFactory.class, registry, |
| 73 | + "Registry must be an instance of " + ListableBeanFactory.class.getSimpleName()); |
| 74 | + ListableBeanFactory beanFactory = (ListableBeanFactory) registry; |
| 75 | + Set<BeanDefinition> candidateComponents = discoverCandidateComponents(beanFactory); |
| 76 | + for (BeanDefinition candidateComponent : candidateComponents) { |
| 77 | + if (candidateComponent instanceof AnnotatedBeanDefinition beanDefinition) { |
| 78 | + registerInterfaceClient(registry, beanDefinition); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void registerInterfaceClient(BeanDefinitionRegistry registry, AnnotatedBeanDefinition beanDefinition) { |
| 84 | + AnnotationMetadata annotatedBeanMetadata = beanDefinition.getMetadata(); |
| 85 | + Assert.isTrue(annotatedBeanMetadata.isInterface(), |
| 86 | + getAnnotation().getSimpleName() + "can only be placed on an interface."); |
| 87 | + MergedAnnotation<? extends Annotation> annotation = annotatedBeanMetadata.getAnnotations().get(getAnnotation()); |
| 88 | + String beanClassName = annotatedBeanMetadata.getClassName(); |
| 89 | + // The value of the annotation is the qualifier to look for related beans |
| 90 | + // while the default beanName corresponds to the simple class name suffixed with |
| 91 | + // `InterfaceClient` |
| 92 | + String clientId = annotation.getString(MergedAnnotation.VALUE); |
| 93 | + String beanName = !ObjectUtils.isEmpty(annotation.getString(BEAN_NAME_ATTRIBUTE_NAME)) |
| 94 | + ? annotation.getString(BEAN_NAME_ATTRIBUTE_NAME) : buildBeanName(clientId); |
| 95 | + BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(getFactoryBeanClass()); |
| 96 | + definitionBuilder.addPropertyValue(BEAN_NAME_ATTRIBUTE_NAME, beanName); |
| 97 | + definitionBuilder.addPropertyValue(CLIENT_ID_ATTRIBUTE_NAME, clientId); |
| 98 | + definitionBuilder.addPropertyValue(BEAN_CLASS_ATTRIBUTE_NAME, beanClassName); |
| 99 | + definitionBuilder.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); |
| 100 | + AbstractBeanDefinition definition = definitionBuilder.getBeanDefinition(); |
| 101 | + BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, beanName, new String[] { clientId }); |
| 102 | + BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); |
| 103 | + } |
| 104 | + |
| 105 | + protected Set<BeanDefinition> discoverCandidateComponents(ListableBeanFactory beanFactory) { |
| 106 | + Set<BeanDefinition> candidateComponents = new HashSet<>(); |
| 107 | + ClassPathScanningCandidateComponentProvider scanner = getScanner(); |
| 108 | + scanner.setResourceLoader(this.resourceLoader); |
| 109 | + scanner.addIncludeFilter(new AnnotationTypeFilter(getAnnotation())); |
| 110 | + List<String> basePackages = AutoConfigurationPackages.get(beanFactory); |
| 111 | + for (String basePackage : basePackages) { |
| 112 | + candidateComponents.addAll(scanner.findCandidateComponents(basePackage)); |
| 113 | + } |
| 114 | + return candidateComponents; |
| 115 | + } |
| 116 | + |
| 117 | + private ClassPathScanningCandidateComponentProvider getScanner() { |
| 118 | + return new ClassPathScanningCandidateComponentProvider(false, this.environment) { |
| 119 | + @Override |
| 120 | + protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { |
| 121 | + boolean isCandidate = false; |
| 122 | + if (beanDefinition.getMetadata().isIndependent()) { |
| 123 | + if (!beanDefinition.getMetadata().isAnnotation()) { |
| 124 | + isCandidate = true; |
| 125 | + } |
| 126 | + } |
| 127 | + return isCandidate; |
| 128 | + } |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void setEnvironment(Environment environment) { |
| 134 | + this.environment = environment; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void setResourceLoader(ResourceLoader resourceLoader) { |
| 139 | + this.resourceLoader = resourceLoader; |
| 140 | + } |
| 141 | + |
| 142 | + protected abstract Class<? extends Annotation> getAnnotation(); |
| 143 | + |
| 144 | + protected abstract Class<?> getFactoryBeanClass(); |
| 145 | + |
| 146 | + private String buildBeanName(String clientId) { |
| 147 | + String normalised = Normalizer.normalize(clientId, Normalizer.Form.NFD); |
| 148 | + String camelCased = CaseUtils.toCamelCase(normalised, false, '-', '_'); |
| 149 | + return camelCased + INTERFACE_CLIENT_SUFFIX; |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments