package org.qinan.safetyeval.start.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("local")
public class LocalJobAutoRegisterDisableConfig {

    private static final String JOB_AUTO_REGISTER_CLASS = "com.jjb.saas.framework.job.core.JobAutoRegister";

    @Bean
    public static BeanDefinitionRegistryPostProcessor localJobAutoRegisterDisablePostProcessor() {
        return new BeanDefinitionRegistryPostProcessor() {
            @Override
            public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
                String[] names = registry.getBeanDefinitionNames();
                for (String name : names) {
                    String beanClassName = registry.getBeanDefinition(name).getBeanClassName();
                    if (JOB_AUTO_REGISTER_CLASS.equals(beanClassName)) {
                        registry.removeBeanDefinition(name);
                    }
                }
            }

            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                // No-op.
            }
        };
    }
}