001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsSessionInfo.java,v $
003: * Date : $Date: 2008-02-27 12:05:39 $
004: * Version: $Revision: 1.21 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.main;
033:
034: import org.opencms.file.CmsRequestContext;
035: import org.opencms.util.CmsUUID;
036:
037: import java.io.Serializable;
038:
039: import org.apache.commons.collections.Buffer;
040: import org.apache.commons.collections.BufferUtils;
041: import org.apache.commons.collections.buffer.UnboundedFifoBuffer;
042:
043: /**
044: * Stores information about a user that has authenticated himself the OpenCms security system.<p>
045: *
046: * This object is used to provide information about all authenticated users in the system
047: * with the {@link org.opencms.main.CmsSessionManager}.<p>
048: *
049: * This object is available for all authenticated users after login.
050: * If a user has not logged in, he may have a session on the servlet engine,
051: * but he will have no session info object attached. For example the "Guest" user
052: * may have multiple sessions, but no session info is created for him.<p>
053: *
054: * @author Alexander Kandzior
055: * @author Andreas Zahner
056: *
057: * @version $Revision: 1.21 $
058: *
059: * @since 6.0.0
060: */
061: public class CmsSessionInfo implements Comparable, Serializable {
062:
063: /** Name of the http session attribute the OpenCms session id is stored in. */
064: public static final String ATTRIBUTE_SESSION_ID = "__org.opencms.main.CmsSessionInfo#m_sessionId";
065:
066: /** Maximum size of the broadcast queue for one user. */
067: public static final int QUEUE_SIZE = 10;
068:
069: /** Serial version UID required for safe serialization. */
070: private static final long serialVersionUID = 927301527031117920L;
071:
072: /** The broadcast queue buffer for the user of this session info. */
073: private transient Buffer m_broadcastQueue;
074:
075: /** The maximum time, in seconds, this session info is allowed to be inactive. */
076: private int m_maxInactiveInterval;
077:
078: /** The fully qualified name of the organizational unit. */
079: private String m_ouFqn;
080:
081: /** The current project id of the user. */
082: private CmsUUID m_projectId;
083:
084: /** The id of the (http) session this session info belongs to. */
085: private CmsUUID m_sessionId;
086:
087: /** The current site of the user. */
088: private String m_siteRoot;
089:
090: /** The time this session info was created. */
091: private long m_timeCreated;
092:
093: /** The time this session info was last updated. */
094: private long m_timeUpdated;
095:
096: /** The id of user to which this session info belongs. */
097: private CmsUUID m_userId;
098:
099: /**
100: * Creates a new CmsSessionInfo object.<p>
101: *
102: * @param context the user context to create this session info for
103: * @param sessionId OpenCms id of the (http) session this session info belongs to
104: * @param maxInactiveInterval the maximum time, in seconds, this session info is allowed to be inactive
105: */
106: public CmsSessionInfo(CmsRequestContext context, CmsUUID sessionId,
107: int maxInactiveInterval) {
108:
109: m_timeCreated = System.currentTimeMillis();
110: m_sessionId = sessionId;
111: m_maxInactiveInterval = maxInactiveInterval;
112: m_userId = context.currentUser().getId();
113: update(context);
114: }
115:
116: /**
117: * Allows sorting session info according to the user names.<p>
118: *
119: * @see java.lang.Comparable#compareTo(java.lang.Object)
120: */
121: public int compareTo(Object obj) {
122:
123: if (obj == this ) {
124: return 0;
125: }
126: if (obj instanceof CmsSessionInfo) {
127: return m_userId.compareTo(((CmsSessionInfo) obj)
128: .getUserId());
129: }
130: return 0;
131: }
132:
133: /**
134: * @see java.lang.Object#equals(java.lang.Object)
135: */
136: public boolean equals(Object obj) {
137:
138: if (obj == this ) {
139: return true;
140: }
141: if (obj instanceof CmsSessionInfo) {
142: return m_userId.equals(((CmsSessionInfo) obj).getUserId());
143: }
144: return false;
145: }
146:
147: /**
148: * Returns the broadcast queue of the user to which this session info belongs.<p>
149: *
150: * @return the broadcast queue of the user to which this session info belongs
151: */
152: public Buffer getBroadcastQueue() {
153:
154: if (m_broadcastQueue == null) {
155: m_broadcastQueue = BufferUtils
156: .synchronizedBuffer(new UnboundedFifoBuffer(
157: QUEUE_SIZE));
158: }
159: return m_broadcastQueue;
160: }
161:
162: /**
163: * Returns the maximum time, in seconds, this session info is allowed to be inactive.<p>
164: *
165: * The inactive time is the time since the last call to the {@link #update(CmsRequestContext)}
166: * method. If the inactive time is greater then the maximum allowed time, this
167: * session info will be removed from the session manager.<p>
168: *
169: * @return the maximum time, in seconds, this session info is allowed to be inactive
170: *
171: * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
172: */
173: public int getMaxInactiveInterval() {
174:
175: return m_maxInactiveInterval;
176: }
177:
178: /**
179: * Returns the fully qualified name of the organizational unit for this session.<p>
180: *
181: * @return the fully qualified name of the organizational unit for this session
182: */
183: public String getOrganizationalUnitFqn() {
184:
185: return m_ouFqn;
186: }
187:
188: /**
189: * Returns the id of the project of the user.<p>
190: *
191: * @return the id of the project
192: */
193: public CmsUUID getProject() {
194:
195: return m_projectId;
196: }
197:
198: /**
199: * Returns the id of the OpenCms (http) session this session info belongs to.<p>
200: *
201: * @return the id of the OpenCms (http) session this session info belongs to
202: *
203: * @see javax.servlet.http.HttpSession#getId()
204: */
205: public CmsUUID getSessionId() {
206:
207: return m_sessionId;
208: }
209:
210: /**
211: * Returns the current site root of the user.<p>
212: *
213: * @return the current site root of the user
214: */
215: public String getSiteRoot() {
216:
217: return m_siteRoot;
218: }
219:
220: /**
221: * Returns the time, in milliseconds, this session has been active,
222: * that is the time of the last update minus the creation time.<p>
223: *
224: * @return the time, in milliseconds, this session has been active
225: */
226: public long getTimeActive() {
227:
228: return m_timeUpdated - m_timeCreated;
229: }
230:
231: /**
232: * Returns the time this session info was created.<p>
233: *
234: * @return the time this session info was created
235: */
236: public long getTimeCreated() {
237:
238: return m_timeCreated;
239: }
240:
241: /**
242: * Returns the time this session info was last updated.<p>
243: *
244: * @return the time this session info was last updated
245: */
246: public long getTimeUpdated() {
247:
248: return m_timeUpdated;
249: }
250:
251: /**
252: * Returns the id of the user to which this session info belongs.<p>
253: *
254: * @return the id of the user to which this session info belongs
255: */
256: public CmsUUID getUserId() {
257:
258: return m_userId;
259: }
260:
261: /**
262: * @see java.lang.Object#hashCode()
263: */
264: public int hashCode() {
265:
266: return m_userId.hashCode();
267: }
268:
269: /**
270: * Returns <code>true</code> if this session info has expired, that
271: * is it has not been updated in the time set by the maximum inactivity interval.<p>
272: *
273: * @return <code>true</code> if this session info has expired
274: */
275: public boolean isExpired() {
276:
277: return ((System.currentTimeMillis() - m_timeUpdated) / 1000) > m_maxInactiveInterval;
278: }
279:
280: /**
281: * @see java.lang.Object#toString()
282: */
283: public String toString() {
284:
285: StringBuffer str = new StringBuffer(64);
286: str.append("[");
287: str.append("sessionId: ").append(m_sessionId).append(", ");
288: str.append("userId: ").append(m_userId).append(", ");
289: str.append("projectId: ").append(m_projectId).append(", ");
290: str.append("siteRoot: ").append(m_siteRoot).append(", ");
291: str.append("timeCreated: ").append(m_timeCreated).append(", ");
292: str.append("timeUpdated: ").append(m_timeUpdated).append(", ");
293: str.append("maxInactiveInterval: ").append(
294: m_maxInactiveInterval);
295: str.append("ouFqn: ").append(m_ouFqn);
296: str.append("]");
297: return str.toString();
298: }
299:
300: /**
301: * Sets the id of the current project of the user of this session info.<p>
302: *
303: * @param projectId the project id to set
304: */
305: protected void setProject(CmsUUID projectId) {
306:
307: m_projectId = projectId;
308: }
309:
310: /**
311: * Updates the session info object with the information from
312: * the given request context.<p>
313: *
314: * @param context the request context to update the session with
315: */
316: protected void update(CmsRequestContext context) {
317:
318: m_timeUpdated = System.currentTimeMillis();
319: m_siteRoot = context.getSiteRoot();
320: setProject(context.currentProject().getUuid());
321: m_ouFqn = context.getOuFqn();
322: }
323: }
|