package com.eactive.eai.rms.common.spring;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;

public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    /**
    * Logger for this class
    */
	final Logger logger = Logger.getLogger(CustomBeanFactoryPostProcessor.class);
	    
	public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
			throws BeansException {
		
		List<DataSourceType> dataSourceTypes = new ArrayList<DataSourceType>();
		
		Map maps = factory.getBeansOfType(DataSourceType.class);
		Iterator iter = maps.keySet().iterator();
		while (iter.hasNext()) {
			String key = (String) iter.next();
			
			DataSourceType obj = (DataSourceType) factory.getBean(key);
			if (logger.isDebugEnabled()){
				logger.debug("dataSourceType key="+key + ", value="+ obj );
			}
			dataSourceTypes.add(obj);
		}
		
		DataSourceTypeManager.setDataSourceTypes(dataSourceTypes);
	
		
	}

}
