1 17 18 19 package org.apache.catalina.manager; 20 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.lang.reflect.Method ; 24 import java.text.MessageFormat ; 25 import java.util.Date ; 26 import java.util.Enumeration ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 import java.util.Vector ; 30 31 import javax.management.MBeanServer ; 32 import javax.management.ObjectInstance ; 33 import javax.management.ObjectName ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import org.apache.catalina.util.RequestUtil; 38 39 47 48 public class StatusTransformer { 49 50 51 53 54 public static void setContentType(HttpServletResponse response, 55 int mode) { 56 if (mode == 0){ 57 response.setContentType("text/html;charset="+Constants.CHARSET); 58 } else if (mode == 1){ 59 response.setContentType("text/xml;charset="+Constants.CHARSET); 60 } 61 } 62 63 64 73 public static void writeHeader(PrintWriter writer, int mode) { 74 if (mode == 0){ 75 writer.print(Constants.HTML_HEADER_SECTION); 77 } else if (mode == 1){ 78 writer.write(Constants.XML_DECLARATION); 79 writer.write 80 (Constants.XML_STYLE); 81 writer.write("<status>"); 82 } 83 } 84 85 86 94 public static void writeBody(PrintWriter writer, Object [] args, int mode) { 95 if (mode == 0){ 96 writer.print(MessageFormat.format 97 (Constants.BODY_HEADER_SECTION, args)); 98 } 99 } 100 101 102 109 public static void writeManager(PrintWriter writer, Object [] args, 110 int mode) { 111 if (mode == 0){ 112 writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args)); 113 } 114 } 115 116 117 public static void writePageHeading(PrintWriter writer, Object [] args, 118 int mode) { 119 if (mode == 0){ 120 writer.print(MessageFormat.format 121 (Constants.SERVER_HEADER_SECTION, args)); 122 } 123 } 124 125 126 public static void writeServerInfo(PrintWriter writer, Object [] args, 127 int mode){ 128 if (mode == 0){ 129 writer.print(MessageFormat.format(Constants.SERVER_ROW_SECTION, args)); 130 } 131 } 132 133 134 137 public static void writeFooter(PrintWriter writer, int mode) { 138 if (mode == 0){ 139 writer.print(Constants.HTML_TAIL_SECTION); 141 } else if (mode == 1){ 142 writer.write("</status>"); 143 } 144 } 145 146 147 151 public static void writeOSState(PrintWriter writer, int mode) { 152 long[] result = new long[16]; 153 boolean ok = false; 154 try { 155 String methodName = "info"; 156 Class paramTypes[] = new Class [1]; 157 paramTypes[0] = result.getClass(); 158 Object paramValues[] = new Object [1]; 159 paramValues[0] = result; 160 Method method = Class.forName("org.apache.tomcat.jni.OS") 161 .getMethod(methodName, paramTypes); 162 method.invoke(null, paramValues); 163 ok = true; 164 } catch (Throwable t) { 165 } 167 168 if (ok) { 169 if (mode == 0){ 170 writer.print("<h1>OS</h1>"); 171 172 writer.print("<p>"); 173 writer.print(" Physical memory: "); 174 writer.print(formatSize(new Long (result[0]), true)); 175 writer.print(" Available memory: "); 176 writer.print(formatSize(new Long (result[1]), true)); 177 writer.print(" Total page file: "); 178 writer.print(formatSize(new Long (result[2]), true)); 179 writer.print(" Free page file: "); 180 writer.print(formatSize(new Long (result[3]), true)); 181 writer.print(" Memory load: "); 182 writer.print(new Long (result[6])); 183 writer.print("<br>"); 184 writer.print(" Process kernel time: "); 185 writer.print(formatTime(new Long (result[11] / 1000), true)); 186 writer.print(" Process user time: "); 187 writer.print(formatTime(new Long (result[12] / 1000), true)); 188 writer.print("</p>"); 189 } else if (mode == 1){ 190 } 191 } 192 193 } 194 195 196 200 public static void writeVMState(PrintWriter writer, int mode) 201 throws Exception { 202 203 if (mode == 0){ 204 writer.print("<h1>JVM</h1>"); 205 206 writer.print("<p>"); 207 writer.print(" Free memory: "); 208 writer.print(formatSize 209 (new Long (Runtime.getRuntime().freeMemory()), true)); 210 writer.print(" Total memory: "); 211 writer.print(formatSize 212 (new Long (Runtime.getRuntime().totalMemory()), true)); 213 writer.print(" Max memory: "); 214 writer.print(formatSize 215 (new Long (Runtime.getRuntime().maxMemory()), true)); 216 writer.print("</p>"); 217 } else if (mode == 1){ 218 writer.write("<jvm>"); 219 220 writer.write("<memory"); 221 writer.write(" free='" + Runtime.getRuntime().freeMemory() + "'"); 222 writer.write(" total='" + Runtime.getRuntime().totalMemory() + "'"); 223 writer.write(" max='" + Runtime.getRuntime().maxMemory() + "'/>"); 224 225 writer.write("</jvm>"); 226 } 227 228 } 229 230 231 234 public static void writeConnectorState(PrintWriter writer, 235 ObjectName tpName, String name, 236 MBeanServer mBeanServer, 237 Vector globalRequestProcessors, 238 Vector requestProcessors, 239 int mode) 240 throws Exception { 241 242 if (mode == 0) { 243 writer.print("<h1>"); 244 writer.print(name); 245 writer.print("</h1>"); 246 247 writer.print("<p>"); 248 writer.print(" Max threads: "); 249 writer.print(mBeanServer.getAttribute(tpName, "maxThreads")); 250 writer.print(" Current thread count: "); 251 writer.print(mBeanServer.getAttribute(tpName, "currentThreadCount")); 252 writer.print(" Current thread busy: "); 253 writer.print(mBeanServer.getAttribute(tpName, "currentThreadsBusy")); 254 try { 255 Object value = mBeanServer.getAttribute(tpName, "keepAliveCount"); 256 writer.print(" Keeped alive sockets count: "); 257 writer.print(value); 258 } catch (Exception e) { 259 } 261 262 writer.print("<br>"); 263 264 ObjectName grpName = null; 265 266 Enumeration enumeration = globalRequestProcessors.elements(); 267 while (enumeration.hasMoreElements()) { 268 ObjectName objectName = (ObjectName ) enumeration.nextElement(); 269 if (name.equals(objectName.getKeyProperty("name"))) { 270 grpName = objectName; 271 } 272 } 273 274 if (grpName == null) { 275 return; 276 } 277 278 writer.print(" Max processing time: "); 279 writer.print(formatTime(mBeanServer.getAttribute 280 (grpName, "maxTime"), false)); 281 writer.print(" Processing time: "); 282 writer.print(formatTime(mBeanServer.getAttribute 283 (grpName, "processingTime"), true)); 284 writer.print(" Request count: "); 285 writer.print(mBeanServer.getAttribute(grpName, "requestCount")); 286 writer.print(" Error count: "); 287 writer.print(mBeanServer.getAttribute(grpName, "errorCount")); 288 writer.print(" Bytes received: "); 289 writer.print(formatSize(mBeanServer.getAttribute 290 (grpName, "bytesReceived"), true)); 291 writer.print(" Bytes sent: "); 292 writer.print(formatSize(mBeanServer.getAttribute 293 (grpName, "bytesSent"), true)); 294 writer.print("</p>"); 295 296 writer.print("<table border=\"0\"><tr><th>Stage</th><th>Time</th><th>B Sent</th><th>B Recv</th><th>Client</th><th>VHost</th><th>Request</th></tr>"); 297 298 enumeration = requestProcessors.elements(); 299 while (enumeration.hasMoreElements()) { 300 ObjectName objectName = (ObjectName ) enumeration.nextElement(); 301 if (name.equals(objectName.getKeyProperty("worker"))) { 302 writer.print("<tr>"); 303 writeProcessorState(writer, objectName, mBeanServer, mode); 304 writer.print("</tr>"); 305 } 306 } 307 308 writer.print("</table>"); 309 310 writer.print("<p>"); 311 writer.print("P: Parse and prepare request S: Service F: Finishing R: Ready K: Keepalive"); 312 writer.print("</p>"); 313 } else if (mode == 1){ 314 writer.write("<connector name='" + name + "'>"); 315 316 writer.write("<threadInfo "); 317 writer.write(" maxThreads=\"" + mBeanServer.getAttribute(tpName, "maxThreads") + "\""); 318 writer.write(" currentThreadCount=\"" + mBeanServer.getAttribute(tpName, "currentThreadCount") + "\""); 319 writer.write(" currentThreadsBusy=\"" + mBeanServer.getAttribute(tpName, "currentThreadsBusy") + "\""); 320 writer.write(" />"); 321 322 ObjectName grpName = null; 323 324 Enumeration enumeration = globalRequestProcessors.elements(); 325 while (enumeration.hasMoreElements()) { 326 ObjectName objectName = (ObjectName ) enumeration.nextElement(); 327 if (name.equals(objectName.getKeyProperty("name"))) { 328 grpName = objectName; 329 } 330 } 331 332 if (grpName != null) { 333 334 writer.write("<requestInfo "); 335 writer.write(" maxTime=\"" + mBeanServer.getAttribute(grpName, "maxTime") + "\""); 336 writer.write(" processingTime=\"" + mBeanServer.getAttribute(grpName, "processingTime") + "\""); 337 writer.write(" requestCount=\"" + mBeanServer.getAttribute(grpName, "requestCount") + "\""); 338 writer.write(" errorCount=\"" + mBeanServer.getAttribute(grpName, "errorCount") + "\""); 339 writer.write(" bytesReceived=\"" + mBeanServer.getAttribute(grpName, "bytesReceived") + "\""); 340 writer.write(" bytesSent=\"" + mBeanServer.getAttribute(grpName, "bytesSent") + "\""); 341 writer.write(" />"); 342 343 writer.write("<workers>"); 344 enumeration = requestProcessors.elements(); 345 while (enumeration.hasMoreElements()) { 346 ObjectName objectName = (ObjectName ) enumeration.nextElement(); 347 if (name.equals(objectName.getKeyProperty("worker"))) { 348 writeProcessorState(writer, objectName, mBeanServer, mode); 349 } 350 } 351 writer.write("</workers>"); 352 } 353 354 writer.write("</connector>"); 355 } 356 357 } 358 359 360 363 protected static void writeProcessorState(PrintWriter writer, 364 ObjectName pName, 365 MBeanServer mBeanServer, 366 int mode) 367 throws Exception { 368 369 Integer stageValue = 370 (Integer ) mBeanServer.getAttribute(pName, "stage"); 371 int stage = stageValue.intValue(); 372 boolean fullStatus = true; 373 boolean showRequest = true; 374 String stageStr = null; 375 376 switch (stage) { 377 378 case (1): 379 stageStr = "P"; 380 fullStatus = false; 381 break; 382 case (2): 383 stageStr = "P"; 384 fullStatus = false; 385 break; 386 case (3): 387 stageStr = "S"; 388 break; 389 case (4): 390 stageStr = "F"; 391 break; 392 case (5): 393 stageStr = "F"; 394 break; 395 case (7): 396 stageStr = "R"; 397 fullStatus = false; 398 break; 399 case (6): 400 stageStr = "K"; 401 fullStatus = true; 402 showRequest = false; 403 break; 404 case (0): 405 stageStr = "R"; 406 fullStatus = false; 407 break; 408 default: 409 stageStr = "?"; 411 fullStatus = false; 412 413 } 414 415 if (mode == 0) { 416 writer.write("<td><strong>"); 417 writer.write(stageStr); 418 writer.write("</strong></td>"); 419 420 if (fullStatus) { 421 writer.write("<td>"); 422 writer.print(formatTime(mBeanServer.getAttribute 423 (pName, "requestProcessingTime"), false)); 424 writer.write("</td>"); 425 writer.write("<td>"); 426 if (showRequest) { 427 writer.print(formatSize(mBeanServer.getAttribute 428 (pName, "requestBytesSent"), false)); 429 } else { 430 writer.write("?"); 431 } 432 writer.write("</td>"); 433 writer.write("<td>"); 434 if (showRequest) { 435 writer.print(formatSize(mBeanServer.getAttribute 436 (pName, "requestBytesReceived"), 437 false)); 438 } else { 439 writer.write("?"); 440 } 441 writer.write("</td>"); 442 writer.write("<td>"); 443 writer.print(filter(mBeanServer.getAttribute 444 (pName, "remoteAddr"))); 445 writer.write("</td>"); 446 writer.write("<td nowrap>"); 447 writer.write(filter(mBeanServer.getAttribute 448 (pName, "virtualHost"))); 449 writer.write("</td>"); 450 writer.write("<td nowrap>"); 451 if (showRequest) { 452 writer.write(filter(mBeanServer.getAttribute 453 (pName, "method"))); 454 writer.write(" "); 455 writer.write(filter(mBeanServer.getAttribute 456 (pName, "currentUri"))); 457 String queryString = (String ) mBeanServer.getAttribute 458 (pName, "currentQueryString"); 459 if ((queryString != null) && (!queryString.equals(""))) { 460 writer.write("?"); 461 writer.print(RequestUtil.filter(queryString)); 462 } 463 writer.write(" "); 464 writer.write(filter(mBeanServer.getAttribute 465 (pName, "protocol"))); 466 } else { 467 writer.write("?"); 468 } 469 writer.write("</td>"); 470 } else { 471 writer.write("<td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td>"); 472 } 473 } else if (mode == 1){ 474 writer.write("<worker "); 475 writer.write(" stage=\"" + stageStr + "\""); 476 477 if (fullStatus) { 478 writer.write(" requestProcessingTime=\"" 479 + mBeanServer.getAttribute 480 (pName, "requestProcessingTime") + "\""); 481 writer.write(" requestBytesSent=\""); 482 if (showRequest) { 483 writer.write("" + mBeanServer.getAttribute 484 (pName, "requestBytesSent")); 485 } else { 486 writer.write("0"); 487 } 488 writer.write("\""); 489 writer.write(" requestBytesReceived=\""); 490 if (showRequest) { 491 writer.write("" + mBeanServer.getAttribute 492 (pName, "requestBytesReceived")); 493 } else { 494 writer.write("0"); 495 } 496 writer.write("\""); 497 writer.write(" remoteAddr=\"" 498 + filter(mBeanServer.getAttribute 499 (pName, "remoteAddr")) + "\""); 500 writer.write(" virtualHost=\"" 501 + filter(mBeanServer.getAttribute 502 (pName, "virtualHost")) + "\""); 503 504 if (showRequest) { 505 writer.write(" method=\"" 506 + filter(mBeanServer.getAttribute 507 (pName, "method")) + "\""); 508 writer.write(" currentUri=\"" 509 + filter(mBeanServer.getAttribute 510 (pName, "currentUri")) + "\""); 511 512 String queryString = (String ) mBeanServer.getAttribute 513 (pName, "currentQueryString"); 514 if ((queryString != null) && (!queryString.equals(""))) { 515 writer.write(" currentQueryString=\"" 516 + RequestUtil.filter(queryString) + "\""); 517 } else { 518 writer.write(" currentQueryString=\"?\""); 519 } 520 writer.write(" protocol=\"" 521 + filter(mBeanServer.getAttribute 522 (pName, "protocol")) + "\""); 523 } else { 524 writer.write(" method=\"?\""); 525 writer.write(" currentUri=\"?\""); 526 writer.write(" currentQueryString=\"?\""); 527 writer.write(" protocol=\"?\""); 528 } 529 } else { 530 writer.write(" requestProcessingTime=\"0\""); 531 writer.write(" requestBytesSent=\"0\""); 532 writer.write(" requestBytesRecieved=\"0\""); 533 writer.write(" remoteAddr=\"?\""); 534 writer.write(" virtualHost=\"?\""); 535 writer.write(" method=\"?\""); 536 writer.write(" currentUri=\"?\""); 537 writer.write(" currentQueryString=\"?\""); 538 writer.write(" protocol=\"?\""); 539 } 540 writer.write(" />"); 541 } 542 543 } 544 545 546 549 public static void writeDetailedState(PrintWriter writer, 550 MBeanServer mBeanServer, int mode) 551 throws Exception { 552 553 if (mode == 0){ 554 ObjectName queryHosts = new ObjectName ("*:j2eeType=WebModule,*"); 555 Set hostsON = mBeanServer.queryNames(queryHosts, null); 556 557 writer.print("<h1>"); 559 writer.print("Application list"); 560 writer.print("</h1>"); 561 562 writer.print("<p>"); 563 int count = 0; 564 Iterator iterator = hostsON.iterator(); 565 while (iterator.hasNext()) { 566 ObjectName contextON = (ObjectName ) iterator.next(); 567 String webModuleName = contextON.getKeyProperty("name"); 568 if (webModuleName.startsWith("//")) { 569 webModuleName = webModuleName.substring(2); 570 } 571 int slash = webModuleName.indexOf("/"); 572 if (slash == -1) { 573 count++; 574 continue; 575 } 576 577 writer.print("<a HREF=\"#" + (count++) + ".0\">"); 578 writer.print(webModuleName); 579 writer.print("</a>"); 580 if (iterator.hasNext()) { 581 writer.print("<br>"); 582 } 583 584 } 585 writer.print("</p>"); 586 587 count = 0; 589 iterator = hostsON.iterator(); 590 while (iterator.hasNext()) { 591 ObjectName contextON = (ObjectName ) iterator.next(); 592 writer.print("<a class=\"A.name\" name=\"" 593 + (count++) + ".0\">"); 594 writeContext(writer, contextON, mBeanServer, mode); 595 } 596 597 } else if (mode == 1){ 598 } 600 601 } 602 603 604 607 protected static void writeContext(PrintWriter writer, 608 ObjectName objectName, 609 MBeanServer mBeanServer, int mode) 610 throws Exception { 611 612 if (mode == 0){ 613 String webModuleName = objectName.getKeyProperty("name"); 614 String name = webModuleName; 615 if (name == null) { 616 return; 617 } 618 619 String hostName = null; 620 String contextName = null; 621 if (name.startsWith("//")) { 622 name = name.substring(2); 623 } 624 int slash = name.indexOf("/"); 625 if (slash != -1) { 626 hostName = name.substring(0, slash); 627 contextName = name.substring(slash); 628 } else { 629 return; 630 } 631 632 ObjectName queryManager = new ObjectName 633 (objectName.getDomain() + ":type=Manager,path=" + contextName 634 + ",host=" + hostName + ",*"); 635 Set managersON = mBeanServer.queryNames(queryManager, null); 636 ObjectName managerON = null; 637 Iterator iterator2 = managersON.iterator(); 638 while (iterator2.hasNext()) { 639 managerON = (ObjectName ) iterator2.next(); 640 } 641 642 ObjectName queryJspMonitor = new ObjectName 643 (objectName.getDomain() + ":type=JspMonitor,WebModule=" + 644 webModuleName + ",*"); 645 Set jspMonitorONs = mBeanServer.queryNames(queryJspMonitor, null); 646 647 if (contextName.equals("/")) { 649 contextName = ""; 650 } 651 652 writer.print("<h1>"); 653 writer.print(name); 654 writer.print("</h1>"); 655 writer.print("</a>"); 656 657 writer.print("<p>"); 658 Object startTime = mBeanServer.getAttribute(objectName, 659 "startTime"); 660 writer.print(" Start time: " + 661 new Date (((Long ) startTime).longValue())); 662 writer.print(" Startup time: "); 663 writer.print(formatTime(mBeanServer.getAttribute 664 (objectName, "startupTime"), false)); 665 writer.print(" TLD scan time: "); 666 writer.print(formatTime(mBeanServer.getAttribute 667 (objectName, "tldScanTime"), false)); 668 if (managerON != null) { 669 writeManager(writer, managerON, mBeanServer, mode); 670 } 671 if (jspMonitorONs != null) { 672 writeJspMonitor(writer, jspMonitorONs, mBeanServer, mode); 673 } 674 writer.print("</p>"); 675 676 String onStr = objectName.getDomain() 677 + ":j2eeType=Servlet,WebModule=" + webModuleName + ",*"; 678 ObjectName servletObjectName = new ObjectName (onStr); 679 Set set = mBeanServer.queryMBeans(servletObjectName, null); 680 Iterator iterator = set.iterator(); 681 while (iterator.hasNext()) { 682 ObjectInstance oi = (ObjectInstance ) iterator.next(); 683 writeWrapper(writer, oi.getObjectName(), mBeanServer, mode); 684 } 685 686 } else if (mode == 1){ 687 } 689 690 } 691 692 693 696 public static void writeManager(PrintWriter writer, ObjectName objectName, 697 MBeanServer mBeanServer, int mode) 698 throws Exception { 699 700 if (mode == 0) { 701 writer.print("<br>"); 702 writer.print(" Active sessions: "); 703 writer.print(mBeanServer.getAttribute 704 (objectName, "activeSessions")); 705 writer.print(" Session count: "); 706 writer.print(mBeanServer.getAttribute 707 (objectName, "sessionCounter")); 708 writer.print(" Max active sessions: "); 709 writer.print(mBeanServer.getAttribute(objectName, "maxActive")); 710 writer.print(" Rejected session creations: "); 711 writer.print(mBeanServer.getAttribute 712 (objectName, "rejectedSessions")); 713 writer.print(" Expired sessions: "); 714 writer.print(mBeanServer.getAttribute 715 (objectName, "expiredSessions")); 716 writer.print(" Longest session alive time: "); 717 writer.print(formatSeconds(mBeanServer.getAttribute( 718 objectName, 719 "sessionMaxAliveTime"))); 720 writer.print(" Average session alive time: "); 721 writer.print(formatSeconds(mBeanServer.getAttribute( 722 objectName, 723 "sessionAverageAliveTime"))); 724 writer.print(" Processing time: "); 725 writer.print(formatTime(mBeanServer.getAttribute 726 (objectName, "processingTime"), false)); 727 } else if (mode == 1) { 728 } 730 731 } 732 733 734 737 public static void writeJspMonitor(PrintWriter writer, 738 Set jspMonitorONs, 739 MBeanServer mBeanServer, 740 int mode) 741 throws Exception { 742 743 int jspCount = 0; 744 int jspReloadCount = 0; 745 746 Iterator iter = jspMonitorONs.iterator(); 747 while (iter.hasNext()) { 748 ObjectName jspMonitorON = (ObjectName ) iter.next(); 749 Object obj = mBeanServer.getAttribute(jspMonitorON, "jspCount"); 750 jspCount += ((Integer ) obj).intValue(); 751 obj = mBeanServer.getAttribute(jspMonitorON, "jspReloadCount"); 752 jspReloadCount += ((Integer ) obj).intValue(); 753 } 754 755 if (mode == 0) { 756 writer.print("<br>"); 757 writer.print(" JSPs loaded: "); 758 writer.print(jspCount); 759 writer.print(" JSPs reloaded: "); 760 writer.print(jspReloadCount); 761 } else if (mode == 1) { 762 } 764 } 765 766 767 770 public static void writeWrapper(PrintWriter writer, ObjectName objectName, 771 MBeanServer mBeanServer, int mode) 772 throws Exception { 773 774 if (mode == 0) { 775 String servletName = objectName.getKeyProperty("name"); 776 777 String [] mappings = (String []) 778 mBeanServer.invoke(objectName, "findMappings", null, null); 779 780 writer.print("<h2>"); 781 writer.print(servletName); 782 if ((mappings != null) && (mappings.length > 0)) { 783 writer.print(" [ "); 784 for (int i = 0; i < mappings.length; i++) { 785 writer.print(mappings[i]); 786 if (i < mappings.length - 1) { 787 writer.print(" , "); 788 } 789 } 790 writer.print(" ] "); 791 } 792 writer.print("</h2>"); 793 794 writer.print("<p>"); 795 writer.print(" Processing time: "); 796 writer.print(formatTime(mBeanServer.getAttribute 797 (objectName, "processingTime"), true)); 798 writer.print(" Max time: "); 799 writer.print(formatTime(mBeanServer.getAttribute 800 (objectName, "maxTime"), false)); 801 writer.print(" Request count: "); 802 writer.print(mBeanServer.getAttribute(objectName, "requestCount")); 803 writer.print(" Error count: "); 804 writer.print(mBeanServer.getAttribute(objectName, "errorCount")); 805 writer.print(" Load time: "); 806 writer.print(formatTime(mBeanServer.getAttribute 807 (objectName, "loadTime"), false)); 808 writer.print(" Classloading time: "); 809 writer.print(formatTime(mBeanServer.getAttribute 810 (objectName, "classLoadTime"), false)); 811 writer.print("</p>"); 812 } else if (mode == 1){ 813 } 815 816 } 817 818 819 826 public static String filter(Object obj) { 827 828 if (obj == null) 829 return ("?"); 830 String message = obj.toString(); 831 832 char content[] = new char[message.length()]; 833 message.getChars(0, message.length(), content, 0); 834 StringBuffer result = new StringBuffer (content.length + 50); 835 for (int i = 0; i < content.length; i++) { 836 switch (content[i]) { 837 case '<': 838 result.append("<"); 839 break; 840 case '>': 841 result.append(">"); 842 break; 843 case '&': 844 result.append("&"); 845 break; 846 case '"': 847 result.append("""); 848 break; 849 default: 850 result.append(content[i]); 851 } 852 } 853 return (result.toString()); 854 855 } 856 857 858 863 public static String formatSize(Object obj, boolean mb) { 864 865 long bytes = -1L; 866 867 if (obj instanceof Long ) { 868 bytes = ((Long ) obj).longValue(); 869 } else if (obj instanceof Integer ) { 870 bytes = ((Integer ) obj).intValue(); 871 } 872 873 if (mb) { 874 long mbytes = bytes / (1024 * 1024); 875 long rest = 876 ((bytes - (mbytes * (1024 * 1024))) * 100) / (1024 * 1024); 877 return (mbytes + "." + ((rest < 10) ? "0" : "") + rest + " MB"); 878 } else { 879 return ((bytes / 1024) + " KB"); 880 } 881 882 } 883 884 885 890 public static String formatTime(Object obj, boolean seconds) { 891 892 long time = -1L; 893 894 if (obj instanceof Long ) { 895 time = ((Long ) obj).longValue(); 896 } else if (obj instanceof Integer ) { 897 time = ((Integer ) obj).intValue(); 898 } 899 900 if (seconds) { 901 return ((((float) time ) / 1000) + " s"); 902 } else { 903 return (time + " ms"); 904 } 905 } 906 907 908 915 public static String formatSeconds(Object obj) { 916 917 long time = -1L; 918 919 if (obj instanceof Long ) { 920 time = ((Long ) obj).longValue(); 921 } else if (obj instanceof Integer ) { 922 time = ((Integer ) obj).intValue(); 923 } 924 925 return (time + " s"); 926 } 927 928 } 929 | Popular Tags |