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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.dispatch;
031:
032: import com.caucho.servlet.comet.CometFilterChain;
033: import com.caucho.util.L10N;
034:
035: import javax.servlet.FilterChain;
036: import javax.servlet.ServletException;
037: import javax.servlet.ServletRequest;
038: import javax.servlet.ServletResponse;
039: import java.io.IOException;
040: import java.util.HashMap;
041: import java.util.logging.Logger;
042: import java.util.logging.Level;
043:
044: /**
045: * A repository for request information gleaned from the uri.
046: */
047: public class ServletInvocation {
048: static final Logger log = Logger.getLogger(ServletInvocation.class
049: .getName());
050: static final L10N L = new L10N(ServletInvocation.class);
051:
052: private final boolean _isFiner;
053:
054: private static final ThreadLocal<ServletRequest> _requestThreadLocal = new ThreadLocal<ServletRequest>();
055:
056: private ClassLoader _classLoader;
057:
058: private String _contextPath = "";
059:
060: private String _contextUri;
061: private String _servletPath;
062: private String _pathInfo;
063:
064: private String _queryString;
065:
066: private String _servletName;
067: private FilterChain _filterChain;
068:
069: private long _requestCount;
070:
071: private HashMap<String, String> _securityRoleMap;
072:
073: /**
074: * Creates a new invocation
075: *
076: * @param contextUri the section of the URI after the context path
077: */
078: public ServletInvocation() {
079: _classLoader = Thread.currentThread().getContextClassLoader();
080:
081: _isFiner = log.isLoggable(Level.FINER);
082: }
083:
084: /**
085: * Returns the mapped context-path.
086: */
087: public final String getContextPath() {
088: return _contextPath;
089: }
090:
091: /**
092: * Sets the context-path.
093: */
094: public void setContextPath(String path) {
095: _contextPath = path;
096: }
097:
098: public void setContextURI(String contextURI) {
099: _contextUri = contextURI;
100: _servletPath = contextURI;
101: }
102:
103: /**
104: * Returns the URI tail, i.e. everything after the context path.
105: */
106: public final String getContextURI() {
107: return _contextUri;
108: }
109:
110: /**
111: * Returns the mapped servlet path.
112: */
113: public final String getServletPath() {
114: return _servletPath;
115: }
116:
117: /**
118: * Sets the mapped servlet path.
119: */
120: public void setServletPath(String servletPath) {
121: _servletPath = servletPath;
122: }
123:
124: /**
125: * Returns the mapped path info.
126: */
127: public final String getPathInfo() {
128: return _pathInfo;
129: }
130:
131: /**
132: * Sets the mapped path info
133: */
134: public void setPathInfo(String pathInfo) {
135: _pathInfo = pathInfo;
136: }
137:
138: /**
139: * Returns the query string. Characters remain unescaped.
140: */
141: public final String getQueryString() {
142: return _queryString;
143: }
144:
145: /**
146: * Returns the query string. Characters remain unescaped.
147: */
148: public final void setQueryString(String queryString) {
149: _queryString = queryString;
150: }
151:
152: /**
153: * Sets the class loader.
154: */
155: public void setClassLoader(ClassLoader loader) {
156: _classLoader = loader;
157: }
158:
159: /**
160: * Gets the class loader.
161: */
162: public ClassLoader getClassLoader() {
163: return _classLoader;
164: }
165:
166: /**
167: * Sets the servlet name
168: */
169: public void setServletName(String servletName) {
170: _servletName = servletName;
171: }
172:
173: /**
174: * Gets the servlet name
175: */
176: public String getServletName() {
177: return _servletName;
178: }
179:
180: /**
181: * Sets the filter chain
182: */
183: public void setFilterChain(FilterChain chain) {
184: _filterChain = chain;
185: }
186:
187: /**
188: * Gets the filter chain
189: */
190: public FilterChain getFilterChain() {
191: return _filterChain;
192: }
193:
194: /**
195: * Gets the security role map.
196: */
197: public HashMap<String, String> getSecurityRoleMap() {
198: return _securityRoleMap;
199: }
200:
201: /**
202: * Sets the security role map.
203: */
204: public void setSecurityRoleMap(HashMap<String, String> roleMap) {
205: _securityRoleMap = roleMap;
206: }
207:
208: /**
209: * Returns the number of requests.
210: */
211: public long getRequestCount() {
212: return _requestCount;
213: }
214:
215: /**
216: * Returns the thread request.
217: */
218: public static ServletRequest getContextRequest() {
219: return _requestThreadLocal.get();
220: }
221:
222: /**
223: * Service a request.
224: *
225: * @param request the servlet request
226: * @param response the servlet response
227: */
228: public void service(ServletRequest request, ServletResponse response)
229: throws IOException, ServletException {
230: synchronized (this ) {
231: _requestCount++;
232: }
233:
234: ThreadLocal<ServletRequest> requestThreadLocal = _requestThreadLocal;
235: ServletRequest oldRequest = requestThreadLocal.get();
236:
237: try {
238: requestThreadLocal.set(request);
239:
240: if (_isFiner)
241: log.finer("Dispatch '" + _contextUri + "' to "
242: + _filterChain);
243:
244: _filterChain.doFilter(request, response);
245: } finally {
246: requestThreadLocal.set(oldRequest);
247: }
248: }
249:
250: /**
251: * Resume a request.
252: *
253: * @param request the servlet request
254: * @param response the servlet response
255: */
256: public boolean doResume(ServletRequest request,
257: ServletResponse response) throws IOException,
258: ServletException {
259: if (_filterChain instanceof CometFilterChain) {
260: CometFilterChain filterChain = (CometFilterChain) _filterChain;
261:
262: return filterChain.doResume(request, response);
263: } else
264: return false;
265: }
266:
267: /**
268: * Copies from the invocation.
269: */
270: public void copyFrom(ServletInvocation invocation) {
271: _classLoader = invocation._classLoader;
272: _contextPath = invocation._contextPath;
273:
274: _contextUri = invocation._contextUri;
275: _servletPath = invocation._servletPath;
276: _pathInfo = invocation._pathInfo;
277:
278: _queryString = invocation._queryString;
279:
280: _servletName = invocation._servletName;
281: _filterChain = invocation._filterChain;
282:
283: _securityRoleMap = invocation._securityRoleMap;
284: }
285:
286: public String toString() {
287: return "ServletInvocation[" + _contextUri + "]";
288: }
289: }
|