001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.dispatch;
030:
031: import javax.servlet.FilterChain;
032: import javax.servlet.Servlet;
033: import javax.servlet.ServletException;
034: import javax.servlet.ServletRequest;
035: import javax.servlet.ServletResponse;
036: import javax.servlet.UnavailableException;
037: import java.io.IOException;
038: import java.util.HashMap;
039:
040: /**
041: * Represents the final servlet in a filter chain.
042: */
043: public class ServletFilterChain extends AbstractFilterChain {
044: public static String SERVLET_NAME = "javax.servlet.error.servlet_name";
045:
046: // servlet config
047: private ServletConfigImpl _config;
048: // servlet
049: private Servlet _servlet;
050:
051: /**
052: * Create the filter chain servlet.
053: *
054: * @param servlet the underlying servlet
055: */
056: public ServletFilterChain(ServletConfigImpl config) {
057: if (config == null)
058: throw new NullPointerException();
059:
060: _config = config;
061: }
062:
063: /**
064: * Returns the servlet name.
065: */
066: public String getServletName() {
067: return _config.getServletName();
068: }
069:
070: /**
071: * Returns the role map.
072: */
073: public HashMap<String, String> getRoleMap() {
074: return _config.getRoleMap();
075: }
076:
077: /**
078: * Invokes the final servlet at the end of the chain.
079: *
080: * @param request the servlet request
081: * @param response the servlet response
082: *
083: * @since Servlet 2.3
084: */
085: public void doFilter(ServletRequest request,
086: ServletResponse response) throws ServletException,
087: IOException {
088: if (_servlet == null) {
089: try {
090: _servlet = (Servlet) _config.createServlet(false);
091: } catch (ServletException e) {
092: throw e;
093: } catch (Exception e) {
094: throw new ServletException(e);
095: }
096: }
097:
098: try {
099: _servlet.service(request, response);
100: } catch (UnavailableException e) {
101: _servlet = null;
102: _config.setInitException(e);
103: _config.killServlet();
104: request
105: .setAttribute(SERVLET_NAME, _config
106: .getServletName());
107: throw e;
108: } catch (ServletException e) {
109: request
110: .setAttribute(SERVLET_NAME, _config
111: .getServletName());
112: throw e;
113: } catch (IOException e) {
114: request
115: .setAttribute(SERVLET_NAME, _config
116: .getServletName());
117: throw e;
118: } catch (RuntimeException e) {
119: request
120: .setAttribute(SERVLET_NAME, _config
121: .getServletName());
122: throw e;
123: }
124: }
125:
126: /**
127: * Resumes the final servlet for a comet request.
128: *
129: * @param request the servlet request
130: * @param response the servlet response
131: *
132: * @since Resin 3.1.3
133: */
134: @Override
135: public boolean doResume(ServletRequest request,
136: ServletResponse response) throws ServletException,
137: IOException {
138: return false;
139: }
140:
141: public String toString() {
142: return getClass().getSimpleName() + "[" + getServletName()
143: + "]";
144: }
145: }
|