Coverage details for org.chwf.servlet.engine.InitFilter

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.engine;
34  
35 import java.io.IOException;
36  
37 import javax.servlet.FilterChain;
38 import javax.servlet.FilterConfig;
39 import javax.servlet.ServletContext;
40 import javax.servlet.ServletException;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43  
44 import org.chwf.resources.ResourceException;
45 import org.chwf.resources.ResourceManager;
46 import org.chwf.servlet.ServletData;
47 import org.chwf.servlet.ServletUtils;
48 import org.chwf.servlet.filter.FilterSupport;
49  
50 /**
51  * A filter that initializes the Chrysalis environment. It invokes init()
52  * during pre-processing and release() during post-processing, with guard
53  * methods to ensure that initialization only happens once per request
54  * (in case this filter appears more than once in the chain).
55  *
56  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
57  */
5814public class InitFilter extends FilterSupport {
59  
60   /** The context. */
61   private ServletContext context;
62  
63   /**
64    * Stores config.
65    *
66    * @param config The config.
67    * @throws ServletException For servlet exceptions.
68    */
69   public void init(FilterConfig config) throws ServletException {
7014    this.context = config.getServletContext();
7114  }
72  
73   /**
74    * Process filter.
75    *
76    * @param request The request.
77    * @param response The response.
78    * @param chain The filter chain.
79    * @throws ServletException For servlet exceptions.
80    * @throws IOException For IO exceptions.
81    */
82   public void doHttpFilter(
83     HttpServletRequest request,
84     HttpServletResponse response,
85     FilterChain chain)
86     throws IOException, ServletException {
87  
88     // In case this method is invoked more than once in the request chain:
898    boolean isTopOfChain = !ServletData.isInitialized();
90  
91     try {
928      if (isTopOfChain) {
938        init(context, request, response);
94       }
95       
968      chain.doFilter(request, response);
977    } finally {
981      if (isTopOfChain) {
998        release(request);
100       }
101     }
1027  }
103  
104   /**
105    * Initialize the Chrysalis environment.
106    *
107    * @param context The servlet context.
108    * @param request The request.
109    * @param response The response.
110    */
111   public static void init(
112     ServletContext context,
113     HttpServletRequest request,
114     HttpServletResponse response) {
115  
116464    ServletData.init(context, request, response);
117464    ServletData.retrieveErrorDataFromSession();
118464    ResourceManager.init();
119464  }
120  
121   /**
122    * Release the Chrysalis environment.
123    *
124    * @param request The request.
125    * @throws ServletException For release errors.
126    */
127   public static void release(HttpServletRequest request)
128     throws ServletException {
129464    Throwable error = ServletUtils.retrieveError(request);
130     try {
131464      ResourceManager.release(error);
132464    } catch (ResourceException ex) {
1330      throw new ServletException(ex);
134     } finally {
1350      ServletData.release();
136     }
137464  }
138 }

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.