Coverage details for org.chwf.servlet.filter.FilterSet

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.servlet.filter;
34  
35 import java.io.IOException;
36 import java.util.Collections;
37 import java.util.Iterator;
38 import java.util.LinkedList;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.WeakHashMap;
42  
43 import javax.servlet.Filter;
44 import javax.servlet.ServletException;
45 import javax.servlet.ServletResponse;
46 import javax.servlet.http.HttpServletRequest;
47  
48 import org.chwf.util.SequencedHashMap;
49  
50 /**
51  * Contains a set of filters.
52  *
53  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
54  */
55 public class FilterSet {
56  
57   /** Map for filter info. */
5833  private final Map filterInfoMap = new SequencedHashMap();
59  
60   /**
61    * Map for caching chains. Synchronized for thread-safety, weak references
62    * for more efficient memory usage.
63    */
6433  private final Map chains = Collections.synchronizedMap(new WeakHashMap());
65  
66   /**
67    * Constructor.
68    *
69    * @param allFilterConfig The configuration for all filters.
70    * @throws ServletException If any filter fails to be initialized.
71    */
7233  public FilterSet(AllFilterConfig allFilterConfig) throws ServletException {
73  
7433    Iterator names = allFilterConfig.getFilterNames();
75149    while (names.hasNext()) {
7683      String name = names.next().toString();
7783      FilterInfo info = new FilterInfo(name, allFilterConfig);
7883      getFilterInfoMap().put(name, info);
79     }
8033  }
81  
82   /**
83    * Run the chain for this request. When the chain is complete, the terminator
84    * filter is invoked. This terminator filter should invoke the resource at the
85    * end of the chain.
86    *
87    * @param request The request.
88    * @param response The response.
89    * @param terminator A filter that invokes the final resource invoked once
90    * the chain is complete.
91    * @throws ServletException For servlet errors.
92    * @throws IOException For I/O errors.
93    */
94   public void doChain(
95     HttpServletRequest request,
96     ServletResponse response,
97     Filter terminator)
98     throws IOException, ServletException {
996    String uri = request.getRequestURI();
1006    FilterChainImpl chain = (FilterChainImpl) chains.get(uri);
1016    if (chain == null) {
1026      chain = new FilterChainImpl(getFilters(request));
1036      chains.put(uri, chain);
104     }
1056    chain.doChain(request, response, terminator);
1066  }
107  
108   /** Destroy all filters. */
109   public void destroy() {
1104    Iterator filters = getFilterInfoMap().values().iterator();
11117    while (filters.hasNext()) {
1129      FilterInfo info = (FilterInfo) filters.next();
1139      info.getFilter().destroy();
114     }
1154  }
116  
117   /**
118    * Get the filter info map. This map contains {@link FilterInfo} objects
119    * indexed by the filter name.
120    *
121    * @return The filter info map.
122    */
123   protected Map getFilterInfoMap() {
124131    return filterInfoMap;
125   }
126  
127   /**
128    * Get the filters associated with a particular request. It matches against
129    * the uri-pattern of the filters, checking against the filters in the
130    * same order in which they appear in the configuration file. The values
131    * returned by this function are not cached, so it is the responsibility
132    * of the calling component to cache the filter chain.
133    *
134    * @param request The request.
135    * @return The list of filters.
136    */
137   private Filter[] getFilters(HttpServletRequest request) {
1386    String contextFreeURI = ResourceMapper.getContextFreeURI(request);
1396    List list = new LinkedList();
1406    Iterator filterInfos = getFilterInfoMap().values().iterator();
14126    while (filterInfos.hasNext()) {
14214      FilterInfo info = (FilterInfo) filterInfos.next();
14314      if (info.isMatch(contextFreeURI)) {
14410        list.add(info.getFilter());
145       }
146     }
1476    Filter[] filters = new Filter[list.size()];
1486    list.toArray(filters);
1496    return filters;
150   }
151 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.