/**
 * Copyright 2008-2009 DRIVER PROJECT (ICM UW)
 * Original author: Marek Horst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.data.index.utils;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.UrlResource;
import org.springframework.util.StringUtils;

/**
 * Spring Utils
 * @author Marek Horst
 * @version 0.7.6
 *
 */
public class SpringUtils {

	protected static final Logger log = Logger.getLogger(SpringUtils.class);
	
	public static final String DEFAULT_RESOURCE="classpath:/eu/dnetlib/data/index/applicationContext-index.xml";
//	public static final String DEFAULT_TEST_RESOURCE="classpath:/eu/dnetlib/data/index/test-spring-bundle.xml";
	public static final String DEFAULT_JUNIT_RESOURCE="classpath:/eu/dnetlib/data/index/junit-spring-bundle.xml";
	
	public static ApplicationContext getDefaultSpringContext(){
		return getSpringContext(DEFAULT_RESOURCE, null);
	}
	
	public static ApplicationContext getSpringContext(String resource){
		return getSpringContext(resource, null);
	}
	
	public static ApplicationContext getSpringContext(String resource, ApplicationContext pctx){
		GenericApplicationContext ctx=new GenericApplicationContext(pctx);
		XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
		String base=StringUtils.applyRelativePath(resource, "");
		PropertyPlaceholderConfigurer ppc=new PropertyPlaceholderConfigurer();
		Properties props=new Properties();
		props.setProperty("base.path", base);
		ppc.setProperties(props);
		ppc.setPlaceholderPrefix("#{");
		log.info("base:"+base);
		ctx.addBeanFactoryPostProcessor(ppc);
		if(resource.startsWith("classpath:/")){
			log.info("Loading classpath resource:"+resource);
			xmlReader.loadBeanDefinitions(new ClassPathResource(resource.substring("classpath:/".length())));
		} else if (resource.startsWith("http://") || resource.startsWith("https://")){
			log.info("Loading url resource:"+resource);
			try {
				xmlReader.loadBeanDefinitions(new UrlResource(resource));
			} catch (Exception e) {
				log.error("Cannot load spring context from resource "+resource,e);
				return null;
			}
		} else {
			log.info("Loading file system resource:"+resource);
			xmlReader.loadBeanDefinitions(new FileSystemResource(resource));
		}
		ctx.refresh();
		return ctx;
	}
}
