Classloader for Tomcat source code analysis

Source: Internet
Author: User
Classloader for Tomcat source code analysis

All articles in this series are Tomcat 7.0 code analysis.

 

1. Basic classloader knowledge 1.1. Parent-Child principal Model

 

We know that in the Java System, the default Loading Method of the Class Loader is to use the parent-child delegate method to load the class, that is, first try to use the parent class loader to load the class, if this class is not found, we can see that this is a recursive loading process. The core code is roughly as follows:

Java code
  1. Protected class <?> Loadclass (string name, Boolean resolve)
  2. Throws classnotfoundexception
  3. {
  4. Synchronized (getclassloadinglock (name )){
  5. // First, check if the class has already been loaded
  6. Class C = findloadedclass (name );
  7. If (C = NULL ){
  8. ......
  9. Try {
  10. If (parent! = NULL ){
  11. C = parent. loadclass (name, false );
  12. } Else {
  13. C = findbootstrapclassornull (name );
  14. }
  15. } Catch (classnotfoundexception e ){
  16. // Classnotfoundexception thrown if class not found
  17. // From the non-null parent class loader
  18. }
  19. If (C = NULL ){
  20. // If still not found, then invoke findclass in order
  21. // To find the class.
  22. ...
  23. C = findclass (name );
  24. ......
  25. }
  26. }
  27. ......
  28. Return C;
  29. }
  30. }
Many classloader inheritance classes use this loading method by default. urlclassloader is widely used. 1.2. runtime model of the Class Loader:

 

When a JVM is started, at least three classloader will be started:

1. Bootstrap Class Loader

Load the core Java packages and place them in the <java_home>/lib directory. This part is part of the JVM and is usually completed using native code.

2. Extensions Class Loader

Load the Java extension package, that is, the Package located under <java_home>/lib/EXT. Note that, some JVMs use the same loader as the bootstrap class loader, while Sun separates the two. Its implementation class is Sun. misc. launcher $ extclassloader.

3. System Class Loader

The default Implementation of Sun is Sun. Misc. launcher $ extclassloader.

The parent-child relationship between the three is: the bootstrap class loader is the parent class loader of the extensions Class Loader, And the extensions class loader is the parent class loader of the system class loader.

2. Tomcat classloader Model

Shows how to load the hierarchical model of classloader of Tomcat 7.0:

 

This model is somewhat different from the previous Tomcat 5.5, because in addition to the common class loader, there are Catalina class loaders and shared class loaders, this can only lead to more configurations and concepts, which are no longer used, although configuration can be performed.

