All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.feign.annotation.BridgeSpringCloudFeignClientsRegistrar Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.feign.annotation;

import org.slf4j.Logger;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.util.MultiValueMap;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
import org.zodiac.commons.util.Classes;
import org.zodiac.commons.util.Reflections;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

public class BridgeSpringCloudFeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {

    private static final Class FEIGNCLIENTS_REGISTRAR_CLASS = Classes.resolveClassNameNullable(
        "org.springframework.cloud.openfeign.FeignClientsRegistrar");
    private static final Method FEIGNCLIENTS_REGISTRAR_SETRESOURCELOADER = Reflections.findMethodNullable(FEIGNCLIENTS_REGISTRAR_CLASS,
        "setResourceLoader", ResourceLoader.class);
    private static final Method FEIGNCLIENTS_REGISTRAR_SETENVIRONMENT = Reflections.findMethodNullable(FEIGNCLIENTS_REGISTRAR_CLASS,
        "setEnvironment", Environment.class);
    private static final Method FEIGNCLIENTS_REGISTRAR_REGISTERBEANDEFINITIOINS = Reflections.findMethodNullable(FEIGNCLIENTS_REGISTRAR_CLASS,
        "registerBeanDefinitions", AnnotationMetadata.class, BeanDefinitionRegistry.class);

    protected final Logger log = SmartSlf4jLoggerFactory.getLogger(getClass());

    private ResourceLoader resourceLoader;
    private Environment environment;

    protected BridgeSpringCloudFeignClientsRegistrar() {
        super();
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        /*Check enabled configuration.*/
        if (!FeignConsumersRegistrar.isEnableConfiguration(environment)) {
            log.warn("No enabled SpringBoot and SpringCloud feign auto configurer!");
            return;
        }

        if (FeignConsumersRegistrar.hasSpringCloudFeignClass()) {
            try {
                Constructor constructor = FEIGNCLIENTS_REGISTRAR_CLASS.getDeclaredConstructor();
                constructor.setAccessible(true);

                Object feignClientsRegistrar = constructor.newInstance();

                FEIGNCLIENTS_REGISTRAR_SETRESOURCELOADER.setAccessible(true);
                Reflections.invokeMethod(FEIGNCLIENTS_REGISTRAR_SETRESOURCELOADER, feignClientsRegistrar, resourceLoader);

                FEIGNCLIENTS_REGISTRAR_SETENVIRONMENT.setAccessible(true);
                Reflections.invokeMethod(FEIGNCLIENTS_REGISTRAR_SETENVIRONMENT, feignClientsRegistrar, environment);

                FEIGNCLIENTS_REGISTRAR_REGISTERBEANDEFINITIOINS.setAccessible(true);
                Reflections.invokeMethod(FEIGNCLIENTS_REGISTRAR_REGISTERBEANDEFINITIOINS, feignClientsRegistrar,
                        new Object[] { new AnnotationMetadata() {

                            @Override
                            public boolean isAnnotated(String annotationName) {
                                return metadata.isAnnotated(annotationName);
                            }

                            @Override
                            public Map getAnnotationAttributes(String annotationName) {
                                /*IMPORTANTS: transform SpringBoot-Feign to SpringCloud-Feign .*/
                                if (annotationName.equals("org.springframework.cloud.openfeign.EnableFeignClients")) {
                                    return metadata
                                            .getAnnotationAttributes(EnableFeignConsumers.class.getCanonicalName());
                                }
                                return metadata.getAnnotationAttributes(annotationName);
                            }

                            @Override
                            public Map getAnnotationAttributes(String annotationName,
                                    boolean classValuesAsString) {
                                return metadata.getAnnotationAttributes(annotationName, classValuesAsString);
                            }

                            @Override
                            public MultiValueMap getAllAnnotationAttributes(String annotationName) {
                                return metadata.getAllAnnotationAttributes(annotationName);
                            }

                            @Override
                            public MultiValueMap getAllAnnotationAttributes(String annotationName,
                                    boolean classValuesAsString) {
                                return metadata.getAllAnnotationAttributes(annotationName, classValuesAsString);
                            }

                            @Override
                            public boolean isConcrete() {
                                return metadata.isConcrete();
                            }

                            @Override
                            public boolean hasEnclosingClass() {
                                return metadata.hasEnclosingClass();
                            }

                            @Override
                            public boolean hasSuperClass() {
                                return metadata.hasSuperClass();
                            }

                            @Override
                            public Set getAnnotationTypes() {
                                return metadata.getAnnotationTypes();
                            }

                            @Override
                            public Set getMetaAnnotationTypes(String annotationName) {
                                return metadata.getMetaAnnotationTypes(annotationName);
                            }

                            @Override
                            public boolean hasAnnotation(String annotationName) {
                                return metadata.hasAnnotation(annotationName);
                            }

                            @Override
                            public boolean hasMetaAnnotation(String metaAnnotationName) {
                                return metadata.hasMetaAnnotation(metaAnnotationName);
                            }

                            @Override
                            public boolean hasAnnotatedMethods(String annotationName) {
                                return metadata.hasAnnotatedMethods(annotationName);
                            }

                            @Override
                            public MergedAnnotations getAnnotations() {
                                return metadata.getAnnotations();
                            }

                            @Override
                            public boolean isInterface() {
                                return metadata.isInterface();
                            }

                            @Override
                            public boolean isIndependent() {
                                return metadata.isIndependent();
                            }

                            @Override
                            public boolean isFinal() {
                                return metadata.isFinal();
                            }

                            @Override
                            public boolean isAnnotation() {
                                return metadata.isAnnotation();
                            }

                            @Override
                            public boolean isAbstract() {
                                return metadata.isAbstract();
                            }

                            @Override
                            public String getSuperClassName() {
                                return metadata.getSuperClassName();
                            }

                            @Override
                            public String[] getMemberClassNames() {
                                return metadata.getMemberClassNames();
                            }

                            @Override
                            public String[] getInterfaceNames() {
                                return metadata.getInterfaceNames();
                            }

                            @Override
                            public String getEnclosingClassName() {
                                return metadata.getEnclosingClassName();
                            }

                            @Override
                            public String getClassName() {
                                return metadata.getClassName();
                            }

                            @Override
                            public Set getAnnotatedMethods(String annotationName) {
                                return metadata.getAnnotatedMethods(annotationName);
                            }
                        }, registry });

            } catch (Exception e) {
                throw new IllegalStateException(String.format("Cannot to bridge invoke to SpringCloud '%s#registerBeanDefinitions'",
                        FEIGNCLIENTS_REGISTRAR_CLASS), e);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy