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

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

Introduction

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

Prototype

@Nullable
String getProperty(String key);

Source Link

Document

Return the property value associated with the given key, or null if the key cannot be resolved.

Usage

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   www  .j  ava 2s.  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);
    }
}

From source file:org.springframework.boot.context.listener.LoggingApplicationListener.java

/**
 * Initialize the logging system according to preferences expressed through the
 * {@link Environment} and the classpath.
 *///from   w ww  .  j a  v a 2  s. c o  m
protected void initialize(ConfigurableEnvironment environment, ClassLoader classLoader) {

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

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

    LoggingSystem system = LoggingSystem.get(classLoader);

    if (environmentChanged) {
        // Re-initialize the defaults in case the Environment changed
        system.beforeInitialize();
    }
    // 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);
    }
}

From source file:org.springframework.boot.context.logging.LoggingApplicationListener.java

private boolean isSet(ConfigurableEnvironment environment, String property) {
    String value = environment.getProperty(property);
    return (value != null && !value.equals("false"));
}

From source file:org.springframework.boot.context.logging.LoggingApplicationListener.java

private void initializeSystem(ConfigurableEnvironment environment, LoggingSystem system, LogFile logFile) {
    LoggingInitializationContext initializationContext = new LoggingInitializationContext(environment);
    String logConfig = environment.getProperty(CONFIG_PROPERTY);
    if (ignoreLogConfig(logConfig)) {
        system.initialize(initializationContext, null, logFile);
    } else {//from w  w w  .  j ava 2  s  .  c  om
        try {
            ResourceUtils.getURL(logConfig).openStream().close();
            system.initialize(initializationContext, logConfig, logFile);
        } catch (Exception ex) {
            // NOTE: We can't use the logger here to report the problem
            System.err.println(
                    "Logging system failed to initialize " + "using configuration from '" + logConfig + "'");
            ex.printStackTrace(System.err);
            throw new IllegalStateException(ex);
        }
    }
}

From source file:org.springframework.boot.logging.LoggingApplicationListener.java

private void initializeSystem(ConfigurableEnvironment environment, LoggingSystem system) {
    LoggingInitializationContext initializationContext = new LoggingInitializationContext(environment);
    LogFile logFile = LogFile.get(environment);
    String logConfig = environment.getProperty(CONFIG_PROPERTY);
    if (ignoreLogConfig(logConfig)) {
        system.initialize(initializationContext, null, logFile);
    } else {//w  ww .  jav  a 2 s  .co  m
        try {
            ResourceUtils.getURL(logConfig).openStream().close();
            system.initialize(initializationContext, logConfig, logFile);
        } catch (Exception ex) {
            // NOTE: We can't use the logger here to report the problem
            System.err.println(
                    "Logging system failed to initialize " + "using configuration from '" + logConfig + "'");
            ex.printStackTrace(System.err);
            throw new IllegalStateException(ex);
        }
    }
}

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

@Override
public PropertySource<?> locate(Environment environment) {
    if (!(environment instanceof ConfigurableEnvironment)) {
        return null;
    }/*from w w  w .  j  a va2  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-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 w w  w.j a  v  a2  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.dataflow.server.cloudfoundry.config.CloudFoundryEnvironmentPostProcessor.java

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    if (Boolean.valueOf(environment.getProperty(FEATURES_PREFIX + FeaturesProperties.TASKS_ENABLED))) {
        logger.warn(FEATURES_PREFIX + FeaturesProperties.TASKS_ENABLED + " has been set to true directly. "
                + "Be advised this is an EXPERIMENTAL feature, normally enabled via " + FEATURES_PREFIX
                + "experimental.tasksEnabled");
    }//from   w  w  w .  j  av  a  2  s.c om

    Map<String, Object> propertiesToOverride = new HashMap<>();
    boolean isTasksEnabled = Boolean
            .valueOf(environment.getProperty(FEATURES_PREFIX + "experimental.tasksEnabled"));
    if (isTasksEnabled) {
        propertiesToOverride.put(FEATURES_PREFIX + FeaturesProperties.TASKS_ENABLED, true);
        environment.getPropertySources()
                .addFirst(new MapPropertySource("CFDataflowServerProperties", propertiesToOverride));
    }
}

From source file:org.springframework.cloud.gateway.test.sse.SseIntegrationTests.java

@Before
public void setup() throws Exception {
    this.server = new ReactorHttpServer();
    this.server.setHandler(createHttpHandler());
    this.server.afterPropertiesSet();
    this.server.start();

    // Set dynamically chosen port
    this.serverPort = this.server.getPort();
    logger.info("SSE Port: " + this.serverPort);

    this.gatewayContext = new SpringApplicationBuilder(GatewayConfig.class)
            .properties("sse.server.port:" + this.serverPort, "server.port=0", "spring.jmx.enabled=false")
            .run();//from w  w  w.  j a  va 2s  .c  om

    ConfigurableEnvironment env = this.gatewayContext.getBean(ConfigurableEnvironment.class);
    this.gatewayPort = Integer.valueOf(env.getProperty("local.server.port"));

    this.webClient = WebClient.create("http://localhost:" + this.gatewayPort + "/sse");

    logger.info("Gateway Port: " + this.gatewayPort);
}

From source file:org.springframework.cloud.gateway.test.websocket.WebSocketIntegrationTests.java

@Before
public void setup() throws Exception {
    this.client = new ReactorNettyWebSocketClient();

    this.server = new ReactorHttpServer();
    this.server.setHandler(createHttpHandler());
    this.server.afterPropertiesSet();
    this.server.start();

    // Set dynamically chosen port
    this.serverPort = this.server.getPort();

    if (this.client instanceof Lifecycle) {
        ((Lifecycle) this.client).start();
    }/*  w  w  w  .j a  va2 s.  co  m*/

    this.gatewayContext = new SpringApplicationBuilder(GatewayConfig.class)
            .properties("ws.server.port:" + this.serverPort, "server.port=0", "spring.jmx.enabled=false").run();

    ConfigurableEnvironment env = this.gatewayContext.getBean(ConfigurableEnvironment.class);
    this.gatewayPort = Integer.valueOf(env.getProperty("local.server.port"));
}