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

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

Introduction

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

Prototype

public ServletContextResourceLoader(ServletContext servletContext) 

Source Link

Document

Create a new ServletContextResourceLoader.

Usage

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   w w  w  .java 2s.  c om
    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: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
    *//* w w  w .j a  v  a  2 s .  c  om*/
    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:net.jawr.web.resource.bundle.locale.message.GrailsMessageBundleScriptCreator.java

public Reader createScript(Charset charset) {

    // Determine wether this is run-app or run-war style of runtime. 
    boolean warDeployed = ((Boolean) this.servletContext.getAttribute(JawrConstant.GRAILS_WAR_DEPLOYED))
            .booleanValue();/* w w w  .j ava2 s.c  om*/

    // Spring message bundle object, the same used by grails. 
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setFallbackToSystemLocale(false);
    Set<String> allPropertyNames = null;

    // Read the properties files to find out the available message keys. It is done differently 
    // for run-app or run-war style of runtimes. 
    if (warDeployed) {
        allPropertyNames = getPropertyNamesFromWar();
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("found a total of " + allPropertyNames.size() + " distinct message keys.");
        messageSource.setResourceLoader(new ServletContextResourceLoader(this.servletContext));
        messageSource.setBasename(PROPERTIES_DIR + configParam.substring(configParam.lastIndexOf('.') + 1));
    } else {
        ResourceBundle bundle;
        if (null != locale)
            bundle = ResourceBundle.getBundle(configParam, locale);
        else
            bundle = ResourceBundle.getBundle(configParam);
        allPropertyNames = new HashSet<String>();
        Enumeration<String> keys = bundle.getKeys();
        while (keys.hasMoreElements())
            allPropertyNames.add(keys.nextElement());
        String basename = "file:./" + configParam.replaceAll("\\.", "/");
        messageSource.setBasename(basename);
    }

    // Pass all messages to a properties file. 
    Properties props = new Properties();
    for (Iterator<String> it = allPropertyNames.iterator(); it.hasNext();) {
        String key = it.next();
        if (matchesFilter(key)) {
            try {
                // Use the property encoding of the file
                String msg = new String(
                        messageSource.getMessage(key, new Object[0], locale).getBytes(CHARSET_ISO_8859_1),
                        charset.displayName());
                props.put(key, msg);
            } catch (NoSuchMessageException e) {
                // This is expected, so it's OK to have an empty catch block. 
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Message key [" + key + "] not found.");
            } catch (UnsupportedEncodingException e) {
                LOGGER.warn("Unable to convert value of message bundle associated to key '" + key
                        + "' because the charset is unknown");
            }
        }
    }

    return doCreateScript(props);
}

From source file:org.solmix.wmix.web.servlet.AbstractWmixFilter.java

@Override
public void init(FilterConfig config) throws ServletException {
    filterConfig = config;//from  w ww .  ja  v a2  s. c  om
    logInBothServletAndLoggingSystem("Initializing filter: " + getFilterName());

    try {
        PropertyValues pvs = new FilterConfigPropertyValues(getFilterConfig(), requiredProperties);
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
        ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
        bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader));
        initBeanWrapper(bw);
        bw.setPropertyValues(pvs, true);
    } catch (Exception e) {
        throw new ServletException("Failed to set bean properties on filter: " + getFilterName(), e);
    }

    try {
        init();
    } catch (Exception e) {
        throw new ServletException("Failed to init filter: " + getFilterName(), e);
    }

    logInBothServletAndLoggingSystem(
            getClass().getSimpleName() + " - " + getFilterName() + ": initialization completed");
}

From source file:org.lightadmin.core.config.context.LightAdminContextConfiguration.java

@Bean
@Autowired
public ServletContextResourceLoader servletContextResourceLoader(ServletContext servletContext) {
    return new ServletContextResourceLoader(servletContext);
}

From source file:net.jawr.web.resource.bundle.locale.message.grails.GrailsMessageBundleScriptCreator.java

public Reader createScript(Charset charset) {

    // Determine wether this is run-app or run-war style of runtime.
    boolean warDeployed = ((Boolean) this.servletContext.getAttribute(JawrGrailsConstant.GRAILS_WAR_DEPLOYED))
            .booleanValue();//from w ww.ja v a2  s  .  c om

    // Spring message bundle object, the same used by grails.
    GrailsBundleMessageSource messageSource = new GrailsBundleMessageSource(warDeployed);

    messageSource.setFallbackToSystemLocale(control.isFallbackToSystemLocale());
    messageSource.setFilters(filterList);

    if (warDeployed) {
        messageSource.setResourceLoader(new ServletContextResourceLoader(this.servletContext));

    }
    messageSource.setBasenames(getBaseNames(warDeployed));

    Properties props = messageSource.getAllMessages(locale);
    return doCreateScript(props);
}

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 ww. j  a  v  a  2 s  .co  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);
        }
    }
}

From source file:org.lightadmin.core.config.bootstrap.LightAdminBeanDefinitionRegistryPostProcessor.java

private ServletContextResourceLoader newResourceLoader(ServletContext servletContext) {
    return new ServletContextResourceLoader(servletContext);
}