KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > main > CmsSessionInfo


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsSessionInfo.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.16 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.main;
33
34 import org.opencms.file.CmsRequestContext;
35 import org.opencms.file.CmsUser;
36
37 import org.apache.commons.collections.Buffer;
38 import org.apache.commons.collections.BufferUtils;
39 import org.apache.commons.collections.buffer.BoundedFifoBuffer;
40
41 /**
42  * Stores information about a user that has authenticated himself the OpenCms security system.<p>
43  *
44  * This object is used to provide information about all authenticated users in the system
45  * with the {@link org.opencms.main.CmsSessionManager}.<p>
46  *
47  * This object is available for all authenticated users after login.
48  * If a user has not logged in, he may have a session on the servlet engine,
49  * but he will have no session info object attached. For example the "Guest" user
50  * may have multiple sessions, but no session info is created for him.<p>
51  *
52  * @author Alexander Kandzior
53  * @author Andreas Zahner
54  *
55  * @version $Revision: 1.16 $
56  *
57  * @since 6.0.0
58  */

59 public class CmsSessionInfo implements Comparable JavaDoc {
60
61     /** Maximum size of the broadcast queue for one user. */
62     public static final int QUEUE_SIZE = 10;
63
64     /** The broadcast queue buffer for the user of this session info. */
65     private Buffer m_broadcastQueue;
66
67     /** The maximum time, in seconds, this session info is allowed to be inactive. */
68     private int m_maxInactiveInterval;
69
70     /** The current project id of the user. */
71     private int m_projectId;
72
73     /** The id of the (http) session this session info belongs to. */
74     private String JavaDoc m_sessionId;
75
76     /** The current site of the user. */
77     private String JavaDoc m_siteRoot;
78
79     /** The time this session info was created. */
80     private long m_timeCreated;
81
82     /** The time this session info was last updated. */
83     private long m_timeUpdated;
84
85     /** The user to which this session info belongs. */
86     private CmsUser m_user;
87
88     /**
89      * Creates a new CmsSessionInfo object.<p>
90      *
91      * @param context the user context to create this session info for
92      * @param sessionId id of the (http) session this session info belongs to
93      * @param maxInactiveInterval the maximum time, in seconds, this session info is allowed to be inactive
94      */

95     public CmsSessionInfo(CmsRequestContext context, String JavaDoc sessionId, int maxInactiveInterval) {
96
97         m_timeCreated = System.currentTimeMillis();
98         m_sessionId = sessionId;
99         m_maxInactiveInterval = maxInactiveInterval;
100         m_user = context.currentUser();
101         update(context);
102         m_broadcastQueue = BufferUtils.synchronizedBuffer(new BoundedFifoBuffer(QUEUE_SIZE));
103     }
104
105     /**
106      * Allows sorting session info according to the user names.<p>
107      *
108      * @see java.lang.Comparable#compareTo(java.lang.Object)
109      */

110     public int compareTo(Object JavaDoc obj) {
111
112         if (obj == this) {
113             return 0;
114         }
115         if (!(obj instanceof CmsSessionInfo)) {
116             return m_user.getName().compareTo(((CmsSessionInfo)obj).getUser().getName());
117         }
118         return 0;
119     }
120
121     /**
122      * Returns the broadcast queue of the user to which this session info belongs.<p>
123      *
124      * @return the broadcast queue of the user to which this session info belongs
125      */

126     public Buffer getBroadcastQueue() {
127
128         return m_broadcastQueue;
129     }
130
131     /**
132      * Returns the maximum time, in seconds, this session info is allowed to be inactive.<p>
133      *
134      * The inactive time is the time since the last call to the {@link #update(CmsRequestContext)}
135      * method. If the inactive time is greater then the maximum allowed time, this
136      * session info will be removed from the session manager.<p>
137      *
138      * @return the maximum time, in seconds, this session info is allowed to be inactive
139      *
140      * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
141      */

142     public int getMaxInactiveInterval() {
143
144         return m_maxInactiveInterval;
145     }
146
147     /**
148      * Returns the id of the project of the user.<p>
149      *
150      * @return the id of the project
151      */

152     public int getProject() {
153
154         return m_projectId;
155     }
156
157     /**
158      * Returns the id of the (http) session this session info belongs to.<p>
159      *
160      * @return the id of the (http) session this session info belongs to
161      *
162      * @see javax.servlet.http.HttpSession#getId()
163      */

164     public String JavaDoc getSessionId() {
165
166         return m_sessionId;
167     }
168
169     /**
170      * Returns the current site root of the user.<p>
171      *
172      * @return the current site root of the user
173      */

174     public String JavaDoc getSiteRoot() {
175
176         return m_siteRoot;
177     }
178
179     /**
180      * Returns the time, in miliseconds, this session has been active,
181      * that is the time of the last update minus the creation time.<p>
182      *
183      * @return the time, in miliseconds, this session has been active
184      */

185     public long getTimeActive() {
186
187         return m_timeUpdated - m_timeCreated;
188     }
189
190     /**
191      * Returns the time this session info was created.<p>
192      *
193      * @return the time this session info was created
194      */

195     public long getTimeCreated() {
196
197         return m_timeCreated;
198     }
199
200     /**
201      * Returns the time this session info was last updated.<p>
202      *
203      * @return the time this session info was last updated
204      */

205     public long getTimeUpdated() {
206
207         return m_timeUpdated;
208     }
209
210     /**
211      * Returns the user to which this session info belongs.<p>
212      *
213      * @return the user to which this session info belongs
214      */

215     public CmsUser getUser() {
216
217         return m_user;
218     }
219
220     /**
221      * Returns <code>true</code> if this session info has expired, that
222      * is it has not been updated in the time set by the maximum inactivitiy interval.<p>
223      *
224      * @return <code>true</code> if this session info has expired
225      */

226     public boolean isExpired() {
227
228         return ((System.currentTimeMillis() - m_timeUpdated) / 1000) > m_maxInactiveInterval;
229     }
230
231     /**
232      * Sets the id of the current project of the user of this session info.<p>
233      *
234      * @param projectId the project id to set
235      */

236     public void setProject(int projectId) {
237
238         m_projectId = projectId;
239     }
240
241     /**
242      * Updates the session info object with the information from
243      * the given request context.<p>
244      *
245      * @param context the requrest context to update the session with
246      */

247     protected void update(CmsRequestContext context) {
248
249         m_timeUpdated = System.currentTimeMillis();
250         m_siteRoot = context.getSiteRoot();
251         setProject(context.currentProject().getId());
252     }
253 }
Popular Tags