craigmcc    00/10/22 02:55:54

  Modified:    catalina/src/share/org/apache/catalina/core
                        ApplicationFilterConfig.java
                        LocalStrings.properties StandardContext.java
                        StandardWrapperValve.java
  Added:       catalina/src/share/org/apache/catalina/core
                        ApplicationFilterChain.java
  Removed:     catalina/src/share/org/apache/catalina/core
                        ApplicationFilterWrapper.java
  Log:
  Update the filters implementation to reflect the revised filter API.
  Compared to the original model, the following changes are notable:
  * Single instance of a filter per filter definition (like servlets
    in that respect).
  * FilterChain objects represent the dynamic state of the handling of
    a particular request.
  * Filters are now initialzed at application startup and finalized at
    application shutdown (as well as around reloads).
  
  Revision  Changes    Path
  1.3       +49 -137   
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ApplicationFilterConfig.java
  
  Index: ApplicationFilterConfig.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ApplicationFilterConfig.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ApplicationFilterConfig.java      2000/08/23 01:07:10     1.2
  +++ ApplicationFilterConfig.java      2000/10/22 09:55:52     1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ApplicationFilterConfig.java,v
 1.2 2000/08/23 01:07:10 craigmcc Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/08/23 01:07:10 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ApplicationFilterConfig.java,v
 1.3 2000/10/22 09:55:52 craigmcc Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/10/22 09:55:52 $
    *
    * ====================================================================
    *
  @@ -73,17 +73,17 @@
   import javax.servlet.FilterConfig;
   import javax.servlet.ServletContext;
   import org.apache.catalina.Context;
  -import org.apache.catalina.Wrapper;
   import org.apache.catalina.deploy.FilterDef;
   import org.apache.catalina.util.Enumerator;
   
   
   /**
    * Implementation of a <code>javax.servlet.FilterConfig</code> useful in
  - * constructing stacks of filters for a particular request.
  + * managing the filter instances instantiated when a web application
  + * is first started.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.2 $ $Date: 2000/08/23 01:07:10 $
  + * @version $Revision: 1.3 $ $Date: 2000/10/22 09:55:52 $
    */
   
   final class ApplicationFilterConfig implements FilterConfig {
  @@ -96,9 +96,9 @@
        * Construct a new ApplicationFilterConfig for the specified filter
        * definition.
        *
  +     * @param context The context with which we are associated
        * @param filterDef Filter definition for which a FilterConfig is to be
        *  constructed
  -     * @param wrapper The Wrapper with which we are associated
        *
        * @exception ClassCastException if the specified class does not implement
        *  the <code>javax.servlet.Filter</code> interface
  @@ -108,12 +108,12 @@
        * @exception InstantiationException if an exception occurs while
        *  instantiating the filter object
        */
  -    public ApplicationFilterConfig(FilterDef filterDef, Wrapper wrapper)
  +    public ApplicationFilterConfig(Context context, FilterDef filterDef)
        throws ClassCastException, ClassNotFoundException,
               IllegalAccessException, InstantiationException {
   
        super();
  -     setWrapper(wrapper);
  +        this.context = context;
        setFilterDef(filterDef);
   
       }
  @@ -140,19 +140,6 @@
       private FilterDef filterDef = null;
   
   
  -    /**
  -     * The <code>ApplicationFilterConfig</code> of the next filter in our
  -     * configured filter stack.
  -     */
  -    private ApplicationFilterConfig nextConfig = null;
  -
  -
  -    /**
  -     * The Wrapper with which we are associated.
  -     */
  -    private Wrapper wrapper = null;
  -
  -
       // --------------------------------------------------- FilterConfig Methods
   
   
  @@ -167,30 +154,6 @@
   
   
       /**
  -     * Return the remaining Filter objects in the Filter stack, in the order
  -     * they have been configured.
  -     */
  -    public Iterator getFilters() {
  -
  -     // NOTE - The list of filters we are about to accumulate includes
  -     // the container-provided filter at the end that wraps the call to
  -     // the servlet's service() method
  -     ArrayList list = new ArrayList();
  -     ApplicationFilterConfig next = getNextConfig();
  -     while (next != null) {
  -         list.add(next.getFilter());
  -         next = next.getNextConfig();
  -     }
  -
  -     // NOTE - The iterator we are about to return probably does support
  -     // remove(), but calling it has no impact on the actual functionality
  -     // of filter processing so I don't see it as a big issue
  -     return (list.iterator());
  -
  -    }
  -
  -
  -    /**
        * Return a <code>String</code> containing the value of the named
        * initialization parameter, or <code>null</code> if the parameter
        * does not exist.
  @@ -224,19 +187,6 @@
   
   
       /**
  -     * Return the next Filter object in the filter stack.
  -     */
  -    public Filter getNext() {
  -
  -     if (nextConfig == null)
  -         return (null);
  -     else
  -         return (nextConfig.getFilter());
  -
  -    }
  -
  -
  -    /**
        * Return the ServletContext of our associated web application.
        */
       public ServletContext getServletContext() {
  @@ -252,8 +202,8 @@
       public String toString() {
   
        StringBuffer sb = new StringBuffer("ApplicationFilterConfig[");
  -     sb.append("wrapper=");
  -     sb.append(wrapper.getName());
  +        sb.append("name=");
  +        sb.append(filterDef.getFilterName());
        sb.append(", filterClass=");
        sb.append(filterDef.getFilterClass());
        sb.append("]");
  @@ -267,34 +217,60 @@
   
       /**
        * 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
        */
  -    Filter getFilter() {
  +    Filter getFilter() throws ClassCastException, ClassNotFoundException,
  +        IllegalAccessException, InstantiationException {
   
  +        // 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();
  +
  +        // Instantiate a new instance of this filter and return it
  +        Class clazz = classLoader.loadClass(filterClass);
  +        this.filter = (Filter) clazz.newInstance();
  +        filter.setFilterConfig(this);
        return (this.filter);
   
       }
   
   
       /**
  -     * Set the application Filter we are configured for.
  -     *
  -     * @param filter The new application Filter
  +     * Return the filter definition we are configured for.
        */
  -    void setFilter(Filter filter) {
  +    FilterDef getFilterDef() {
   
  -     this.filter = filter;
  +     return (this.filterDef);
   
       }
   
   
       /**
  -     * Return the filter definition we are configured for.
  +     * Release the Filter instance associated with this FilterConfig,
  +     * if there is one.
        */
  -    FilterDef getFilterDef() {
  +    void release() {
   
  -     return (this.filterDef);
  +        if (this.filter != null)
  +            filter.setFilterConfig(null);
  +        this.filter = null;
   
  -    }
  +     }
   
   
       /**
  @@ -325,74 +301,10 @@
   
        } else {
   
  -         // Identify the class loader we will be using
  -         String filterClass = filterDef.getFilterClass();
  -         ClassLoader classLoader = null;
  -         // FIXME - share this test with StandardWrapper somehow
  -         if (filterClass.startsWith("org.apache.catalina."))
  -             classLoader = this.getClass().getClassLoader();
  -         else
  -             classLoader = context.getLoader().getClassLoader();
  -
  -         // Instantiate a new instance of this filter
  -         Class clazz = classLoader.loadClass(filterClass);
  -         this.filter = (Filter) clazz.newInstance();
  -         filter.setFilterConfig(this);
  -     }
  -
  -    }
  +            // Allocate a new filter instance
  +            Filter filter = getFilter();
   
  -
  -    /**
  -     * Return the <code>ApplicationFilterConfig</code> of the next filter
  -     * in our filter stack.
  -     */
  -    ApplicationFilterConfig getNextConfig() {
  -
  -     return (nextConfig);
  -
  -    }
  -
  -
  -    /**
  -     * Set the <code>ApplicationFilterConfig</code> of the next filter
  -     * in our filter stack.
  -     *
  -     * @param nextConfig The next filter configuration object
  -     */
  -    void setNextConfig(ApplicationFilterConfig nextConfig) {
  -
  -     this.nextConfig = nextConfig;
  -     if (nextConfig == null) {
  -         this.filter = null;
  -         this.context = null;
        }
  -
  -    }
  -
  -
  -    /**
  -     * Return the Wrapper we are configured for.
  -     */
  -    Wrapper getWrapper() {
  -
  -     return (this.wrapper);
  -
  -    }
  -
  -
  -    /**
  -     * Set the Wrapper we are configured for.
  -     *
  -     * @param wrapper The new Wrapper
  -     */
  -    void setWrapper(Wrapper wrapper) {
  -
  -     this.wrapper = wrapper;
  -     if (this.wrapper == null)
  -         this.context = null;
  -     else
  -         this.context = (Context) this.wrapper.getParent();
   
       }
   
  
  
  
  1.17      +3 -0      
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/LocalStrings.properties,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- LocalStrings.properties   2000/10/14 21:43:54     1.16
  +++ LocalStrings.properties   2000/10/22 09:55:52     1.17
  @@ -15,6 +15,8 @@
   containerBase.alreadyStarted=Container has already been started
   containerBase.notConfigured=No basic Valve has been configured
   containerBase.notStarted=Container has not been started
  +filterChain.filter=Filter execution threw an exception
  +filterChain.servlet=Servlet execution threw an exception
   httpContextMapper.container=This container is not a StandardContext
   httpEngineMapper.container=This container is not a StandardEngine
   httpHostMapper.container=This container is not a StandardHost
  @@ -26,6 +28,7 @@
   standardContext.filterMap.either=Filter mapping must specify either a <url-pattern> 
or a <servlet-name>
   standardContext.filterMap.name=Filter mapping specifies an unknown filter name {0}
   standardContext.filterMap.pattern=Invalid <url-pattern> {0} in filter mapping
  +standardContext.filterStart=Exception starting filter {0}
   standardContext.isUnavailable=This application is not currently available
   standardContext.listenerStart=Exception sending context initialized event to 
listener instance of class {0}
   standardContext.listenerStop=Exception sending context destroyed event to listener 
instance of class {0}
  
  
  
  1.23      +63 -6     
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardContext.java
  
  Index: StandardContext.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardContext.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- StandardContext.java      2000/10/21 13:33:12     1.22
  +++ StandardContext.java      2000/10/22 09:55:52     1.23
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardContext.java,v
 1.22 2000/10/21 13:33:12 craigmcc Exp $
  - * $Revision: 1.22 $
  - * $Date: 2000/10/21 13:33:12 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardContext.java,v
 1.23 2000/10/22 09:55:52 craigmcc Exp $
  + * $Revision: 1.23 $
  + * $Date: 2000/10/22 09:55:52 $
    *
    * ====================================================================
    *
  @@ -71,6 +71,7 @@
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.TreeMap;
  +import javax.servlet.FilterConfig;
   import javax.servlet.ServletContext;
   import javax.servlet.ServletContextEvent;
   import javax.servlet.ServletContextListener;
  @@ -113,7 +114,7 @@
    * requests directed to a particular servlet.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.22 $ $Date: 2000/10/21 13:33:12 $
  + * @version $Revision: 1.23 $ $Date: 2000/10/22 09:55:52 $
    */
   
   public final class StandardContext
  @@ -241,6 +242,13 @@
   
   
       /**
  +     * The set of filter configurations (and associated filter instances) we
  +     * have initialized, keyed by filter name.
  +     */
  +    private HashMap filterConfigs = new HashMap();
  +
  +
  +    /**
        * The set of filter definitions for this application, keyed by
        * filter name.
        */
  @@ -2455,7 +2463,28 @@
           if (debug >= 1)
               log("Starting filters");
   
  -        return (true);  // No actions currently required
  +        // Instantiate and record a FilterConfig for each defined filter
  +        boolean ok = true;
  +        synchronized (filterConfigs) {
  +            filterConfigs.clear();
  +            Iterator names = filterDefs.keySet().iterator();
  +            while (names.hasNext()) {
  +                String name = (String) names.next();
  +                if (debug >= 1)
  +                    log(" Starting filter '" + name + "'");
  +                ApplicationFilterConfig filterConfig = null;
  +                try {
  +                    filterConfig = new ApplicationFilterConfig
  +                      (this, (FilterDef) filterDefs.get(name));
  +                    filterConfigs.put(name, filterConfig);
  +                } catch (Throwable t) {
  +                    log(sm.getString("standardContext.filterStart", name), t);
  +                    ok = false;
  +                }
  +            }
  +        }
  +
  +        return (ok);
   
       }
   
  @@ -2469,8 +2498,36 @@
   
           if (debug >= 1)
               log("Stopping filters");
  +
  +        // Release all Filter and FilterConfig instances
  +        synchronized (filterConfigs) {
  +            Iterator names = filterConfigs.keySet().iterator();
  +            while (names.hasNext()) {
  +                String name = (String) names.next();
  +                if (debug >= 1)
  +                    log(" Stopping filter '" + name + "'");
  +                ApplicationFilterConfig filterConfig =
  +                  (ApplicationFilterConfig) filterConfigs.get(name);
  +                filterConfig.release();
  +            }
  +            filterConfigs.clear();
  +        }
  +        return (true);
  +
  +    }
   
  -        return (true);  // No actions currently required
  +
  +    /**
  +     * Find and return the initialized <code>FilterConfig</code> for the
  +     * specified filter name, if any; otherwise return <code>null</code>.
  +     *
  +     * @param name Name of the desired filter
  +     */
  +    public FilterConfig findFilterConfig(String name) {
  +
  +        synchronized (filterConfigs) {
  +            return ((FilterConfig) filterConfigs.get(name));
  +        }
   
       }
   
  
  
  
  1.13      +117 -171  
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardWrapperValve.java
  
  Index: StandardWrapperValve.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardWrapperValve.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- StandardWrapperValve.java 2000/10/18 19:20:40     1.12
  +++ StandardWrapperValve.java 2000/10/22 09:55:52     1.13
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardWrapperValve.java,v
 1.12 2000/10/18 19:20:40 craigmcc Exp $
  - * $Revision: 1.12 $
  - * $Date: 2000/10/18 19:20:40 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardWrapperValve.java,v
 1.13 2000/10/22 09:55:52 craigmcc Exp $
  + * $Revision: 1.13 $
  + * $Date: 2000/10/22 09:55:52 $
    *
    * ====================================================================
    *
  @@ -67,7 +67,6 @@
   
   import java.io.IOException;
   import java.io.PrintWriter;
  -import javax.servlet.Filter;
   import javax.servlet.FilterConfig;
   import javax.servlet.RequestDispatcher;
   import javax.servlet.Servlet;
  @@ -103,7 +102,7 @@
    * <code>StandardWrapper</code> container implementation.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.12 $ $Date: 2000/10/18 19:20:40 $
  + * @version $Revision: 1.13 $ $Date: 2000/10/22 09:55:52 $
    */
   
   final class StandardWrapperValve
  @@ -243,34 +242,17 @@
            servlet = null;
        }
   
  -     // Create the filter stack for this request
  -     ApplicationFilterConfig filters = null;
  -     try {
  -         if (servlet != null) {
  -             filters = createFilters(request, servlet);
  -         }
  -     } catch (Throwable e) {
  -         log(sm.getString("standardWrapper.createFilters",
  -                          wrapper.getName()), e);
  -         throwable = e;
  -         exception(request, response, e);
  -         servlet = null;
  -     }
  +     // Create the filter chain for this request
  +        ApplicationFilterChain filterChain =
  +          createFilterChain(request, servlet);
   
  -     // Call the filter stack for this request
  +     // Call the filter chain for this request
  +        // NOTE: This also calls the servlet's service() method
        try {
  -         if (servlet != null) {
  +         if ((servlet != null) && (filterChain != null)) {
                support.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,
                                          servlet);
  -             filters.getFilter().doFilter(sreq, sres);
  -             /*
  -             if ((servlet instanceof HttpServlet) &&
  -                 (hreq != null) && (hres != null)) {
  -                 ((HttpServlet) servlet).service(hreq, hres);
  -             } else {
  -                 servlet.service(sreq, sres);
  -             }
  -             */
  +                filterChain.doFilter(sreq, sres);
                support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                          servlet);
            }
  @@ -305,9 +287,10 @@
            exception(request, response, e);
        }
   
  -     // Release the filter stack (if any) for this request
  +     // Release the filter chain (if any) for this request
        try {
  -         releaseFilters(request, filters);
  +            if (filterChain != null)
  +                filterChain.release();
        } catch (Throwable e) {
            log(sm.getString("standardWrapper.releaseFilters",
                             wrapper.getName()), e);
  @@ -344,122 +327,108 @@
   
   
       /**
  -     * Construct and return a linked list of <code>FilterConfig</code>
  -     * objects, and their associated <code>Filters</code>, that are relevant
  -     * for this request.  A container-supplied filter to call the servlet
  -     * method will always be appended, so this method is guaranteed to
  -     * return at least one filter.
  +     * Construct and return a FilterChain implementation that will wrap the
  +     * execution of the specified servlet instance.  If we should not execute
  +     * a filter chain at all, return <code>null</code>.
        * <p>
  -     * <strong>IMPLEMENTATION NOTE</strong>:  This initial implementation is
  -     * not at all optimized, because it dynamically instantiates the relevant
  -     * filter instances every time, with no pooling.  Functionality first,
  -     * then optimization.
  +     * <strong>FIXME</strong> - Pool the chain instances!
        *
  -     * @param request The request we are processing
  -     * @param servlet The servlet we are wrapping
  -     *
  -     * @exception Exception if an exception occurs while creating the
  -     *  filter list
  +     * @param request The servlet request we are processing
  +     * @param servlet The servlet instance to be wrapped
        */
  -    private ApplicationFilterConfig createFilters(Request request,
  -                                               Servlet servlet)
  -     throws Exception {
  -
  -     // Acquire the filter mappings for this Context
  -     Wrapper wrapper = (Wrapper) getContainer();
  -     Context context = (Context) wrapper.getParent();
  -     FilterMap filterMaps[] = context.findFilterMaps();
  -
  -     // Acquire the information we will need to match filter mappings
  -     String requestPath = null;
  -     if (request instanceof HttpRequest) {
  -         HttpServletRequest hreq =
  -             (HttpServletRequest) request.getRequest();
  -         String contextPath = hreq.getContextPath();
  -         if (contextPath == null)
  -             contextPath = "";
  -         String requestURI = hreq.getRequestURI();
  -         if (requestURI.length() >= contextPath.length())
  -             requestPath = requestURI.substring(contextPath.length());
  -     }
  -     String servletName = wrapper.getName();
  +    private ApplicationFilterChain createFilterChain(Request request,
  +                                                     Servlet servlet) {
   
  -     if (debug >= 1)
  -         log("Creating filter stack for request path '" + requestPath +
  -             "' and servlet '" + servlet + "'");
  +        // If there is no servlet to execute, return null
  +        if (servlet == null)
  +            return (null);
  +
  +        // Create and initialize a filter chain object
  +        ApplicationFilterChain filterChain =
  +          new ApplicationFilterChain();
  +        filterChain.setServlet(servlet);
  +
  +        // Acquire the filter mappings for this Context
  +        Wrapper wrapper = (Wrapper) getContainer();
  +        StandardContext context = (StandardContext) wrapper.getParent();
  +        FilterMap filterMaps[] = context.findFilterMaps();
  +
  +        // If there are no filter mappings, we are done
  +        if ((filterMaps == null) || (filterMaps.length == 0))
  +            return (filterChain);
  +//        if (debug >= 1)
  +//            log("createFilterChain:  Processing " + filterMaps.length +
  +//                " filter map entries");
  +
  +        // Acquire the information we will need to match filter mappings
  +        String requestPath = null;
  +        if (request instanceof HttpRequest) {
  +            HttpServletRequest hreq =
  +                (HttpServletRequest) request.getRequest();
  +            String contextPath = hreq.getContextPath();
  +            if (contextPath == null)
  +                contextPath = "";
  +            String requestURI = hreq.getRequestURI();
  +            if (requestURI.length() >= contextPath.length())
  +                requestPath = requestURI.substring(contextPath.length());
  +        }
  +        String servletName = wrapper.getName();
  +//        if (debug >= 1) {
  +//            log(" requestPath=" + requestPath);
  +//            log(" servletName=" + servletName);
  +//        }
  +        int n = 0;
  +
  +        // Add the relevant path-mapped filters to this filter chain
  +        for (int i = 0; i < filterMaps.length; i++) {
  +//            if (debug >= 2)
  +//                log(" Checking path-mapped filter '" +
  +//                    filterMaps[i] + "'");
  +            if (!matchFiltersURL(filterMaps[i], requestPath))
  +                continue;
  +            ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
  +                context.findFilterConfig(filterMaps[i].getFilterName());
  +            if (filterConfig == null) {
  +//                if (debug >= 2)
  +//                    log(" Missing path-mapped filter '" +
  +//                        filterMaps[i] + "'");
  +                ;       // FIXME - log configuration problem
  +                continue;
  +            }
  +//            if (debug >= 2)
  +//                log(" Adding path-mapped filter '" +
  +//                    filterConfig.getFilterName() + "'");
  +            filterChain.addFilter(filterConfig);
  +            n++;
  +        }
   
  -     // Create a stack of the relevant filters only
  -     int n = 0;
  -     ApplicationFilterConfig first = null;
  -     ApplicationFilterConfig last = null;
  -     ApplicationFilterConfig next = null;
  -
  -     // Add filters that match on URL pattern first
  -     for (int i = 0; i < filterMaps.length; i++) {
  -         if (!matchFiltersURL(filterMaps[i], requestPath))
  -             continue;
  -         FilterDef filterDef =
  -             context.findFilterDef(filterMaps[i].getFilterName());
  -         if (filterDef == null) {
  -             ;       // FIXME - log configuration problem
  -             continue;
  -         }
  -         if (debug >= 2)
  -             log(" Adding filter '" + filterDef.getFilterName() + "'");
  -         next = new ApplicationFilterConfig(filterDef, wrapper);
  -         if (first == null)
  -             first = next;
  -         if (last != null)
  -             last.setNextConfig(next);
  -         last = next;
  -         n++;
  -     }
  -
  -     // Add filters that match on servlet name second
  -     for (int i = 0; i < filterMaps.length; i++) {
  -         if (!matchFiltersServlet(filterMaps[i], servletName))
  -             continue;
  -         FilterDef filterDef =
  -             context.findFilterDef(filterMaps[i].getFilterName());
  -         if (filterDef == null) {
  -             ;       // FIXME - log configuration problem
  -             continue;
  -         }
  -         if (debug >= 2)
  -             log(" Adding filter '" + filterDef.getFilterName() + "'");
  -         next = new ApplicationFilterConfig(filterDef, wrapper);
  -         if (first == null)
  -             first = next;
  -         if (last != null)
  -             last.setNextConfig(next);
  -         last = next;
  -         n++;
  -     }
  -
  -     // Add an internal filter to call the servlet itself third
  -     if (debug >= 2)
  -         log(" Adding container-provided wrapper filter");
  -     if (this.filterDef == null) {
  -         FilterDef newDef = new FilterDef();
  -         newDef.setFilterClass("org.apache.catalina.core.ApplicationFilterWrapper");
  -         newDef.setFilterName("org.apache.catalina.core.ApplicationFilterWrapper");
  -         this.filterDef = newDef;
  -     }
  -     next = new ApplicationFilterConfig(this.filterDef, wrapper);
  -     ApplicationFilterWrapper filter =
  -         (ApplicationFilterWrapper) next.getFilter();
  -     filter.setServlet(servlet);
  -     if (first == null)
  -         first = next;
  -     if (last != null)
  -         last.setNextConfig(next);
  -     last = next;
  -     n++;
  -     if (debug >= 1)
  -         log(" Total of " + n + " filters configured");
  +        // Add filters that match on servlet name second
  +        for (int i = 0; i < filterMaps.length; i++) {
  +//            if (debug >= 2)
  +//                log(" Checking servlet-mapped filter '" +
  +//                    filterMaps[i] + "'");
  +            if (!matchFiltersServlet(filterMaps[i], servletName))
  +                continue;
  +            ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
  +                context.findFilterConfig(filterMaps[i].getFilterName());
  +            if (filterConfig == null) {
  +//                if (debug >= 2)
  +//                    log(" Missing servlet-mapped filter '" +
  +//                        filterMaps[i] + "'");
  +                ;       // FIXME - log configuration problem
  +                continue;
  +            }
  +//            if (debug >= 2)
  +//                log(" Adding servlet-mapped filter '" +
  +//                     filterMaps[i] + "'");
  +            filterChain.addFilter(filterConfig);
  +            n++;
  +        }
   
  -     // Return the constructed filter chain
  -     return (first);
  +        // Return the completed filter chain
  +//        if (debug >= 2)
  +//            log(" Returning chain with " + n + " filters");
  +        return (filterChain);
   
       }
   
  @@ -716,9 +685,9 @@
       private boolean matchFiltersServlet(FilterMap filterMap,
                                        String servletName) {
   
  -     if (debug >= 3)
  -         log("  Matching servlet name '" + servletName +
  -             "' against mapping " + filterMap);
  +//   if (debug >= 3)
  +//       log("  Matching servlet name '" + servletName +
  +//           "' against mapping " + filterMap);
   
        if (servletName == null)
            return (false);
  @@ -739,9 +708,9 @@
       private boolean matchFiltersURL(FilterMap filterMap,
                                    String requestPath) {
   
  -     if (debug >= 3)
  -         log("  Matching request path '" + requestPath +
  -             "' against mapping " + filterMap);
  +//   if (debug >= 3)
  +//       log("  Matching request path '" + requestPath +
  +//           "' against mapping " + filterMap);
   
        if (requestPath == null)
            return (false);
  @@ -780,7 +749,6 @@
                                        requestPath.substring(period + 1)));
        }
   
  -
        // Case 4 - "Default" Match
        return (false); // NOTE - Not relevant for selecting filters
   
  @@ -788,28 +756,6 @@
   
   
       /**
  -     * Release the specified filter stack associated with this request.
  -     *
  -     * @param request The request being processed
  -     * @param filters The filter stack being released
  -     *
  -     * @exception Exception if an exception occurs during the release
  -     */
  -    private void releaseFilters(Request request,
  -                             ApplicationFilterConfig filters)
  -     throws Exception {
  -
  -     while (filters != null) {
  -         filters.getFilter().setFilterConfig(null);
  -         filters.setFilter(null);
  -         ApplicationFilterConfig next = filters.getNextConfig();
  -         filters.setNextConfig(null);
  -         filters = next;
  -     }
  -
  -    }
  -
  -    /**
        * Handle the HTTP status code (and corresponding message) generated
        * while processing the specified Request to produce the specified
        * Response.  Any exceptions that occur during generation of the error
  @@ -899,7 +845,7 @@
               if (debug >= 1)
                   log("status.write", e);
        }
  -        
  +
   
       }
   
  
  
  
  1.1                  
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ApplicationFilterChain.java
  
  Index: ApplicationFilterChain.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ApplicationFilterChain.java,v
 1.1 2000/10/22 09:55:51 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/10/22 09:55:51 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.core;
  
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Iterator;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.Servlet;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.catalina.util.StringManager;
  
  
  /**
   * Implementation of <code>javax.servlet.FilterChain</code> used to manage
   * the execution of a set of filters for a particular request.  When the
   * set of defined filters has all been executed, the next call to
   * <code>doFilter()</code> will execute the servlet's <code>service()</code>
   * method itself.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/10/22 09:55:51 $
   */
  
  final class ApplicationFilterChain implements FilterChain {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a new chain instance with no defined filters.
       */
      public ApplicationFilterChain() {
  
          super();
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The set of filters that will be executed on this chain.
       */
      private ArrayList filters = new ArrayList();
  
  
      /**
       * The iterator that is used to maintain the current position in the filter 
chain.
       * This iterator is called the first time that <code>doFilter()</code>
       * is called.
       */
      private Iterator iterator = null;
  
  
      /**
       * The servlet instance to be executed by this chain.
       */
      private Servlet servlet = null;
  
  
      /**
       * The string manager for our package.
       */
      private static final StringManager sm =
        StringManager.getManager(Constants.Package);
  
  
      // ---------------------------------------------------- FilterChain Methods
  
  
      /**
       * Invoke the next filter in this chain, passing the specified request
       * and response.
       *
       * @param request The servlet request we are processing
       * @param response The servlet response we are creating
       *
       * @exception IOException if an input/output error occurs
       * @exception ServletException if a servlet exception occurs
       */
      public void doFilter(ServletRequest request, ServletResponse response)
          throws IOException, ServletException {
  
          // Construct an iterator the first time this method is called
          if (this.iterator == null)
              this.iterator = filters.iterator();
  
          // Call the next filter if there is one
          if (this.iterator.hasNext()) {
              ApplicationFilterConfig filterConfig =
                (ApplicationFilterConfig) iterator.next();
              try {
                  filterConfig.getFilter().doFilter(request, response, this);
              } catch (IOException e) {
                  throw e;
              } catch (ServletException e) {
                  throw e;
              } catch (Throwable e) {
                  throw new ServletException
                    (sm.getString("filterChain.filter"), e);
              }
              return;
          }
  
          // We fell off the end of the chain -- call the servlet instance
          try {
              if ((request instanceof HttpServletRequest) &&
                  (response instanceof HttpServletResponse))
                  servlet.service((HttpServletRequest) request,
                                  (HttpServletResponse) response);
              else
                  servlet.service(request, response);
          } catch (IOException e) {
              throw e;
          } catch (ServletException e) {
              throw e;
          } catch (Throwable e) {
              throw new ServletException
                (sm.getString("filterChain.servlet"), e);
          }
  
      }
  
  
      // -------------------------------------------------------- Package Methods
  
  
  
      /**
       * Add a filter to the set of filters that will be executed in this chain.
       *
       * @param filterConfig The FilterConfig for the servlet to be executed
       */
      void addFilter(ApplicationFilterConfig filterConfig) {
  
          this.filters.add(filterConfig);
  
      }
  
  
      /**
       * Release references to the filters and wrapper executed by this chain.
       */
      void release() {
  
          this.filters.clear();
          this.iterator = iterator;
          this.servlet = null;
  
      }
  
  
      /**
       * Set the servlet that will be executed at the end of this chain.
       *
       * @param wrapper The Wrapper for the servlet to be executed
       */
      void setServlet(Servlet servlet) {
  
          this.servlet = servlet;
  
      }
  
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to