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

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

Introduction

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

Prototype

MutablePropertySources getPropertySources();

Source Link

Document

Return the PropertySources for this Environment in mutable form, allowing for manipulation of the set of PropertySource objects that should be searched when resolving properties against this Environment object.

Usage

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 {//  ww w  . ja  v a 2  s. com
            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:gov.uscis.vis.api.config.SwaggerPropertiesLoader.java

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    ConfigurableEnvironment env = event.getEnvironment();
    env.getPropertySources().addLast(new PropertiesPropertySource("swagger", createSwaggerProperties(env)));
}

From source file:io.kahu.hawaii.config.HawaiiAppContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    ConfigurableEnvironment env = applicationContext.getEnvironment();
    env.getPropertySources().addFirst(getPropertySource());
}

From source file:org.alfresco.bm.web.WebApp.java

@Override
public void onStartup(ServletContext container) {
    // Grab the server capabilities, otherwise just use the java version
    String javaVersion = System.getProperty("java.version");
    String systemCapabilities = System.getProperty(PROP_SYSTEM_CAPABILITIES, javaVersion);

    String appDir = new File(".").getAbsolutePath();
    String appContext = container.getContextPath();
    String appName = container.getContextPath().replace(SEPARATOR, "");

    // Create an application context (don't start, yet)
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocations(new String[] { "classpath:config/spring/app-context.xml" });

    // Pass our properties to the new context
    Properties ctxProperties = new Properties();
    {/*  w w w  .ja  v a2 s .  co m*/
        ctxProperties.put(PROP_SYSTEM_CAPABILITIES, systemCapabilities);
        ctxProperties.put(PROP_APP_CONTEXT_PATH, appContext);
        ctxProperties.put(PROP_APP_DIR, appDir);
    }
    ConfigurableEnvironment ctxEnv = ctx.getEnvironment();
    ctxEnv.getPropertySources().addFirst(new PropertiesPropertySource(appName, ctxProperties));
    // Override all properties with system properties
    ctxEnv.getPropertySources().addFirst(new PropertiesPropertySource("system", System.getProperties()));
    // Bind to shutdown
    ctx.registerShutdownHook();

    ContextLoaderListener ctxLoaderListener = new ContextLoaderListener(ctx);
    container.addListener(ctxLoaderListener);

    ServletRegistration.Dynamic jerseyServlet = container.addServlet("jersey-serlvet", SpringServlet.class);
    jerseyServlet.setInitParameter("com.sun.jersey.config.property.packages", "org.alfresco.bm.rest");
    jerseyServlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    jerseyServlet.addMapping("/api/*");
}

From source file:bootstrap.EnvironmentBootstrapConfiguration.java

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    if (!environment.getPropertySources().contains(CONFIG_SERVER_BOOTSTRAP)) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("spring.cloud.config.uri",
                "${CONFIG_SERVER_URI:${vcap.services.${PREFIX:}configserver.credentials.uri:http://localhost:8888}}");
        if (ClassUtils.isPresent("org.springframework.cloud.sleuth.zipkin.ZipkinProperties", null)) {
            map.put("spring.zipkin.host",
                    "${ZIPKIN_HOST:${vcap.services.${PREFIX:}zipkin.credentials.host:localhost}}");
            map.put("logging.pattern.console",
                    "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([trace=%X{X-Trace-Id:-},span=%X{X-Span-Id:-}]){yellow} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex");
            String zipkinHost = environment
                    .resolvePlaceholders("${ZIPKIN_HOST:${vcap.services.${PREFIX:}zipkin.credentials.host:}}");
            if (!"".equals(zipkinHost)) {
                map.put("fleet.zipkin.enabled", "true");
            }//from w ww.j av  a 2  s. c  o m
        }
        String space = environment.resolvePlaceholders("${vcap.application.space_name:dev}");
        log.info("Spacename: " + space);
        if (space.startsWith("dev")) {
            environment.addActiveProfile("dev");
        }
        map.put("encrypt.failOnError", "false");
        map.put("endpoints.shutdown.enabled", "true");
        map.put("endpoints.restart.enabled", "true");
        environment.getPropertySources().addLast(new MapPropertySource(CONFIG_SERVER_BOOTSTRAP, map));
    }
}

