KickJava   Java API By Example, From Geeks To Geeks.

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


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  * UserSession.java
20  *
21  * The user object identifying a user to the security system.
22  */

23
24 // the package
25
package com.rift.coad.lib.security;
26
27 // java imports
28
import java.util.Set JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.HashSet JavaDoc;
31
32 // coadunation imports
33
import com.rift.coad.lib.common.RandomGuid;
34
35 /**
36  * The user object identifying a user to the security system.
37  *
38  * @author Brett Chaldecott
39  */

40 public class UserSession implements PrincipalContainer, Cloneable JavaDoc {
41     
42     // classes member variables
43
private String JavaDoc name = null;
44     private String JavaDoc sessionId = null;
45     private Set JavaDoc principals = null;
46     private Date JavaDoc touchTime = new Date JavaDoc();
47     private long expiryTime = 0;
48     private boolean valid = true;
49     
50     /**
51      * The default constructor responsible for creating the default nobody user.
52      *
53      * @exception SecurityException
54      */

55     public UserSession() throws SecurityException JavaDoc {
56         try {
57             name = "nobody";
58             sessionId = RandomGuid.getInstance().getGuid();
59             this.principals = new HashSet JavaDoc();
60         } catch (Exception JavaDoc ex) {
61             throw new SecurityException JavaDoc(
62                     "Failed to initialize the users session : " +
63                     ex.getMessage(),ex);
64         }
65     }
66     
67     
68     /**
69      * Creates a new instance of UserSession
70      */

71     public UserSession(String JavaDoc name, Set JavaDoc principals) throws SecurityException JavaDoc {
72         try {
73             this.name = name;
74             sessionId = RandomGuid.getInstance().getGuid();
75             this.principals = principals;
76         } catch (Exception JavaDoc ex) {
77             throw new SecurityException JavaDoc(
78                     "Failed to initialize the users session : " +
79                     ex.getMessage(),ex);
80         }
81     }
82     
83     
84     /**
85      * Creates a new instance of UserSession
86      *
87      * @param name The name associated with the new user session.
88      * @param sessionId The id of the session.
89      * @param principals The principals of this user.
90      */

91     public UserSession(String JavaDoc name, String JavaDoc sessionId, Set JavaDoc principals) {
92         this.name = name;
93         this.sessionId = sessionId;
94         this.principals = principals;
95     }
96     
97     
98     /**
99      * The getter method for the name of this role.
100      *
101      * @return The string containing the name of this role.
102      */

103     public String JavaDoc getName() {
104         return name;
105     }
106     
107     
108     /**
109      * This method returns the id of the users session id.
110      *
111      * @return The id of the users session.
112      */

113     public String JavaDoc getSessionId() {
114         return sessionId;
115     }
116     
117     
118     /**
119      * This method returns the id of the users session id.
120      *
121      * @return The id of the users session.
122      */

123     public void setSessionId(String JavaDoc sessionId) {
124         this.sessionId = sessionId;
125     }
126     
127     
128     /**
129      * This method returns the list of principals.
130      *
131      * @return The list of principals.
132      */

133     public Set JavaDoc getPrincipals() {
134         return principals;
135     }
136     
137     
138     /**
139      * This method set the list of principals.
140      *
141      * @param The list of principals.
142      */

143     public void setPrincipals(Set JavaDoc principals) {
144         this.principals = principals;
145     }
146     
147     
148     /**
149      * This method returns the touch time of the user session.
150      *
151      * @return The last time this object was touched.
152      */

153     public synchronized Date JavaDoc getTouchTime() {
154         return touchTime;
155     }
156     
157     
158     /**
159      * This method results in the users session being touched.
160      *
161      * @exception SecurityException
162      */

163     public synchronized void touch() throws SecurityException JavaDoc {
164         if (isExpired() || valid == false) {
165             throw new SecurityException JavaDoc(
166                     "The object has expired or been invalidated.");
167         }
168         touchTime = new Date JavaDoc();
169     }
170     
171     
172     /**
173      * The getter for the expiry time value.
174      *
175      * @return the expiry time of this object.
176      */

177     public synchronized long getExpiryTime() {
178         return expiryTime;
179     }
180     
181     
182     /**
183      * The setter for the expiry time value.
184      *
185      * @param expiryTime The new expiry time.
186      */

187     public synchronized void setExpiryTime(long expiryTime) {
188         this.expiryTime = expiryTime;
189     }
190     
191     
192     /**
193      * This method returns true if this object has expired in memory.
194      *
195      * @return TRUE if expired FALSE if not.
196      */

197     public synchronized boolean isExpired() {
198         if (!valid) {
199             return true;
200         }
201         else if (expiryTime == 0) {
202             return false;
203         }
204         else if (new Date JavaDoc().getTime() > (touchTime.getTime() + expiryTime)) {
205             return true;
206         }
207         return false;
208     }
209     
210     
211     /**
212      * This method will mark this session as invalid.
213      */

214     public synchronized void invalidate() {
215         valid = false;
216     }
217     
218     
219     /**
220      * This method returns a clone of the original user object.
221      *
222      * @return A clone of the orinal object.
223      */

224     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
225         return super.clone();
226     }
227 }
228
Popular Tags