001: /*
002: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
003: *
004: * The Apache Software License, Version 1.1
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Scott Ferguson
047: */
048:
049: package com.caucho.services.server;
050:
051: import javax.servlet.ServletException;
052: import javax.servlet.ServletRequest;
053: import java.util.HashMap;
054:
055: /**
056: * Context for a service, to handle request-specific information.
057: */
058: public class ServiceContext {
059: private static final ThreadLocal _localContext = new ThreadLocal();
060:
061: private ServletRequest _request;
062: private String _serviceName;
063: private String _objectId;
064: private int _count;
065: private HashMap _headers = new HashMap();
066:
067: private ServiceContext() {
068: }
069:
070: /**
071: * Sets the request object prior to calling the service's method.
072: *
073: * @param request the calling servlet request
074: * @param serviceId the service identifier
075: * @param objectId the object identifier
076: */
077: public static void begin(ServletRequest request,
078: String serviceName, String objectId)
079: throws ServletException {
080: ServiceContext context = (ServiceContext) _localContext.get();
081:
082: if (context == null) {
083: context = new ServiceContext();
084: _localContext.set(context);
085: }
086:
087: context._request = request;
088: context._serviceName = serviceName;
089: context._objectId = objectId;
090: context._count++;
091: }
092:
093: /**
094: * Returns the service request.
095: */
096: public static ServiceContext getContext() {
097: return (ServiceContext) _localContext.get();
098: }
099:
100: /**
101: * Adds a header.
102: */
103: public void addHeader(String header, Object value) {
104: _headers.put(header, value);
105: }
106:
107: /**
108: * Gets a header.
109: */
110: public Object getHeader(String header) {
111: return _headers.get(header);
112: }
113:
114: /**
115: * Gets a header from the context.
116: */
117: public static Object getContextHeader(String header) {
118: ServiceContext context = (ServiceContext) _localContext.get();
119:
120: if (context != null)
121: return context.getHeader(header);
122: else
123: return null;
124: }
125:
126: /**
127: * Returns the service request.
128: */
129: public static ServletRequest getContextRequest() {
130: ServiceContext context = (ServiceContext) _localContext.get();
131:
132: if (context != null)
133: return context._request;
134: else
135: return null;
136: }
137:
138: /**
139: * Returns the service id, corresponding to the pathInfo of the URL.
140: */
141: public static String getContextServiceName() {
142: ServiceContext context = (ServiceContext) _localContext.get();
143:
144: if (context != null)
145: return context._serviceName;
146: else
147: return null;
148: }
149:
150: /**
151: * Returns the object id, corresponding to the ?id= of the URL.
152: */
153: public static String getContextObjectId() {
154: ServiceContext context = (ServiceContext) _localContext.get();
155:
156: if (context != null)
157: return context._objectId;
158: else
159: return null;
160: }
161:
162: /**
163: * Cleanup at the end of a request.
164: */
165: public static void end() {
166: ServiceContext context = (ServiceContext) _localContext.get();
167:
168: if (context != null && --context._count == 0) {
169: context._request = null;
170:
171: context._headers.clear();
172: }
173: }
174:
175: /**
176: * Returns the service request.
177: *
178: * @deprecated
179: */
180: public static ServletRequest getRequest() {
181: ServiceContext context = (ServiceContext) _localContext.get();
182:
183: if (context != null)
184: return context._request;
185: else
186: return null;
187: }
188:
189: /**
190: * Returns the service id, corresponding to the pathInfo of the URL.
191: *
192: * @deprecated
193: */
194: public static String getServiceName() {
195: ServiceContext context = (ServiceContext) _localContext.get();
196:
197: if (context != null)
198: return context._serviceName;
199: else
200: return null;
201: }
202:
203: /**
204: * Returns the object id, corresponding to the ?id= of the URL.
205: *
206: * @deprecated
207: */
208: public static String getObjectId() {
209: ServiceContext context = (ServiceContext) _localContext.get();
210:
211: if (context != null)
212: return context._objectId;
213: else
214: return null;
215: }
216: }
|