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

org.zalando.spring.boot.config.Registry Maven / Gradle / Ivy

There is a newer version: 0.0.16
Show newest version
package org.zalando.spring.boot.config;

import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.LOWER_HYPHEN;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;

import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanReference;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.ManagedList;

public class Registry {

    private static final Logger LOG = LoggerFactory.getLogger(Registry.class);

    private final BeanDefinitionRegistry registry;

    public Registry(BeanDefinitionRegistry beanDefinitionRegistry) {
        this.registry = beanDefinitionRegistry;
    }


    public boolean isRegistered(final Class type) {
        return isRegistered(generateBeanName(type));
    }

    public boolean isRegistered(final String id, final Class type) {
        return isRegistered(generateBeanName(id, type));
    }

    public boolean isRegistered(final String name) {
        return registry.isBeanNameInUse(name);
    }

    public  String registerIfAbsent(final Class type, final Supplier factory) {
        final String name = generateBeanName(type);

        if (isRegistered(name)) {
            LOG.debug("Bean [{}] is already registered, skipping it.", name);
            return name;
        }

        registry.registerBeanDefinition(name, factory.get().getBeanDefinition());

        return name;
    }

    public  String registerIfAbsent(final String id, final Class type) {
        return registerIfAbsent(id, type, () -> genericBeanDefinition(type));
    }

    public  String registerIfAbsent(final String id, final Class type,
            final Supplier factory) {

        final String name = generateBeanName(id, type);

        if (isRegistered(name)) {
            LOG.debug("Bean [{}] is already registered, skipping it.", name);
            return name;
        }

        final AbstractBeanDefinition definition = factory.get().getBeanDefinition();

        definition.addQualifier(new AutowireCandidateQualifier(Qualifier.class, id));
        // seems it is not possible to have multiple AutowireCandidateQualifier for same 'type'
        // with different 'value'
//        definition.addQualifier(new AutowireCandidateQualifier(Qualifier.class, name));

        registry.registerBeanDefinition(name, definition);

        return name;
    }

    public static  String generateBeanName(final Class type) {
        return UPPER_CAMEL.to(LOWER_CAMEL, type.getSimpleName());
    }

    public static  String generateBeanName(final String id, final Class type) {
        return LOWER_HYPHEN.to(LOWER_CAMEL, id) + type.getSimpleName();
    }

    public static BeanReference ref(final String beanName) {
        return new RuntimeBeanReference(beanName);
    }

    @SafeVarargs
    public static  List list(final T... elements) {
        final ManagedList list = new ManagedList<>();
        Collections.addAll(list, elements);
        return list;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy