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.webapp;
030:
031: import com.caucho.log.Log;
032:
033: import javax.servlet.FilterChain;
034: import javax.servlet.ServletException;
035: import javax.servlet.ServletRequest;
036: import javax.servlet.ServletRequestEvent;
037: import javax.servlet.ServletRequestListener;
038: import javax.servlet.ServletResponse;
039: import java.io.IOException;
040: import java.util.logging.Logger;
041:
042: /**
043: * Represents the next filter in a filter chain. The final filter will
044: * be the servlet itself.
045: */
046: public class DispatchFilterChain implements FilterChain {
047: private static final Logger log = Log
048: .open(DispatchFilterChain.class);
049:
050: // Next filter chain
051: private FilterChain _next;
052:
053: // app
054: private WebApp _app;
055: // class loader
056: private ClassLoader _classLoader;
057:
058: private ServletRequestListener[] _requestListeners;
059:
060: /**
061: * Creates a new FilterChainFilter.
062: *
063: * @param next the next filterChain
064: * @param app the webApp
065: */
066: public DispatchFilterChain(FilterChain next, WebApp app) {
067: _next = next;
068: _app = app;
069: _classLoader = app.getClassLoader();
070: _requestListeners = app.getRequestListeners();
071: }
072:
073: /**
074: * Invokes the next filter in the chain or the final servlet at
075: * the end of the chain.
076: *
077: * @param request the servlet request
078: * @param response the servlet response
079: * @since Servlet 2.3
080: */
081: public void doFilter(ServletRequest request,
082: ServletResponse response) throws ServletException,
083: IOException {
084: Thread thread = Thread.currentThread();
085: ClassLoader oldLoader = thread.getContextClassLoader();
086:
087: try {
088: thread.setContextClassLoader(_classLoader);
089:
090: for (int i = 0; i < _requestListeners.length; i++) {
091: ServletRequestEvent event = new ServletRequestEvent(
092: _app, request);
093:
094: _requestListeners[i].requestInitialized(event);
095: }
096:
097: _next.doFilter(request, response);
098: } finally {
099: for (int i = _requestListeners.length - 1; i >= 0; i--) {
100: ServletRequestEvent event = new ServletRequestEvent(
101: _app, request);
102:
103: _requestListeners[i].requestDestroyed(event);
104: }
105:
106: thread.setContextClassLoader(oldLoader);
107: }
108: }
109: }
|