Example usage for org.springframework.core.env ConfigurableEnvironment getActiveProfiles

List of usage examples for org.springframework.core.env ConfigurableEnvironment getActiveProfiles

Introduction

In this page you can find the example usage for org.springframework.core.env ConfigurableEnvironment getActiveProfiles.

Prototype

String[] getActiveProfiles();

Source Link

Document

Return the set of profiles explicitly made active for this environment.

Usage

From source file:org.springframework.cloud.aws.paramstore.AwsParamStorePropertySourceLocator.java

@Override
public PropertySource<?> locate(Environment environment) {
    if (!(environment instanceof ConfigurableEnvironment)) {
        return null;
    }/* w w w .  j  a  v  a 2 s  .c  o  m*/

    ConfigurableEnvironment env = (ConfigurableEnvironment) environment;

    String appName = properties.getName();

    if (appName == null) {
        appName = env.getProperty("spring.application.name");
    }

    List<String> profiles = Arrays.asList(env.getActiveProfiles());

    String prefix = this.properties.getPrefix();

    String defaultContext = prefix + "/" + this.properties.getDefaultContext();
    this.contexts.add(defaultContext + "/");
    addProfiles(this.contexts, defaultContext, profiles);

    String baseContext = prefix + "/" + appName;
    this.contexts.add(baseContext + "/");
    addProfiles(this.contexts, baseContext, profiles);

    Collections.reverse(this.contexts);

    CompositePropertySource composite = new CompositePropertySource("aws-param-store");

    for (String propertySourceContext : this.contexts) {
        try {
            composite.addPropertySource(create(propertySourceContext));
        } catch (Exception e) {
            if (this.properties.isFailFast()) {
                logger.error(
                        "Fail fast is set and there was an error reading configuration from AWS Parameter Store:\n"
                                + e.getMessage());
                ReflectionUtils.rethrowRuntimeException(e);
            } else {
                logger.warn("Unable to load AWS config from " + propertySourceContext, e);
            }
        }
    }

    return composite;
}

From source file:org.springframework.cloud.aws.secretsmanager.AwsSecretsManagerPropertySourceLocator.java

@Override
public PropertySource<?> locate(Environment environment) {
    if (!(environment instanceof ConfigurableEnvironment)) {
        return null;
    }/*from   www  . j  a v a 2 s.co m*/

    ConfigurableEnvironment env = (ConfigurableEnvironment) environment;

    String appName = properties.getName();

    if (appName == null) {
        appName = env.getProperty("spring.application.name");
    }

    List<String> profiles = Arrays.asList(env.getActiveProfiles());

    String prefix = this.properties.getPrefix();

    String defaultContext = prefix + "/" + this.properties.getDefaultContext();
    this.contexts.add(defaultContext);
    addProfiles(this.contexts, defaultContext, profiles);

    String baseContext = prefix + "/" + appName;
    this.contexts.add(baseContext);
    addProfiles(this.contexts, baseContext, profiles);

    Collections.reverse(this.contexts);

    CompositePropertySource composite = new CompositePropertySource("aws-secrets-manager");

    for (String propertySourceContext : this.contexts) {
        try {
            composite.addPropertySource(create(propertySourceContext));
        } catch (Exception e) {
            if (this.properties.isFailFast()) {
                logger.error(
                        "Fail fast is set and there was an error reading configuration from AWS Secrets Manager:\n"
                                + e.getMessage());
                ReflectionUtils.rethrowRuntimeException(e);
            } else {
                logger.warn("Unable to load AWS secret from " + propertySourceContext, e);
            }
        }
    }

    return composite;
}

From source file:org.springframework.cloud.bootstrap.config.ConfigServiceBootstrapConfiguration.java

@Bean
public ConfigServicePropertySourceLocator configServicePropertySource(ConfigurableEnvironment environment) {
    ConfigServicePropertySourceLocator locator = new ConfigServicePropertySourceLocator();
    String[] profiles = environment.getActiveProfiles();
    if (profiles.length == 0) {
        profiles = environment.getDefaultProfiles();
    }//  ww w  . ja  v  a  2 s  . co  m
    locator.setEnv(StringUtils.arrayToCommaDelimitedString(profiles));
    return locator;
}

From source file:org.springframework.cloud.zookeeper.config.ZookeeperPropertySourceLocator.java

@Override
public PropertySource<?> locate(Environment environment) {
    if (environment instanceof ConfigurableEnvironment) {
        ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
        String appName = env.getProperty("spring.application.name");
        List<String> profiles = Arrays.asList(env.getActiveProfiles());

        String root = this.properties.getRoot();
        this.contexts = new ArrayList<>();

        String defaultContext = root + "/" + this.properties.getDefaultContext();
        this.contexts.add(defaultContext);
        addProfiles(this.contexts, defaultContext, profiles);

        StringBuilder baseContext = new StringBuilder(root);
        if (!appName.startsWith("/")) {
            baseContext.append("/");
        }/* w w  w . j av a 2 s . c  o m*/
        baseContext.append(appName);
        this.contexts.add(baseContext.toString());
        addProfiles(this.contexts, baseContext.toString(), profiles);

        CompositePropertySource composite = new CompositePropertySource("zookeeper");

        Collections.reverse(this.contexts);

        for (String propertySourceContext : this.contexts) {
            try {
                PropertySource propertySource = create(propertySourceContext);
                composite.addPropertySource(propertySource);
                // TODO: howto call close when /refresh
            } catch (Exception e) {
                if (this.properties.isFailFast()) {
                    ReflectionUtils.rethrowRuntimeException(e);
                } else {
                    log.warn("Unable to load zookeeper config from " + propertySourceContext, e);
                }
            }
        }

        return composite;
    }
    return null;
}

From source file:org.springframework.core.env.AbstractEnvironment.java

@Override
public void merge(ConfigurableEnvironment parent) {
    for (PropertySource<?> ps : parent.getPropertySources()) {
        if (!this.propertySources.contains(ps.getName())) {
            this.propertySources.addLast(ps);
        }// w  ww.j  a  v  a2 s  .  c  om
    }
    String[] parentActiveProfiles = parent.getActiveProfiles();
    if (!ObjectUtils.isEmpty(parentActiveProfiles)) {
        synchronized (this.activeProfiles) {
            for (String profile : parentActiveProfiles) {
                this.activeProfiles.add(profile);
            }
        }
    }
    String[] parentDefaultProfiles = parent.getDefaultProfiles();
    if (!ObjectUtils.isEmpty(parentDefaultProfiles)) {
        synchronized (this.defaultProfiles) {
            this.defaultProfiles.remove(RESERVED_DEFAULT_PROFILE_NAME);
            for (String profile : parentDefaultProfiles) {
                this.defaultProfiles.add(profile);
            }
        }
    }
}