How tomcat works 之第十一章之 FilterDef

来源:互联网 发布:剑三喵萝异瞳捏脸数据 编辑:程序博客网 时间:2024/04/30 03:59

org.apache.catalian.deploy.FilterDef类代表一个filter定义,就如在部署描述符中定义的filter元素。


package org.apache.catalina.deploy;
import java.util.HashMap;
import java.util.Map;

public final class FilterDef {
   /**
     * The description of this filter.
     */
   private String description = null;
   public String getDescription() {
      return (this.description);
   }
   public void setDescription(String description) {
      this.description = description;
   }

   /**
     * The display name of this filter.
     */
   private String displayName = null;
   public String getDisplayName() {
      return (this.displayName);
   }
   public void setDisplayName(String displayName) {
      this.displayName = displayName;
   }

   /**

     * The fully qualified name of the Java class that implements this
     * filter.
     */
   private String filterClass = null;
   public String getFilterClass() {
      return (this.filterClass);
   }
   public void setFilterclass(String filterClass) {
      this.filterClass = filterClass;
   }

   /**
     * The name of this filter, which must be unique among the filters
     * defined for a particular web application.
     */
   private String filterName = null;
public String getFilterName() {
   return (this.filterName);
}
public void setFilterName(String filterName) {
   this.filterName = filterName;
}

/**
  * The large icon associated with this filter.
  */
private String largeIcon = null;
public String getLargeIcon() {
   return (this.largeIcon);
}
public void setLargeIcon(String largeIcon) {
   this.largeIcon = largeIcon;
}

/**
  * The set of initialization parameters for this filter, keyed by
  * parameter name.
  */
private Map parameters = new HashMap();
public Map getParameterMap() {
   return (this.parameters);
}

/**
  * The small icon associated with this filter.
  */
private String smallIcon = null;
public String getSmallIcon() {
   return (this.smallIcon);
}
public void setSmallIcon(String smallIcon) {
   this.smallIcon = smallIcon;
}

public void addInitParameter(String name, String value) {
   parameters.put(name, value);

}
/**
  * Render a String representation of this object.

*/
   public String toString() {
      StringBuffer sb = new StringBuffer("FilterDef[");
      sb.append("filterName=");
      sb.append(this.filterName);
      sb.append(", filterClass=");
      sb.append(this.filterClass);
      sb.append("]");
      return (sb.toString());
   }
}



FilterDef类中的每个属性代表着会在filter元素中出现的子元素。这个类也包含了一个命名为parameters的Map代表一个Map,维护这个filter的所有初始化参数。addInitParameter方法增加name/value键值对。


ApplicationFilterConfig

此类实现了FilterConfig接口.ApplicationFilterConfig管理了过滤器实例,当web应用第一次启动的时候被创建了这些过滤器实例.

 

通过传递Context对象和FilterDef对象给ApplicationFilterConfig的构造函数来创建一个ApplicationFilterConfig对象.

 

publicApplicationFilterConfig(Context context, FilterDef filterDef) throwsClassCastException, ClassNotFoundException, IllegalAccessException,InstantiationException, ServletException

 

 

Context对象表示一个web应用,FilterDef对象表示一个filter对象.ApplicationFilterConfig类有getFilter方法来返回一个Filter对象.这个方法加载Filter类并实例化它.

 

Filter getFilter()throws ClassCastException, ClassNotFoundException, IllegalAccessException,InstantiationException, ServletException { // Return the existing filterinstance, if any if (this.filter != null) return (this.filter);

// Identify theclass loader we will be using

String filterClass= filterDef.getFilterClass(); ClassLoader classLoader = null; if(filterClass.startsWith("org.apache.catalina."))

 

classLoader =this.getClass().getClassLoader(); else classLoader =context.getLoader().getClassLoader(); ClassLoader oldCtxClassLoader = Thread.currentthread().getContextClassLoader();// Instantiate a new instance of this filter and return it Class clazz =classLoader.loadClass(filterClass); this.filter = (Filter) clazz.newInstance();filter.init(this); return (this.filter); }



0 0