1 2 24 25 package com.lutris.appserver.server.sessionEnhydra.persistent; 26 27 import java.util.Enumeration ; 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 85 public class PersistentSessionHome extends PagedSessionHome { 86 87 90 static String dbName = null; 91 92 96 static String dbTableName = "PersistentSession"; 98 101 private static final String DB_NAME_KEY = "DatabaseName"; 102 private static final String DB_TABLE_NAME_KEY = "DBTableName"; 103 104 117 public PersistentSessionHome(StandardSessionManager sessionMgr, 118 Config config, ClassLoader 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 139 public String getDatabaseName() { 140 return dbName; 141 } 142 143 152 public static String getDatabaseName(Config config) throws ConfigException { 153 String 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 169 protected PagedSession newSession(StandardSessionManager mgr, 170 String sessionKey) 171 throws SessionException { 172 PersistentSession session = new PersistentSession(mgr, sessionKey, this); 173 DBUtil.dbInsert(session, dbName); 174 return session; 175 } 176 177 184 protected void deleteSession(String sessionKey) 185 throws SessionException { 186 DBUtil.dbDelete(sessionKey, dbName); 188 } 189 190 197 protected synchronized void pageOut(PagedSession s) throws SessionException { 198 if (DBUtil.dbUpdate((PersistentSession)s, dbName) <= 0) { 205 DBUtil.dbInsert((PersistentSession)s, dbName); 206 } 207 } 208 209 218 protected synchronized PagedSession pageIn(String 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 231 protected synchronized int getPagedSessionCount() throws SessionException { 232 SizeQuery sizeQuery = new SizeQuery(); 233 Integer size = (Integer )DBUtil.dbQuery(sizeQuery, dbName); 234 return size.intValue(); 235 } 236 237 244 protected boolean pagedSessionKeyExists(String sessionKey) 245 throws SessionException { 246 KeyExistsQuery keyExistsQuery = new KeyExistsQuery(sessionKey); 247 Boolean exists = (Boolean )DBUtil.dbQuery(keyExistsQuery, dbName); 248 return exists.booleanValue(); 249 } 250 251 258 protected Enumeration getPagedSessionKeys() throws SessionException { 259 return (Enumeration )DBUtil.dbQuery(new KeysQuery(), dbName); 260 } 261 262 267 protected boolean cleanupNewPagedSession() throws SessionException { 268 String key = null; 269 UnusedQuery unusedQuery = new UnusedQuery(); 270 key = (String )DBUtil.dbQuery(unusedQuery, dbName); 271 if (key != null) { 272 removeSession(key); 273 return true; 274 } 275 return false; 276 } 277 278 281 public void shutdown() { 282 } 284 285 290 protected void debug(String msg) { 291 debug(0, msg); 292 } 293 294 300 protected void debug(int level, String 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 |