001: /*
002: * JOSSO: Java Open Single Sign-On
003: *
004: * Copyright 2004-2008, Atricore, Inc.
005: *
006: * This is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as
008: * published by the Free Software Foundation; either version 2.1 of
009: * the License, or (at your option) any later version.
010: *
011: * This software is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this software; if not, write to the Free
018: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
020: */
021: package org.josso.gateway.session.service;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.josso.Lookup;
026: import org.josso.gateway.event.security.SSOSecurityEventManager;
027:
028: /**
029: * @author <a href="mailto:sgonzalez@josso.org">Sebastian Gonzalez Oyuela</a>
030: * @version $Id: BaseSessionImpl.java 508 2008-02-18 13:32:29Z sgonzalez $
031: */
032:
033: public class BaseSessionImpl implements BaseSession {
034:
035: private static final Log logger = LogFactory
036: .getLog(BaseSessionImpl.class);
037:
038: // This session identifier.
039: protected String _id;
040:
041: // Flag indicating if this session is valid.
042: protected boolean _valid;
043:
044: // Session creation time.
045: protected long _creationTime;
046:
047: // Session max inactive interval.
048: protected int _maxInactiveInterval = -1;
049:
050: // Session access time.
051: protected long _lastAccessedTime;
052:
053: // Session access count.
054: protected long _accessCount;
055:
056: // Indicates that this session is expiring.
057: protected boolean _expiring;
058:
059: // The username associated to this session
060: protected String _username;
061:
062: public BaseSessionImpl() {
063: }
064:
065: // ---------------------------------------------------------------
066: // SSOSession interface.
067: // ---------------------------------------------------------------
068:
069: /**
070: * The SSO Session id. This is a unique id.
071: *
072: * @return the session id.
073: */
074: public String getId() {
075: return _id;
076: }
077:
078: /**
079: * This method returns true if the session is valid.
080: * It checks if this session should be expired.
081: *
082: * @return the session status.
083: */
084: public boolean isValid() {
085:
086: if (!_valid) {
087: return _valid;
088: }
089:
090: if (_maxInactiveInterval >= 0) {
091: long timeNow = System.currentTimeMillis();
092: int timeIdle = (int) ((timeNow - _lastAccessedTime) / 1000L);
093: if (timeIdle >= _maxInactiveInterval) {
094: expire();
095: }
096: }
097:
098: return (_valid);
099: }
100:
101: /**
102: * Set the maximum time interval, in seconds, between client requests
103: * before the SSO Service will invalidate the session. A negative
104: * time indicates that the session should never time out.
105: *
106: * @param interval The new maximum interval
107: */
108: public void setMaxInactiveInterval(int interval) {
109: _maxInactiveInterval = interval;
110: isValid();
111: }
112:
113: public int getMaxInactiveInterval() {
114: return _maxInactiveInterval;
115: }
116:
117: /**
118: * Gets this session creation time in milliseconds.
119: */
120: public long getCreationTime() {
121: return _creationTime;
122: }
123:
124: /**
125: * Gets this session last access time in milliseconds.
126: */
127: public long getLastAccessTime() {
128: return _lastAccessedTime;
129: }
130:
131: /**
132: * Gets this session access count.
133: */
134: public long getAccessCount() {
135: return _accessCount;
136: }
137:
138: // ----------------------------------------------------------
139: // Base Session
140: // ----------------------------------------------------------
141:
142: /**
143: * Update the accessed time information for this session.
144: */
145: public void access() {
146:
147: _lastAccessedTime = System.currentTimeMillis();
148:
149: // Check if the session is valid ...
150: isValid();
151:
152: _accessCount++;
153:
154: }
155:
156: /**
157: * This method expires a session. The isValid method will return false.
158: */
159: public void expire() {
160:
161: setValid(false);
162:
163: // Mark this session as "being expired" if needed
164: if (_expiring)
165: return;
166:
167: synchronized (this ) {
168:
169: _expiring = true;
170: _accessCount = 0;
171: setValid(false);
172:
173: // Notify interested session event listeners
174: fireSessionEvent(BaseSession.SESSION_DESTROYED_EVENT, null);
175:
176: // We have completed expire of this session
177: _expiring = false;
178:
179: }
180:
181: }
182:
183: // -----------------------------------------------------------------------
184: // Package utils
185: // -----------------------------------------------------------------------
186:
187: /**
188: * Set the id of this session, used when initializing new sessions.
189: *
190: * @param id the session id.
191: */
192: public void setId(String id) {
193: _id = id;
194:
195: // Notify session event listeners
196: fireSessionEvent(BaseSession.SESSION_CREATED_EVENT, null);
197: }
198:
199: /**
200: * Set the creation time for this session.
201: *
202: * @param time The new creation time
203: */
204: public void setCreationTime(long time) {
205: _creationTime = time;
206: _lastAccessedTime = time;
207: }
208:
209: /**
210: * Set the valid flag for this session.
211: *
212: * @param valid The new value for the valid property.
213: */
214: public void setValid(boolean valid) {
215: _valid = valid;
216: }
217:
218: /**
219: * Notify all session event listeners that a particular event has
220: * occurred for this Session. The default implementation performs
221: * this notification synchronously using the calling thread.
222: *
223: * Note : Do not use this method outside the GWY ...
224: *
225: * @param type Event type
226: * @param data Event data
227: */
228: public void fireSessionEvent(String type, Object data) {
229:
230: try {
231: SSOSecurityEventManager em = (SSOSecurityEventManager) Lookup
232: .getInstance().lookupSecurityDomain()
233: .getEventManager();
234: em
235: .fireSessionEvent(this .getUsername(), getId(),
236: type, data);
237: } catch (Exception e) {
238: logger.error(
239: "Can't send session event : " + e.getMessage(), e);
240: }
241: }
242:
243: /**
244: * Getter for the username associated with this session, if any
245: */
246: public String getUsername() {
247: return _username;
248: }
249:
250: /**
251: * Setter for the username associated with this session
252: */
253: public void setUsername(String username) {
254: _username = username;
255: }
256:
257: public String toString() {
258: return _id + " [" + _username + "] "
259: + new java.util.Date(_creationTime);
260: }
261:
262: }
|