KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.dispatch;
31
32 import com.caucho.jsp.Page;
33 import com.caucho.jsp.QServlet;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36
37 import javax.servlet.FilterChain JavaDoc;
38 import javax.servlet.ServletContext JavaDoc;
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.ServletRequest JavaDoc;
41 import javax.servlet.ServletResponse JavaDoc;
42 import javax.servlet.SingleThreadModel JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.io.FileNotFoundException JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.lang.ref.SoftReference JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * Represents the final servlet in a filter chain.
52  */

53 public class PageFilterChain implements FilterChain JavaDoc {
54   private static final Logger JavaDoc log = Log.open(PageFilterChain.class);
55   private static final L10N L = new L10N(PageFilterChain.class);
56   
57   public static String JavaDoc SERVLET_NAME = "javax.servlet.error.servlet_name";
58   public static String JavaDoc SERVLET_EXN = "javax.servlet.error.exception";
59
60   private ServletContext JavaDoc _application;
61   private QServlet _servlet;
62   private String JavaDoc _jspFile;
63   private ServletConfigImpl _config;
64   private ServletContext JavaDoc _servletContext;
65   private SoftReference JavaDoc<Page> _pageRef;
66   private boolean _isSingleThread;
67
68   /**
69    * Create the filter chain servlet.
70    *
71    * @param servlet the JSP servlet
72    */

73   PageFilterChain(ServletContext JavaDoc application, QServlet servlet)
74   {
75     _application = application;
76     _servlet = servlet;
77   }
78
79   /**
80    * Create the filter chain servlet.
81    *
82    * @param servlet the JSP servlet
83    */

84   PageFilterChain(ServletContext JavaDoc application, QServlet servlet,
85                   String JavaDoc jspFile, ServletConfigImpl config)
86   {
87     _application = application;
88     _servlet = servlet;
89     _jspFile = jspFile;
90     _config = config;
91   }
92
93   /**
94    * Sets the servlet context.
95    */

96   public void setServletContext(ServletContext JavaDoc servletContext)
97   {
98     _servletContext = servletContext;
99   }
100
101   /**
102    * Gets the servlet context.
103    */

104   public ServletContext JavaDoc getServletContext()
105   {
106     return _servletContext;
107   }
108
109   /**
110    * Returns the servlet.
111    */

112   public QServlet getServlet()
113   {
114     return _servlet;
115   }
116   
117   /**
118    * Invokes the final servlet at the end of the chain.
119    *
120    * @param req the servlet request
121    * @param res the servlet response
122    */

123   public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
124     throws ServletException JavaDoc, IOException JavaDoc
125   {
126     HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
127     HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
128     FileNotFoundException JavaDoc notFound = null;
129
130     SoftReference JavaDoc<Page> pageRef = _pageRef;
131
132     Page page;
133
134     if (pageRef != null)
135       page = pageRef.get();
136     else
137       page = null;
138
139     if (page == null || page.cauchoIsModified()) {
140       try {
141     _pageRef = null;
142     
143     page = compilePage(page, req, res);
144
145     if (page != null) {
146       _pageRef = new SoftReference JavaDoc<Page>(page);
147       
148       _isSingleThread = page instanceof SingleThreadModel JavaDoc;
149     }
150       } catch (FileNotFoundException JavaDoc e) {
151     page = null;
152
153     notFound = e;
154       }
155     }
156
157     if (page == null) {
158       // jsp/01cg
159
if (notFound == null)
160     return;
161       
162       String JavaDoc errorUri = (String JavaDoc) req.getAttribute("javax.servlet.error.request_uri");
163       String JavaDoc uri = (String JavaDoc) req.getAttribute("javax.servlet.include.request_uri");
164       String JavaDoc forward = (String JavaDoc) req.getAttribute("caucho.forward");
165
166       // jsp/01ch
167
if (uri != null) {
168     //throw new FileNotFoundException(uri);
169
throw notFound;
170       }
171       else if (forward != null) {
172     //throw new FileNotFoundException(req.getRequestURI());
173
throw notFound;
174       }
175       else if (errorUri != null) {
176     //throw new FileNotFoundException(errorUri);
177
throw notFound;
178       }
179     
180       ((HttpServletResponse JavaDoc) res).sendError(HttpServletResponse.SC_NOT_FOUND);
181     }
182     else if (req instanceof HttpServletRequest JavaDoc) {
183       try {
184     if (_isSingleThread) {
185       synchronized (page) {
186         page.pageservice(req, res);
187       }
188     }
189     else
190       page.pageservice(req, res);
191       } catch (ServletException JavaDoc e) {
192     request.setAttribute(SERVLET_EXN, e);
193     if (_config != null)
194       request.setAttribute(SERVLET_NAME, _config.getServletName());
195     throw e;
196       } catch (IOException JavaDoc e) {
197     request.setAttribute(SERVLET_EXN, e);
198     if (_config != null)
199       request.setAttribute(SERVLET_NAME, _config.getServletName());
200     throw e;
201       } catch (RuntimeException JavaDoc e) {
202     request.setAttribute(SERVLET_EXN, e);
203     if (_config != null)
204       request.setAttribute(SERVLET_NAME, _config.getServletName());
205     throw e;
206       }
207     }
208   }
209
210   /**
211    * Compiles the page, returning the new page.
212    */

213   private Page compilePage(Page oldPage,
214                HttpServletRequest JavaDoc req,
215                HttpServletResponse JavaDoc res)
216     throws ServletException JavaDoc, FileNotFoundException JavaDoc
217   {
218     Page newPage = null;
219     
220     if (oldPage != null && ! oldPage.startRecompiling()) {
221       return oldPage;
222     }
223
224     try {
225       if (_jspFile != null) {
226     req.setAttribute("caucho.jsp.jsp-file", _jspFile);
227     req.setAttribute("caucho.jsp.servlet-config", _config);
228       }
229
230       if (_config != null)
231     newPage = (Page) _config.createServlet(false);
232       else {
233     newPage = _servlet.getPage(req, res);
234
235     if (newPage != null && ! newPage.isInit()) {
236       ServletConfigImpl config = new ServletConfigImpl();
237       config.setServletContext(_application);
238       config.setServletName(req.getServletPath());
239       newPage.init(config);
240     }
241       }
242
243       // XXX: In theory, should let the requests drain. In practice,
244
// the JSP destroy() method doesn't do anything.
245
if (oldPage != null && ! oldPage.isDead())
246     oldPage.destroy();
247
248       if (newPage != null)
249     newPage._caucho_use();
250
251       return newPage;
252     } catch (ServletException JavaDoc e) {
253       throw e;
254     } catch (RuntimeException JavaDoc e) {
255       throw e;
256     } catch (FileNotFoundException JavaDoc e) {
257       throw e;
258     } catch (Exception JavaDoc e) {
259       throw new ServletException JavaDoc(e);
260     } finally {
261       if (_jspFile != null) {
262     req.removeAttribute("caucho.jsp.jsp-file");
263     req.removeAttribute("caucho.jsp.servlet-config");
264       }
265     }
266   }
267 }
268
Popular Tags