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:
039: /**
040: * Represents the final servlet in a filter chain.
041: */
042: public class SingleThreadServletFilterChain implements FilterChain {
043: // servlet config
044: private ServletConfigImpl _config;
045: // servlet
046: private Servlet _servlet;
047:
048: /**
049: * Create the filter chain servlet.
050: *
051: * @param servlet the underlying servlet
052: */
053: public SingleThreadServletFilterChain(ServletConfigImpl config) {
054: if (config == null)
055: throw new NullPointerException();
056:
057: _config = config;
058: }
059:
060: /**
061: * Invokes the final servlet at the end of the chain.
062: *
063: * @param request the servlet request
064: * @param response the servlet response
065: *
066: * @since Servlet 2.3
067: */
068: public void doFilter(ServletRequest request,
069: ServletResponse response) throws ServletException,
070: IOException {
071: Servlet servlet = null;
072:
073: synchronized (this ) {
074: servlet = _servlet;
075: _servlet = null;
076: }
077:
078: if (servlet == null) {
079: try {
080: servlet = (Servlet) _config.createServlet(true);
081: } catch (ServletException e) {
082: throw e;
083: } catch (Exception e) {
084: throw new ServletException(e);
085: }
086: }
087:
088: try {
089: servlet.service(request, response);
090: } catch (UnavailableException e) {
091: _config.setInitException(e);
092: // application.killServlet(servlet, config);
093:
094: throw e;
095: }
096:
097: synchronized (this) {
098: if (_servlet == null) {
099: _servlet = servlet;
100: servlet = null;
101: }
102: }
103:
104: if (servlet != null)
105: servlet.destroy();
106: }
107: }
|