KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.server.connection.AbstractHttpRequest;
33 import com.caucho.server.connection.AbstractHttpResponse;
34 import com.caucho.transaction.TransactionImpl;
35 import com.caucho.transaction.TransactionManagerImpl;
36
37 import javax.servlet.FilterChain JavaDoc;
38 import javax.servlet.ServletException JavaDoc;
39 import javax.servlet.ServletRequest JavaDoc;
40 import javax.servlet.ServletResponse JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.transaction.Status JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Represents the next filter in a filter chain. The final filter will
49  * be the servlet itself.
50  */

51 public class ContextFilterChain implements FilterChain JavaDoc {
52   private static final Logger JavaDoc log = Log.open(ContextFilterChain.class);
53   
54   // Next filter chain
55
private FilterChain JavaDoc _next;
56   
57   // class loader
58
private ClassLoader JavaDoc _classLoader;
59   // transaction manager
60
private TransactionManagerImpl _tm;
61   // error page manager
62
private ErrorPageManager _errorPageManager;
63
64   /**
65    * Creates a new FilterChainFilter.
66    *
67    * @param next the next filterChain
68    * @param filter the user's filter
69    */

70   public ContextFilterChain(FilterChain JavaDoc next)
71   {
72     _next = next;
73     
74     _classLoader = Thread.currentThread().getContextClassLoader();
75
76     try {
77       _tm = TransactionManagerImpl.getInstance();
78     } catch (Throwable JavaDoc e) {
79       log.log(Level.WARNING, e.toString(), e);
80     }
81   }
82
83   /**
84    * Sets the error page manager.
85    */

86   public void setErrorPageManager(ErrorPageManager errorPageManager)
87   {
88     _errorPageManager = errorPageManager;
89   }
90   
91   /**
92    * Invokes the next filter in the chain or the final servlet at
93    * the end of the chain.
94    *
95    * @param request the servlet request
96    * @param response the servlet response
97    * @since Servlet 2.3
98    */

99   public void doFilter(ServletRequest JavaDoc request,
100                        ServletResponse JavaDoc response)
101     throws ServletException JavaDoc, IOException JavaDoc
102   {
103     Thread JavaDoc thread = Thread.currentThread();
104     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
105     
106     try {
107       thread.setContextClassLoader(_classLoader);
108       _next.doFilter(request, response);
109     } catch (ServletException JavaDoc e) {
110       if (_errorPageManager != null)
111         _errorPageManager.sendServletError(e, request, response);
112       else
113         throw e;
114     } catch (IOException JavaDoc e) {
115       if (_errorPageManager != null)
116         _errorPageManager.sendServletError(e, request, response);
117       else
118         throw e;
119     } catch (RuntimeException JavaDoc e) {
120       if (_errorPageManager != null)
121         _errorPageManager.sendServletError(e, request, response);
122       else
123         throw e;
124     } finally {
125       // needed for things like closing the session
126
if (request instanceof AbstractHttpRequest)
127         ((AbstractHttpRequest) request).finish();
128
129       if (_tm != null) {
130         try {
131           TransactionImpl transaction = _tm.getCurrent();
132           if (transaction.getStatus() != Status.STATUS_NO_TRANSACTION) {
133             log.warning("Transaction not properly closed for " + ((HttpServletRequest JavaDoc) request).getRequestURL());
134           }
135           transaction.close();
136         } catch (Throwable JavaDoc e) {
137           log.log(Level.WARNING, e.getMessage(), e);
138         }
139       }
140       
141       if (response instanceof AbstractHttpResponse)
142         ((AbstractHttpResponse) response).finish();
143
144       thread.setContextClassLoader(oldLoader);
145     }
146   }
147 }
148
Popular Tags