001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: PagedSessionHandle.java,v 1.2 2006-06-15 13:44:07 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.sessionEnhydra;
025:
026: import com.lutris.appserver.server.session.SessionException;
027: import com.lutris.util.PersistentStore;
028:
029: /**
030: * A lightweight handle to a session which has been paged to disk.
031: * Referenced to the session manger and user associated with the
032: * session are not paged. Instead they are heald by this object
033: * until the session and corresponding session data are read
034: * back from disk.
035: *
036: * @see PagedSessionHome
037: * @version $Revision: 1.2 $
038: * @author Kyle Clark
039: */
040: class PagedSessionHandle {
041:
042: /**
043: * Keep track if the session object that is paged to disk
044: * is new.
045: */
046: private boolean sessionIsNew;
047:
048: /**
049: * Keep track of time session was created.
050: */
051: private long sessionTimeCreate;
052:
053: /**
054: * The store interface.
055: */
056: private PersistentStore store;
057:
058: /**
059: * The session that is being paged.
060: */
061: private PagedSession session;
062:
063: /**
064: * The session key.
065: */
066: private String sessionKey;
067:
068: /**
069: * Reference to transient data that should be
070: * held by the handle.
071: */
072: private Object[] transientData;
073:
074: /**
075: * Creates an instance of PagedSessionHandle.
076: *
077: * @param config
078: * configuration settings required by this object.
079: * @param session
080: * the session represented by this object.
081: */
082: PagedSessionHandle(PagedSession session, PersistentStore store) {
083: this .session = session;
084: this .store = store;
085:
086: // Convenience - performance
087: this .sessionIsNew = session.isNew();
088: this .sessionTimeCreate = session.getTimeCreated();
089: this .sessionKey = session.getSessionKey();
090: /**DACHA & TUFA
091: * need for restoring transient data when restart application
092: */
093: this .transientData = session.getTransientData();
094: }
095:
096: /**
097: * Pages the session to the file system.
098: *
099: * @exception SessionException if an error occurs.
100: */
101: synchronized void write() throws SessionException {
102: try {
103: transientData = session.getTransientData();
104: store.store(sessionKey, session);
105: // delete reference to session
106: // can be cleaned up via garbage collector.
107: session = null;
108: } catch (Exception e) {
109: throw new SessionException(
110: "Unable to write session to persistent store.", e);
111: }
112: }
113:
114: /**
115: * Reads the session from the file system. The session is
116: * deleted from the file system. May return null.
117: *
118: * @return the session.
119: * @exception SessionException if an error occurs.
120: */
121: synchronized PagedSession read() throws SessionException {
122: if (session != null) {
123: return session;
124: }
125: try {
126: session = (PagedSession) store.remove(sessionKey);
127: session.restoreTransientData(transientData);
128: return session;
129: } catch (Exception e) {
130: throw new SessionException(
131: "Unable to read session from persistent store.", e);
132: }
133: }
134:
135: /**
136: * Deletes the paged session.
137: *
138: * @exception SessionException if an error occurs.
139: */
140: synchronized void delete() throws SessionException {
141: try {
142: store.delete(sessionKey);
143: } catch (Exception e) {
144: throw new SessionException(
145: "Unable to delete session from persistent store.",
146: e);
147: }
148: }
149:
150: /**
151: * Returns true if the session referenced by this handle is new.
152: *
153: * @return
154: * true if the session referenced by this handle is new.
155: */
156: boolean isNew() {
157: return sessionIsNew;
158: }
159:
160: /**
161: * Returns the time the session referenced by this handle
162: * was created.
163: *
164: * @return
165: * the time the session referenced by this handle
166: * was created.
167: */
168: long getTimeCreated() {
169: return sessionTimeCreate;
170: }
171:
172: /**
173: * Returns the session key associated with this object.
174: *
175: * @return
176: * the session key.
177: */
178: String getSessionKey() {
179: return sessionKey;
180: }
181:
182: }
|