/**
   * Return the application Filter we are configured for.
   *
   * @exception ClassCastException if the specified class does not implement the <code>
   *     javax.servlet.Filter</code> interface
   * @exception ClassNotFoundException if the filter class cannot be found
   * @exception IllegalAccessException if the filter class cannot be publicly instantiated
   * @exception InstantiationException if an exception occurs while instantiating the filter object
   * @exception ServletException if thrown by the filter's init() method
   */
  Filter getFilter()
      throws ClassCastException, ClassNotFoundException, IllegalAccessException,
          InstantiationException, ServletException {

    // Return the existing filter instance, if any
    if (this.filter != null) return (this.filter);

    // Identify the class 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();
    if (context instanceof StandardContext && ((StandardContext) context).getSwallowOutput()) {
      try {
        SystemLogHandler.startCapture();
        filter.init(this);
      } finally {
        String log = SystemLogHandler.stopCapture();
        if (log != null && log.length() > 0) {
          getServletContext().log(log);
        }
      }
    } else {
      filter.init(this);
    }
    return (this.filter);
  }
  private void initFilter() throws ServletException {
    if (context instanceof StandardContext && context.getSwallowOutput()) {
      try {
        SystemLogHandler.startCapture();
        filter.init(this);
      } finally {
        String capturedlog = SystemLogHandler.stopCapture();
        if (capturedlog != null && capturedlog.length() > 0) {
          getServletContext().log(capturedlog);
        }
      }
    } else {
      filter.init(this);
    }

    // Expose filter via JMX
    registerJMX();
  }