KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sessionEnhydra > persistent > PersistentSessionHome


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: PersistentSessionHome.java,v 1.2 2005/03/24 10:51:20 slobodan Exp $
23  */

24
25 package com.lutris.appserver.server.sessionEnhydra.persistent;
26
27 import java.util.Enumeration JavaDoc;
28
29 import com.lutris.appserver.server.Enhydra;
30 import com.lutris.appserver.server.session.SessionException;
31 import com.lutris.appserver.server.sessionEnhydra.PagedSession;
32 import com.lutris.appserver.server.sessionEnhydra.PagedSessionHome;
33 import com.lutris.appserver.server.sessionEnhydra.StandardSessionManager;
34 import com.lutris.logging.Logger;
35 import com.lutris.util.Config;
36 import com.lutris.util.ConfigException;
37
38 /**
39  * PersistentSessionHome writes all passive sessions
40  * to a database. The sessions are written by serializing
41  * all the data (excluding the session manager) that is
42  * associated with a session. This requires that
43  * the session data and user associated with a session
44  * are serializable. PersistentSessionHome
45  * should be used by applications that want failover support
46  * or that want to run in a clustered environment.<p>
47  *
48  * The session data is written to a table in the database
49  * that is defined as (Informix):<p>
50  *
51  * <pre>
52  * CREATE TABLE PersistentSession
53  * (
54  * sessionKey VARCHAR(64) NOT NULL,
55  * isNew CHAR(1) DEFAULT "1" NOT NULL,
56  * timeCreated DECIMAL(19,0),
57  * timeLastUsed DECIMAL(19,0),
58  * timeExpires DECIMAL(19,0),
59  * maxIdleTime DECIMAL(19,0),
60  * maxNoUserIdleTime INTEGER,
61  * userName VARCHAR(255),
62  * data BYTE,
63  * PRIMARY KEY(sessionKey)
64  * )
65  * </pre>
66  *
67  * The configuration settings for PersistentSessionHome:<p>
68  *
69  * <ul>
70  *
71  * <li><code>DatabaseName: {String}</code><p>
72  * The database that will be accessed. This session is optional. If
73  * not set then the default database is accessed.
74  *
75  * <li><code>DBTableName: {String}</code><p>
76  * The name of the table in the database where session data
77  * will be written. The default table name is "PersistentSession".<p>
78  *
79  * </ul>
80  *
81  * @see StandardSessionManager
82  * @version $Revision: 1.2 $
83  * @author Kyle Clark
84  */

