KickJava   Java API By Example, From Geeks To Geeks.

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


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: DiskPagedSessionHome.java,v 1.3 2005/03/24 12:19:38 slobodan Exp $
23  */

24
25 package com.lutris.appserver.server.sessionEnhydra;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29
30 import com.lutris.appserver.server.Enhydra;
31 import com.lutris.appserver.server.session.SessionException;
32 import com.lutris.logging.Logger;
33 import com.lutris.util.Config;
34 import com.lutris.util.ConfigException;
35 import com.lutris.util.FilePersistentStore;
36 import com.lutris.util.PersistentStore;
37 import com.lutris.util.PersistentStoreException;
38
39 /**
40  * The StandardSessionManager uses PagedSessionHome to manage
41  * a collection of sessions that can be paged to disk.<p>
42  *
43  * PagedSessionHome will page sessions to disk
44  * as soon as a pre-configured threshold of sessions has been
45  * reached. Only sessions in the 'passive' state will be
46  * paged to disk. If all sessions are in the 'active' state
47  * and the threshold has been reached, then a request to create
48  * a new session will block until one of the 'active' sessions
49  * goes into the 'passive' state. At this point the session
50  * that just became 'passive' is paged to disk and a new
51  * session is created.<p>
52  *
53  * Sessions are paged to disk by serializing
54  * the all data (excluding the session manager) that is
55  * associated with a session. This requires that
56  * the session data and user associated with a session
57  * are serializable.<p>
58  *
59  * The following parameters can be used to configure
60  * the PagedSessionHome. They should be grouped together in a section,
61  * normally <code>SessionManager.SessionHome</code>, which is specified to
62  * the constructor.<p>
63  *
64  * <ul>
65  *
66  * <li><code>PageThreshold: {int}</code><p>
67  * Sessions are paged if the total number of sessions exceeds PageThreshold.
68  * If not set, defaults to 100.<p>
69  *
70  * <li><code>PageWait: {int}</code><p>
71  * Specifies the maximum time (in milliseconds) that a thread will
72  * wait for a session to be created or retrieved. If the
73  * page threshold has been reached, then a thread has to wait for
74  * an existing session to be paged before a new session can be
75  * created or a pre-existing session (that has been paged to
76  * disk) can be activated. If not set, defaults to 1 minute.
77  * If this time is exceeded then an exception is thrown - this
78  * prevents a possible deadlock situation.<p>
79  *
80  * <li><code>PageDir: {String}</code><p>
81  * The directory where sessions will be paged. This setting is
82  * required.<p>
83  *
84  * <li><code>MaxSessions: {int}</code><p>
85  * Specifies the maximum number of in use concurrent sessions. If this value
86  * is exceeded then CreateSession() will throw a CreateSessionException.
87  * -1 indicates unlimited session. If MaxSessions is not set
88  * in the application's configuration then MaxSessions defaults to
89  * unlimited sessions.<p>
90  *
91  * <li><code>SaveOnRestart: (true|false}</code><p>
92  * Indicate that shold we saves all (active and passive) sessions on disk
93  * when restart application.
94  * If we use <code>SessionManager.MemoryPersistence = true</code> then ignore
95  * <code>SaveOnRestart</code> parameter.
96  *
97  * </ul>
98  *
99  * @see StandardSession
100  * @see StandardSessionManager
101  * @version $Revision: 1.3 $
102  * @author Kyle Clark
103  */

