1 package com.lutris.appserver.server.sessionContainerAdapter; 2 3 import java.lang.reflect.Method ; 4 import java.util.Date ; 5 import java.util.Enumeration ; 6 import java.util.Set ; 7 import java.util.Vector ; 8 9 import javax.management.MBeanServer ; 10 import javax.management.MBeanServerFactory ; 11 import javax.management.ObjectName ; 12 13 import javax.servlet.http.HttpSession ; 14 15 import com.lutris.appserver.server.Application; 16 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms; 17 import com.lutris.appserver.server.session.Session; 18 import com.lutris.appserver.server.session.SessionException; 19 import com.lutris.appserver.server.session.SessionManager; 20 import com.lutris.appserver.server.sessionEnhydra.CreateSessionException; 21 import com.lutris.appserver.server.user.User; 22 23 34 public class TomcatContainerAdapterSessionManager extends 35 ContainerAdapterSessionManager { 36 37 private Object manager; 38 39 private Method createSession; 40 41 private Method expireSession; 42 43 private Method findSession; 44 45 private Method findSessions; 46 47 private Method getActiveSessions; 48 49 private Method getMaxActive; 50 51 private Method getExpiredSessions; 52 53 private Application application; 54 55 private boolean saveOnRestart = false; 56 57 private int maxSessions = -1; 58 59 private boolean initialized = false; 60 61 public TomcatContainerAdapterSessionManager( 62 com.lutris.appserver.server.Application application, 63 com.lutris.util.Config config, com.lutris.logging.LogChannel logger) 64 throws com.lutris.appserver.server.session.SessionException { 65 super(application, config, logger); 66 this.application = application; 67 68 try { 69 if (config.containsKey("SessionHome.SaveOnRestart")) { 70 saveOnRestart = config.getBoolean("SessionHome.SaveOnRestart",false); 71 } 72 } catch (com.lutris.util.ConfigException e) { 73 throw new com.lutris.appserver.server.session.SessionException(e); 74 } 75 76 try { 77 if (config.containsKey("SessionHome.MaxSessions")) { 78 maxSessions = config.getInt("SessionHome.MaxSessions"); 79 } 80 } catch (com.lutris.util.ConfigException e) { 81 throw new com.lutris.appserver.server.session.SessionException(e); 82 } 83 } 84 85 91 public Session createSession() 92 throws com.lutris.appserver.server.session.SessionException { 93 94 if (!initialized) 95 initialize(); 96 97 HttpSession httpSession; 98 99 try { 100 httpSession = (HttpSession ) createSession.invoke(manager, 101 new Object [] {}); 102 } catch (Exception e) { 103 throw new CreateSessionException( 104 "New session cannot be instantiated " + e.getMessage()); 105 } 106 if (httpSession != null) { 107 ContainerAdapterSession enhydraSession = new ContainerAdapterSession( 108 this, httpSession); 109 httpSession.setAttribute(SESSION, enhydraSession); 110 httpSession.setMaxInactiveInterval(maxSessionIdleTime); 111 112 return enhydraSession; 113 } else { 114 throw new CreateSessionException( 115 "New session cannot be instantiated"); 116 } 117 } 118 119 124 public Session createSession(String ipPortToken) 125 throws com.lutris.appserver.server.session.SessionException { 126 127 128 throw new java.lang.UnsupportedOperationException ( 129 "Method createSession(String ipPortToken) not implemented."); 130 } 131 132 138 public Session createSession(HttpPresentationComms comms) 139 throws com.lutris.appserver.server.session.SessionException { 140 141 if (!initialized) 142 initialize(); 143 144 return super.createSession(comms); 145 } 146 147 153 public void deleteSession(Session parm1) 154 throws com.lutris.appserver.server.session.SessionException { 155 if (!initialized) 156 initialize(); 157 try { 158 expireSession.invoke(manager, 159 new Object [] { parm1.getSessionKey() }); 160 } catch (Exception e) { 161 throw new SessionException("Session cannot be deleteded " 162 + e.getMessage()); 163 } 164 165 } 166 167 173 public void deleteSession(String parm1) 174 throws com.lutris.appserver.server.session.SessionException { 175 if (!initialized) 176 initialize(); 177 try { 178 expireSession.invoke(manager, new Object [] { parm1 }); 179 } catch (Exception e) { 180 throw new SessionException("Session cannot be deleteded " 181 + e.getMessage()); 182 } 183 } 184 185 199 public Session getSession(String sessionId) 200 throws com.lutris.appserver.server.session.SessionException { 201 202 if (!initialized) 203 initialize(); 204 HttpSession httpSession; 205 206 try { 207 httpSession = (HttpSession ) findSession.invoke(manager, 208 new Object [] { sessionId }); 209 } catch (Exception e) { 210 throw new SessionException("Problem while lookup for session " 211 + e.getMessage()); 212 } 213 214 Session enhydraSession = null; 215 216 if (httpSession != null) { 217 218 try { 219 enhydraSession = (Session ) httpSession.getAttribute(SESSION); 220 } catch (IllegalStateException ex) { 221 return null; 223 } 224 if (enhydraSession != null) 225 chackSessionManager(enhydraSession); 226 227 return enhydraSession; 228 } else { 229 return null; 230 } 231 } 232 233 247 248 public Session getSession(Thread parm1, String sessionId) 249 throws com.lutris.appserver.server.session.SessionException { 250 return getSession(sessionId); 251 } 252 253 266 public Session getSession(Thread parm1, String sessionId, 267 HttpPresentationComms comms) 268 throws com.lutris.appserver.server.session.SessionException { 269 270 if (!initialized) 271 initialize(); 272 273 return super.getSession(parm1, sessionId, comms); 274 } 275 276 283 public Enumeration getSessionKeys(User parm1) 284 throws com.lutris.appserver.server.session.SessionException { 285 286 if (!initialized) 287 initialize(); 288 289 HttpSession [] httpSession; 290 try { 291 httpSession = (HttpSession []) findSessions.invoke(manager, 292 new Object [] {}); 293 294 } catch (Exception e) { 295 throw new SessionException("Problem while lookup for session Keys " 296 + e.getMessage()); 297 } 298 299 Vector list = new Vector (); 300 301 if (httpSession != null) { 302 303 for (int i = 0; i < httpSession.length; i++) { 304 Session enhydraSession = null; 305 306 try { 307 enhydraSession = (Session ) httpSession[i] 308 .getAttribute(SESSION); 309 } catch (IllegalStateException ex) { 310 continue; 312 } 313 User user = enhydraSession.getUser(); 314 if (user != null && parm1 != null && parm1.getName() != null 315 && user.getName() != null 316 && parm1.getName().equals(user.getName())) 317 list.add(httpSession[i].getId()); 318 } 319 320 return list.elements(); 321 322 } else { 323 return list.elements(); 324 } 325 } 326 327 334 public int activeSessionCount() 335 throws com.lutris.appserver.server.session.SessionException { 336 337 if (!initialized) 338 initialize(); 339 Integer count; 340 341 try { 342 count = (Integer ) getActiveSessions 343 .invoke(manager, new Object [] {}); 344 } catch (Exception e) { 345 throw new SessionException( 346 "Problem while lookup for active session count " 347 + e.getMessage()); 348 } 349 return count.intValue(); 350 } 351 352 358 public int expiredSessionCount() 359 throws com.lutris.appserver.server.session.SessionException { 360 if (!initialized) 361 initialize(); 362 Integer count; 363 try { 364 count = (Integer ) getExpiredSessions.invoke(manager, 365 new Object [] {}); 366 } catch (Exception e) { 367 return -1; 368 } 369 370 return count.intValue(); 371 } 372 373 378 public int maxSessionCount() { 379 380 Integer count; 381 try { 382 if (!initialized) 383 initialize(); 384 count = (Integer ) getMaxActive.invoke(manager, new Object [] {}); 385 } catch (Exception e) { 386 return -1; 387 } 388 return count.intValue(); 389 390 } 391 392 397 public Date maxSessionCountDate() { 398 399 400 throw new java.lang.UnsupportedOperationException ( 401 "Method maxSessionCountDate() not implemented."); 402 } 403 404 411 412 public void resetMaxSessionCount() 413 throws com.lutris.appserver.server.session.SessionException { 414 415 try { 416 417 Method setMaxActive = manager.getClass().getMethod("setMaxActive", 418 new Class [] { int.class }); 419 420 int current = activeSessionCount(); 421 422 setMaxActive.invoke(manager, new Object [] { new Integer (current) }); 423 424 } catch (Exception e) { 425 throw new SessionException("Problem while reset Max Session Count " 426 + e.getMessage()); 427 } 428 } 429 430 437 public Enumeration getSessionKeys() 438 throws com.lutris.appserver.server.session.SessionException { 439 440 if (!initialized) 441 initialize(); 442 Object [] httpSession; 443 444 try { 445 httpSession = (Object []) findSessions.invoke(manager, 446 new Object [] {}); 447 } catch (Exception e) { 448 throw new SessionException("Problem while lookup for session Keys " 449 + e.getMessage()); 450 } 451 452 Vector list = new Vector (); 453 454 if (httpSession != null) { 455 456 for (int i = 0; i < httpSession.length; i++) { 457 list.add(((HttpSession ) httpSession[i]).getId()); 458 } 459 return list.elements(); 460 } else { 461 return list.elements(); 462 } 463 464 } 465 466 public void initialize() 467 throws com.lutris.appserver.server.session.SessionException { 468 try { 469 470 MBeanServer mBeanServer = findMBeanServer(); 471 Object current = application.getClass().getClassLoader(); 472 473 ObjectName oname = new ObjectName ("*:j2eeType=WebModule,*"); 474 475 Set moduls = mBeanServer.queryNames(oname, null); 476 Object [] o = (Object []) moduls.toArray(); 477 478 for (int j = 0; j < o.length; j++) { 479 480 Object context = (Object ) mBeanServer.invoke((ObjectName ) o[j], 481 "findMappingObject", new Object [] {}, new String [] {}); 482 483 Method getLoader = context.getClass().getMethod("getLoader", 484 new Class [] {}); 485 486 Object webAppLoader = getLoader 487 .invoke(context, new Object [] {}); 488 489 Method getClassLoader = null; 490 getClassLoader = webAppLoader.getClass().getMethod( 491 "getClassLoader", new Class [] {}); 492 Object webAppClassLoader = getClassLoader.invoke(webAppLoader, 493 new Object [] {}); 494 495 if (webAppClassLoader.equals(current)) { 496 497 Method getManager = context.getClass().getMethod( 498 "getManager", new Class [] {}); 499 500 manager = getManager.invoke(context, new Object [] {}); 501 502 createSession = manager.getClass().getMethod( 503 "createSession", new Class [] {}); 504 expireSession = manager.getClass().getMethod( 505 "expireSession", new Class [] { String .class }); 506 findSession = manager.getClass().getMethod("findSession", 507 new Class [] { String .class }); 508 findSessions = manager.getClass().getMethod("findSessions", 509 new Class [] {}); 510 getActiveSessions = manager.getClass().getMethod( 511 "getActiveSessions", new Class [] {}); 512 getMaxActive = manager.getClass().getMethod("getMaxActive", 513 new Class [] {}); 514 515 getExpiredSessions = manager.getClass().getMethod( 516 "getExpiredSessions", new Class [] {}); 517 518 if (!saveOnRestart) { 519 try { 520 Method setPathname = manager.getClass() 521 .getMethod("setPathname", 522 new Class [] { String .class }); 523 524 setPathname.invoke(manager, new Object [] { null }); 525 } catch (Exception e) { 526 } 528 } 529 530 if (maxSessions > 0) { 531 try { 532 Method setMaxActiveSessions = manager.getClass() 533 .getMethod("setMaxActiveSessions", 534 new Class [] { int.class }); 535 536 setMaxActiveSessions.invoke(manager, 537 new Object [] { new Integer (maxSessions) }); 538 } catch (Exception e) { 539 } 541 } 542 543 initialized = true; 544 return; 545 } 546 } 547 548 } catch (Exception e) { 549 throw new SessionException( 550 "Problem in initialization of TomcatContainerAdapterSessionManager : " 551 + e.getMessage()); 552 } 553 554 } 555 556 private MBeanServer findMBeanServer() { 557 MBeanServer mBeanServer; 558 try { 559 java.util.ArrayList server = MBeanServerFactory 560 .findMBeanServer(null); 561 if (server == null) { 562 return null; 563 } else { 564 mBeanServer = (MBeanServer ) server.get(0); 565 } 566 } catch (Exception e) { 567 mBeanServer = null; 568 } 569 return mBeanServer; 570 } 571 574 public void shutdown() { 575 super.shutdown(); 576 initialized = false; 577 application = null; 578 manager = null; 579 createSession = null; 580 expireSession = null; 581 findSession = null; 582 findSessions = null; 583 getActiveSessions = null; 584 getMaxActive = null; 585 getExpiredSessions = null; 586 } 587 588 private void chackSessionManager(Session session) { 589 590 SessionManager sessionManager = session.getSessionManager(); 591 592 if (sessionManager == null) { 593 ((ContainerAdapterSession) session).setSessionManager(this); 594 } 595 } 596 } | Popular Tags |