85 public class PersistentSessionHome extends PagedSessionHome {
86
87     /**
88      * The name of the database that will be accessed.
89      */

90     static String JavaDoc dbName = null;
91
92     /**
93      * The name of the database table where session information
94      * will be stored.
95      */

96     static String JavaDoc dbTableName = "PersistentSession"; // Default
97

98     /**
99      * Configuration keys.
100      */

101     private static final String JavaDoc DB_NAME_KEY = "DatabaseName";
102     private static final String JavaDoc DB_TABLE_NAME_KEY = "DBTableName";
103
104     /**
105      * @param sessionMgr
106      * The session manager associated with this session home.
107      * @param config
108      * Object parsed from configuration file. This should be
109      * for the section containing the standard session home configuration.
110      * @param loader
111      * The class load to use when load objects from persistent store.
112      * @exception ConfigException
113      * signifies a problem in the configuration file.
114      * @exception SessionException
115      * if the initialization fails.
116      */

117     public PersistentSessionHome(StandardSessionManager sessionMgr,
118                                 Config config, ClassLoader JavaDoc loader)
119         throws SessionException, ConfigException {
120         super(sessionMgr, config, loader);
121         if (config.containsKey(DB_TABLE_NAME_KEY)) {
122             dbTableName = config.getString(DB_TABLE_NAME_KEY);
123         }
124         debug(DB_TABLE_NAME_KEY + " = " + dbTableName);
125         dbName = getDatabaseName(config);
126         if (dbName != null) {
127             debug(DB_NAME_KEY + " = " + dbName);
128         } else {
129             debug(DB_NAME_KEY + " = DEFAULT");
130         }
131     }
132
133     /**
134      * Returns the name of the database being accessed by this manager.
135      *
136      * @return the database name. May be null if the default database is
137      * being accessed.
138      */

139     public String JavaDoc getDatabaseName() {
140         return dbName;
141     }
142
143     /**
144      * Returns the name of the database that should be accessed.
145      *
146      * @param config the config in whihc to look up the database
147      * setting.
148      * @return the database name. May be null if the default database is
149      * being accessed.
150      * @exception ConfigException if an error occurs.
151      */

152     public static String JavaDoc getDatabaseName(Config config) throws ConfigException {
153         String JavaDoc s = null;
154         if (config.containsKey(DB_NAME_KEY)) {
155             s = config.getString(DB_NAME_KEY);
156             if (s.length() == 0) {
157                 s = null;
158             }
159         }
160         return s;
161     }
162
163     /**
164      * Creates a new session object. This method is intended to be
165      * overriden by classes that extend PagedSessionHome.
166      *
167      * @return a new session.
168      */

169     protected PagedSession newSession(StandardSessionManager mgr,
170                                       String JavaDoc sessionKey)
171         throws SessionException {
172         PersistentSession session = new PersistentSession(mgr, sessionKey, this);
173         DBUtil.dbInsert(session, dbName);
174         return session;
175     }
176
177     /**
178      * Deletes a paged session. If the session doesn't exist then this
179      * is a noop.
180      *
181      * @param sessionKey the key identifying the session
182      * that should be deleted.
183      */

184     protected void deleteSession(String JavaDoc sessionKey)
185         throws SessionException {
186         // Delete from database
187
DBUtil.dbDelete(sessionKey, dbName);
188     }
189
190     /**
191      * Pages a session to disk.
192      *
193      * @param session the session to page.
194      * @exception SessionException if the paged session could not be
195      * paged out.
196      */

197     protected synchronized void pageOut(PagedSession s) throws SessionException {
198         // N.B. it is possible for the session to be removed
199
// from the database if running in a clustered environment.
200
// The idle timer in another application instance may run
201
// while this session is in the active state.
202
// If it was removed then the update would fail and we
203
// need to re-insert.
204
if (DBUtil.dbUpdate((PersistentSession)s, dbName) <= 0) {
205             DBUtil.dbInsert((PersistentSession)s, dbName);
206         }
207     }
208
209     /**
210      * Reads a paged session from disk.
211      *
212      * @param sessionKey the key identifying the session that should
213      * be paged in.
214      * @return the paged session that was read in.
215      * @exception SessionException if the paged session could not be
216      * read in or does not exist.
217      */

218     protected synchronized PagedSession pageIn(String JavaDoc sessionKey) throws SessionException {
219         SessionQuery sessionQuery = new SessionQuery(sessionKey, loader);
220         PersistentSession s = (PersistentSession)DBUtil.dbQuery(sessionQuery, dbName);
221         if (s != null) {
222             s.restoreSessionHome(this);
223             s.restoreSessionManager(sessionMgr);
224         }
225         return s;
226     }
227
228     /**
229      * Returns the number of paged sessions.
230      */

231     protected synchronized int getPagedSessionCount() throws SessionException {
232         SizeQuery sizeQuery = new SizeQuery();
233         Integer JavaDoc size = (Integer JavaDoc)DBUtil.dbQuery(sizeQuery, dbName);
234         return size.intValue();
235     }
236
237     /**
238      * Returns true if the specified session key is in use
239      * by a session that has been paged out.
240      *
241      * @param sessionKey the session key to test.
242      * @return true if the session key is in use by a paged session.
243      */

244     protected boolean pagedSessionKeyExists(String JavaDoc sessionKey)
245         throws SessionException {
246         KeyExistsQuery keyExistsQuery = new KeyExistsQuery(sessionKey);
247         Boolean JavaDoc exists = (Boolean JavaDoc)DBUtil.dbQuery(keyExistsQuery, dbName);
248         return exists.booleanValue();
249     }
250
251     /**
252      * Returns an enumeration of the keys of all the sessions that have
253      * been paged out to persistent storage.
254      *
255      * @return the session key enumeration.
256      * @exception SessionException if an error occurs.
257      */

258     protected Enumeration JavaDoc getPagedSessionKeys() throws SessionException {
259         return (Enumeration JavaDoc)DBUtil.dbQuery(new KeysQuery(), dbName);
260     }
261
262     /**
263      * Removes a session that is new and paged.
264      *
265      * @exception SessionException if an error occurs.
266      */

267     protected boolean cleanupNewPagedSession() throws SessionException {
268         String JavaDoc key = null;
269         UnusedQuery unusedQuery = new UnusedQuery();
270         key = (String JavaDoc)DBUtil.dbQuery(unusedQuery, dbName);
271         if (key != null) {
272             removeSession(key);
273             return true;
274         }
275         return false;
276     }
277
278     /**
279      * Shuts dows the session home.
280      */

281     public void shutdown() {
282         // TODO
283
}
284
285     /**
286      * Prints debug information under Logger.DEBUG.
287      *
288      * @param msg the message to print.
289      */

290     protected void debug(String JavaDoc msg) {
291     debug(0, msg);
292     }
293
294     /**
295      * Prints debug information under Logger.DEBUG.
296      *
297      * @param level the debug level.
298      * @param msg the message to print.
299      */

300     protected void debug(int level, String JavaDoc msg) {
301      int dbg = Logger.DEBUG;
302      switch (level) {
303      case 1:
304          dbg = Logger.DEBUG1;
305          break;
306      case 2:
307          dbg = Logger.DEBUG2;
308          break;
309      case 3:
310          dbg = Logger.DEBUG3;
311          break;
312      case 4:
313          dbg = Logger.DEBUG4;
314          break;
315      case 5:
316          dbg = Logger.DEBUG5;
317          break;
318      case 6:
319          dbg = Logger.DEBUG6;
320          break;
321      case 7:
322          dbg = Logger.DEBUG7;
323          break;
324      case 8:
325          dbg = Logger.DEBUG8;
326          break;
327      case 9:
328          dbg = Logger.DEBUG9;
329          break;
330      default:
331          dbg = Logger.DEBUG;
332          break;
333      }
334     Enhydra.getLogChannel().write(dbg, "PersistentSessionHome("
335                                       + Thread.currentThread().getName()
336                                       + "): " + msg);
337     }
338 }
339
Popular Tags