001 /* 002 * Copyright (c) 2001-2008 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 javax.servlet.ServletResponse; 054 055 import java.util.HashMap; 056 057 /** 058 * Context for a service, to handle request-specific information. 059 */ 060 public class ServiceContext { 061 private static final ThreadLocal<ServiceContext> _localContext 062 = new ThreadLocal<ServiceContext>(); 063 064 private ServletRequest _request; 065 private ServletResponse _response; 066 private String _serviceName; 067 private String _objectId; 068 private int _count; 069 private HashMap _headers = new HashMap(); 070 071 private ServiceContext() 072 { 073 } 074 075 /** 076 * Sets the request object prior to calling the service's method. 077 * 078 * @param request the calling servlet request 079 * @param serviceId the service identifier 080 * @param objectId the object identifier 081 */ 082 public static void begin(ServletRequest request, 083 ServletResponse response, 084 String serviceName, 085 String objectId) 086 throws ServletException 087 { 088 ServiceContext context = (ServiceContext) _localContext.get(); 089 090 if (context == null) { 091 context = new ServiceContext(); 092 _localContext.set(context); 093 } 094 095 context._request = request; 096 context._response = response; 097 context._serviceName = serviceName; 098 context._objectId = objectId; 099 context._count++; 100 } 101 102 /** 103 * Returns the service request. 104 */ 105 public static ServiceContext getContext() 106 { 107 return (ServiceContext) _localContext.get(); 108 } 109 110 /** 111 * Adds a header. 112 */ 113 public void addHeader(String header, Object value) 114 { 115 _headers.put(header, value); 116 } 117 118 /** 119 * Gets a header. 120 */ 121 public Object getHeader(String header) 122 { 123 return _headers.get(header); 124 } 125 126 /** 127 * Gets a header from the context. 128 */ 129 public static Object getContextHeader(String header) 130 { 131 ServiceContext context = (ServiceContext) _localContext.get(); 132 133 if (context != null) 134 return context.getHeader(header); 135 else 136 return null; 137 } 138 139 /** 140 * Returns the service request. 141 */ 142 public static ServletRequest getContextRequest() 143 { 144 ServiceContext context = (ServiceContext) _localContext.get(); 145 146 if (context != null) 147 return context._request; 148 else 149 return null; 150 } 151 152 /** 153 * Returns the service request. 154 */ 155 public static ServletResponse getContextResponse() 156 { 157 ServiceContext context = (ServiceContext) _localContext.get(); 158 159 if (context != null) 160 return context._response; 161 else 162 return null; 163 } 164 165 /** 166 * Returns the service id, corresponding to the pathInfo of the URL. 167 */ 168 public static String getContextServiceName() 169 { 170 ServiceContext context = (ServiceContext) _localContext.get(); 171 172 if (context != null) 173 return context._serviceName; 174 else 175 return null; 176 } 177 178 /** 179 * Returns the object id, corresponding to the ?id= of the URL. 180 */ 181 public static String getContextObjectId() 182 { 183 ServiceContext context = (ServiceContext) _localContext.get(); 184 185 if (context != null) 186 return context._objectId; 187 else 188 return null; 189 } 190 191 /** 192 * Cleanup at the end of a request. 193 */ 194 public static void end() 195 { 196 ServiceContext context = (ServiceContext) _localContext.get(); 197 198 if (context != null && --context._count == 0) { 199 context._request = null; 200 context._response = null; 201 202 context._headers.clear(); 203 204 _localContext.set(null); 205 } 206 } 207 208 /** 209 * Returns the service request. 210 * 211 * @deprecated 212 */ 213 public static ServletRequest getRequest() 214 { 215 ServiceContext context = (ServiceContext) _localContext.get(); 216 217 if (context != null) 218 return context._request; 219 else 220 return null; 221 } 222 223 /** 224 * Returns the service id, corresponding to the pathInfo of the URL. 225 * 226 * @deprecated 227 */ 228 public static String getServiceName() 229 { 230 ServiceContext context = (ServiceContext) _localContext.get(); 231 232 if (context != null) 233 return context._serviceName; 234 else 235 return null; 236 } 237 238 /** 239 * Returns the object id, corresponding to the ?id= of the URL. 240 * 241 * @deprecated 242 */ 243 public static String getObjectId() 244 { 245 ServiceContext context = (ServiceContext) _localContext.get(); 246 247 if (context != null) 248 return context._objectId; 249 else 250 return null; 251 } 252 }