KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sessionEnhydra > PagedSessionHandle


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: PagedSessionHandle.java,v 1.2 2005/03/24 10:51:20 slobodan Exp $
23  */

24
25 package com.lutris.appserver.server.sessionEnhydra;
26
27 import com.lutris.appserver.server.session.SessionException;
28 import com.lutris.util.PersistentStore;
29
30 /**
31  * A lightweight handle to a session which has been paged to disk.
32  * Referenced to the session manger and user associated with the
33  * session are not paged. Instead they are heald by this object
34  * until the session and corresponding session data are read
35  * back from disk.
36  *
37  * @see PagedSessionHome
38  * @version $Revision: 1.2 $
39  * @author Kyle Clark
40  */

41 class PagedSessionHandle {
42
43     /**
44      * Keep track if the session object that is paged to disk
45      * is new.
46      */

47     private boolean sessionIsNew;
48
49     /**
50      * Keep track of time session was created.
51      */

52     private long sessionTimeCreate;
53
54     /**
55      * The store interface.
56      */

57     private PersistentStore store;
58
59     /**
60      * The session that is being paged.
61      */

62     private PagedSession session;
63
64     /**
65      * The session key.
66      */

67     private String JavaDoc sessionKey;
68
69     /**
70      * Reference to transient data that should be
71      * held by the handle.
72      */

73     private Object JavaDoc[] transientData;
74
75     /**
76      * Creates an instance of PagedSessionHandle.
77      *
78      * @param config
79      * configuration settings required by this object.
80      * @param session
81      * the session represented by this object.
82      */

83     PagedSessionHandle(PagedSession session, PersistentStore store) {
84         this.session = session;
85         this.store = store;
86
87         // Convenience - performance
88
this.sessionIsNew = session.isNew();
89         this.sessionTimeCreate = session.getTimeCreated();
90         this.sessionKey = session.getSessionKey();
91         /**DACHA & TUFA
92          * need for restoring transient data when restart application
93          */

94         this.transientData = session.getTransientData();
95     }
96
97     /**
98      * Pages the session to the file system.
99      *
100      * @exception SessionException if an error occurs.
101      */

102     synchronized void write() throws SessionException {
103         try {
104             transientData = session.getTransientData();
105             store.store(sessionKey, session);
106             // delete reference to session
107
// can be cleaned up via garbage collector.
108
session = null;
109         } catch (Exception JavaDoc e) {
110             throw new SessionException("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 JavaDoc e) {
130             throw new SessionException("Unable to read session from persistent store.", e);
131         }
132     }
133
134     /**
135      * Deletes the paged session.
136      *
137      * @exception SessionException if an error occurs.
138      */

139     synchronized void delete() throws SessionException {
140         try {
141             store.delete(sessionKey);
142         } catch (Exception JavaDoc e) {
143             throw new SessionException("Unable to delete session from persistent store.", e);
144         }
145     }
146
147     /**
148      * Returns true if the session referenced by this handle is new.
149      *
150      * @return
151      * true if the session referenced by this handle is new.
152      */

153     boolean isNew() {
154         return sessionIsNew;
155     }
156
157     /**
158      * Returns the time the session referenced by this handle
159      * was created.
160      *
161      * @return
162      * the time the session referenced by this handle
163      * was created.
164      */

165     long getTimeCreated() {
166         return sessionTimeCreate;
167     }
168
169     /**
170      * Returns the session key associated with this object.
171      *
172      * @return
173      * the session key.
174      */

175     String JavaDoc getSessionKey() {
176         return sessionKey;
177     }
178
179 }
180
181
Popular Tags