<commit_before>/*
 * Copyright 2013-2014 the original author or authors.
 *
 * 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 org.springframework.platform.bootstrap.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.platform.config.client.ConfigServicePropertySourceLocator;
import org.springframework.platform.config.client.PropertySourceLocator;

/**
 * @author <NAME>
 *
 */
@Configuration
@EnableConfigurationProperties
public class ConfigServiceBootstrapConfiguration implements
		ApplicationContextInitializer<ConfigurableApplicationContext> {

	private static Log logger = LogFactory.getLog(ConfigServiceBootstrapConfiguration.class);

	@Autowired(required = false)
	private List<PropertySourceLocator> propertySourceLocators = new ArrayList<PropertySourceLocator>();
	
	public void setPropertySourceLocators(
			Collection<PropertySourceLocator> propertySourceLocators) {
		this.propertySourceLocators = new ArrayList<PropertySourceLocator>(propertySourceLocators);
	}

	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		CompositePropertySource composite = new CompositePropertySource("bootstrap");
		AnnotationAwareOrderComparator.sort(propertySourceLocators);
		boolean empty = true;
		for (PropertySourceLocator locator : propertySourceLocators) {
			PropertySource<?> source = null;
			try {
				source = locator.locate();
			} catch (Exception e) {
				logger.error("Could not locate PropertySource: " + e.getMessage());
			}
			if (source == null) {
				continue;
			}
			logger.info("Located property source: " + source);
			composite.addPropertySource(source);
			empty = false;
		}
		if (!empty) {
			MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
			if (propertySources.contains("bootstrap")) {
				propertySources.replace("bootstrap", composite);
			} else {
				propertySources.addFirst(composite);
			}
		}
	}

	@Bean
	public ConfigServicePropertySourceLocator configServicePropertySource() {
		return new ConfigServicePropertySourceLocator();<commit_msg>Ensure profiles are used when fetching remote config<commit_after>/*
 * Copyright 2013-2014 the original author or authors.
 *
 * 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 org.springframework.platform.bootstrap.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.platform.config.client.ConfigServicePropertySourceLocator;
import org.springframework.platform.config.client.PropertySourceLocator;
import org.springframework.util.StringUtils;

/**
 * @author <NAME>
 *
 */
@Configuration
@EnableConfigurationProperties
public class ConfigServiceBootstrapConfiguration implements
		ApplicationContextInitializer<ConfigurableApplicationContext> {

	private static Log logger = LogFactory
			.getLog(ConfigServiceBootstrapConfiguration.class);

	@Autowired(required = false)
	private List<PropertySourceLocator> propertySourceLocators = new ArrayList<PropertySourceLocator>();

	public void setPropertySourceLocators(
			Collection<PropertySourceLocator> propertySourceLocators) {
		this.propertySourceLocators = new ArrayList<PropertySourceLocator>(
				propertySourceLocators);
	}

	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		CompositePropertySource composite = new CompositePropertySource("bootstrap");
		AnnotationAwareOrderComparator.sort(propertySourceLocators);
		boolean empty = true;
		for (PropertySourceLocator locator : propertySourceLocators) {
			PropertySource<?> source = null;
			try {
				source = locator.locate();
			}
			catch (Exception e) {
				logger.error("Could not locate PropertySource: " + e.getMessage());
			}
			if (source == null) {
				continue;
			}
			logger.info("Located property source: " + source);
			composite.addPropertySource(source);
			empty = false;
		}
		if (!empty) {
			MutablePropertySources propertySources = applicationContext.getEnvironment()
					.getPropertySources();
			if (propertySources.contains("bootstrap")) {
				propertySources.replace("bootstrap", composite);
			}
			else {
				propertySources.addFirst(composite);
			}
		}
	}

	@Bean
	public ConfigServicePropertySourceLocator configServicePropertySource(
			ConfigurableEnvironment environment) {
		ConfigServicePropertySourceLocator locator = new ConfigServicePropertySourceLocator();
		locator.setEnv(StringUtils.arrayToCommaDelimitedString(environment
				.getActiveProfiles()));
		return locator;
{'fraction_non_alphanumeric': 0.08454072302714868, 'fraction_numerical': 0.003376002250668167, 'mean_word_length': 5.856316297010608, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4248028', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1750, 'n_words': 442}