Example usage for org.springframework.web.context.support ServletContextResourceLoader getResource

List of usage examples for org.springframework.web.context.support ServletContextResourceLoader getResource

Introduction

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

Prototype

@Override
    public Resource getResource(String location) 

Source Link

Usage

From source file:org.pegadi.webapp.spring.PropertyReplacer.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory)
        throws BeansException {

    ServletContextResourceLoader loader = new ServletContextResourceLoader(servletContext);
    Properties properties = new Properties();
    File file = (File) servletContext.getAttribute(DataDirectoryPropertyReplacer.SERVLET_CONTEXT_ATTR);
    properties.setProperty("dataDir", file.getAbsolutePath());

    List<Resource> resources = new ArrayList<Resource>();
    resources.add(loader.getResource("/WEB-INF/pegadi.conf"));
    /* Check wether dataDir/pegadi.conf exist. If it does add it as a location
    *  Also add /WEB-INF/pegadi.conf, so that if the file doesn't exist use default values
    *//*www .  j a va  2 s  . com*/
    File configFile = new File(file.getAbsolutePath(), "pegadi.conf");
    if (configFile.exists()) {
        resources.add(loader.getResource("file:" + configFile.getAbsolutePath()));
    }

    PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
    configurer.setLocations(resources.toArray(new Resource[] {}));

    configurer.setProperties(properties);
    configurer.postProcessBeanFactory(configurableListableBeanFactory);

}

From source file:no.dusken.common.plugin.spring.PropertyReplacer.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory)
        throws BeansException {

    ServletContextResourceLoader loader = new ServletContextResourceLoader(servletContext);
    Properties properties = new Properties();
    File file = (File) servletContext.getAttribute(DataDirectoryPropertyReplacer.SERVLET_CONTEXT_ATTR);
    properties.setProperty("dataDir", file.getAbsolutePath());

    List<Resource> resources = new ArrayList<Resource>();
    resources.add(loader.getResource("/WEB-INF/" + filename));
    /* Check whether dataDir/common.conf exist. If it does add it as a location
    *  Also add /WEB-INF/common.conf, so that if the file doesn't exist use default values
    *//*from  ww  w  .j a va 2s  . c o m*/
    File configFile = new File(file.getAbsolutePath(), filename);
    if (configFile.exists()) {
        resources.add(loader.getResource("file:" + configFile.getAbsolutePath()));
    }

    PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
    configurer.setLocations(resources.toArray(new Resource[resources.size()]));

    configurer.setProperties(properties);
    configurer.postProcessBeanFactory(configurableListableBeanFactory);

}

From source file:architecture.ee.component.core.lifecycle.RepositoryImpl.java

public void setServletContext(ServletContext servletContext) {

    // 1.  ?? ? ? ?  : ARCHITECTURE_INSTALL_ROOT
    String value = servletContext.getInitParameter(ApplicationConstants.ARCHITECTURE_PROFILE_ROOT_ENV_KEY);
    if (!StringUtils.isEmpty(value)) {
        try {//  w w  w  . ja  v  a  2s. c  o  m
            ServletContextResourceLoader servletResoruceLoader = new ServletContextResourceLoader(
                    servletContext);
            Resource resource = servletResoruceLoader.getResource(value);
            if (resource.exists()) {
                log.debug(L10NUtils.format("003003", ApplicationConstants.ARCHITECTURE_PROFILE_ROOT_ENV_KEY,
                        resource.getURI()));
                this.rootResource = resource;
                setState(State.INITIALIZED);
                initailized = true;
            }
        } catch (Throwable e) {
            this.rootResource = null;
        }
    }

    if (!initailized && !StringUtils.isEmpty(value)) {
        Resource obj;
        try {
            obj = resoruceLoader.getResource(value);
            if (obj.exists()) {
                log.debug(L10NUtils.format("003003", ApplicationConstants.ARCHITECTURE_PROFILE_ROOT_ENV_KEY,
                        obj.getURI()));
                this.rootResource = obj;
                setState(State.INITIALIZED);
                initailized = true;

            }
        } catch (Throwable e) {
            log.error(e);
        }
    }

    if (!initailized) {
        try {
            ServletContextResource resource = new ServletContextResource(servletContext, "/WEB-INF");
            File file = resource.getFile();
            if (file.exists()) {
                this.rootResource = new FileSystemResource(file);
                setState(State.INITIALIZED);
                initailized = true;
            }
        } catch (Throwable e) {
            log.error(e);
        }
    }
}