KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > jsdkapi > GenericSession


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
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,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /**
66  * GenericSession.java
67  *
68  * Copyright 2000, 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.core.jsdkapi;
71
72 import com.jcorporate.expresso.core.misc.ConfigManager;
73 import com.jcorporate.expresso.core.misc.SerializableString;
74 import com.jcorporate.expresso.kernel.util.ClassLocator;
75
76 import javax.servlet.ServletException JavaDoc;
77 import javax.servlet.http.HttpServletRequest JavaDoc;
78 import java.io.Serializable JavaDoc;
79 import java.util.Enumeration JavaDoc;
80
81
82 /**
83  * Different servlet API's have slightly different methods
84  * for putting & getting attributes into the session, so we
85  * have a method here for cross-API access to the session,
86  * and methods that automatically "wrap" string values
87  * stored in the session in SerializableString, so the web-apps
88  * can be distributed & session shared by appropriate app servers
89  *
90  * @author Mike Nash
91  */

92 public class GenericSession {
93
94     private GenericSession() {
95     } // no instantiations
96

97     // This is used for optimization. The actual session is loaded
98
// only once, if this flag indicates that it hasn't been so far
99
private static boolean classLoaded = false;
100
101     // This is the actual dispatcher that is filled in at runtime
102
// There are no compile time dependencies because instead of using
103
// the real JSDK-2.0 or JSDK-2.1 implementation classes
104
// we simply use a generic interface here.
105
private static APIAwareSession mySession = null;
106
107     // The fully-qualified name of this class
108
private static final java.lang.String JavaDoc thisClass = GenericSession.class.getName();
109
110     // This is the name of the class that implements the Servlet API 2.2 Session
111
private static final java.lang.String JavaDoc ServletAPI2_2Session =
112             "com.jcorporate.expresso.core.jsdkapi.ServletAPI2_2Session";
113
114     /**
115      * Store a String (wrapped in a SerializableString) in the session
116      *
117      * @param req The HttpServletRequest object
118      * @param code the attribute code
119      * @param value the attribute value
120      */

121     public synchronized static void setAttribute(HttpServletRequest JavaDoc req,
122                                                  String JavaDoc code, String JavaDoc value)
123             throws ServletException JavaDoc {
124
125         // Name of this method, used for debug-printing
126
SerializableString ss = new SerializableString(value);
127
128         // Check and load the APIAwareSession class if necessary
129
if (classLoaded == false) {
130             loadClass();
131         }
132
133         // Let the APIAwareSession do the setAttribute operation
134
mySession.setAttribute(req, code, ss);
135     } /* setAttribute(HttpServletRequest, String, STring) */
136
137
138     /**
139      * Return a string stored in the session, unwrapping it from the
140      * SerializableString it was stored in
141      *
142      * @param req the HttpServletRequest
143      * @param code the attribute key
144      * @return java.lang.String
145      * @throws ServletException upon error
146      */

147     public synchronized static String JavaDoc getAttributeString(HttpServletRequest JavaDoc req,
148                                                          String JavaDoc code)
149             throws ServletException JavaDoc {
150
151         // Check and load the APIAwareSession class if necessary
152
if (classLoaded == false) {
153             loadClass();
154         }
155
156         Object JavaDoc o = mySession.getAttribute(req, code);
157
158         if (o == null) {
159             return "";
160         }
161
162         return o.toString();
163
164         /* if (o instanceof SerializableString) {
165
166 SerializableString ss = (SerializableString) o;
167
168 return StringUtil.notNull(ss.toString());
169
170 } else {
171
172 throw new ServletException(myName + ":Object stored in session with key '"
173
174 + code + "' was not a SerializableString - it was a '"
175
176 + o.getClass().getName() + "' with value '" + o.toString() + "'");
177
178 } */

179     } /* getAttributeString(HttpServletRequest, String) */
180
181
182     /**
183      * Retrieve any other object
184      *
185      * @param req The servlet request object
186      * @param code the attribute key
187      * @return the attribute value object
188      * @throws ServletException upon error
189      */

190     public static Object JavaDoc getAttribute(HttpServletRequest JavaDoc req, String JavaDoc code)
191             throws ServletException JavaDoc {
192         if (classLoaded == false) {
193             loadClass();
194         }
195
196         return mySession.getAttribute(req, code);
197     } /* getAttribute(HttpServletRequest, String) */
198
199
200     /**
201      * Invalidate the session
202      *
203      * @param req the servlet request object
204      * @throws ServletException upon error
205      */

206     public synchronized static void invalidate(HttpServletRequest JavaDoc req)
207             throws ServletException JavaDoc {
208
209         // Check and load the APIAwareSession class if necessary
210
if (classLoaded == false) {
211             loadClass();
212         }
213
214         // Let the APIAwareSession do the setAttribute operation
215
mySession.invalidate(req);
216     } /* invalidate(HttpServletRequest) */
217
218
219     /**
220      * Store any Serializable object in the session
221      *
222      * @param req the ServletRequest object
223      * @param code the attirbute code
224      * @param s the serializable value to save to the session
225      * @throws ServletException upon error
226      */

227     public synchronized static void setAttribute(HttpServletRequest JavaDoc req,
228                                                  String JavaDoc code, Serializable JavaDoc s)
229             throws ServletException JavaDoc {
230
231         // Check and load the APIAwareSession class if necessary
232
if (classLoaded == false) {
233             loadClass();
234         }
235
236         // Let the APIAwareSession do the setAttribute operation
237
mySession.setAttribute(req, code, s);
238     } /* setAttribute(HttpServletRequest, String, Serializable) */
239
240
241     /**
242      * Remove an attribute from the session
243      *
244      * @param req the servletrequest object
245      * @param code the attribute key
246      * @throws ServletException upon error
247      */

248     public synchronized static void removeAttribute(HttpServletRequest JavaDoc req,
249                                                     String JavaDoc code)
250             throws ServletException JavaDoc {
251
252         // Check and load the APIAwareSession class if necessary
253
if (classLoaded == false) {
254             loadClass();
255         }
256
257         // Let the APIAwareSession do the removeAttribute operation
258
mySession.removeAttribute(req, code);
259     } /* removeAttribute(HttpServletRequest, String) */
260
261
262     /**
263      * Retrieve all attribute names
264      *
265      * @param req the servlet request object
266      * @return an Enumeration of all the names in the session
267      * @throws ServletException upon error
268      */

269     public static Enumeration JavaDoc getAttributeNames(HttpServletRequest JavaDoc req)
270             throws ServletException JavaDoc {
271
272         // Check and load the APIAwareSession class if necessary
273
if (classLoaded == false) {
274             loadClass();
275         }
276
277         // Let the APIAwareSession do the removeAttribute operation
278
return mySession.getAttributeNames(req);
279     } /* getAttributeNames(HttpServletRequest) */
280
281
282     /**
283      * Retrieve the session id
284      *
285      * @param req the servlet request
286      * @return the session id string
287      * @throws ServletException upon error
288      */

289     public static String JavaDoc getId(HttpServletRequest JavaDoc req)
290             throws ServletException JavaDoc {
291
292         // Check and load the APIAwareSession class if necessary
293
if (classLoaded == false) {
294             loadClass();
295         }
296
297         // Let the APIAwareSession do the removeAttribute operation
298
return mySession.getId(req);
299     } /* getId(HttpServletRequest) */
300
301
302     /**
303      * Retrieve the context path
304      *
305      * @param req the servlet request object
306      * @return the string for the servlet context path
307      * @throws ServletException upon error
308      */

309     public synchronized static String JavaDoc getContextPath(HttpServletRequest JavaDoc req)
310             throws ServletException JavaDoc {
311
312
313         // Check and load the APIAwareSession class if necessary
314
if (classLoaded == false) {
315             loadClass();
316         }
317
318         // Let the APIAwareSession do the getContextPath operation
319
return mySession.getContextPath(req);
320     } /* getContextPath(HttpServletRequest) */
321
322
323     /**
324      * This method dynamically loads the correct APIAwareSession
325      * class based on whether the user specified Servlet API-2.0
326      * compatibility or not. Currently the switch is pretty "dumb".
327      * If the user explicitly specifies Servlet API 2.0 compatibilty
328      * by using a config parameter ("servletAPI=2_0"), then the
329      * Servlet API 2.0 compatible RequestSession is loaded.
330      */

331     private static void loadClass()
332             throws ServletException JavaDoc {
333
334         // The name of this class, used for debug-printing
335
String JavaDoc myName = thisClass + "loadClass()";
336
337         try {
338             String JavaDoc apiVersion = ConfigManager.getServletAPIVersion();
339             Class JavaDoc c = null;
340
341             // if (apiVersion.equals("2_2")) {
342
c = ClassLocator.loadClass(ServletAPI2_2Session);
343
344             mySession = (APIAwareSession) c.newInstance();
345
346             // Handle the various problems that might happen!
347
} catch (ClassNotFoundException JavaDoc cn) {
348             throw new ServletException JavaDoc(myName +
349                     ":APIAwareSession class not found:" +
350                     cn.getMessage());
351         } catch (InstantiationException JavaDoc ie) {
352             throw new ServletException JavaDoc(myName +
353                     ":APIAwareSession class cannot be instantiated:" +
354                     ie.getMessage());
355         } catch (IllegalAccessException JavaDoc iae) {
356             throw new ServletException JavaDoc(myName +
357                     ":Illegal access loading APIAwareSession object:" +
358                     iae.getMessage());
359         }
360
361         // We only want to go through this once!
362
classLoaded = true;
363     } /* loadClass() */
364
365
366 } /* GenericSession */
367
Popular Tags