Example usage for org.springframework.web.context.support WebApplicationContextUtils initServletPropertySources

List of usage examples for org.springframework.web.context.support WebApplicationContextUtils initServletPropertySources

Introduction

In this page you can find the example usage for org.springframework.web.context.support WebApplicationContextUtils initServletPropertySources.

Prototype

public static void initServletPropertySources(MutablePropertySources sources,
        @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) 

Source Link

Document

Replace Servlet -based StubPropertySource stub property sources with actual instances populated with the given servletContext and servletConfig objects.

Usage

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

@Override
public void initialize(ConfigurableWebApplicationContext applicationContext) {

    Resource resource = null;//from   w ww .  java  2s .  c  om
    ServletContext servletContext = applicationContext.getServletContext();
    WebApplicationContextUtils.initServletPropertySources(
            applicationContext.getEnvironment().getPropertySources(), servletContext,
            applicationContext.getServletConfig());

    ServletConfig servletConfig = applicationContext.getServletConfig();
    String locations = servletConfig == null ? null
            : servletConfig.getInitParameter(PROFILE_CONFIG_FILE_LOCATIONS);
    resource = getResource(servletContext, applicationContext, locations);

    if (resource == null) {
        servletContext.log("No YAML environment properties from servlet.  Defaulting to servlet context.");
        locations = servletContext.getInitParameter(PROFILE_CONFIG_FILE_LOCATIONS);
        resource = getResource(servletContext, applicationContext, locations);
    }

    try {
        servletContext.log("Loading YAML environment properties from location: " + resource);
        YamlMapFactoryBean factory = new YamlMapFactoryBean();
        factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE);

        List<Resource> resources = new ArrayList<Resource>();

        String defaultLocation = servletConfig == null ? null
                : servletConfig.getInitParameter(PROFILE_CONFIG_FILE_DEFAULT);
        if (defaultLocation != null) {
            Resource defaultResource = new ClassPathResource(defaultLocation);
            if (defaultResource.exists()) {
                resources.add(defaultResource);
            }
        }

        resources.add(resource);
        factory.setResources(resources.toArray(new Resource[resources.size()]));

        Map<String, Object> map = factory.getObject();
        String yamlStr = (new Yaml()).dump(map);
        map.put(rawYamlKey, yamlStr);
        NestedMapPropertySource properties = new NestedMapPropertySource("servletConfigYaml", map);
        applicationContext.getEnvironment().getPropertySources().addLast(properties);
        applySpringProfiles(applicationContext.getEnvironment(), servletContext);
        applyLog4jConfiguration(applicationContext.getEnvironment(), servletContext);

    } catch (Exception e) {
        servletContext.log("Error loading YAML environment properties from location: " + resource, e);
    }

}