1 17 18 19 package org.apache.catalina.session; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.BufferedInputStream ; 24 import java.io.BufferedOutputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectOutputStream ; 32 import java.security.AccessController ; 33 import java.security.PrivilegedActionException ; 34 import java.security.PrivilegedExceptionAction ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 import javax.servlet.ServletContext ; 38 import org.apache.catalina.Container; 39 import org.apache.catalina.Context; 40 import org.apache.catalina.Globals; 41 import org.apache.catalina.Lifecycle; 42 import org.apache.catalina.LifecycleException; 43 import org.apache.catalina.LifecycleListener; 44 import org.apache.catalina.Loader; 45 import org.apache.catalina.Session; 46 import org.apache.catalina.util.CustomObjectInputStream; 47 import org.apache.catalina.util.LifecycleSupport; 48 49 import org.apache.catalina.security.SecurityUtil; 50 64 65 public class StandardManager 66 extends ManagerBase 67 implements Lifecycle, PropertyChangeListener { 68 69 private class PrivilegedDoLoad 71 implements PrivilegedExceptionAction { 72 73 PrivilegedDoLoad() { 74 } 75 76 public Object run() throws Exception { 77 doLoad(); 78 return null; 79 } 80 } 81 82 private class PrivilegedDoUnload 83 implements PrivilegedExceptionAction { 84 85 PrivilegedDoUnload() { 86 } 87 88 public Object run() throws Exception { 89 doUnload(); 90 return null; 91 } 92 93 } 94 95 96 98 99 102 protected static final String info = "StandardManager/1.0"; 103 104 105 108 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 109 110 111 114 protected int maxActiveSessions = -1; 115 116 117 120 protected static String name = "StandardManager"; 121 122 123 131 protected String pathname = "SESSIONS.ser"; 132 133 134 137 protected boolean started = false; 138 139 140 143 protected int rejectedSessions = 0; 144 145 146 149 protected long processingTime = 0; 150 151 152 154 155 162 public void setContainer(Container container) { 163 164 if ((this.container != null) && (this.container instanceof Context )) 166 ((Context ) this.container).removePropertyChangeListener(this); 167 168 super.setContainer(container); 170 171 if ((this.container != null) && (this.container instanceof Context )) { 173 setMaxInactiveInterval 174 ( ((Context ) this.container).getSessionTimeout()*60 ); 175 ((Context ) this.container).addPropertyChangeListener(this); 176 } 177 178 } 179 180 181 186 public String getInfo() { 187 188 return (info); 189 190 } 191 192 193 197 public int getMaxActiveSessions() { 198 199 return (this.maxActiveSessions); 200 201 } 202 203 204 208 public int getRejectedSessions() { 209 return rejectedSessions; 210 } 211 212 213 public void setRejectedSessions(int rejectedSessions) { 214 this.rejectedSessions = rejectedSessions; 215 } 216 217 218 224 public void setMaxActiveSessions(int max) { 225 226 int oldMaxActiveSessions = this.maxActiveSessions; 227 this.maxActiveSessions = max; 228 support.firePropertyChange("maxActiveSessions", 229 new Integer (oldMaxActiveSessions), 230 new Integer (this.maxActiveSessions)); 231 232 } 233 234 235 238 public String getName() { 239 240 return (name); 241 242 } 243 244 245 248 public String getPathname() { 249 250 return (this.pathname); 251 252 } 253 254 255 261 public void setPathname(String pathname) { 262 263 String oldPathname = this.pathname; 264 this.pathname = pathname; 265 support.firePropertyChange("pathname", oldPathname, this.pathname); 266 267 } 268 269 270 272 282 public Session createSession(String sessionId) { 283 284 if ((maxActiveSessions >= 0) && 285 (sessions.size() >= maxActiveSessions)) { 286 rejectedSessions++; 287 throw new IllegalStateException 288 (sm.getString("standardManager.createSession.ise")); 289 } 290 291 return (super.createSession(sessionId)); 292 293 } 294 295 296 305 public void load() throws ClassNotFoundException , IOException { 306 if (SecurityUtil.isPackageProtectionEnabled()){ 307 try{ 308 AccessController.doPrivileged( new PrivilegedDoLoad() ); 309 } catch (PrivilegedActionException ex){ 310 Exception exception = ex.getException(); 311 if (exception instanceof ClassNotFoundException ){ 312 throw (ClassNotFoundException )exception; 313 } else if (exception instanceof IOException ){ 314 throw (IOException )exception; 315 } 316 if (log.isDebugEnabled()) 317 log.debug("Unreported exception in load() " 318 + exception); 319 } 320 } else { 321 doLoad(); 322 } 323 } 324 325 326 335 protected void doLoad() throws ClassNotFoundException , IOException { 336 if (log.isDebugEnabled()) 337 log.debug("Start: Loading persisted sessions"); 338 339 sessions.clear(); 341 342 File file = file(); 344 if (file == null) 345 return; 346 if (log.isDebugEnabled()) 347 log.debug(sm.getString("standardManager.loading", pathname)); 348 FileInputStream fis = null; 349 ObjectInputStream ois = null; 350 Loader loader = null; 351 ClassLoader classLoader = null; 352 try { 353 fis = new FileInputStream (file.getAbsolutePath()); 354 BufferedInputStream bis = new BufferedInputStream (fis); 355 if (container != null) 356 loader = container.getLoader(); 357 if (loader != null) 358 classLoader = loader.getClassLoader(); 359 if (classLoader != null) { 360 if (log.isDebugEnabled()) 361 log.debug("Creating custom object input stream for class loader "); 362 ois = new CustomObjectInputStream(bis, classLoader); 363 } else { 364 if (log.isDebugEnabled()) 365 log.debug("Creating standard object input stream"); 366 ois = new ObjectInputStream (bis); 367 } 368 } catch (FileNotFoundException e) { 369 if (log.isDebugEnabled()) 370 log.debug("No persisted data file found"); 371 return; 372 } catch (IOException e) { 373 log.error(sm.getString("standardManager.loading.ioe", e), e); 374 if (ois != null) { 375 try { 376 ois.close(); 377 } catch (IOException f) { 378 ; 379 } 380 ois = null; 381 } 382 throw e; 383 } 384 385 synchronized (sessions) { 387 try { 388 Integer count = (Integer ) ois.readObject(); 389 int n = count.intValue(); 390 if (log.isDebugEnabled()) 391 log.debug("Loading " + n + " persisted sessions"); 392 for (int i = 0; i < n; i++) { 393 StandardSession session = getNewSession(); 394 session.readObjectData(ois); 395 session.setManager(this); 396 sessions.put(session.getIdInternal(), session); 397 session.activate(); 398 session.endAccess(); 399 } 400 } catch (ClassNotFoundException e) { 401 log.error(sm.getString("standardManager.loading.cnfe", e), e); 402 if (ois != null) { 403 try { 404 ois.close(); 405 } catch (IOException f) { 406 ; 407 } 408 ois = null; 409 } 410 throw e; 411 } catch (IOException e) { 412 log.error(sm.getString("standardManager.loading.ioe", e), e); 413 if (ois != null) { 414 try { 415 ois.close(); 416 } catch (IOException f) { 417 ; 418 } 419 ois = null; 420 } 421 throw e; 422 } finally { 423 try { 425 if (ois != null) 426 ois.close(); 427 } catch (IOException f) { 428 } 430 431 if (file != null && file.exists() ) 433 file.delete(); 434 } 435 } 436 437 if (log.isDebugEnabled()) 438 log.debug("Finish: Loading persisted sessions"); 439 } 440 441 442 449 public void unload() throws IOException { 450 if (SecurityUtil.isPackageProtectionEnabled()){ 451 try{ 452 AccessController.doPrivileged( new PrivilegedDoUnload() ); 453 } catch (PrivilegedActionException ex){ 454 Exception exception = ex.getException(); 455 if (exception instanceof IOException ){ 456 throw (IOException )exception; 457 } 458 if (log.isDebugEnabled()) 459 log.debug("Unreported exception in unLoad() " 460 + exception); 461 } 462 } else { 463 doUnload(); 464 } 465 } 466 467 468 475 protected void doUnload() throws IOException { 476 477 if (log.isDebugEnabled()) 478 log.debug("Unloading persisted sessions"); 479 480 File file = file(); 482 if (file == null) 483 return; 484 if (log.isDebugEnabled()) 485 log.debug(sm.getString("standardManager.unloading", pathname)); 486 FileOutputStream fos = null; 487 ObjectOutputStream oos = null; 488 try { 489 fos = new FileOutputStream (file.getAbsolutePath()); 490 oos = new ObjectOutputStream (new BufferedOutputStream (fos)); 491 } catch (IOException e) { 492 log.error(sm.getString("standardManager.unloading.ioe", e), e); 493 if (oos != null) { 494 try { 495 oos.close(); 496 } catch (IOException f) { 497 ; 498 } 499 oos = null; 500 } 501 throw e; 502 } 503 504 ArrayList list = new ArrayList (); 506 synchronized (sessions) { 507 if (log.isDebugEnabled()) 508 log.debug("Unloading " + sessions.size() + " sessions"); 509 try { 510 oos.writeObject(new Integer (sessions.size())); 511 Iterator elements = sessions.values().iterator(); 512 while (elements.hasNext()) { 513 StandardSession session = 514 (StandardSession) elements.next(); 515 list.add(session); 516 ((StandardSession) session).passivate(); 517 session.writeObjectData(oos); 518 } 519 } catch (IOException e) { 520 log.error(sm.getString("standardManager.unloading.ioe", e), e); 521 if (oos != null) { 522 try { 523 oos.close(); 524 } catch (IOException f) { 525 ; 526 } 527 oos = null; 528 } 529 throw e; 530 } 531 } 532 533 try { 535 oos.flush(); 536 oos.close(); 537 oos = null; 538 } catch (IOException e) { 539 if (oos != null) { 540 try { 541 oos.close(); 542 } catch (IOException f) { 543 ; 544 } 545 oos = null; 546 } 547 throw e; 548 } 549 550 if (log.isDebugEnabled()) 552 log.debug("Expiring " + list.size() + " persisted sessions"); 553 Iterator expires = list.iterator(); 554 while (expires.hasNext()) { 555 StandardSession session = (StandardSession) expires.next(); 556 try { 557 session.expire(false); 558 } catch (Throwable t) { 559 ; 560 } finally { 561 session.recycle(); 562 } 563 } 564 565 if (log.isDebugEnabled()) 566 log.debug("Unloading complete"); 567 568 } 569 570 571 573 574 579 public void addLifecycleListener(LifecycleListener listener) { 580 581 lifecycle.addLifecycleListener(listener); 582 583 } 584 585 586 590 public LifecycleListener[] findLifecycleListeners() { 591 592 return lifecycle.findLifecycleListeners(); 593 594 } 595 596 597 602 public void removeLifecycleListener(LifecycleListener listener) { 603 604 lifecycle.removeLifecycleListener(listener); 605 606 } 607 608 616 public void start() throws LifecycleException { 617 618 if( ! initialized ) 619 init(); 620 621 if (started) { 623 return; 624 } 625 lifecycle.fireLifecycleEvent(START_EVENT, null); 626 started = true; 627 628 if (log.isDebugEnabled()) 630 log.debug("Force random number initialization starting"); 631 String dummy = generateSessionId(); 632 if (log.isDebugEnabled()) 633 log.debug("Force random number initialization completed"); 634 635 try { 637 load(); 638 } catch (Throwable t) { 639 log.error(sm.getString("standardManager.managerLoad"), t); 640 } 641 642 } 643 644 645 653 public void stop() throws LifecycleException { 654 655 if (log.isDebugEnabled()) 656 log.debug("Stopping"); 657 658 if (!started) 660 throw new LifecycleException 661 (sm.getString("standardManager.notStarted")); 662 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 663 started = false; 664 665 try { 667 unload(); 668 } catch (Throwable t) { 669 log.error(sm.getString("standardManager.managerUnload"), t); 670 } 671 672 Session sessions[] = findSessions(); 674 for (int i = 0; i < sessions.length; i++) { 675 Session session = sessions[i]; 676 try { 677 if (session.isValid()) { 678 session.expire(); 679 } 680 } catch (Throwable t) { 681 ; 682 } finally { 683 session.recycle(); 686 } 687 } 688 689 this.random = null; 691 692 if( initialized ) { 693 destroy(); 694 } 695 } 696 697 698 700 701 706 public void propertyChange(PropertyChangeEvent event) { 707 708 if (!(event.getSource() instanceof Context )) 710 return; 711 Context context = (Context ) event.getSource(); 712 713 if (event.getPropertyName().equals("sessionTimeout")) { 715 try { 716 setMaxInactiveInterval 717 ( ((Integer ) event.getNewValue()).intValue()*60 ); 718 } catch (NumberFormatException e) { 719 log.error(sm.getString("standardManager.sessionTimeout", 720 event.getNewValue().toString())); 721 } 722 } 723 724 } 725 726 727 729 730 734 protected File file() { 735 736 if ((pathname == null) || (pathname.length() == 0)) 737 return (null); 738 File file = new File (pathname); 739 if (!file.isAbsolute()) { 740 if (container instanceof Context ) { 741 ServletContext servletContext = 742 ((Context ) container).getServletContext(); 743 File tempdir = (File ) 744 servletContext.getAttribute(Globals.WORK_DIR_ATTR); 745 if (tempdir != null) 746 file = new File (tempdir, pathname); 747 } 748 } 749 return (file); 752 753 } 754 } 755 | Popular Tags |