GenericFilterBean.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:8k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.web.filter;
  17. import java.util.Enumeration;
  18. import java.util.HashSet;
  19. import java.util.Set;
  20. import javax.servlet.Filter;
  21. import javax.servlet.FilterConfig;
  22. import javax.servlet.ServletContext;
  23. import javax.servlet.ServletException;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.springframework.beans.BeanWrapper;
  27. import org.springframework.beans.BeanWrapperImpl;
  28. import org.springframework.beans.BeansException;
  29. import org.springframework.beans.FatalBeanException;
  30. import org.springframework.beans.MutablePropertyValues;
  31. import org.springframework.beans.PropertyValue;
  32. import org.springframework.beans.PropertyValues;
  33. import org.springframework.core.io.Resource;
  34. import org.springframework.core.io.ResourceEditor;
  35. import org.springframework.core.io.ResourceLoader;
  36. import org.springframework.util.StringUtils;
  37. import org.springframework.web.context.support.ServletContextResourceLoader;
  38. /**
  39.  * Simple base implementation of javax.servlet.Filter that treats its config
  40.  * parameters as bean properties. A very handy superclass for any type of filter.
  41.  * Type conversion is automatic. It is also possible for subclasses to specify
  42.  * required properties.
  43.  *
  44.  * <p>This filter leaves actual filtering to subclasses.
  45.  *
  46.  * @author Juergen Hoeller
  47.  * @since 06.12.2003
  48.  * @see #initFilterBean
  49.  */
  50. public abstract class GenericFilterBean implements Filter {
  51. protected final Log logger = LogFactory.getLog(getClass());
  52. /**
  53.  * Set of required properties (Strings) that must be supplied as
  54.  * config parameters to this filter.
  55.  */
  56. private final Set requiredProperties = new HashSet();
  57. /* The FilterConfig of this filter */
  58. private FilterConfig filterConfig;
  59. /**
  60.  * Subclasses can invoke this method to specify that this property
  61.  * (which must match a JavaBean property they expose) is mandatory,
  62.  * and must be supplied as a config parameter.
  63.  * @param property name of the required property
  64.  */
  65. protected final void addRequiredProperty(String property) {
  66. this.requiredProperties.add(property);
  67. }
  68. /**
  69.  * Alternative way of initializing this filter.
  70.  * Used by Servlet Filter version that shipped with WebLogic 6.1.
  71.  * @param filterConfig the configuration for this filter
  72.  * @throws FatalBeanException wrapping a ServletException
  73.  * thrown by the init method
  74.  * @see #init
  75.  */
  76. public final void setFilterConfig(FilterConfig filterConfig) {
  77. try {
  78. init(filterConfig);
  79. }
  80. catch (ServletException ex) {
  81. throw new FatalBeanException("Couldn't initialize filter bean", ex);
  82. }
  83. }
  84. /**
  85.  * Map config parameters onto bean properties of this filter, and
  86.  * invoke subclass initialization.
  87.  * @param filterConfig the configuration for this filter
  88.  * @throws ServletException if bean properties are invalid (or required
  89.  * properties are missing), or if subclass initialization fails.
  90.  * @see #initFilterBean
  91.  */
  92. public final void init(FilterConfig filterConfig) throws ServletException {
  93. logger.info("Initializing filter '" + filterConfig.getFilterName() + "'");
  94. this.filterConfig = filterConfig;
  95. // set bean properties
  96. try {
  97. PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
  98. BeanWrapper bw = new BeanWrapperImpl(this);
  99. ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
  100. bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader));
  101. initBeanWrapper(bw);
  102. bw.setPropertyValues(pvs);
  103. }
  104. catch (BeansException ex) {
  105. String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage();
  106. logger.error(msg, ex);
  107. throw new ServletException(msg, ex);
  108. }
  109. // let subclasses do whatever initialization they like
  110. initFilterBean();
  111. logger.info("Filter '" + filterConfig.getFilterName() + "' configured successfully");
  112. }
  113. /**
  114.  * Initialize the BeanWrapper for this GenericFilterBean,
  115.  * possibly with custom editors.
  116.  * @param bw the BeanWrapper to initialize
  117.  * @throws BeansException if thrown by BeanWrapper methods
  118.  * @see org.springframework.beans.BeanWrapper#registerCustomEditor
  119.  */
  120. protected void initBeanWrapper(BeanWrapper bw) throws BeansException {
  121. }
  122. /**
  123.  * Make the FilterConfig of this filter available.
  124.  * Analogous to GenericServlet's getServletConfig.
  125.  * <p>Public to resemble the getFilterConfig method of the
  126.  * Servlet Filter version that shipped with WebLogic 6.1.
  127.  * @see javax.servlet.GenericServlet#getServletConfig
  128.  */
  129. public final FilterConfig getFilterConfig() {
  130. return this.filterConfig;
  131. }
  132. /**
  133.  * Make the name of this filter available to subclasses.
  134.  * Analogous to GenericServlet's getServletName.
  135.  * @see javax.servlet.GenericServlet#getServletName
  136.  */
  137. protected final String getFilterName() {
  138. return this.filterConfig.getFilterName();
  139. }
  140. /**
  141.  * Make the ServletContext of this filter available to subclasses.
  142.  * Analogous to GenericServlet's getServletContext.
  143.  * @see javax.servlet.GenericServlet#getServletContext
  144.  */
  145. protected final ServletContext getServletContext() {
  146. return this.filterConfig.getServletContext();
  147. }
  148. /**
  149.  * Subclasses may override this to perform custom initialization.
  150.  * All bean properties of this filter will have been set before this
  151.  * method is invoked. This default implementation does nothing.
  152.  * @throws ServletException if subclass initialization fails
  153.  */
  154. protected void initFilterBean() throws ServletException {
  155. }
  156. /**
  157.  * Subclasses may override this to perform custom filter shutdown.
  158.  * This default implementation does nothing.
  159.  */
  160. public void destroy() {
  161. }
  162. /**
  163.  * PropertyValues implementation created from FilterConfig init parameters.
  164.  */
  165. private static class FilterConfigPropertyValues extends MutablePropertyValues {
  166. /**
  167.  * Create new FilterConfigPropertyValues.
  168.  * @param config FilterConfig we'll use to take PropertyValues from
  169.  * @param requiredProperties set of property names we need, where
  170.  * we can't accept default values
  171.  * @throws ServletException if any required properties are missing
  172.  */
  173. private FilterConfigPropertyValues(FilterConfig config, Set requiredProperties) throws ServletException {
  174. Set missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ?
  175. new HashSet(requiredProperties) : null;
  176. Enumeration enum = config.getInitParameterNames();
  177. while (enum.hasMoreElements()) {
  178. String property = (String) enum.nextElement();
  179. Object value = config.getInitParameter(property);
  180. addPropertyValue(new PropertyValue(property, value));
  181. if (missingProps != null) {
  182. missingProps.remove(property);
  183. }
  184. }
  185. // fail if we are still missing properties
  186. if (missingProps != null && missingProps.size() > 0) {
  187. throw new ServletException("Initialization from FilterConfig for filter '" + config.getFilterName() +
  188.  "' failed; the following required properties were missing: " +
  189.  StringUtils.collectionToDelimitedString(missingProps, ", "));
  190. }
  191. }
  192. }
  193. }