KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > DispatchFilterChain


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.webapp;
30
31 import com.caucho.log.Log;
32
33 import javax.servlet.FilterChain JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.ServletRequest JavaDoc;
36 import javax.servlet.ServletRequestEvent JavaDoc;
37 import javax.servlet.ServletRequestListener JavaDoc;
38 import javax.servlet.ServletResponse JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Represents the next filter in a filter chain. The final filter will
44  * be the servlet itself.
45  */

46 public class DispatchFilterChain implements FilterChain JavaDoc {
47   private static final Logger JavaDoc log = Log.open(DispatchFilterChain.class);
48   
49   // Next filter chain
50
private FilterChain JavaDoc _next;
51
52   // app
53
private WebApp _app;
54   // class loader
55
private ClassLoader JavaDoc _classLoader;
56
57   private ServletRequestListener JavaDoc []_requestListeners;
58   
59   /**
60    * Creates a new FilterChainFilter.
61    *
62    * @param next the next filterChain
63    * @param app the webApp
64    */

65   public DispatchFilterChain(FilterChain JavaDoc next, WebApp app)
66   {
67     _next = next;
68     _app = app;
69     _classLoader = app.getClassLoader();
70     _requestListeners = app.getRequestListeners();
71   }
72   
73   /**
74    * Invokes the next filter in the chain or the final servlet at
75    * the end of the chain.
76    *
77    * @param request the servlet request
78    * @param response the servlet response
79    * @since Servlet 2.3
80    */

81   public void doFilter(ServletRequest JavaDoc request,
82                        ServletResponse JavaDoc response)
83     throws ServletException JavaDoc, IOException JavaDoc
84   {
85     Thread JavaDoc thread = Thread.currentThread();
86     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
87     
88     try {
89       thread.setContextClassLoader(_classLoader);
90
91       for (int i = 0; i < _requestListeners.length; i++) {
92     ServletRequestEvent JavaDoc event = new ServletRequestEvent JavaDoc(_app, request);
93     
94     _requestListeners[i].requestInitialized(event);
95       }
96       
97       _next.doFilter(request, response);
98     } finally {
99       for (int i = _requestListeners.length - 1; i >= 0; i--) {
100     ServletRequestEvent JavaDoc event = new ServletRequestEvent JavaDoc(_app, request);
101     
102     _requestListeners[i].requestDestroyed(event);
103       }
104       
105       thread.setContextClassLoader(oldLoader);
106     }
107   }
108 }
109
Popular Tags