GenericFilterBean lazily initializes its default environment
Alignment with HttpServletBean; consistent use of EnvironmentCapable. Issue: SPR-15469
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -38,10 +38,12 @@ import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.EnvironmentCapable;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceEditor;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.context.ServletContextAware;
|
||||
import org.springframework.web.context.support.ServletContextResourceLoader;
|
||||
@@ -75,25 +77,21 @@ import org.springframework.web.util.NestedServletException;
|
||||
* @see #initFilterBean
|
||||
* @see #doFilter
|
||||
*/
|
||||
public abstract class GenericFilterBean implements
|
||||
Filter, BeanNameAware, EnvironmentAware, ServletContextAware, InitializingBean, DisposableBean {
|
||||
public abstract class GenericFilterBean implements Filter, BeanNameAware, EnvironmentAware,
|
||||
EnvironmentCapable, ServletContextAware, InitializingBean, DisposableBean {
|
||||
|
||||
/** Logger available to subclasses */
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
/**
|
||||
* Set of required properties (Strings) that must be supplied as
|
||||
* config parameters to this filter.
|
||||
*/
|
||||
private final Set<String> requiredProperties = new HashSet<>();
|
||||
private String beanName;
|
||||
|
||||
private Environment environment;
|
||||
|
||||
private ServletContext servletContext;
|
||||
|
||||
private FilterConfig filterConfig;
|
||||
|
||||
private String beanName;
|
||||
|
||||
private Environment environment = new StandardServletEnvironment();
|
||||
|
||||
private ServletContext servletContext;
|
||||
private final Set<String> requiredProperties = new HashSet<>(4);
|
||||
|
||||
|
||||
/**
|
||||
@@ -104,24 +102,47 @@ public abstract class GenericFilterBean implements
|
||||
* @see #getFilterName()
|
||||
*/
|
||||
@Override
|
||||
public final void setBeanName(String beanName) {
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* Set the {@code Environment} that this filter runs in.
|
||||
* <p>Any environment set here overrides the {@link StandardServletEnvironment}
|
||||
* provided by default.
|
||||
* <p>This {@code Environment} object is used only for resolving placeholders in
|
||||
* resource paths passed into init-parameters for this filter. If no init-params are
|
||||
* used, this {@code Environment} can be essentially ignored.
|
||||
* @see #init(FilterConfig)
|
||||
*/
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Environment} associated with this filter.
|
||||
* <p>If none specified, a default environment will be initialized via
|
||||
* {@link #createEnvironment()}.
|
||||
* @since 4.3.9
|
||||
*/
|
||||
@Override
|
||||
public Environment getEnvironment() {
|
||||
if (this.environment == null) {
|
||||
this.environment = createEnvironment();
|
||||
}
|
||||
return this.environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and return a new {@link StandardServletEnvironment}.
|
||||
* <p>Subclasses may override this in order to configure the environment or
|
||||
* specialize the environment type returned.
|
||||
* @since 4.3.9
|
||||
*/
|
||||
protected Environment createEnvironment() {
|
||||
return new StandardServletEnvironment();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the ServletContext that the bean factory runs in.
|
||||
* <p>Only relevant in case of initialization as bean, to have a ServletContext
|
||||
@@ -130,7 +151,7 @@ public abstract class GenericFilterBean implements
|
||||
* @see #getServletContext()
|
||||
*/
|
||||
@Override
|
||||
public final void setServletContext(ServletContext servletContext) {
|
||||
public void setServletContext(ServletContext servletContext) {
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
@@ -147,6 +168,16 @@ public abstract class GenericFilterBean implements
|
||||
initFilterBean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses may override this to perform custom filter shutdown.
|
||||
* <p>Note: This method will be called from standard filter destruction
|
||||
* as well as filter bean destruction in a Spring application context.
|
||||
* <p>This default implementation is empty.
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Subclasses can invoke this method to specify that this property
|
||||
@@ -180,19 +211,25 @@ public abstract class GenericFilterBean implements
|
||||
this.filterConfig = filterConfig;
|
||||
|
||||
// Set bean properties from init parameters.
|
||||
try {
|
||||
PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
|
||||
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
|
||||
ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
|
||||
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, this.environment));
|
||||
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);
|
||||
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.
|
||||
@@ -214,6 +251,20 @@ public abstract class GenericFilterBean implements
|
||||
protected void initBeanWrapper(BeanWrapper bw) throws BeansException {
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses may override this to perform custom initialization.
|
||||
* All bean properties of this filter will have been set before this
|
||||
* method is invoked.
|
||||
* <p>Note: This method will be called from standard filter initialization
|
||||
* as well as filter bean initialization in a Spring application context.
|
||||
* Filter name and ServletContext will be available in both cases.
|
||||
* <p>This default implementation is empty.
|
||||
* @throws ServletException if subclass initialization fails
|
||||
* @see #getFilterName()
|
||||
* @see #getServletContext()
|
||||
*/
|
||||
protected void initFilterBean() throws ServletException {
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the FilterConfig of this filter available, if any.
|
||||
@@ -258,32 +309,6 @@ public abstract class GenericFilterBean implements
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Subclasses may override this to perform custom initialization.
|
||||
* All bean properties of this filter will have been set before this
|
||||
* method is invoked.
|
||||
* <p>Note: This method will be called from standard filter initialization
|
||||
* as well as filter bean initialization in a Spring application context.
|
||||
* Filter name and ServletContext will be available in both cases.
|
||||
* <p>This default implementation is empty.
|
||||
* @throws ServletException if subclass initialization fails
|
||||
* @see #getFilterName()
|
||||
* @see #getServletContext()
|
||||
*/
|
||||
protected void initFilterBean() throws ServletException {
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses may override this to perform custom filter shutdown.
|
||||
* <p>Note: This method will be called from standard filter destruction
|
||||
* as well as filter bean destruction in a Spring application context.
|
||||
* <p>This default implementation is empty.
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* PropertyValues implementation created from FilterConfig init parameters.
|
||||
*/
|
||||
@@ -298,14 +323,14 @@ public abstract class GenericFilterBean implements
|
||||
* @throws ServletException if any required properties are missing
|
||||
*/
|
||||
public FilterConfigPropertyValues(FilterConfig config, Set<String> requiredProperties)
|
||||
throws ServletException {
|
||||
throws ServletException {
|
||||
|
||||
Set<String> missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ?
|
||||
new HashSet<>(requiredProperties) : null;
|
||||
Set<String> missingProps = (!CollectionUtils.isEmpty(requiredProperties) ?
|
||||
new HashSet<>(requiredProperties) : null);
|
||||
|
||||
Enumeration<?> en = config.getInitParameterNames();
|
||||
while (en.hasMoreElements()) {
|
||||
String property = (String) en.nextElement();
|
||||
Enumeration<String> paramNames = config.getInitParameterNames();
|
||||
while (paramNames.hasMoreElements()) {
|
||||
String property = paramNames.nextElement();
|
||||
Object value = config.getInitParameter(property);
|
||||
addPropertyValue(new PropertyValue(property, value));
|
||||
if (missingProps != null) {
|
||||
@@ -314,11 +339,11 @@ public abstract class GenericFilterBean implements
|
||||
}
|
||||
|
||||
// Fail if we are still missing properties.
|
||||
if (missingProps != null && missingProps.size() > 0) {
|
||||
if (!CollectionUtils.isEmpty(missingProps)) {
|
||||
throw new ServletException(
|
||||
"Initialization from FilterConfig for filter '" + config.getFilterName() +
|
||||
"' failed; the following required properties were missing: " +
|
||||
StringUtils.collectionToDelimitedString(missingProps, ", "));
|
||||
"Initialization from FilterConfig for filter '" + config.getFilterName() +
|
||||
"' failed; the following required properties were missing: " +
|
||||
StringUtils.collectionToDelimitedString(missingProps, ", "));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user