01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.session;
05:
06: import com.tc.util.sequence.Sequence;
07:
08: public class SessionManagerImpl implements SessionManager,
09: SessionProvider {
10:
11: private final Sequence sequence;
12: private SessionID sessionID = SessionID.NULL_ID;
13: private SessionID nextSessionID = SessionID.NULL_ID;
14:
15: public SessionManagerImpl(Sequence sequence) {
16: this .sequence = sequence;
17: }
18:
19: public synchronized SessionID getSessionID() {
20: return sessionID;
21: }
22:
23: /*
24: * Return the next session id will be when call newSession.
25: * This advances session id but not apply to messages creation.
26: * Message filter uses it to drop old messages when session changes.
27: */
28: public synchronized SessionID nextSessionID() {
29: if (nextSessionID == SessionID.NULL_ID) {
30: nextSessionID = new SessionID(sequence.next());
31: }
32: return (nextSessionID);
33: }
34:
35: public synchronized void newSession() {
36: if (nextSessionID != SessionID.NULL_ID) {
37: sessionID = nextSessionID;
38: nextSessionID = SessionID.NULL_ID;
39: } else {
40: sessionID = new SessionID(sequence.next());
41: }
42: }
43:
44: public synchronized boolean isCurrentSession(SessionID compare) {
45: return sessionID.equals(compare);
46: }
47:
48: public synchronized String toString() {
49: return getClass().getName() + "[current session=" + sessionID
50: + "]";
51: }
52:
53: }
|