1. common Class Loader: loads class files and jar packages and their resource files under $ catalina_home/lib and $ catalina_base/lib. These files will be shared by all web applications, it will also be used when Tomcat is running. It is configured in Catalina. in properties, if no configuration is available, the system class loader is used by default. Configuration example: Common. loader =$ {Catalina. home}/lib, $ {Catalina. home}/lib /*. jar2. app Class Loader: Class Loader for Web applications, resource files under Web applications, and jar packages in the class file box, which comply with Java Web standards, they are located under the/WEB-INF/classes and/WEB-INF/lib folder of the Web application. 3. Implementation of the Class Loader

 

The Tomcat class loader has three class implementations: standardclassloader, webappclassloader, and jasperloader. These three class loaders are subclasses of urlclassloader. The difference is that standardclassloader does not customize the urlclassloader method, that is, it also uses the delegate method to load classes. The loadclass () method is overwritten.

1. The standardclassloader Class Loader creates an instance of the common class loader.

2. webappclassloader creates a Class Loader instance for each application. We do not recommend that you use the Parent-Child principal model when loading the web application class, but must ensure that the class is Java. * And javax. * packages starting with the bootstrap loader must be loaded by the system class loader.

The core code is as follows:

Java code
  1. Public class loadclass (string name, Boolean resolve)
  2. Throws classnotfoundexception {
  3. ......
  4. // (0) Check our previusly loaded local class Cache
  5. Clazz = findloadedclass0 (name );
  6. If (clazz! = NULL ){
  7. If (log. isdebugenabled ())
  8. Log. debug ("returning class from cache ");
  9. If (RESOLVE)
  10. Resolveclass (clazz );
  11. Return (clazz );
  12. }
  13. // (0.1) Check our previusly loaded class Cache
  14. Clazz = findloadedclass (name );
  15. If (clazz! = NULL ){
  16. If (log. isdebugenabled ())
  17. Log. debug ("returning class from cache ");
  18. If (RESOLVE)
  19. Resolveclass (clazz );
  20. Return (clazz );
  21. }
  22. // (0.2) Try loading the class with the system class loader, to prevent
  23. // The webapp from overriding j2se classes
  24. // Ensure that packages starting with Java. * And javax. * must be loaded by the system class loader.
  25. Try {
  26. Clazz = system. loadclass (name );
  27. If (clazz! = NULL ){
  28. If (RESOLVE)
  29. Resolveclass (clazz );
  30. Return (clazz );
  31. }
  32. } Catch (classnotfoundexception e ){
  33. // Ignore
  34. }
  35. ......
  36. Boolean delegateload = delegate | filter (name );
  37. // (1) delegate to our parent if requested
  38. // Whether the delegate method is used when mbeans are loaded
  39. If (delegateload ){
  40. If (log. isdebugenabled ())
  41. Log. debug ("delegating to parent classloader1" + parent );
  42. Classloader loader = parent;
  43. If (loader = NULL)
  44. Loader = system;
  45. Try {
  46. Clazz = loader. loadclass (name );
  47. If (clazz! = NULL ){
  48. If (log. isdebugenabled ())
  49. Log. debug ("loading class from parent ");
  50. If (RESOLVE)
  51. Resolveclass (clazz );
  52. Return (clazz );
  53. }
  54. } Catch (classnotfoundexception e ){
  55. ;
  56. }
  57. }
  58. // (2) Search Local repositories
  59. // Load locally without entrusting the parent Loader
  60. If (log. isdebugenabled ())
  61. Log. debug ("Searching Local repositories ");
  62. Try {
  63. Clazz = findclass (name );
  64. If (clazz! = NULL ){
  65. If (log. isdebugenabled ())
  66. Log. debug ("loading class from local repository ");
  67. If (RESOLVE)
  68. Resolveclass (clazz );
  69. Return (clazz );
  70. }
  71. } Catch (classnotfoundexception e ){
  72. ;
  73. }
  74. // Finally load with the parent Loader
  75. If (! Delegateload ){
  76. If (log. isdebugenabled ())
  77. Log. debug ("delegating to parent classloader at end:" + parent );
  78. Classloader loader = parent;
  79. If (loader = NULL)
  80. Loader = system;
  81. Try {
  82. Clazz = loader. loadclass (name );
  83. If (clazz! = NULL ){
  84. If (log. isdebugenabled ())
  85. Log. debug ("loading class from parent ");
  86. If (RESOLVE)
  87. Resolveclass (clazz );
  88. Return (clazz );
  89. }
  90. } Catch (classnotfoundexception e ){
  91. ;
  92. }
  93. }
  94. Throw new classnotfoundexception (name );
  95. }

We can see that it is not a simple parent-child delegation method to load classes.

3. jasperloader is a Class Loader created to load the servlet compiled by JSP. it overwrites the loadclass () method, except for over-loading Org. apache. JSP..

The core code is as follows:

Java code
  1. Public class loadclass (final string name, Boolean resolve)
  2. Throws classnotfoundexception {
  3. Class clazz = NULL;
  4. // (0) Check our previusly loaded class Cache
  5. Clazz = findloadedclass (name );
  6. If (clazz! = NULL ){
  7. If (RESOLVE)
  8. Resolveclass (clazz );
  9. Return (clazz );
  10. }
  11. // (. 5) permission to access this class when using a securitymanager
  12. ......
  13. If (! Name. startswith (constants. jsp_package_name )){
  14. // Class is not in org. Apache. jsp, therefore, have our
  15. // Parent load it
  16. Clazz = parent. loadclass (name );
  17. If (RESOLVE)
  18. Resolveclass (clazz );
  19. Return clazz;
  20. }
  21. // All files starting with org. Apache. jsp. * are loaded by the class loader.
  22. Return findclass (name );
  23. }

 

 

I will talk about it today.

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.