KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > SessionManager


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * SessionManager.java
20  *
21  * This object is responsible for managing the ThreadPermissionSession objects
22  * in memory.
23  */

24
25 // package definition
26
package com.rift.coad.lib.security;
27
28 // log 4 j imports
29
import org.apache.log4j.Logger;
30
31
32 /**
33  * This object is responsible for managing the ThreadPermissionSession objects
34  * in memory.
35  *
36  * @author Brett Chaldecott
37  */

38 public final class SessionManager {
39     
40     // log object
41
private Logger log =
42         Logger.getLogger(SessionManager.class.getName());
43     
44     
45     // the classes private singleton member variables
46
private static SessionManager singleton = null;
47     
48     // the classes private member variables
49
private ThreadsPermissionContainer permissions = null;
50     
51     
52     /**
53      * Creates a new instance of SessionManager
54      */

55     private SessionManager(ThreadsPermissionContainer permissions) {
56         this.permissions = permissions;
57     }
58     
59     
60     /**
61      * The init method for the session manager. This will be called to
62      * instanciate the session manager.
63      *
64      * @return The reference to the session manager.
65      * @param permissions The reference to the permission object.
66      */

67     public synchronized static SessionManager init(
68             ThreadsPermissionContainer permissions)
69             throws SecurityException JavaDoc {
70         if (singleton != null) {
71             throw new SecurityException JavaDoc(
72                 "The SessionManager has already been initialized.");
73         }
74         singleton = new SessionManager(permissions);
75         return singleton;
76     }
77     
78     
79     /**
80      * This method returns the current instance of the session manager of a new
81      * on if none exists.
82      *
83      * @return A reference to the singleton session manager.
84      */

85     public synchronized static SessionManager getInstance()
86         throws SecurityException JavaDoc {
87         if (singleton != null) {
88             return singleton;
89         }
90         throw new SecurityException JavaDoc(
91                 "The session manager has not been initialized");
92     }
93     
94     
95     /**
96      * This method will init a new session for the given thread. If one does
97      * exist that session will be over written.
98      *
99      * @param user The user object used to init the session.
100      * @exception SecurityException
101      */

102     public void initSession() throws SecurityException JavaDoc {
103         Long JavaDoc threadId = new Long JavaDoc(Thread.currentThread().getId());
104         permissions.putSession(threadId,
105                 new ThreadPermissionSession(threadId,new UserSession()));
106     }
107     
108     
109     /**
110      * This method returns the permission information for the current session
111      * identified by the thread making the call.
112      *
113      * @return The thread permission session object.
114      * @exception SecurityException
115      */

116     public ThreadPermissionSession getSession()
117     throws SecurityException JavaDoc {
118         return permissions.getSession();
119     }
120     
121     
122     /**
123      * This method removes the session information for the calling thread.
124      *
125      * @exception SecurityException
126      */

127     public void purgeSession() throws SecurityException JavaDoc {
128         Long JavaDoc threadId = new Long JavaDoc(Thread.currentThread().getId());
129         permissions.removeSession(threadId);
130     }
131 }
132
Popular Tags