KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > tools > sessionmanagement > SessionManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.tools.sessionmanagement;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedActionException JavaDoc;
26 import java.util.*;
27 import oracle.toplink.essentials.logging.*;
28 import oracle.toplink.essentials.sessions.*;
29 import oracle.toplink.essentials.exceptions.*;
30 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
31 import oracle.toplink.essentials.internal.security.PrivilegedGetClassLoaderForClass;
32 import oracle.toplink.essentials.internal.sessions.AbstractSession;
33
34 /**
35  * <p>
36  * <b>Purpose</b>: Global session location.</p>
37  *
38  * <p><b>Description</b>: This allows for a global session local which can
39  * be accessed globally from other classes. This is needed for EJB data stores
40  * as they must have a globally accessible place to access the session.
41  * This can be by EJB session beans, BMP beans and CMP beans as well as Servlets and
42  * other three-tier services.</p>
43  *
44  * <p><b>Responsibilities</b>:
45  * <ul>
46  * <li> Store a global session.
47  * <li> Allow the storage of alternative sessions as well.
48  * </ul></p>
49  *
50  * @author James Sutherland
51  * @since TOPLink/Java 3.0
52  */

53 public class SessionManager {
54     protected static SessionManager manager;
55     protected AbstractSession defaultSession;
56     protected Hashtable sessions = new Hashtable();
57     
58     /**
59      * PUBLIC:
60      * The default constructor to create a new session manager.
61      */

62     public SessionManager() {
63         sessions = new Hashtable(5);
64     }
65
66     /**
67        * INTERNAL:
68        * add an named session to the hashtable.
69        * session must have a name prior to setting into session Manager
70        */

71     public void addSession(Session session) {
72         getSessions().put(session.getName(), session);
73     }
74
75     /**
76      * ADVANCED:
77      * add an named session to the hashtable.
78      */

79     public void addSession(String JavaDoc sessionName, Session session) {
80         session.setName(sessionName);
81         getSessions().put(sessionName, session);
82     }
83
84     /**
85      * PUBLIC:
86      * Return the default session.
87      */

88     public Session getDefaultSession() {
89         if (defaultSession == null) {
90             defaultSession = getSession("default");
91         }
92         return defaultSession;
93     }
94
95     /**
96      * INTERNAL:
97      * Destroy the session defined by sessionName on this manager.
98      */

99     public void destroySession(String JavaDoc sessionName) {
100         DatabaseSession session = (DatabaseSession)getSessions().get(sessionName);
101
102         if (session != null) {
103             destroy(session);
104         } else {
105             logAndThrowException(SessionLog.WARNING, ValidationException.noSessionRegisteredForName(sessionName));
106         }
107     }
108
109     private void destroy(DatabaseSession session) {
110         if (session.isConnected()) {
111             session.logout();
112         }
113
114         sessions.remove(session.getName());
115         session = null;
116     }
117
118     /**
119      * INTERNAL:
120      * Destroy all sessions held onto by this manager.
121      */

122     public void destroyAllSessions() {
123         Enumeration toRemoveSessions = getSessions().elements();
124
125         while (toRemoveSessions.hasMoreElements()) {
126             destroy((DatabaseSession)toRemoveSessions.nextElement());
127         }
128     }
129
130     private ClassLoader JavaDoc getMyClassLoader(){
131         ClassLoader JavaDoc classLoader = null;
132         if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
133             try{
134                 return (ClassLoader JavaDoc)AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(this.getClass()));
135             } catch (PrivilegedActionException JavaDoc exc){
136                 return null;
137             }
138         } else {
139             return PrivilegedAccessHelper.getClassLoaderForClass(this.getClass());
140         }
141     }
142     
143     /**
144      * PUBLIC:
145      * Return the singleton session manager.
146      * This allow global access to a set of named sessions.
147      */

148     public synchronized static SessionManager getManager() {
149         if (manager == null) {
150             manager = new SessionManager();
151         }
152
153         return manager;
154     }
155
156     /**
157      * PUBLIC:
158      * Return the session by name.
159      * Use the classLoader that loaded the SessionManager.
160      */

161     public AbstractSession getSession(String JavaDoc sessionName) {
162         return getSession(sessionName, getMyClassLoader(), true, false, false);
163     }
164
165     /**
166      * PUBLIC:
167      * Return the session by name.
168      * Use the classLoader that loaded the SessionManager.
169      * Log the session in only if the user specifies to.
170      */

