/**
 * Copyright 2008-2009 DRIVER PROJECT (Bielefeld University)
 * Original author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * 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.enabling.manager.utils;

import java.util.Properties;
import java.util.Random;

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 <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 * @version
 * 
 */
public class SpringUtils {

	protected static final Logger log = Logger.getLogger(SpringUtils.class);

	public static final String DEFAULT_RESOURCE = "classpath:/eu/dnetlib/enabling/manager/applicationContext-dmanager.xml";

	public static final String DEFAULT_JUNIT_RESOURCE = "classpath:/eu/dnetlib/enabling/manager/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;
	}
	
	/**
	 * Generates random identifier.
	 * 
	 * @return randomIdentifier  
	 */
	public static String generateRandomIdentifier() {
		return System.currentTimeMillis() + "-"
				+ (new Random().nextInt(1000));
	}

}