Search in sources :

Example 1 with ServletContextResourceLoader

use of org.springframework.web.context.support.ServletContextResourceLoader in project spring-framework by spring-projects.

the class HttpServletBean method init.

/**
 * Map config parameters onto bean properties of this servlet, and
 * invoke subclass initialization.
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 */
@Override
public final void init() throws ServletException {
    // Set bean properties from init parameters.
    PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            if (logger.isErrorEnabled()) {
                logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
            }
            throw ex;
        }
    }
    // Let subclasses do whatever initialization they like.
    initServletBean();
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValues(org.springframework.beans.PropertyValues) ResourceEditor(org.springframework.core.io.ResourceEditor) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Example 2 with ServletContextResourceLoader

use of org.springframework.web.context.support.ServletContextResourceLoader in project spring-framework by spring-projects.

the class GenericFilterBean method init.

/**
 * Standard way of initializing this filter.
 * Map config parameters onto bean properties of this filter, and
 * invoke subclass initialization.
 * @param filterConfig the configuration for this filter
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 * @see #initFilterBean
 */
@Override
public final void init(FilterConfig filterConfig) throws ServletException {
    Assert.notNull(filterConfig, "FilterConfig must not be null");
    this.filterConfig = filterConfig;
    // Set bean properties from init parameters.
    PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
            Environment env = this.environment;
            if (env == null) {
                env = new StandardServletEnvironment();
            }
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, env));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage();
            logger.error(msg, ex);
            throw new NestedServletException(msg, ex);
        }
    }
    // Let subclasses do whatever initialization they like.
    initFilterBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Filter '" + filterConfig.getFilterName() + "' configured for use");
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValues(org.springframework.beans.PropertyValues) NestedServletException(org.springframework.web.util.NestedServletException) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) Environment(org.springframework.core.env.Environment) ResourceEditor(org.springframework.core.io.ResourceEditor) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Example 3 with ServletContextResourceLoader

use of org.springframework.web.context.support.ServletContextResourceLoader in project webapp by elimu-ai.

the class EnvironmentContextLoaderListener method customizeContext.

@Override
protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
    logger.info("customizeContext");
    // Load default settings
    try {
        Resource resourceConfig = new ServletContextResourceLoader(servletContext).getResource("classpath:config.properties");
        PROPERTIES.load(resourceConfig.getInputStream());
        Resource resourceJdbc = new ServletContextResourceLoader(servletContext).getResource("classpath:jdbc.properties");
        PROPERTIES.load(resourceJdbc.getInputStream());
        logger.debug("properties (before overriding): " + PROPERTIES);
    } catch (IOException ex) {
        logger.error(ex);
    }
    if ((env == Environment.TEST) || (env == Environment.PROD)) {
        InputStream inputStream = null;
        try {
            // Override config.properties
            Resource resourceConfig = new ServletContextResourceLoader(servletContext).getResource("classpath:config_" + env + ".properties");
            PROPERTIES.load(resourceConfig.getInputStream());
            // Override jdbc.properties
            Resource resourceJdbc = new ServletContextResourceLoader(servletContext).getResource("classpath:jdbc_" + env + ".properties");
            PROPERTIES.load(resourceJdbc.getInputStream());
            String contentLanguage = (String) servletContext.getAttribute("content_language");
            logger.info("contentLanguage: " + contentLanguage);
            PROPERTIES.put("content.language", contentLanguage);
            String jdbcUrl = (String) servletContext.getAttribute("jdbc_url");
            PROPERTIES.put("jdbc.url", jdbcUrl);
            String jdbcUsername = (String) servletContext.getAttribute("jdbc_username");
            PROPERTIES.put("jdbc.username", jdbcUsername);
            String jdbcPasswordAttr = (String) servletContext.getAttribute("jdbc_password");
            PROPERTIES.put("jdbc.password", jdbcPasswordAttr);
            String googleApiSecret = (String) servletContext.getAttribute("google_api_secret");
            PROPERTIES.put("google.api.secret", googleApiSecret);
            String gitHubApiSecret = (String) servletContext.getAttribute("github_api_secret");
            PROPERTIES.put("github.api.secret", gitHubApiSecret);
            String covalentApiKey = (String) servletContext.getAttribute("covalent_api_key");
            PROPERTIES.put("covalent.api.key", covalentApiKey);
            if (env == Environment.PROD) {
                String discordWebhookUrl = (String) servletContext.getAttribute("discord_webhook_url");
                PROPERTIES.put("discord.webhook.url", discordWebhookUrl);
            }
            logger.debug("properties (after overriding): " + PROPERTIES);
        } catch (FileNotFoundException ex) {
            logger.error(ex);
        } catch (IOException ex) {
            logger.error(ex);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ex) {
                logger.error(ex);
            }
        }
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
        propertyPlaceholderConfigurer.setProperties(PROPERTIES);
        applicationContext.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {

            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
                PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
                propertyPlaceholderConfigurer.setProperties(PROPERTIES);
                propertyPlaceholderConfigurer.postProcessBeanFactory(configurableListableBeanFactory);
            }
        });
    }
    // Add all supported languages
    PROPERTIES.put("supported.languages", Language.values());
    // Add config properties to application scope
    servletContext.setAttribute("configProperties", PROPERTIES);
    servletContext.setAttribute("newLineCharRn", "\r\n");
    servletContext.setAttribute("newLineCharR", "\r");
    servletContext.setAttribute("newLineCharR", "\n");
    super.customizeContext(servletContext, applicationContext);
}
Also used : PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Aggregations

BeansException (org.springframework.beans.BeansException)3 ServletContextResourceLoader (org.springframework.web.context.support.ServletContextResourceLoader)3 BeanWrapper (org.springframework.beans.BeanWrapper)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 PropertyValues (org.springframework.beans.PropertyValues)2 ResourceEditor (org.springframework.core.io.ResourceEditor)2 ResourceLoader (org.springframework.core.io.ResourceLoader)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 PropertyPlaceholderConfigurer (org.springframework.beans.factory.config.PropertyPlaceholderConfigurer)1 Environment (org.springframework.core.env.Environment)1 Resource (org.springframework.core.io.Resource)1 StandardServletEnvironment (org.springframework.web.context.support.StandardServletEnvironment)1 NestedServletException (org.springframework.web.util.NestedServletException)1