KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > services > server > ServiceContext


1 /*
2  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Scott Ferguson
47  */

48
49 package com.caucho.services.server;
50
51 import javax.servlet.ServletException JavaDoc;
52 import javax.servlet.ServletRequest JavaDoc;
53 import java.util.HashMap JavaDoc;
54
55 /**
56  * Context for a service, to handle request-specific information.
57  */

58 public class ServiceContext {
59   private static final ThreadLocal JavaDoc _localContext = new ThreadLocal JavaDoc();
60
61   private ServletRequest JavaDoc _request;
62   private String JavaDoc _serviceName;
63   private String JavaDoc _objectId;
64   private int _count;
65   private HashMap JavaDoc _headers = new HashMap JavaDoc();
66
67   private ServiceContext()
68   {
69   }
70   
71   /**
72    * Sets the request object prior to calling the service's method.
73    *
74    * @param request the calling servlet request
75    * @param serviceId the service identifier
76    * @param objectId the object identifier
77    */

78   public static void begin(ServletRequest JavaDoc request,
79                String JavaDoc serviceName,
80                String JavaDoc objectId)
81     throws ServletException JavaDoc
82   {
83     ServiceContext context = (ServiceContext) _localContext.get();
84
85     if (context == null) {
86       context = new ServiceContext();
87       _localContext.set(context);
88     }
89
90     context._request = request;
91     context._serviceName = serviceName;
92     context._objectId = objectId;
93     context._count++;
94   }
95
96   /**
97    * Returns the service request.
98    */

99   public static ServiceContext getContext()
100   {
101     return (ServiceContext) _localContext.get();
102   }
103
104   /**
105    * Adds a header.
106    */

107   public void addHeader(String JavaDoc header, Object JavaDoc value)
108   {
109     _headers.put(header, value);
110   }
111
112   /**
113    * Gets a header.
114    */

115   public Object JavaDoc getHeader(String JavaDoc header)
116   {
117     return _headers.get(header);
118   }
119
120   /**
121    * Gets a header from the context.
122    */

123   public static Object JavaDoc getContextHeader(String JavaDoc header)
124   {
125     ServiceContext context = (ServiceContext) _localContext.get();
126
127     if (context != null)
128       return context.getHeader(header);
129     else
130       return null;
131   }
132
133   /**
134    * Returns the service request.
135    */

136   public static ServletRequest JavaDoc getContextRequest()
137   {
138     ServiceContext context = (ServiceContext) _localContext.get();
139
140     if (context != null)
141       return context._request;
142     else
143       return null;
144   }
145
146   /**
147    * Returns the service id, corresponding to the pathInfo of the URL.
148    */

149   public static String JavaDoc getContextServiceName()
150   {
151     ServiceContext context = (ServiceContext) _localContext.get();
152
153     if (context != null)
154       return context._serviceName;
155     else
156       return null;
157   }
158
159   /**
160    * Returns the object id, corresponding to the ?id= of the URL.
161    */

162   public static String JavaDoc getContextObjectId()
163   {
164     ServiceContext context = (ServiceContext) _localContext.get();
165
166     if (context != null)
167       return context._objectId;
168     else
169       return null;
170   }
171
172   /**
173    * Cleanup at the end of a request.
174    */

175   public static void end()
176   {
177     ServiceContext context = (ServiceContext) _localContext.get();
178
179     if (context != null && --context._count == 0) {
180       context._request = null;
181
182       context._headers.clear();
183     }
184   }
185
186   /**
187    * Returns the service request.
188    *
189    * @deprecated
190    */

191   public static ServletRequest JavaDoc getRequest()
192   {
193     ServiceContext context = (ServiceContext) _localContext.get();
194
195     if (context != null)
196       return context._request;
197     else
198       return null;
199   }
200
201   /**
202    * Returns the service id, corresponding to the pathInfo of the URL.
203    *
204    * @deprecated
205    */

206   public static String JavaDoc getServiceName()
207   {
208     ServiceContext context = (ServiceContext) _localContext.get();
209
210     if (context != null)
211       return context._serviceName;
212     else
213       return null;
214   }
215
216   /**
217    * Returns the object id, corresponding to the ?id= of the URL.
218    *
219    * @deprecated
220    */

221   public static String JavaDoc getObjectId()
222   {
223     ServiceContext context = (ServiceContext) _localContext.get();
224
225     if (context != null)
226       return context._objectId;
227     else
228       return null;
229   }
230 }
231
Popular Tags