001: package com.lutris.appserver.server.sessionContainerAdapter;
002:
003: import java.io.Serializable;
004: import java.util.Date;
005:
006: import javax.servlet.http.HttpSession;
007: import javax.servlet.http.HttpSessionActivationListener;
008: import javax.servlet.http.HttpSessionBindingEvent;
009: import javax.servlet.http.HttpSessionBindingListener;
010: import javax.servlet.http.HttpSessionEvent;
011:
012: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
013: import com.lutris.appserver.server.session.SessionData;
014: import com.lutris.appserver.server.session.SessionException;
015: import com.lutris.appserver.server.session.SessionManager;
016: import com.lutris.appserver.server.user.User;
017:
018: /**
019: * <p>Description: </p>
020: * Session object, used to keep the session data
021: * @version 1.0
022: */
023: public class ContainerAdapterSession implements
024: com.lutris.appserver.server.session.Session,
025: HttpSessionActivationListener, HttpSessionBindingListener,
026: Serializable {
027:
028: /**
029: * user object
030: */
031: private User user;
032: /**
033: * Session dat, must be serializable since the servlet container really manages
034: * the sessions, and Tomcat's standard manager implementation needs serializable objects
035: */
036: private SerializableSessionData sessionData;
037: /**
038: * SessionManager that is used to create this Session object
039: */
040: private transient SessionManager sessionManager;
041:
042: private transient HttpSession httpSession;
043:
044: /**
045: * Name of the session object in the HttpSession
046: */
047: protected String SESSION = "session";
048:
049: /**
050: * Default constructor
051: */
052: public ContainerAdapterSession() {
053: }
054:
055: /**
056: *
057: * @param sessionManager SessionManager that is used to create this Session object
058: * @param sessionKey The identifiction of the session
059: */
060: public ContainerAdapterSession(
061: ContainerAdapterSessionManager sessionManager,
062: HttpSession httpSession) {
063:
064: this .sessionManager = sessionManager;
065: this .httpSession = httpSession;
066: sessionData = new SerializableSessionData();
067: user = null;
068: }
069:
070: /**
071: * @return the User object;
072: */
073: public User getUser() {
074: return user;
075: }
076:
077: /**
078: * Sets the user
079: * @param user - the user object of the session
080: * @throws SessionException
081: */
082: public void setUser(User user)
083: throws com.lutris.appserver.server.session.SessionException {
084: this .user = user;
085: }
086:
087: /**
088: * clears the user object
089: * @throws SessionException
090: */
091: public void clearUser()
092: throws com.lutris.appserver.server.session.SessionException {
093: user = null;
094: }
095:
096: /**
097: *
098: * @return the session identification
099: */
100: public String getSessionKey() {
101: if (httpSession != null)
102: return httpSession.getId();
103: else
104: return null;
105: }
106:
107: /**
108: *
109: * @return the session creation time
110: */
111: public long getTimeCreated() {
112: if (httpSession != null)
113: return httpSession.getCreationTime();
114: else
115: return -1;
116: }
117:
118: /**
119: * Obtain the time of last use for this object. The time is in
120: * milliseconds since Midnight, Jan 1, 1970 (epoch).
121: *
122: * @return The time of last use since epoch for this object.
123: */
124:
125: public long getTimeLastUsed() {
126: if (httpSession != null)
127: return httpSession.getLastAccessedTime();
128: else
129: return -1;
130: }
131:
132: protected void setTimeCreated(long cTime) {
133: }
134:
135: /**
136: * Obtain the maximum idle time for this object. Zero (or negative)
137: * indicates that sessions may be idle indefinetly.
138: *
139: * @return The maximum number of milliseconds this session
140: * may be idle.
141: */
142: public long getMaxIdleTime() {
143: if (httpSession != null)
144: return httpSession.getMaxInactiveInterval();
145: else
146: return -1;
147: }
148:
149: /**
150: * Set the maximum idle time for this object. Set this to zero
151: * (or negative) to disable idle checking.
152: *
153: * @param maxIdleTime The maximum number of milliseconds this
154: * session may be idle, or zero (or negative) to allow sessions to be idle
155: * indefinetly.
156: */
157: public void setMaxIdleTime(int maxIdleTime) {
158: if (httpSession != null)
159: httpSession.setMaxInactiveInterval(maxIdleTime);
160: }
161:
162: /**
163: *
164: * @return the <code> SessionManager </code>used to create this session
165: */
166: public SessionManager getSessionManager() {
167: return sessionManager;
168:
169: }
170:
171: public void setSessionManager(SessionManager sessionManager) {
172: this .sessionManager = sessionManager;
173: }
174:
175: /**
176: *
177: * @return the <code> SessionData </code>
178: */
179: public SessionData getSessionData() {
180: return sessionData;
181: }
182:
183: /**
184: * puts the <code> SessionData </code> into this session
185: * @param sessionData
186: */
187: public void setSessionData(SessionData sessionData) {
188: this .sessionData = (SerializableSessionData) sessionData;
189: }
190:
191: public boolean isNew() {
192: if (httpSession != null)
193: return (httpSession.getCreationTime() == httpSession
194: .getLastAccessedTime());
195: else
196: return true;
197: }
198:
199: public String toString() {
200:
201: StringBuffer result = new StringBuffer();
202: Date ct = new Date(getTimeCreated());
203: result.append("CreationTime:");
204: result.append(" ");
205: result.append(ct.toString());
206:
207: if (getUser() != null) {
208: result.append("\n");
209: result.append("User:");
210: result.append(" ");
211: result.append(getUser().getName());
212:
213: }
214: if (getSessionData() != null) {
215: String data = getSessionData().toString();
216: if (data != null) {
217: result.append("\n");
218: result.append("SessionData:");
219: result.append(" ");
220: result.append(data);
221: }
222: }
223: return result.toString();
224:
225: }
226:
227: public HttpSession getHttpSession() {
228: return httpSession;
229: }
230:
231: public void setHttpSession(HttpSession httpSession) {
232: this .httpSession = httpSession;
233: //this.httpSession.setAttribute(SESSION,this);
234: }
235:
236: public void valueBound(HttpSessionBindingEvent event) {
237:
238: }
239:
240: public void valueUnbound(HttpSessionBindingEvent event) {
241: if (SESSION.equals(event.getName())) {
242: sessionManager = null;
243: httpSession = null;
244: user = null;
245: sessionData = null;
246: }
247: }
248:
249: public void sessionWillPassivate(HttpSessionEvent event) {
250: sessionManager = null;
251: httpSession = null;
252: }
253:
254: public void sessionDidActivate(HttpSessionEvent event) {
255: httpSession = event.getSession();
256: }
257:
258: }
|