package com.atomikos.examples.micrometer;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.jta.atomikos.AtomikosConnectionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.atomikos.jdbc.internal.AbstractDataSourceBean;
import com.atomikos.micrometer.jdbc.MicrometerDataSourceBean;
import com.atomikos.micrometer.jms.MicrometerConnectionFactoryBean;

@Configuration
public class MicroMeterConfiguration {
    

    public MicroMeterConfiguration() {
    }
    
    
    
    @Bean
    public BeanPostProcessor openTracingDataSourcePostProcessor() {
        return new BeanPostProcessor() {
        	@Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof AbstractDataSourceBean) {
                    return new MicrometerDataSourceBean((AbstractDataSourceBean) bean);
                }
                if (bean instanceof AtomikosConnectionFactoryBean) {
                	return new MicrometerConnectionFactoryBean((AtomikosConnectionFactoryBean)bean);
                }
                return bean;
            }
        };
    }
    
}
