KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > applications > HttpSessionWrapper


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 22-AUG-2003, Jahia Solutions Sarl: Fulco Houkes
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41 package org.jahia.services.applications;
42
43 import javax.servlet.ServletContext JavaDoc;
44 import javax.servlet.http.HttpSession JavaDoc;
45 import javax.servlet.http.HttpSessionContext JavaDoc;
46 import java.util.Enumeration JavaDoc;
47 import java.util.Hashtable JavaDoc;
48
49 /**
50  * This wrapper ensures the fact that Sessions values are not shared between applications
51  * running under Jahia. This isn't the best solution because ideally we should reimplement
52  * the SessionManager thread in order to make sure that Session objects are never shared.
53  * For development time reasons, we have chose to just modify the keys of storage for
54  * attribute names to make sure they are unique.
55  *
56  * @author Serge Huber
57  * @version 1.0
58  */

59 public class HttpSessionWrapper implements HttpSession JavaDoc {
60
61     /** logging */
62     private static final org.apache.log4j.Logger logger =
63             org.apache.log4j.Logger.getLogger (HttpSessionWrapper.class);
64
65     private String JavaDoc appName;
66     private String JavaDoc contextID;
67     private HttpSession JavaDoc originalSession;
68     private final static String JavaDoc KEY_PREFIX = "org.jahia.";
69     private final static String JavaDoc KEY_SEPARATOR = ".";
70     private final static boolean debugOutput = false;
71     private boolean inheritJahiaSessionAttributes = false;
72
73     /**
74      * This constructor sets the wrapping parameters internally. Nothing special done here.
75      *
76      * @param origSession Original session object around which this wrapper wraps :)
77      * @param applicationName Name of the application used to create unique keys. FIXME : This key is
78      * @param contextID the context ID
79      * @param inheritJahiaSessionAttributes not checked to see if it is globally unique
80      */

81     public HttpSessionWrapper (HttpSession JavaDoc origSession,
82                                String JavaDoc applicationName,
83                                String JavaDoc contextID,
84                                boolean inheritJahiaSessionAttributes) {
85         if (debugOutput) {
86             logger.debug ("Creating session wrapper for application [" +
87                     applicationName + "], contextID [" + contextID +
88                     "] from original session object [" + origSession.toString () + "]");
89         }
90         originalSession = origSession;
91         appName = applicationName;
92         this.contextID = contextID;
93         this.inheritJahiaSessionAttributes = inheritJahiaSessionAttributes;
94         if (originalSession.getAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID) == null) {
95             originalSession.setAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID,
96                     new Hashtable JavaDoc ());
97         }
98     }
99
100     public long getCreationTime () {
101         return originalSession.getCreationTime ();
102     }
103
104     public String JavaDoc getId () {
105         return originalSession.getId ();
106     }
107
108     public long getLastAccessedTime () {
109         return originalSession.getLastAccessedTime ();
110     }
111
112     public void setMaxInactiveInterval (int interval) {
113         originalSession.setMaxInactiveInterval (interval);
114     }
115
116     public int getMaxInactiveInterval () {
117         return originalSession.getMaxInactiveInterval ();
118     }
119
120     /**
121      * @deprecated
122      */

123     public HttpSessionContext JavaDoc getSessionContext () {
124         return originalSession.getSessionContext ();
125     }
126
127     public ServletContext JavaDoc getServletContext () {
128         //return originalSession.getServletContext();
129
// the following is a way to test if the servlet API contains this
130
// method. This is the only way to allow support for old versions
131
// of the yet unfinalized servlet API
132
try {
133             Class JavaDoc sessionClass = originalSession.getClass ();
134             java.lang.reflect.Method JavaDoc theMethod = sessionClass.getMethod ("getServletContext",
135                     null);
136             if (theMethod == null) {
137                 return null;
138             } else {
139                 Object JavaDoc returnValue = theMethod.invoke (originalSession, null);
140                 if (returnValue instanceof ServletContext JavaDoc) {
141                     return (ServletContext JavaDoc) returnValue;
142                 } else {
143                     return null;
144                 }
145             }
146         } catch (Throwable JavaDoc t) {
147             return null;
148         }
149     }
150
151     public Object JavaDoc getAttribute (String JavaDoc name) {
152         if (debugOutput) {
153             logger.debug (
154                     "emulatedSession.getAttribute(" + name + ") for app [" + KEY_PREFIX + appName + "]");
155         }
156         Hashtable JavaDoc appAttributes = (Hashtable JavaDoc) originalSession.getAttribute (
157                 KEY_PREFIX + appName + KEY_SEPARATOR + contextID);
158         if (debugOutput) {
159             logger.debug ("...result=[" + appAttributes.get (name) + "]");
160         }
161         // Try the application attributes, then the global attributes if the
162
// flag has been activated.
163
Object JavaDoc result = appAttributes.get (name);
164         if (inheritJahiaSessionAttributes) {
165             if (result == null)
166                 result = originalSession.getAttribute (name);
167         }
168         return result;
169     }
170
171     /**
172      * @deprecated
173      */

174     public Object JavaDoc getValue (String JavaDoc name) {
175         return getAttribute (name);
176     }
177
178     public Enumeration JavaDoc getAttributeNames () {
179         Hashtable JavaDoc appAttributes = (Hashtable JavaDoc) originalSession.getAttribute (
180                 KEY_PREFIX + appName + KEY_SEPARATOR + contextID);
181         return appAttributes.keys ();
182     }
183
184     /**
185      * @deprecated
186      */

187     public String JavaDoc[] getValueNames () {
188         Hashtable JavaDoc appAttributes = (Hashtable JavaDoc) originalSession.getAttribute (
189                 KEY_PREFIX + appName + KEY_SEPARATOR + contextID);
190         return (String JavaDoc[]) appAttributes.keySet ().toArray ();
191     }
192
193     public void setAttribute (String JavaDoc name,
194                               Object JavaDoc value) {
195         Hashtable JavaDoc appAttributes = (Hashtable JavaDoc) originalSession.getAttribute (
196                 KEY_PREFIX + appName + KEY_SEPARATOR + contextID);
197         appAttributes.put (name, value);
198     }
199
200     /**
201      * @deprecated
202      */

203     public void putValue (java.lang.String JavaDoc name,
204                           java.lang.Object JavaDoc value) {
205         setAttribute (name, value);
206     }
207
208     public void removeAttribute (String JavaDoc name) {
209         Hashtable JavaDoc appAttributes = (Hashtable JavaDoc) originalSession.getAttribute (
210                 KEY_PREFIX + appName + KEY_SEPARATOR + contextID);
211         appAttributes.remove (name);
212     }
213
214     /**
215      * @deprecated
216      */

217     public void removeValue (String JavaDoc name) {
218         removeAttribute (name);
219     }
220
221     public void invalidate () {
222         originalSession.removeAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID);
223         // originalSession.invalidate();
224
if (originalSession.getAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID) == null) {
225             originalSession.setAttribute (KEY_PREFIX + appName + KEY_SEPARATOR + contextID,
226                     new Hashtable JavaDoc ());
227         }
228     }
229
230     public boolean isNew () {
231         return originalSession.isNew ();
232     }
233
234 }
235
Popular Tags