From source file:io.jmnarloch.spring.cloud.discovery.DiscoveryClientPropertySourceConfigurer.java

/**
 * Configures the property sources// w  ww  .  j  ava 2  s  . c  o m
 *
 * @param environment the application environment
 */
protected void configurePropertySources(ConfigurableEnvironment environment) {
    environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
            new DiscoveryClientPropertySource("discoveryClient"));
}

From source file:io.micrometer.spring.MetricsEnvironmentPostProcessor.java

private void addDefaultProperty(ConfigurableEnvironment environment, String name, String value) {
    MutablePropertySources sources = environment.getPropertySources();
    Map<String, Object> map = null;
    if (sources.contains("defaultProperties")) {
        PropertySource<?> source = sources.get("defaultProperties");
        if (source instanceof MapPropertySource) {
            map = ((MapPropertySource) source).getSource();
        }//ww  w.  j  a v a 2  s. co m
    } else {
        map = new LinkedHashMap<>();
        sources.addLast(new MapPropertySource("defaultProperties", map));
    }
    if (map != null) {
        map.put(name, value);
    }
}

From source file:com.santander.serenity.devstack.springfox.config.itest.EnvironmentListenerIntegrationTests.java

/**
 * Checks that the property "security.ignored" is in the environment after SpringFoxEnvironmentPostProcessor is executed.
 *//*ww w. ja v  a  2s . com*/
@Test
public void securityIgnoredPropertyTest() {
    ConfigurableEnvironment environment = context.getEnvironment();

    MutablePropertySources propertySources = environment.getPropertySources();
    PropertySource<?> modifiedSecurityIgnoredPropertySource = propertySources
            .get("ModifiedSecurityIgnoredPropertySource");
    Object securityIgnoredProperty = modifiedSecurityIgnoredPropertySource.getProperty("security.ignored");

    Assert.assertNotNull("security.gnored property must not be null", securityIgnoredProperty);
}

From source file:com.github.ljtfreitas.restify.spring.configure.RestifyProperties.java

public RestifyApiClient client(RestifyableType type) {
    RestifyApiClient restifyApiClient = new RestifyApiClient();

    RelaxedDataBinder dataBinder = new RelaxedDataBinder(restifyApiClient, "restify." + type.name());

    ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment;
    dataBinder.bind(new PropertySourcesPropertyValues(configurableEnvironment.getPropertySources()));

    return restifyApiClient;
}

From source file:com.santander.serenity.devstack.springfox.config.itest.EnvironmentListenerIntegrationTests.java

/**
 * Checks that the property "security.ignored" has the correct values
 *//*from   w  ww .j  av  a 2 s .c  o  m*/
@Test
public void securityIgnoredPropertyValuesTest() {
    ConfigurableEnvironment environment = context.getEnvironment();

    MutablePropertySources propertySources = environment.getPropertySources();
    PropertySource<?> modifiedSecurityIgnoredPropertySource = propertySources
            .get("ModifiedSecurityIgnoredPropertySource");
    Object securityIgnoredPropertyValues = modifiedSecurityIgnoredPropertySource
            .getProperty("security.ignored");

    LinkedHashSet set = (LinkedHashSet) securityIgnoredPropertyValues;

    Assert.assertTrue("/webjars/** property is not in the security.ignored set", set.contains("/webjars/**"));
    Assert.assertTrue("/swagger-resources property is not in the security.ignored set",
            set.contains("/swagger-resources"));
    Assert.assertTrue("/v2/api-docs property is not in the security.ignored set", set.contains("/v2/api-docs"));
    Assert.assertTrue("/configuration/ui property is not in the security.ignored set",
            set.contains("/configuration/ui"));
    Assert.assertTrue("/configuration/security property is not in the security.ignored set",
            set.contains("/configuration/security"));
    Assert.assertTrue("/swagger-ui.html property is not in the security.ignored set",
            set.contains("/swagger-ui.html"));
}