1 16 17 package org.springframework.web.filter; 18 19 import java.util.Enumeration ; 20 import java.util.HashSet ; 21 import java.util.Set ; 22 23 import javax.servlet.Filter ; 24 import javax.servlet.FilterConfig ; 25 import javax.servlet.ServletContext ; 26 import javax.servlet.ServletException ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.springframework.beans.BeanWrapper; 32 import org.springframework.beans.BeanWrapperImpl; 33 import org.springframework.beans.BeansException; 34 import org.springframework.beans.MutablePropertyValues; 35 import org.springframework.beans.PropertyValue; 36 import org.springframework.beans.PropertyValues; 37 import org.springframework.beans.factory.BeanInitializationException; 38 import org.springframework.beans.factory.BeanNameAware; 39 import org.springframework.beans.factory.DisposableBean; 40 import org.springframework.beans.factory.InitializingBean; 41 import org.springframework.core.io.Resource; 42 import org.springframework.core.io.ResourceEditor; 43 import org.springframework.core.io.ResourceLoader; 44 import org.springframework.util.Assert; 45 import org.springframework.util.StringUtils; 46 import org.springframework.web.context.ServletContextAware; 47 import org.springframework.web.context.support.ServletContextResourceLoader; 48 import org.springframework.web.util.NestedServletException; 49 50 72 public abstract class GenericFilterBean implements 73 Filter , BeanNameAware, ServletContextAware, InitializingBean, DisposableBean { 74 75 76 protected final Log logger = LogFactory.getLog(getClass()); 77 78 82 private final Set requiredProperties = new HashSet (); 83 84 85 private FilterConfig filterConfig; 86 87 88 private String beanName; 89 90 private ServletContext servletContext; 91 92 93 100 public final void setBeanName(String beanName) { 101 this.beanName = beanName; 102 } 103 104 111 public final void setServletContext(ServletContext servletContext) { 112 this.servletContext = servletContext; 113 } 114 115 123 public void afterPropertiesSet() throws ServletException { 124 initFilterBean(); 125 } 126 127 128 137 protected final void addRequiredProperty(String property) { 138 this.requiredProperties.add(property); 139 } 140 141 150 public final void init(FilterConfig filterConfig) throws ServletException { 151 Assert.notNull(filterConfig, "FilterConfig must not be null"); 152 if (logger.isDebugEnabled()) { 153 logger.debug("Initializing filter '" + filterConfig.getFilterName() + "'"); 154 } 155 156 this.filterConfig = filterConfig; 157 158 try { 160 PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties); 161 BeanWrapper bw = new BeanWrapperImpl(this); 162 ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext()); 163 bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader)); 164 initBeanWrapper(bw); 165 bw.setPropertyValues(pvs, true); 166 } 167 catch (BeansException ex) { 168 String msg = "Failed to set bean properties on filter '" + 169 filterConfig.getFilterName() + "': " + ex.getMessage(); 170 logger.error(msg, ex); 171 throw new NestedServletException(msg, ex); 172 } 173 174 initFilterBean(); 176 177 if (logger.isDebugEnabled()) { 178 logger.debug("Filter '" + filterConfig.getFilterName() + "' configured successfully"); 179 } 180 } 181 182 191 public final void setFilterConfig(FilterConfig filterConfig) { 192 try { 193 init(filterConfig); 194 } 195 catch (ServletException ex) { 196 throw new BeanInitializationException("Could not initialize filter bean", ex); 197 } 198 } 199 200 208 protected void initBeanWrapper(BeanWrapper bw) throws BeansException { 209 } 210 211 212 220 public final FilterConfig getFilterConfig() { 221 return this.filterConfig; 222 } 223 224 235 protected final String getFilterName() { 236 return (this.filterConfig != null ? this.filterConfig.getFilterName() : this.beanName); 237 } 238 239 250 protected final ServletContext getServletContext() { 251 return (this.filterConfig != null ? this.filterConfig.getServletContext() : this.servletContext); 252 } 253 254 255 267 protected void initFilterBean() throws ServletException { 268 } 269 270 276 public void destroy() { 277 } 278 279 280 283 private static class FilterConfigPropertyValues extends MutablePropertyValues { 284 285 292 public FilterConfigPropertyValues(FilterConfig config, Set requiredProperties) 293 throws ServletException { 294 295 Set missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ? 296 new HashSet (requiredProperties) : null; 297 298 Enumeration en = config.getInitParameterNames(); 299 while (en.hasMoreElements()) { 300 String property = (String ) en.nextElement(); 301 Object value = config.getInitParameter(property); 302 addPropertyValue(new PropertyValue(property, value)); 303 if (missingProps != null) { 304 missingProps.remove(property); 305 } 306 } 307 308 if (missingProps != null && missingProps.size() > 0) { 310 throw new ServletException ( 311 "Initialization from FilterConfig for filter '" + config.getFilterName() + 312 "' failed; the following required properties were missing: " + 313 StringUtils.collectionToDelimitedString(missingProps, ", ")); 314 } 315 } 316 } 317 318 } 319 | Popular Tags |