Simulate the tostring method of Tomcat Class Loader Using Reflection

Source: Internet
Author: User

I always think that the tostring method of the Tomcat 5 Class Loader is cool. Why? Because its tostring method lists all the parent class loaders and the resources loaded by the class loaders (that is, classpath ). some friends may not have noticed it. If you want to see how cool it is, you can find a JSP.
<% SYSTEM. Out. println (COM. syj. Test. Class. getclassloader (); %>

WEB-INF, classes, COM, syj, test

Start tomcat to access JSP and check the console:

Information: exposing service with name {http://ws.syj.com} demo
16:15:32 org. Apache. Coyote. http11.http11protocol start
Information: Starting coyote HTTP/1.1 on http-8080
16:15:33 org. Apache. JK. Common. channelsocket init
Information: JK2: ajp13 listening on/0.0.0.0: 8009
16:15:33 org. Apache. JK. server. jkmain start
Information: JK running id = 0 time = 15/78 Config = D:/syj. Work/syj. Server/tomcat5/CONF/jk2.properties
16:15:33 org. Apache. Catalina. startup. Catalina start
Information: server startup in 6907 MS

Reprinted please indicate the source http://blog.csdn.net/sunyujia/
Webappclassloader
Delegate: false
Repositories:
/WEB-INF/classes/
----------> Parent classloader:
Standardclassloader
Delegate: True
Repositories:
File: D:/syj. Work/syj. Server/tomcat5/shared/classes/
----------> Parent classloader:
Standardclassloader
Delegate: True
Repositories:
File: D:/syj. Work/syj. Server/tomcat5/common/classes/
File: D:/syj. Work/syj. Server/tomcat5/common/lib/ant-launcher.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/ant. Jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/commons-collections-3.1.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/commons-dbcp-1.2.1.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/commons-el.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/commons-pool-1.2.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/jasper-compiler.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/jasper-runtime.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/jsp-api.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/mssql_all.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/naming-common.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/naming-factory.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/naming-java.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/naming-resources.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/servlet-api.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/tools. Jar
----------> Parent classloader:
Sun. Misc. launcher $ appclassloader @ 18d0000f

 

The classpath provides clear debugging information and is useful in some complex class loading scenarios,

Decompile tomcat5/Server/lib/Catalina. Jar/webappclassloader. Class class to view the source code

  1. Public String tostring ()
  2. {
  3. Stringbuffer sb = new stringbuffer ("webappclassloader/R/N ");
  4. SB. append ("delegate :");
  5. SB. append (_ flddelegate );
  6. SB. append ("/R/N ");
  7. SB. append ("repositories:/R/N ");
  8. If (repositories! = NULL)
  9. {
  10. For (INT I = 0; I <repositories. length; I ++)
  11. {
  12. SB. append ("");
  13. SB. append (repositories [I]);
  14. SB. append ("/R/N ");
  15. }
  16. }
  17. If (parent! = NULL)
  18. {
  19. SB. append ("----------> parent classloader:/R/N ");
  20. SB. append (parent. tostring ());
  21. SB. append ("/R/N ");
  22. }
  23. Return sb. tostring ();
  24. }

 

So easy, so I want to write an util class and create a public static string tostring (classloader) method. I just want to do it right away, but the repositories attribute is self-built by Tomcat, in the class loader provided by Sun, which attribute stores classpath? I am not interested in reading the source code of the Tomcat class loading process, so I found a channel in the memory in debug and found that it was originally stored in the urlclassloader's urlclasspath UCP attribute, although not public, but it does not matter, because reflection can be achieved, but reflection is a strong point. Haha, there are not many codes and I will post them to share the results with you.

 

  1. Import java. Io. file;
  2. Import java. Lang. Reflect. field;
  3. Import java.net. url;
  4. Import java.net. urlclassloader;
  5. Import java. util. List;
  6. /**
  7. * <P>
  8. * Title: Debugging auxiliary class
  9. * </P>
  10. *
  11. * <P>
  12. * Description:
  13. * </P>
  14. *
  15. * <P>
  16. * Copyright: Reprinted please indicate the source http://blog.csdn.net/sunyujia/
  17. * </P>
  18. *
  19. * @ Author Sun Xiaojia
  20. * @ Main sunyujia@yahoo.cn
  21. * @ Date Sep 20,200 8 3:27:23 pm
  22. */
  23. Public class debugutil {
  24. /**
  25. *
  26. * Description: converts a class loader to a string for printing and debugging.
  27. * Reprinted please indicate the source http://blog.csdn.net/sunyujia/
  28. *
  29. * @ Mail sunyujia@yahoo.cn
  30. *
  31. * @ Since: SEP 20,200 8 3:29:59
  32. */
  33. Public static string tostring (classloader ){
  34. Stringbuffer sb = new stringbuffer (classloader. getclass (). getname ());
  35. SB. append ("/R/N ");
  36. Try {
  37. Field [] fields = urlclassloader. Class. getdeclaredfields (); // Retrieves all attributes of the urlclassloader class loader.
  38. For (INT I = 0; I <fields. length; I ++) {// traverses attributes
  39. Field field = Fields [I];
  40. If (field. getname (). Equals ("UCP") {// locate the UCP attribute Field object
  41. Field. setaccessible (true); // open the access permission
  42. Final object UCP = field. Get (classloader); // obtain the UCP object in the Class Loader
  43. Field [] ucpfields = UCP. getclass (). getdeclaredfields (); // retrieve all attributes of the UCP object
  44. For (int K = 0; k <ucpfields. length; k ++) {// traverses attributes of a UCP object
  45. Ucpfields [K]. setaccessible (true); // open the access permission
  46. If (ucpfields [K]. getname (). Equals ("path") {// obtain the path attribute Field object in the UCP object
  47. List list = (list) ucpfields [K]. Get (UCP); // obtain the path object in the UCP object
  48. SB. append ("repositories:/R/N ");
  49. For (Int J = 0; j <list. Size (); j ++ ){
  50. SB. append ("");
  51. SB. append (list. Get (j ));
  52. SB. append ("/R/N ");
  53. }
  54. }
  55. }
  56. }
  57. }
  58. If (classloader. getparent ()! = NULL ){
  59. SB. append ("----------> parent classloader:/R/N ");
  60. SB. append (tostring (classloader. getparent (); // Recursion
  61. SB. append ("/R/N ");
  62. }
  63. } Catch (exception e ){
  64. E. printstacktrace ();
  65. }
  66. Return sb. tostring ();
  67. }
  68. Public static void main (string [] ARGs) throws exception {
  69. File file1 = new file (
  70. "D:/syj. Work/syj. Server/tomcat5/common/lib/ant. Jar ");
  71. File file2 = new file (
  72. "D:/syj. Work/syj. Server/tomcat5/webapps/webservicesdemo/WEB-INF/classes ");
  73. Urlclassloader MCL = new urlclassloader (new URL [] {
  74. File1.touri (). tourl (), file2.touri (). tourl ()});
  75. System. Out. println ("demo 1 :");
  76. System. Out
  77. . Println (debugutil. tostring (debugutil. Class. getclassloader ()));
  78. System. Out. println ("DEMO 2 :");
  79. Class clazz = MCL. loadclass ("com. syj. test ");
  80. System. Out. println (debugutil. tostring (clazz. getclassloader ()));
  81. }
  82. }

Program output result:

 

Demonstration 1:
Sun. Misc. launcher $ appclassloader
Repositories:
File:/D:/syj. Work/syj. workspace/WS1/myblogtest/bin/
----------> Parent classloader:
Sun. Misc. launcher $ extclassloader
Repositories:
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/dnsns. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/localedata. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunjce_provider.jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunmscapi. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunpkcs11.jar

Demonstration 2:
Java.net. urlclassloader
Repositories:
File:/D:/syj. Work/syj. Server/tomcat5/common/lib/ant. Jar
File:/D:/syj. Work/syj. Server/tomcat5/webapps/webservicesdemo/WEB-INF/classes/
----------> Parent classloader:
Sun. Misc. launcher $ appclassloader
Repositories:
File:/D:/syj. Work/syj. workspace/WS1/myblogtest/bin/
----------> Parent classloader:
Sun. Misc. launcher $ extclassloader
Repositories:
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/dnsns. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/localedata. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunjce_provider.jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunmscapi. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunpkcs11.jar

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.