Example usage for org.springframework.web.context ConfigurableWebApplicationContext getServletConfig

List of usage examples for org.springframework.web.context ConfigurableWebApplicationContext getServletConfig

Introduction

In this page you can find the example usage for org.springframework.web.context ConfigurableWebApplicationContext getServletConfig.

Prototype

@Nullable
ServletConfig getServletConfig();

Source Link

Document

Return the ServletConfig for this web application context, if any.

Usage

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

@Override
public void initialize(ConfigurableWebApplicationContext applicationContext) {

    Resource resource = null;//from   www .  j  a  v  a2s . com
    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);
    }

}