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();
}
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");
}
}
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);
}
Aggregations