All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.bootstrap.AppPropertySourceBootstrapConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.bootstrap;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.logging.LogFile;
import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.zodiac.commons.constants.Constants;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.commons.util.Colls;
import org.zodiac.commons.util.spring.Springs;
import org.zodiac.core.bootstrap.config.AppPropertySourceLocator;
import org.zodiac.core.context.environment.AppEnvironmentChangeEvent;
import org.zodiac.core.logging.AppLoggingRebinder;

import static org.springframework.core.env.StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME;
import static org.zodiac.autoconfigure.bootstrap.AppPropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME;

@SpringBootConfiguration
@EnableConfigurationProperties(value = {AppPropertySourceBootstrapProperties.class})
public class AppPropertySourceBootstrapConfiguration
    implements ApplicationContextInitializer, Ordered {

    /**
     * Bootstrap property source name.
     */
    public static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = Constants.Zodiac.BOOTSTRAP_PROPERTY_SOURCE_NAME;

    private static Logger logger = LoggerFactory.getLogger(AppPropertySourceBootstrapConfiguration.class);

    private int order = Ordered.HIGHEST_PRECEDENCE + 10;

    @Autowired(required = false)
    private List propertySourceLocators = Colls.list();

    public AppPropertySourceBootstrapConfiguration() {
        super();
    }

    @Override
    public int getOrder() {
        return this.order;
    }

    public void setPropertySourceLocators(Collection propertySourceLocators) {
        this.propertySourceLocators = Colls.list(propertySourceLocators);
    }

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        if (Colls.emptyColl(this.propertySourceLocators)) {
            Map locatorsMap = Springs.getBeansMap(applicationContext, AppPropertySourceLocator.class);
        }
        List> composite = Colls.list();
        AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
        boolean empty = true;
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        for (AppPropertySourceLocator locator : this.propertySourceLocators) {
            Collection> source = locator.locatePropertySourceCollection(environment);
            if (source == null || source.size() == 0) {
                continue;
            }
            List> sourceList = Colls.list();
            for (PropertySource p : source) {
                sourceList.add(new BootstrapPropertySource<>(p));
            }
            logger.info("Located property source: " + sourceList);
            composite.addAll(sourceList);
            empty = false;
        }
        if (!empty) {
            MutablePropertySources propertySources = environment.getPropertySources();
            String logConfig =
                environment.resolvePlaceholders("${" + SystemPropertiesConstants.Spring.LOGGING_CONFIG + ":}");
            LogFile logFile = LogFile.get(environment);
            for (PropertySource p : environment.getPropertySources()) {
                if (p.getName().startsWith(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
                    propertySources.remove(p.getName());
                }
            }
            insertPropertySources(propertySources, composite);
            reinitializeLoggingSystem(environment, logConfig, logFile);
            setLogLevels(applicationContext, environment);
            handleIncludedProfiles(environment);
        }
    }

    private void reinitializeLoggingSystem(ConfigurableEnvironment environment, String oldLogConfig,
        LogFile oldLogFile) {
        Map props = Binder.get(environment).bind("logging", Bindable.mapOf(String.class, Object.class))
            .orElseGet(Collections::emptyMap);
        if (!props.isEmpty()) {
            String logConfig =
                environment.resolvePlaceholders("${" + SystemPropertiesConstants.Spring.LOGGING_CONFIG + ":}");
            LogFile logFile = LogFile.get(environment);
            LoggingSystem system = LoggingSystem.get(LoggingSystem.class.getClassLoader());
            try {
                ResourceUtils.getURL(logConfig).openStream().close();
                /*
                 * Three step initialization that accounts for the clean up of the logging
                 * context before initialization. Spring Boot doesn't initialize a logging
                 * system that hasn't had this sequence applied (since 1.4.1).
                 * */
                system.cleanUp();
                system.beforeInitialize();
                system.initialize(new LoggingInitializationContext(environment), logConfig, logFile);
            } catch (Exception ex) {
                AppPropertySourceBootstrapConfiguration.logger.warn("Error opening logging config file " + logConfig,
                    ex);
            }
        }
    }

    private void setLogLevels(ConfigurableApplicationContext applicationContext, ConfigurableEnvironment environment) {
        AppLoggingRebinder rebinder = new AppLoggingRebinder();
        rebinder.setEnvironment(environment);
        /*
         * We can't fire the event in the ApplicationContext here (too early), but we can
         * create our own listener and poke it (it doesn't need the key changes).
         * */
        rebinder.onApplicationEvent(new AppEnvironmentChangeEvent(applicationContext, Collections.emptySet()));
    }

    private void insertPropertySources(MutablePropertySources propertySources, List> composite) {
        MutablePropertySources incoming = new MutablePropertySources();
        List> reversedComposite = Colls.list(composite);
        /*
         * Reverse the list so that when we call addFirst below we are maintaining the
         * same order of PropertySources.
         * */
        /*
         * Wherever we call addLast we can use the order in the List since the first item
         * will end up before the rest.
         * */
        Collections.reverse(reversedComposite);
        for (PropertySource p : reversedComposite) {
            incoming.addFirst(p);
        }
        AppPropertySourceBootstrapProperties remoteProperties = new AppPropertySourceBootstrapProperties();
        Binder.get(environment(incoming)).bind(SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_CONFIG_PREFIX,
            Bindable.ofInstance(remoteProperties));
        if (!remoteProperties.isAllowOverride()
            || (!remoteProperties.isOverrideNone() && remoteProperties.isOverrideSystemProperties())) {
            for (PropertySource p : reversedComposite) {
                propertySources.addFirst(p);
            }
            return;
        }
        if (remoteProperties.isOverrideNone()) {
            for (PropertySource p : composite) {
                propertySources.addLast(p);
            }
            return;
        }
        if (propertySources.contains(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
            if (!remoteProperties.isOverrideSystemProperties()) {
                for (PropertySource p : reversedComposite) {
                    propertySources.addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, p);
                }
            } else {
                for (PropertySource p : composite) {
                    propertySources.addBefore(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, p);
                }
            }
        } else {
            for (PropertySource p : composite) {
                propertySources.addLast(p);
            }
        }
    }

    private Environment environment(MutablePropertySources incoming) {
        StandardEnvironment environment = new StandardEnvironment();
        for (PropertySource source : environment.getPropertySources()) {
            environment.getPropertySources().remove(source.getName());
        }
        for (PropertySource source : incoming) {
            environment.getPropertySources().addLast(source);
        }
        return environment;
    }

    private void handleIncludedProfiles(ConfigurableEnvironment environment) {
        Set includeProfiles = new TreeSet<>();
        for (PropertySource propertySource : environment.getPropertySources()) {
            addIncludedProfilesTo(includeProfiles, propertySource);
        }
        List activeProfiles = Colls.list();
        Collections.addAll(activeProfiles, environment.getActiveProfiles());

        /*If it's already accepted we assume the order was set intentionally.*/
        includeProfiles.removeAll(activeProfiles);
        if (includeProfiles.isEmpty()) {
            return;
        }
        /*Prepend each added profile (last wins in a property key clash).*/
        for (String profile : includeProfiles) {
            activeProfiles.add(0, profile);
        }
        environment.setActiveProfiles(activeProfiles.toArray(new String[activeProfiles.size()]));
    }

    private Set addIncludedProfilesTo(Set profiles, PropertySource propertySource) {
        if (propertySource instanceof CompositePropertySource) {
            for (PropertySource nestedPropertySource : ((CompositePropertySource)propertySource)
                .getPropertySources()) {
                addIncludedProfilesTo(profiles, nestedPropertySource);
            }
        } else {
            Collections.addAll(profiles, getProfilesForValue(
                propertySource.getProperty(ConfigFileApplicationListener.INCLUDE_PROFILES_PROPERTY)));
        }
        return profiles;
    }

    private String[] getProfilesForValue(Object property) {
        final String value = (property == null ? null : property.toString());
        return property == null ? new String[0] : StringUtils.tokenizeToStringArray(value, ",");
    }

}

class BootstrapPropertySource extends EnumerablePropertySource {

    private PropertySource p;

    BootstrapPropertySource(PropertySource p) {
        super(BOOTSTRAP_PROPERTY_SOURCE_NAME + "-" + p.getName(), p.getSource());
        this.p = p;
    }

    @Override
    public Object getProperty(String name) {
        return this.p.getProperty(name);
    }

    @Override
    public String[] getPropertyNames() {
        Set names = Colls.linkedSet();
        if (!(this.p instanceof EnumerablePropertySource)) {
            throw new IllegalStateException(
                "Failed to enumerate property names due to non-enumerable property source: " + this.p);
        }
        names.addAll(Arrays.asList(((EnumerablePropertySource)this.p).getPropertyNames()));

        return StringUtils.toStringArray(names);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy