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

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

Introduction

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

Prototype

boolean containsProperty(String key);

Source Link

Document

Return whether the given property key is available for resolution, i.e.

Usage

From source file:io.lavagna.common.LavagnaEnvironment.java

private static void setSystemPropertyIfNull(ConfigurableEnvironment env, String name, String value) {
    if (!env.containsProperty(name) || StringUtils.isBlank(env.getProperty(name))) {
        LOG.warn("Property {} is not set, using default value: {}", name, value);
        Map<String, Object> source = Collections.singletonMap(name, (Object) value);
        env.getPropertySources().addFirst(new MapPropertySource(name, source));
    }/*from  ww w  . ja  v  a  2s  . c o m*/
}

From source file:org.openbaton.autoscaling.utils.Utils.java

public static void loadExternalProperties(ConfigurableEnvironment properties) {
    if (properties.containsProperty("external-properties-file")
            && properties.getProperty("external-properties-file") != null) {
        try {/*from   w w  w .  j ava 2 s .  c o  m*/
            InputStream is = new FileInputStream(new File(properties.getProperty("external-properties-file")));
            Properties externalProperties = new Properties();
            externalProperties.load(is);
            PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(
                    "external-properties", externalProperties);

            MutablePropertySources propertySources = properties.getPropertySources();
            propertySources.addFirst(propertiesPropertySource);
        } catch (IOException e) {
            log.warn("Not found external-properties-file: "
                    + properties.getProperty("external-properties-file"));
        }
    }
}

From source file:at.ac.univie.isc.asio.spring.JerseyLogInitializer.java

private boolean debugModeEnabled(final ConfigurableEnvironment environment) {
    return environment.containsProperty(GLOBAL_DEBUG_FLAG)
            || environment.getProperty(JERSEY_DEBUG_FLAG, Boolean.class, false);
}

From source file:io.lavagna.common.LavagnaEnvironment.java

public LavagnaEnvironment(ConfigurableEnvironment environment) {
    this.environment = environment;

    if (environment.containsProperty(LAVAGNA_CONFIG_LOCATION)
            && StringUtils.isNotBlank(environment.getProperty(LAVAGNA_CONFIG_LOCATION))) {

        String configLocation = environment.getProperty(LAVAGNA_CONFIG_LOCATION);

        LOG.info("Detected config file {}, loading it", configLocation);
        try {//from w w w  . ja  v a 2 s. c  o m
            environment.getPropertySources()
                    .addFirst(new ResourcePropertySource(new UrlResource(configLocation)));
        } catch (IOException ioe) {
            throw new IllegalStateException(
                    "error while loading external configuration file at " + configLocation, ioe);
        }
    }

    setSystemPropertyIfNull(environment, "datasource.dialect", "HSQLDB");
    setSystemPropertyIfNull(environment, "datasource.url", "jdbc:hsqldb:mem:lavagna");
    setSystemPropertyIfNull(environment, "datasource.username", "sa");
    setSystemPropertyIfNull(environment, "datasource.password", "");
    setSystemPropertyIfNull(environment, "spring.profiles.active", "dev");

    logUse("datasource.dialect");
    logUse("datasource.url");
    logUse("datasource.username");
    logUse("spring.profiles.active");

}

From source file:org.cloudfoundry.identity.uaa.config.YamlServletProfileInitializer.java

private void applySpringProfiles(ConfigurableEnvironment environment, ServletContext servletContext) {
    if (environment.containsProperty("spring_profiles")) {
        String profiles = (String) environment.getProperty("spring_profiles");
        servletContext.log("Setting active profiles: " + profiles);
        environment.setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles));
    }//from w  w  w .  j  a  v  a  2s .  c o m
}

From source file:org.cloudfoundry.identity.uaa.config.YamlServletProfileInitializer.java

private void applyLog4jConfiguration(ConfigurableEnvironment environment, ServletContext servletContext) {

    String log4jConfigLocation = "classpath:log4j.properties";

    if (environment.containsProperty("logging.file")) {
        String location = environment.getProperty("logging.file");
        servletContext.log("Setting LOG_FILE: " + location);
        System.setProperty("LOG_FILE", location);
    }//  ww w  . j  a  va2s.c om

    else if (environment.containsProperty("logging.path")) {
        String location = environment.getProperty("logging.path");
        servletContext.log("Setting LOG_PATH: " + location);
        System.setProperty("LOG_PATH", location);
    }

    else if (environment.containsProperty("logging.config")) {
        log4jConfigLocation = environment.getProperty("logging.config");
    }

    try {
        servletContext.log("Loading log4j config from location: " + log4jConfigLocation);
        Log4jConfigurer.initLogging(log4jConfigLocation);
    } catch (FileNotFoundException e) {
        servletContext.log("Error loading log4j config from location: " + log4jConfigLocation, e);
    }

    MDC.put("context", servletContext.getContextPath());

}

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 {/*w  w w .j  a v  a 2s. 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);
    }
}

From source file:org.springframework.boot.cloudfoundry.VcapApplicationListener.java

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    ConfigurableEnvironment environment = event.getEnvironment();
    if (!environment.containsProperty(VCAP_APPLICATION) && !environment.containsProperty(VCAP_SERVICES)) {
        return;//ww w  .ja v  a  2  s .com
    }
    Properties properties = new Properties();
    addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application.");
    addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services.");
    MutablePropertySources propertySources = environment.getPropertySources();
    if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
        propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
                new PropertiesPropertySource("vcap", properties));
    } else {
        propertySources.addFirst(new PropertiesPropertySource("vcap", properties));
    }
}

From source file:org.springframework.boot.cloudfoundry.VcapEnvironmentPostProcessor.java

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    if (!environment.containsProperty(VCAP_APPLICATION) && !environment.containsProperty(VCAP_SERVICES)) {
        return;//w  ww  .ja  v a  2  s  .  com
    }
    Properties properties = new Properties();
    addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application.");
    addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services.");
    MutablePropertySources propertySources = environment.getPropertySources();
    if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
        propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
                new PropertiesPropertySource("vcap", properties));
    } else {
        propertySources.addFirst(new PropertiesPropertySource("vcap", properties));
    }
}

From source file:org.springframework.boot.context.initializer.LoggingApplicationContextInitializer.java

/**
 * Initialize the logging system according to preferences expressed through the
 * {@link Environment} and the classpath.
 *///from  w w w .j  a v  a  2 s .c  o  m
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {

    ConfigurableEnvironment environment = applicationContext.getEnvironment();

    if (this.parseArgs && this.springBootLogging == null) {
        if (environment.containsProperty("debug")) {
            this.springBootLogging = LogLevel.DEBUG;
        }
        if (environment.containsProperty("trace")) {
            this.springBootLogging = LogLevel.TRACE;
        }
    }

    for (Map.Entry<String, String> mapping : ENVIRONMENT_SYSTEM_PROPERTY_MAPPING.entrySet()) {
        if (environment.containsProperty(mapping.getKey())) {
            System.setProperty(mapping.getValue(), environment.getProperty(mapping.getKey()));
        }
    }

    LoggingSystem system = LoggingSystem.get(applicationContext.getClassLoader());

    // User specified configuration
    if (environment.containsProperty("logging.config")) {
        String value = environment.getProperty("logging.config");
        try {
            ResourceUtils.getURL(value).openStream().close();
            system.initialize(value);
            return;
        } catch (Exception ex) {
            // Swallow exception and continue
        }
        this.logger.warn("Logging environment value '" + value + "' cannot be opened and will be ignored");
    }

    system.initialize();
    if (this.springBootLogging != null) {
        initializeLogLevel(system, this.springBootLogging);
    }
}