package com.dev.swms.server.context;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ContextLoader {
    private static Log logger = LogFactory.getLog(ContextLoader.class);
    
    public static final String DEFAULT_ADAPTER_CONTEXTCONFIGLOCATION = "adapterContextConfigLocation";
    public static final String DEFAULT_ADAPTER_LOCATORFACTORYSELECTOR = "adapterLocatorFactorySelector";
    public static final String DEFAULT_ADAPTER_PARENTCONTEXTKEY = "adapterContextKey";
    
    private String[] adapterContextConfigLocation = new String[0];
    private String adapterLocatorFactorySelector = "classpath:swms-beanAdapterContext.xml";
    private String adapterParentContextKey = "com.dev.adapter";
    
    protected ApplicationContext applicationContext;
    private BeanFactoryReference parentContextAdapter;

    public void createApplicationContext() {
        applicationContext = new ClassPathXmlApplicationContext(adapterContextConfigLocation, loadParentContext());
    }

    protected ApplicationContext loadParentContext()
            throws BeansException {
        ApplicationContext parentContext = null;

        if (adapterLocatorFactorySelector != null) {
            BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(adapterLocatorFactorySelector);

            if (logger.isDebugEnabled()) {
                logger.debug("Getting parent context definition: using parent context key of '" +
                		adapterParentContextKey + "' with BeanFactoryLocator");
            }

            this.parentContextAdapter = locator.useBeanFactory(adapterParentContextKey);
            parentContext = (ApplicationContext) this.parentContextAdapter.getFactory();
        }

        return parentContext;
    }

    public void closeApplicationContext() {
        if (logger.isInfoEnabled()) {
            logger.info("Closing Spring root Kangaroo ApplicationContext");
        }

        try {
            if (this.applicationContext instanceof ConfigurableApplicationContext) {
                ((ConfigurableApplicationContext) this.applicationContext).close();
            }
        } finally {
            if (this.parentContextAdapter != null) {
                this.parentContextAdapter.release();
            }
        }
    }


	public String getAdapterParentContextKey() {
		return adapterParentContextKey;
	}

	public void setAdapterParentContextKey(String adapterParentContextKey) {
		this.adapterParentContextKey = adapterParentContextKey;
	}

	public String getAdapterLocatorFactorySelector() {
		return adapterLocatorFactorySelector;
	}

	public void setAdapterLocatorFactorySelector(String adapterLocatorFactorySelector) {
		if(adapterLocatorFactorySelector == null){
			return;
		}
		this.adapterLocatorFactorySelector = adapterLocatorFactorySelector;
	}

	public String[] getAdapterContextConfigLocation() {
		return adapterContextConfigLocation;
	}

	public void setAdapterContextConfigLocation(
			String[] adapterContextConfigLocation) {
		if(adapterContextConfigLocation == null || adapterContextConfigLocation.length == 0){
			return;
		}
		this.adapterContextConfigLocation = adapterContextConfigLocation;
	}

	
}
