package org.encuestame.business.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.CollectionUtils;

/**
 * Extend {@link PropertyPlaceholderConfigurer}
 * @author Picado, Juan juanATencuestame.org
 * @since Mar 17, 2011
 */
public class EncuestamePlaceHolderConfigurer extends PropertyPlaceholderConfigurer {

    /**
     * JVM encuestame path param.
     */
    private static String overwriteJvmParam = "encuestame.custom.config";

    /**
     * List of Properties.
     */
    private static Map<String, String> propertiesMap;


    private static Log log = LogFactory.getLog(EncuestamePlaceHolderConfigurer.class);

    /**
     * Process collection of properties.
     */
    @SuppressWarnings("deprecation")
    @Override
    protected void processProperties(
            ConfigurableListableBeanFactory beanFactory, Properties props)
            throws BeansException {

        //get form system property enviroment out file path.
        final String enviromentPropertyFile = System.getProperty(overwriteJvmParam);
        File customEncuestameFile;
        if(enviromentPropertyFile != null && enviromentPropertyFile.length() > 0) {
            customEncuestameFile = new File(enviromentPropertyFile);
            // make sure the file exists, then try and load it
            if(customEncuestameFile != null && customEncuestameFile.exists()) {
                InputStream is = null;
                try {
                    //trying to load custom file.
                    is = new FileInputStream(customEncuestameFile);
                } catch (FileNotFoundException e) {
                    log.error("Encuestame JVM Config file: File not Found from "+
                            customEncuestameFile.getAbsolutePath());
                }
                try {
                    //cool, if we can load all properties on the parent list.
                    props.load(is);
                } catch (IOException e) {
                    log.error("Encuestame JVM Config file: I/O Error, Permission Error? Check your path. "+
                            customEncuestameFile.getAbsolutePath());
                }
                log.info("Encuestame JVM Config file: Successfully loaded custom properties from "+
                        customEncuestameFile.getAbsolutePath());
            } else {
                log.info("Encuestame JVM Config file: Failed to load custom properties from "+
                        customEncuestameFile.getAbsolutePath());
            }
        }
        super.processProperties(beanFactory, props);
        propertiesMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            log.debug("Property--->"+keyStr+ " value="+props.getProperty(keyStr));
            propertiesMap.put(
                    keyStr,
                    parseStringValue(props.getProperty(keyStr), props,
                            new HashSet()));
        }
    }

    /**
     * Get property.
     * @param name property key
     * @return property value.
     */
    public static String getProperty(final String name) {
        return propertiesMap.get(name);
    }
}

@@ -582,157 +582,8 @@
 rer;
-%0Aimport org.springframework.core.io.FileSystemResource;%0Aimport org.springframework.core.io.Resource;%0Aimport org.springframework.util.CollectionUtils;
 %0A%0A/*

Clean checkstyle