171     public AbstractSession getSession(String JavaDoc sessionName, boolean shouldLoginSession) {
172         return getSession(sessionName, getMyClassLoader(), shouldLoginSession, false, false);
173     }
174
175     /**
176      * PUBLIC:
177      * Return the session by name.
178      * Use the classLoader that loaded the SessionManager.
179      * Log the session in only if the user specifies to.
180      * Refresh the session only if the user specifies to.
181      */

182     public AbstractSession getSession(String JavaDoc sessionName, boolean shouldLoginSession, boolean shouldRefreshSession) {
183         return getSession(sessionName, getMyClassLoader(), shouldLoginSession, shouldRefreshSession, false);
184     }
185
186     /**
187      * PUBLIC:
188      * Return the session by name.
189      * Provide the class loader for loading the project, the configuration file
190      * and the deployed classes.
191      * E.g. SessionManager.getManager().getSession("mySession", MySessionBean.getClassLoader());
192      * This method will cause the class loader to be compared with the classloader
193      * used to load the original session of this name, with this classloader. If
194      * they are not the same then the session will be refreshed.
195      */

196     public AbstractSession getSession(String JavaDoc sessionName, ClassLoader JavaDoc objectClassLoader) {
197         return getSession(sessionName, objectClassLoader, true, false, false);
198     }
199
200     /**
201      * PUBLIC:
202      * Return the session by name, loading the configuration from the file
203      * specified in the xmlLoader. Provide the class loader for loading the
204      * project, the configuration file and the deployed classes. Pass in true for
205      * shouldLoginSession if the session returned should be logged in before
206      * returned otherwise false. Pass in true for shouldRefreshSession if the
207      * XMLSessionConfigLoader should reparse the configuration file for new
208      * sessions. False, will cause the XMLSessionConfigLoader not to parse the
209      * file again.
210      * This method will cause the class loader to be compared with the classloader
211      * used to load the original session of this name, with this classloader. If
212      * they are not the same then the session will be refreshed.
213      */

214     public synchronized AbstractSession getSession(String JavaDoc sessionName, ClassLoader JavaDoc objectClassLoader, boolean shouldLoginSession, boolean shouldRefreshSession, boolean shouldCheckClassLoader) {
215         AbstractSession session = (AbstractSession)getSessions().get(sessionName);
216         if (shouldCheckClassLoader && (session != null) && !session.getDatasourcePlatform().getConversionManager().getLoader().equals(objectClassLoader)) {
217             //bug 3766808 if a different classloader is being used then a reload of the session should
218
//be completed otherwise failures may occur
219
shouldRefreshSession = true;
220         }
221         if ((session == null) || shouldRefreshSession) {
222             if (session != null) {
223                 if (session.isDatabaseSession() && session.isConnected()) {
224                     ((DatabaseSession)session).logout();
225                 }
226
227                 getSessions().remove(sessionName);
228             }
229         }
230
231         if (session == null) {
232             logAndThrowException(SessionLog.WARNING, ValidationException.noSessionFound(sessionName, ""));
233         } else if (shouldLoginSession && !session.isConnected()) {
234             ((DatabaseSession)session).login();
235         }
236
237         return session;
238     }
239
240     /**
241      * INTERNAL:
242      * Log exceptions to the default log then throw them.
243      */

244     private void logAndThrowException(int level, RuntimeException JavaDoc exception) throws RuntimeException JavaDoc {
245         AbstractSessionLog.getLog().logThrowable(level, exception);
246         throw exception;
247     }
248
249     /**
250      * INTERNAL:
251      * Set a hashtable of all sessions
252      */

253     public void setSessions(Hashtable sessions) {
254         this.sessions = sessions;
255     }
256
257     /**
258      * INTERNAL:
259      * Return a hashtable on all sessions.
260      */

261     public Hashtable getSessions() {
262         return sessions;
263     }
264
265     /**
266      * PUBLIC:
267      * Set the default session.
268      * Other sessions are supported through the getSession by name API.
269      */

270     public void setDefaultSession(Session defaultSession) {
271         this.defaultSession = (AbstractSession)defaultSession;
272         addSession("default", defaultSession);
273     }
274
275     /**
276      * INTERNAL:
277      * Set the singleton session manager.
278      * This allows global access to a set of named sessions.
279      */

280     public static void setManager(SessionManager theManager) {
281         manager = theManager;
282     }
283 }
284
Popular Tags