1 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 ; 38 import javax.servlet.ServletContext ; 39 import javax.servlet.ServletException ; 40 import javax.servlet.ServletRequest ; 41 import javax.servlet.ServletResponse ; 42 import javax.servlet.SingleThreadModel ; 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import java.io.FileNotFoundException ; 46 import java.io.IOException ; 47 import java.lang.ref.SoftReference ; 48 import java.util.logging.Logger ; 49 50 53 public class PageFilterChain implements FilterChain { 54 private static final Logger log = Log.open(PageFilterChain.class); 55 private static final L10N L = new L10N(PageFilterChain.class); 56 57 public static String SERVLET_NAME = "javax.servlet.error.servlet_name"; 58 public static String SERVLET_EXN = "javax.servlet.error.exception"; 59 60 private ServletContext _application; 61 private QServlet _servlet; 62 private String _jspFile; 63 private ServletConfigImpl _config; 64 private ServletContext _servletContext; 65 private SoftReference <Page> _pageRef; 66 private boolean _isSingleThread; 67 68 73 PageFilterChain(ServletContext application, QServlet servlet) 74 { 75 _application = application; 76 _servlet = servlet; 77 } 78 79 84 PageFilterChain(ServletContext application, QServlet servlet, 85 String jspFile, ServletConfigImpl config) 86 { 87 _application = application; 88 _servlet = servlet; 89 _jspFile = jspFile; 90 _config = config; 91 } 92 93 96 public void setServletContext(ServletContext servletContext) 97 { 98 _servletContext = servletContext; 99 } 100 101 104 public ServletContext getServletContext() 105 { 106 return _servletContext; 107 } 108 109 112 public QServlet getServlet() 113 { 114 return _servlet; 115 } 116 117 123 public void doFilter(ServletRequest request, ServletResponse response) 124 throws ServletException , IOException 125 { 126 HttpServletRequest req = (HttpServletRequest ) request; 127 HttpServletResponse res = (HttpServletResponse ) response; 128 FileNotFoundException notFound = null; 129 130 SoftReference <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 <Page>(page); 147 148 _isSingleThread = page instanceof SingleThreadModel ; 149 } 150 } catch (FileNotFoundException e) { 151 page = null; 152 153 notFound = e; 154 } 155 } 156 157 if (page == null) { 158 if (notFound == null) 160 return; 161 162 String errorUri = (String ) req.getAttribute("javax.servlet.error.request_uri"); 163 String uri = (String ) req.getAttribute("javax.servlet.include.request_uri"); 164 String forward = (String ) req.getAttribute("caucho.forward"); 165 166 if (uri != null) { 168 throw notFound; 170 } 171 else if (forward != null) { 172 throw notFound; 174 } 175 else if (errorUri != null) { 176 throw notFound; 178 } 179 180 ((HttpServletResponse ) res).sendError(HttpServletResponse.SC_NOT_FOUND); 181 } 182 else if (req instanceof HttpServletRequest ) { 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 e) { 192 request.setAttribute(SERVLET_EXN, e); 193 if (_config != null) 194 request.setAttribute(SERVLET_NAME, _config.getServletName()); 195 throw e; 196 } catch (IOException e) { 197 request.setAttribute(SERVLET_EXN, e); 198 if (_config != null) 199 request.setAttribute(SERVLET_NAME, _config.getServletName()); 200 throw e; 201 } catch (RuntimeException 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 213 private Page compilePage(Page oldPage, 214 HttpServletRequest req, 215 HttpServletResponse res) 216 throws ServletException , FileNotFoundException 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 if (oldPage != null && ! oldPage.isDead()) 246 oldPage.destroy(); 247 248 if (newPage != null) 249 newPage._caucho_use(); 250 251 return newPage; 252 } catch (ServletException e) { 253 throw e; 254 } catch (RuntimeException e) { 255 throw e; 256 } catch (FileNotFoundException e) { 257 throw e; 258 } catch (Exception e) { 259 throw new ServletException (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 |