104 public class DiskPagedSessionHome extends PagedSessionHome {
105
106     /**
107      * The paged cache contains handles to passive
108      * sessions that have been paged to disk.
109      */

110     private Hashtable JavaDoc pagedCache = new Hashtable JavaDoc();
111
112     /**
113      * The persistent storage interface.
114      */

115     private PersistentStore store;
116
117     /**
118      * Configuration keys.
119      */

120     private final String JavaDoc PAGE_DIR_KEY = "PageDir";
121
122     /**DACHA & TUFA
123      * variables which determine should we do page sessions
124      * during restart application
125      */

126     private final String JavaDoc SAVE_ON_RESTART_KEY = "SaveOnRestart";
127     private boolean saveSessions = false;
128     String JavaDoc saveSess = "false";
129
130
131     /**
132      * @param sessionMgr
133      * The session manager associated with this session home.
134      * @param config
135      * Object parsed from configuration file. This should be
136      * for the section containing the standard session home configuration.
137      * @param loader
138      * The class load to use when load objects from persistent store.
139      * @exception ConfigException
140      * signifies a problem in the configuration file.
141      * @exception SessionException
142      * if the initialization fails.
143      */

144     public DiskPagedSessionHome(StandardSessionManager sessionMgr,
145                                 Config config, ClassLoader JavaDoc loader)
146         throws SessionException, ConfigException {
147         super(sessionMgr, config, loader);
148         String JavaDoc pageDir = config.getString(PAGE_DIR_KEY);
149         /**DACHA & TUFA
150          * read from config file SAVE_ON_RESTART key
151          */

152         if(config.containsKey(SAVE_ON_RESTART_KEY)){
153           saveSess = config.getString(SAVE_ON_RESTART_KEY);
154           if(saveSess.equals("true")) saveSessions = true;
155         }
156         debug(PAGE_DIR_KEY + " = " + pageDir);
157         try {
158             store = new FilePersistentStore(pageDir, loader);
159               /**DACHA & TUFA
160                * load all paged sessions from disk
161                */

162               if(saveSessions)
163                   loadPagedSessions();
164         } catch (Exception JavaDoc e) {
165             throw new SessionException(e);
166         }
167     }
168
169     /**
170      * Creates a new session object. This method is intended to be
171      * overriden by classes that extend PagedSessionHome.
172      *
173      * @return a new session.
174      */

175     protected PagedSession newSession(StandardSessionManager mgr,
176                                       String JavaDoc sessionKey)
177         throws SessionException {
178         return new PagedSession(mgr, sessionKey);
179     }
180
181     /**
182      * Deletes a paged session. If the session doesn't exist then this
183      * is a noop.
184      *
185      * @param sessionKey the key identifying the session
186      * that should be deleted.
187      */

188     protected void deleteSession(String JavaDoc sessionKey)
189         throws SessionException {
190         PagedSessionHandle pagedSession =
191             (PagedSessionHandle)pagedCache.remove(sessionKey);
192         if (pagedSession != null) {
193             pagedSession.delete();
194         }
195     }
196
197     /**
198      * Pages a session to disk.
199      *
200      * @param session the session to page.
201      * @exception SessionException if the paged session could not be
202      * paged out.
203      */

204     protected synchronized void pageOut(PagedSession s) throws SessionException {
205 // v. strahinja, 01 okt 2002 debug(3, "page: write session to disk: " + s.getSessionKey());
206
debug("page: write session to disk: " + s.getSessionKey());
207         PagedSessionHandle page = new PagedSessionHandle(s, store);
208         page.write();
209         pagedCache.put(s.getSessionKey(), page);
210     }
211
212     /**
213      * Reads a paged session from disk.
214      *
215      * @param sessionKey the key identifying the session that should
216      * be paged in.
217      * @return the paged session that was read in.
218      * @exception SessionException if the paged session could not be
219      * read in or does not exist.
220      */

221     protected synchronized PagedSession pageIn(String JavaDoc sessionKey) throws SessionException {
222         PagedSessionHandle sessHandle =
223             (PagedSessionHandle)pagedCache.get(sessionKey);
224         PagedSession session = null;
225         try {
226             if (sessHandle != null) {
227 // v. strahinja, 01 okt 2002 debug(3, "page: read session from disk: " + sessHandle.getSessionKey());
228
debug("page: read session from disk: " + sessHandle.getSessionKey());
229                 session = sessHandle.read();
230 // v. strahinja, 01 okt 2002 debug(3, "page: read data: " + session);
231
debug("page: read data: " + session);
232                 // delete paged version from disk...
233
sessHandle.delete();
234             }
235         } catch (SessionException e) {
236         Enhydra.getLogChannel().write(Logger.ALERT,
237                                           "Session not found on disk.", e);
238         }
239         pagedCache.remove(sessionKey);
240         return session;
241     }
242
243     /**
244      * Returns the number of paged sessions.
245      * @exception SessionException if the paged session count
246      * cannot be retrieved.
247      */

248     protected synchronized int getPagedSessionCount() throws SessionException {
249         return pagedCache.size();
250     }
251
252     /**
253      * Returns true if the specified session key is in use
254      * by a session that has been paged out.
255      *
256      * @param sessionKey the session key to test.
257      * @return true if the session key is in use by a paged session.
258      */

259     protected boolean pagedSessionKeyExists(String JavaDoc sessionKey)
260         throws SessionException {
261         return pagedCache.containsKey(sessionKey);
262     }
263
264     /**
265      * Returns an enumeration of the keys of all the sessions that have
266      * been paged out to persistent storage.
267      *
268      * @return the session key enumeration.
269      * @exception SessionException if an error occurs.
270      */

271     protected Enumeration JavaDoc getPagedSessionKeys() throws SessionException {
272         return pagedCache.keys();
273     }
274
275     /**
276      * Removes a session that is new and paged.
277      *
278      * @exception SessionException if an error occurs.
279      */

280     protected boolean cleanupNewPagedSession() throws SessionException {
281         long oldestTime = -1;
282         String JavaDoc key = null;
283         Enumeration JavaDoc e = pagedCache.keys();
284         while (e.hasMoreElements()) {
285             PagedSessionHandle s = (PagedSessionHandle)pagedCache.get(e.nextElement());
286             if (s.isNew()) {
287                 if ((oldestTime < 0)
288                     || (s.getTimeCreated() < oldestTime)) {
289                     oldestTime = s.getTimeCreated();
290                     key = s.getSessionKey();
291                 }
292             }
293         }
294         if (key != null) {
295             removeSession(key);
296             return true;
297         }
298         return false;
299     }
300
301     /**
302      * Shuts dows the session home. Removes paged sessions
303      * from disk or page all sessions if key SaveOnRestart = true.
304      */

305
306     public void shutdown() {
307     /**DACHA & TUFA
308      * method PagedSessionHome.shutdown() responsible for
309      * paging all (active and passive) sessions to disk
310      */

311       if(saveSessions){
312          super.shutdown();
313         }else{
314           Enumeration JavaDoc enumeration = pagedCache.elements();
315           for (int i=0; i<pagedCache.size(); i++) {
316               try {
317                   ((PagedSessionHandle)enumeration.nextElement()).delete();
318               } catch (Exception JavaDoc e) {
319                   // ignore
320
}
321           }
322         }
323     }
324     /**DACHA & TUFA
325      * method for loading paged sessions when application start
326      */

327     private void loadPagedSessions()
328     throws PersistentStoreException{
329       Enumeration JavaDoc enumeration = store.keys();
330       while (enumeration.hasMoreElements()){
331         String JavaDoc sessionKey = (String JavaDoc) enumeration.nextElement();
332         PagedSession session = (PagedSession) store.retrieve(sessionKey);
333         Object JavaDoc[] obj = {sessionMgr} ;
334         session.restoreTransientData(obj);
335         PagedSessionHandle psh = new PagedSessionHandle(session,store);
336         session = null;
337         pagedCache.put( sessionKey,psh);
338         }
339     }
340
341
342 }
343
344
345
346
347
Popular Tags