1 7 package org.jboss.web.tomcat.tc5.session; 8 9 import org.apache.catalina.*; 10 import org.jboss.mx.util.MBeanServerLocator; 11 12 import java.beans.PropertyChangeListener ; 13 import java.beans.PropertyChangeSupport ; 14 15 import java.io.IOException ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Collections ; 20 import java.beans.PropertyChangeEvent ; 21 import javax.management.MBeanServer ; 22 import javax.management.ObjectName ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.http.Cookie ; 25 26 import org.jboss.logging.Logger; 27 import org.jboss.metadata.WebMetaData; 28 import org.jboss.web.tomcat.statistics.ReplicationStatistics; 29 import org.apache.catalina.Container; 30 import org.apache.catalina.Context; 31 import org.apache.catalina.Lifecycle; 32 import org.apache.catalina.LifecycleException; 33 import org.apache.catalina.LifecycleListener; 34 import org.apache.catalina.Session; 35 import org.apache.catalina.util.LifecycleSupport; 36 37 44 public abstract class JBossManager 45 implements AbstractJBossManager, Lifecycle, 46 JBossManagerMBean, PropertyChangeListener 47 { 48 49 53 private static final String info_ = "JBossManager/1.0"; 54 55 protected ReplicationStatistics stats_ = new ReplicationStatistics(); 57 58 61 protected int invalidateSessionPolicy_ = WebMetaData.SESSION_INVALIDATE_SET_AND_NON_PRIMITIVE_GET; 62 65 protected int replicationGranularity_ = WebMetaData.REPLICATION_GRANULARITY_SESSION; 66 69 protected LifecycleSupport lifecycle_ = new LifecycleSupport(this); 70 73 protected boolean started_ = false; 74 77 protected ObjectName objectName_; 78 81 protected Logger log_ = Logger.getLogger(this.getClass().getName()); 82 85 protected Container container_; 86 92 protected boolean distributable_; 93 97 protected int maxInactiveInterval_ = 60; 98 101 protected int sessionIdLength_ = 16; 102 103 protected int maxActive_ = -1; 105 106 protected int createdCounter_ = 0; 108 109 protected int rejectedCounter_ = 0; 111 112 protected int activeCounter_ = 0; 114 115 protected int expiredCounter_ = 0; 117 118 protected long timeSinceLastReset_ = 0; 119 120 123 protected Map sessions_; 124 125 128 protected boolean useLocalCache_ = false; 129 130 133 protected PropertyChangeSupport support_ = new PropertyChangeSupport (this); 134 135 protected SessionIDGenerator sessionIDGenerator_; 136 137 139 public JBossManager() 140 { 141 sessionIDGenerator_ = SessionIDGenerator.getInstance(); 142 sessions_ = Collections.synchronizedMap(new HashMap ()); 143 } 144 145 public void init(String name, WebMetaData webMetaData, boolean useJK, boolean useLocalCache) throws ClusteringNotSupportedException 146 { 147 replicationGranularity_ = webMetaData.getReplicationGranularity(); 148 invalidateSessionPolicy_ = webMetaData.getInvalidateSessionPolicy(); 149 useLocalCache_ = useLocalCache; 150 log_.info("init(): replicationGranularity_ is " + replicationGranularity_ + 151 " and invaldateSessionPolicy is " + invalidateSessionPolicy_); 152 153 try 154 { 155 objectName_ = new ObjectName ("jboss.web:service=ClusterManager,WebModule=" + name); 157 } 158 catch (Throwable e) 159 { 160 log_.error("Could not create ObjectName", e); 161 throw new ClusteringNotSupportedException(e.toString()); 162 } 163 } 164 165 public int getInvalidateSessionPolicy() 166 { 167 return this.invalidateSessionPolicy_; 168 } 169 170 175 public Engine getEngine() 176 { 177 Engine e = null; 178 for (Container c = getContainer(); e == null && c != null; c = c.getParent()) 179 { 180 if (c != null && c instanceof Engine) 181 { 182 e = (Engine) c; 183 } 184 } 185 return e; 186 } 187 188 193 public String getJvmRoute() 194 { 195 Engine e = getEngine(); 196 return e == null ? null : e.getJvmRoute(); 197 } 198 199 204 protected String getNextId() 205 { 206 return sessionIDGenerator_.getSessionId(); 207 } 208 209 public boolean isUseLocalCache() 210 { 211 return useLocalCache_; 212 } 213 214 219 public void setSessionCookie(String sessionId) 220 { 221 HttpServletResponse response = (HttpServletResponse ) ClusteredSessionValve.responseThreadLocal.get(); 222 setNewSessionCookie(sessionId, response); 223 } 224 225 public void setNewSessionCookie(String sessionId, HttpServletResponse response) 226 { 227 if (response != null) 228 { 229 Context context = (Context) container_; 230 if (context.getCookies()) 231 { 232 Cookie newCookie = new Cookie (Globals.SESSION_COOKIE_NAME, sessionId); 234 if (log_.isDebugEnabled()) 235 { 236 log_.debug("Setting cookie with session id:" + sessionId + " & name:" + Globals.SESSION_COOKIE_NAME); 237 } 238 newCookie.setMaxAge(-1); 239 newCookie.setPath(context.getPath()); 240 response.addCookie(newCookie); 241 } 242 } 243 } 244 245 247 public ReplicationStatistics getReplicationStatistics() 248 { 249 return stats_; 250 } 251 252 public void resetStats() 253 { 254 stats_.resetStats(); 255 timeSinceLastReset_ = System.currentTimeMillis(); 256 activeCounter_ = 0; 257 rejectedCounter_ = 0; 258 createdCounter_ = 0; 259 expiredCounter_ = 0; 260 } 261 262 public long timeInSecondsSinceLastReset() 263 { 264 return (System.currentTimeMillis() - timeSinceLastReset_) / (1000L); 265 } 266 267 public long getActiveSessionCount() 268 { 269 return getActiveSessions(); 270 } 271 272 public long getCreatedSessionCount() 273 { 274 return createdCounter_; 275 } 276 277 public long getExpiredSessionCount() 278 { 279 return expiredCounter_; 280 } 281 282 public long getRejectedSessionCount() 283 { 284 return rejectedCounter_; 285 } 286 287 public int getSessionMaxAliveTime() 288 { 289 return 0; 290 } 291 292 public void setSessionMaxAliveTime(int sessionMaxAliveTime) 293 { 294 } 295 296 public int getSessionAverageAliveTime() 297 { 298 return 0; 299 } 300 301 public void setSessionAverageAliveTime(int sessionAverageAliveTime) 302 { 303 } 304 305 public String reportReplicationStatistics() 306 { 307 StringBuffer tmp = new StringBuffer (); 308 HashMap copy = new HashMap (stats_.getStats()); 309 Iterator iter = copy.entrySet().iterator(); 310 tmp.append("<table><tr>"); 311 tmp.append("<th>sessionID</th>"); 312 tmp.append("<th>replicationCount</th>"); 313 tmp.append("<th>minPassivationTime</th>"); 314 tmp.append("<th>maxPassivationTime</th>"); 315 tmp.append("<th>totalPassivationTime</th>"); 316 tmp.append("<th>minReplicationTime</th>"); 317 tmp.append("<th>maxReplicationTime</th>"); 318 tmp.append("<th>totalReplicationlTime</th>"); 319 tmp.append("<th>loadCount</th>"); 320 tmp.append("<th>minLoadTime</th>"); 321 tmp.append("<th>maxLoadTime</th>"); 322 tmp.append("<th>totalLoadlTime</th>"); 323 while (iter.hasNext()) 324 { 325 Map.Entry entry = (Map.Entry ) iter.next(); 326 ReplicationStatistics.TimeStatistic stat = (ReplicationStatistics.TimeStatistic) entry.getValue(); 327 tmp.append("<tr><td>"); 328 tmp.append(entry.getKey()); 329 tmp.append("</td><td>"); 330 tmp.append(stat.replicationCount); 331 tmp.append("</td><td>"); 332 tmp.append(stat.minPassivationTime); 333 tmp.append("</td><td>"); 334 tmp.append(stat.maxPassivationTime); 335 tmp.append("</td><td>"); 336 tmp.append(stat.totalPassivationTime); 337 tmp.append("</td><td>"); 338 tmp.append(stat.minReplicationTime); 339 tmp.append("</td><td>"); 340 tmp.append(stat.maxReplicationTime); 341 tmp.append("</td><td>"); 342 tmp.append(stat.totalReplicationlTime); 343 tmp.append("</td><td>"); 344 tmp.append(stat.loadCount); 345 tmp.append("</td><td>"); 346 tmp.append(stat.minLoadTime); 347 tmp.append("</td><td>"); 348 tmp.append(stat.maxLoadTime); 349 tmp.append("</td><td>"); 350 tmp.append(stat.totalLoadlTime); 351 tmp.append("</td></tr>"); 352 } 353 tmp.append("</table>"); 354 copy.clear(); 355 return tmp.toString(); 356 357 } 358 359 361 public void addLifecycleListener(LifecycleListener listener) 362 { 363 lifecycle_.addLifecycleListener(listener); 364 } 365 366 public LifecycleListener[] findLifecycleListeners() 367 { 368 return lifecycle_.findLifecycleListeners(); 369 } 370 371 public void removeLifecycleListener(LifecycleListener listener) 372 { 373 lifecycle_.removeLifecycleListener(listener); 374 } 375 376 382 public void start() throws LifecycleException 383 { 384 startManager(); 385 } 386 387 393 public void stop() throws LifecycleException 394 { 395 resetStats(); 396 stopManager(); 397 } 398 399 410 protected void startManager() throws LifecycleException 411 { 412 log_.info("Starting JBossManager"); 413 414 if (started_) 416 throw new LifecycleException 417 ("JBossManager alreadyStarted"); 418 lifecycle_.fireLifecycleEvent(START_EVENT, null); 419 started_ = true; 420 421 try 423 { 424 MBeanServer server = MBeanServerLocator.locateJBoss(); 425 server.registerMBean(this, objectName_); 426 } 427 catch (Exception e) 428 { 429 log_.error("Could not register ClusterManagerMBean to MBeanServer", e); 430 } 431 } 432 433 443 protected void stopManager() throws LifecycleException 444 { 445 log_.info("Stopping JBossManager"); 446 447 if (!started_) 449 throw new LifecycleException 450 ("JBossManager notStarted"); 451 lifecycle_.fireLifecycleEvent(STOP_EVENT, null); 452 started_ = false; 453 454 try 456 { 457 MBeanServer server = MBeanServerLocator.locateJBoss(); 458 server.unregisterMBean(objectName_); 459 } 460 catch (Exception e) 461 { 462 log_.error("Could not unregister ClusterManagerMBean from MBeanServer", e); 463 } 464 } 465 466 public Container getContainer() 468 { 469 return container_; 470 } 471 472 public void setContainer(Container container) 473 { 474 475 if ((this.container_ != null) && (this.container_ instanceof Context)) 477 ((Context) this.container_).removePropertyChangeListener(this); 478 479 this.container_ = container; 481 482 if ((this.container_ != null) && (this.container_ instanceof Context)) 484 { 485 setMaxInactiveInterval 486 (((Context) this.container_).getSessionTimeout() * 60); 487 ((Context) this.container_).addPropertyChangeListener(this); 488 } 489 } 490 491 public boolean getDistributable() 492 { 493 return distributable_; 494 } 495 496 public void setDistributable(boolean distributable) 497 { 498 this.distributable_ = distributable; 499 } 500 501 public String getInfo() 502 { 503 return info_; 504 } 505 506 public int getMaxInactiveInterval() 507 { 508 return maxInactiveInterval_; 509 } 510 511 public void setMaxInactiveInterval(int interval) 512 { 513 this.maxInactiveInterval_ = interval; 514 } 515 516 public int getSessionIdLength() 517 { 518 return sessionIdLength_; 519 } 520 521 public void setSessionIdLength(int idLength) 522 { 523 this.sessionIdLength_ = idLength; 524 } 525 526 public int getSessionCounter() 527 { 528 return createdCounter_; 529 } 530 531 public void setSessionCounter(int sessionCounter) 532 { 533 this.createdCounter_ = sessionCounter; 534 } 535 536 public int getMaxActive() 537 { 538 return maxActive_; 539 } 540 541 public void setMaxActive(int maxActive) 542 { 543 this.maxActive_ = maxActive; 544 } 545 546 public int getExpiredSessions() 547 { 548 return expiredCounter_; 549 } 550 551 public void setExpiredSessions(int expiredSessions) 552 { 553 this.expiredCounter_ = expiredSessions; 554 } 555 556 public int getRejectedSessions() 557 { 558 return rejectedCounter_; 559 } 560 561 public void setRejectedSessions(int rejectedSessions) 562 { 563 this.rejectedCounter_ = rejectedSessions; 564 } 565 566 public void addPropertyChangeListener(PropertyChangeListener listener) 567 { 568 support_.addPropertyChangeListener(listener); 569 } 570 571 577 public abstract void removeLocal(Session session); 578 579 584 public abstract boolean storeSession(Session session); 585 586 public int getActiveSessions() 587 { 588 return activeCounter_; 589 } 590 591 622 623 public void load() throws ClassNotFoundException , IOException 624 { 625 throw new RuntimeException ("JBossManager.load(): Method not implemented."); 627 } 628 629 public void removePropertyChangeListener(PropertyChangeListener listener) 630 { 631 support_.removePropertyChangeListener(listener); 632 } 633 634 public void unload() throws IOException 635 { 636 throw new RuntimeException ("JBossManager.load(): Method not implemented."); 638 } 639 640 641 public void backgroundProcess() 642 { 643 processExpires(); 645 } 646 647 650 protected void processExpires() 651 { 652 655 Session sessions[] = findSessions(); 657 if (log_.isDebugEnabled()) 658 { 659 log_.debug("Looking for sessions that have expired ..."); 660 } 661 662 for (int i = 0; i < sessions.length; ++i) 663 { 664 ClusteredSession session = (ClusteredSession) sessions[i]; 665 666 if (!session.isValid()) 668 { 669 continue; 670 } 671 672 701 } 702 } 703 704 public void propertyChange(PropertyChangeEvent evt) 705 { 706 } 708 709 713 abstract public ClusteredSession[] findLocalSessions(); 714 715 720 abstract public ClusteredSession findLocalSession(String realId); 721 722 } 723 | Popular Tags |