Example usage for org.springframework.boot.env YamlPropertySourceLoader YamlPropertySourceLoader

List of usage examples for org.springframework.boot.env YamlPropertySourceLoader YamlPropertySourceLoader

Introduction

In this page you can find the example usage for org.springframework.boot.env YamlPropertySourceLoader YamlPropertySourceLoader.

Prototype

YamlPropertySourceLoader

Source Link

Usage

From source file:com.netflix.spinnaker.fiat.YamlFileApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    try {/*from w  ww  .j a  va2 s. c o  m*/
        Resource resource = applicationContext.getResource("classpath:application.yml");
        YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
        PropertySource<?> yamlTestProperties = sourceLoader.load("yamlTestProperties", resource, null);
        applicationContext.getEnvironment().getPropertySources().addLast(yamlTestProperties);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.createnet.raptor.auth.service.YamlFileApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {

    File configFile = new File(configPath);
    if (!configFile.exists()) {
        logger.warn("Configurations file does not exists: {}", configPath);
        return;/*from  w w  w .j a  v  a  2s. co m*/
    }

    try {

        String context = System.getenv("SPRING_PROFILES_ACTIVE");
        if (context == null || context.isEmpty()) {
            context = "default";
        }

        Resource resource = new FileSystemResource(configFile);
        YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
        PropertySource<?> yamlTestProperties = sourceLoader.load("etcProperties", resource, context);
        applicationContext.getEnvironment().getPropertySources().addFirst(yamlTestProperties);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.devnexus.ting.common.IntegrationTestApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {

    LOGGER.info("System property 'ting-spring-profile' not set. Setting active profile to '{}'.",
            SpringContextMode.DemoContextConfiguration.getCode());
    applicationContext.getEnvironment().addActiveProfile(SpringContextMode.DemoContextConfiguration.getCode());

    final ConfigurableEnvironment environment = applicationContext.getEnvironment();

    final String demoPropertySourceLocation = "classpath:application.yml";

    final PropertySource<?> propertySource;
    final YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();

    try {/*from   w  w  w .j a  v  a2s  .c  om*/
        propertySource = yamlPropertySourceLoader.load("devnexus-test",
                new DefaultResourceLoader().getResource(demoPropertySourceLocation), null);
    } catch (IOException e) {
        throw new IllegalStateException(
                "Unable to get ResourcePropertySource '" + demoPropertySourceLocation + "'", e);
    }
    environment.getPropertySources().addFirst(propertySource);

    LOGGER.info("Properties for demo mode loaded [" + demoPropertySourceLocation + "]");

}

From source file:com.devnexus.ting.DefaultApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {

    final CloudEnvironment env = new CloudEnvironment();
    if (env.getInstanceInfo() != null) {
        LOGGER.info("Running in the cloud - API: " + env.getCloudApiUri());
        applicationContext.getEnvironment().setActiveProfiles(SpringProfile.CLOUD);
    } else {/*  ww  w. jav  a2s  .  c  o m*/

        final Apphome apphome = SystemInformationUtils.retrieveBasicSystemInformation();
        SpringContextMode springContextMode;

        LOGGER.info("DevNexus Apphome: " + apphome);

        if (SystemInformationUtils.existsConfigFile(apphome.getAppHomePath())) {
            springContextMode = SpringContextMode.ProductionContextConfiguration;
        } else {
            springContextMode = SpringContextMode.DemoContextConfiguration;
        }

        applicationContext.getEnvironment().setActiveProfiles(springContextMode.getCode());
    }

    final ConfigurableEnvironment environment = applicationContext.getEnvironment();

    if (environment.acceptsProfiles(SpringProfile.STANDALONE)) {
        final String tingHome = environment.getProperty(Apphome.APP_HOME_DIRECTORY);
        final PropertySource<?> propertySource;
        final YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();

        final String productionPropertySourceLocation;
        final Apphome apphome = SystemInformationUtils.retrieveBasicSystemInformation();
        if (apphome.getAppHomeSource() == AppHomeSource.USER_DIRECTORY) {
            productionPropertySourceLocation = "file:" + apphome.getAppHomePath() + File.separator
                    + SystemInformationUtils.TING_CONFIG_FILENAME;
        } else {
            productionPropertySourceLocation = "file:" + tingHome + File.separator
                    + SystemInformationUtils.TING_CONFIG_FILENAME;
        }

        try {
            propertySource = yamlPropertySourceLoader.load("devnexus-standalone",
                    new DefaultResourceLoader().getResource(productionPropertySourceLocation), null);
        } catch (IOException e) {
            throw new IllegalStateException(
                    "Unable to get ResourcePropertySource '" + productionPropertySourceLocation + "'", e);
        }
        environment.getPropertySources().addFirst(propertySource);
        LOGGER.info("Properties for standalone mode loaded [" + productionPropertySourceLocation + "].");
    } else {
        LOGGER.info("Using Properties for demo mode.");
    }

    final String emailProviderAsString = environment.getProperty("devnexus.mail.emailProvider");
    final EmailProvider emailProvider = EmailProvider.valueOf(emailProviderAsString.trim().toUpperCase());
    final boolean twitterEnabled = environment.getProperty("devnexus.twitter.enabled", Boolean.class,
            Boolean.FALSE);
    final boolean websocketEnabled = environment.getProperty("devnexus.websocket.enabled", Boolean.class,
            Boolean.FALSE);
    final boolean payPalEnabled = environment.containsProperty("PAYPAL_MODE");

    LOGGER.info("Uses Settings:" + "\nEmail Provider: " + emailProvider + "\nTwitter Enabled: " + twitterEnabled
            + "\nPayPal Enabled: " + payPalEnabled + "\nWebsocket Enabled: " + websocketEnabled);

    if (EmailProvider.SENDGRID.equals(emailProvider)) {
        applicationContext.getEnvironment().addActiveProfile(SpringProfile.SENDGRID_ENABLED);
    } else if (EmailProvider.SMTP.equals(emailProvider)) {
        applicationContext.getEnvironment().addActiveProfile(SpringProfile.SMTP_ENABLED);
    } else if (EmailProvider.AMAZON_SES.equals(emailProvider)) {
        applicationContext.getEnvironment().addActiveProfile(SpringProfile.AMAZON_SES_ENABLED);
    } else if (EmailProvider.NONE.equals(emailProvider)) {
    } else {
        throw new IllegalArgumentException("Unknown EmailProvider: " + emailProvider);
    }

    if (!EmailProvider.NONE.equals(emailProvider)) {
        applicationContext.getEnvironment().addActiveProfile(SpringProfile.MAIL_ENABLED);
    }

    if (twitterEnabled) {
        applicationContext.getEnvironment().addActiveProfile(SpringProfile.TWITTER_ENABLED);
    }
    if (websocketEnabled) {
        applicationContext.getEnvironment().addActiveProfile(SpringProfile.WEBSOCKET_ENABLED);
    }
    if (payPalEnabled) {
        applicationContext.getEnvironment().addActiveProfile(SpringProfile.PAYPAL_ENABLED);
        switch (environment.getProperty("PAYPAL_MODE")) {
        case "live":
            applicationContext.getEnvironment().addActiveProfile(SpringProfile.PAYPAL_LIVE);
            break;
        default:
            applicationContext.getEnvironment().addActiveProfile(SpringProfile.PAYPAL_SANDBOX);
            break;
        }
    }
    if (environment.getProperty("DEVELOPMENT_MODE", Boolean.class, Boolean.FALSE)) {
        applicationContext.getEnvironment().addActiveProfile(SpringProfile.DEVELOPMENT_ENABLED);
    }
}