KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > dispatch > ServletFilterChain


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.dispatch;
30
31 import javax.servlet.FilterChain JavaDoc;
32 import javax.servlet.Servlet JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.ServletRequest JavaDoc;
35 import javax.servlet.ServletResponse JavaDoc;
36 import javax.servlet.UnavailableException JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.util.HashMap JavaDoc;
39
40 /**
41  * Represents the final servlet in a filter chain.
42  */

43 public class ServletFilterChain implements FilterChain JavaDoc {
44   public static String JavaDoc SERVLET_NAME = "javax.servlet.error.servlet_name";
45   
46   // servlet config
47
private ServletConfigImpl _config;
48   // servlet
49
private Servlet JavaDoc _servlet;
50
51   /**
52    * Create the filter chain servlet.
53    *
54    * @param servlet the underlying servlet
55    */

56   public ServletFilterChain(ServletConfigImpl config)
57   {
58     if (config == null)
59       throw new NullPointerException JavaDoc();
60     
61     _config = config;
62   }
63
64   /**
65    * Returns the servlet name.
66    */

67   public String JavaDoc getServletName()
68   {
69     return _config.getServletName();
70   }
71
72   /**
73    * Returns the role map.
74    */

75   public HashMap JavaDoc<String JavaDoc,String JavaDoc> getRoleMap()
76   {
77     return _config.getRoleMap();
78   }
79   
80   /**
81    * Invokes the final servlet at the end of the chain.
82    *
83    * @param request the servlet request
84    * @param response the servlet response
85    *
86    * @since Servlet 2.3
87    */

88   public void doFilter(ServletRequest JavaDoc request,
89                        ServletResponse JavaDoc response)
90     throws ServletException JavaDoc, IOException JavaDoc
91   {
92     if (_servlet == null) {
93       try {
94         _servlet = (Servlet JavaDoc) _config.createServlet(false);
95       } catch (ServletException JavaDoc e) {
96         throw e;
97       } catch (Exception JavaDoc e) {
98         throw new ServletException JavaDoc(e);
99       }
100     }
101     
102     try {
103       _servlet.service(request, response);
104     } catch (UnavailableException JavaDoc e) {
105       _servlet = null;
106       _config.setInitException(e);
107       _config.killServlet();
108       request.setAttribute(SERVLET_NAME, _config.getServletName());
109       throw e;
110     } catch (ServletException JavaDoc e) {
111       request.setAttribute(SERVLET_NAME, _config.getServletName());
112       throw e;
113     } catch (IOException JavaDoc e) {
114       request.setAttribute(SERVLET_NAME, _config.getServletName());
115       throw e;
116     } catch (RuntimeException JavaDoc e) {
117       request.setAttribute(SERVLET_NAME, _config.getServletName());
118       throw e;
119     }
120   }
121 }
122
Popular Tags