package com.capgemini.archaius.spring;

import java.io.IOException;
import java.util.Properties;

import com.netflix.config.ConcurrentCompositeConfiguration;
import com.netflix.config.DynamicPropertyFactory;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;

/**
 *
 * @author Andrew Harmel-Law
 */
class ArchaiusSpringPropertyPlaceholderSupport {

    private static final Logger LOGGER = LoggerFactory.getLogger(ArchaiusSpringPropertyPlaceholderSupport.class);
    
    protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) {
        return DynamicPropertyFactory.getInstance().getStringProperty(placeholder, null).get();
    }
    
    protected void setLocation(Resource location) throws Exception {
        
        if (DynamicPropertyFactory.getBackingConfigurationSource() != null) {
            LOGGER.error("There was already a config source (or sources) configured.");
            throw new RuntimeException("Archaius is already configured with a property source/sources.");
        }
        
        ConcurrentCompositeConfiguration config = new ConcurrentCompositeConfiguration();
        config.addConfiguration(new PropertiesConfiguration(location.getURL()));

        DynamicPropertyFactory.initWithConfigurationSource(config);
    }
    
    protected void setLocations(Resource[] locations,  boolean ignoreResourceNotFound) throws Exception {
        
        if (DynamicPropertyFactory.getBackingConfigurationSource() != null) {
            LOGGER.error("There was already a config source (or sources) configured.");
            throw new Exception("Archaius is already configured with a property source/sources.");
        }
        
        ConcurrentCompositeConfiguration config = new ConcurrentCompositeConfiguration();
        for (int i = locations.length -1 ; i >= 0 ; i--) {
            try {
                config.addConfiguration(new PropertiesConfiguration(locations[i].getURL()));
            } catch (IOException ex) {
                LOGGER.error("IOException thrown when adding a configuration location.", ex);
                if (ignoreResourceNotFound != true) throw ex;
            } 
        }

        DynamicPropertyFactory.initWithConfigurationSource(config);
    }
}

@@ -193,32 +193,96 @@
 ropertyFactory;%0A
+import org.apache.commons.configuration.ConfigurationException;%0A
 import org.apach
@@ -2169,24 +2169,82 @@
 ption ex) %7B%0A
+                if (ignoreResourceNotFound != true) %7B%0A    
             
@@ -2345,68 +2345,314 @@
     
-if (ignoreResourceNotFound != true) throw ex;%0A            %7D 
+    throw ex;%0A                %7D%0A            %7D catch (ConfigurationException ce) %7B%0A                if (ignoreResourceNotFound != true) %7B%0A                    LOGGER.error(%22ConfigurationException thrown when adding a configuration location.%22, ce);%0A                    throw ce;%0A                %7D%0A            %7D
 %0A   

Fix issue with IgnoreResourceNotFound when running under Spring Boot.