Tomcat class loader delegation model to break the parents

We divided into four parts to discuss:

1. What is the class loading mechanism?
2. What is the parents appointed model?
3. How undermine parents appointment model?
4. Tomcat class loader is how design?


I think that before the study tomcat class loader, we review the default or consolidate about java class loader. The landlord before the class is loaded muddle, to take this opportunity, but also a good refresher.

The landlord opened the book of God "in-depth understanding of the Java Virtual Machine," second edition, p227, part regarding class loader. Take a look:

1. What is the class loading mechanism?
Code compilation of the results of the transition from native machine code into bytecode, storage format is a small step, but it is a big step in the development of programming languages.

Described in the Java virtual machine loads data from Class class file into memory, and verify the data, and parsing initialization conversion, may ultimately form the pictures directly Java virtual machine type, which is a virtual machine class loading mechanism.

Virtual machine design team in the class loading phase "to get through the fully qualified name of a class description of such a binary byte stream" this action to the outside Java virtual machine to achieve, to allow the application to decide how to get the the necessary classes. This operation code modules to achieve a "class loader."

The relationship between class and class loader
class loader although only for loading implementation class action, but it played a role in a Java program is far from limited to class loading stage. For any class, we need to establish a loaded his class loader and the class itself with its uniqueness in the Java virtual machine, each class loader has a separate class namespace. This sentence can express some of the more popular: comparison of two classes whether "equal", and only under the premise of these two classes are loaded by the same class loader makes sense, otherwise, even if both are from the same class class files, is loaded with a virtual machine, as long as the class loader to load their different, and that the two classes will surely not equal.

2. What is the model parent appointed
from the perspective of the Java virtual machine is, there are only two different class loaders: one is to start class loader (Bootstrap ClassLoader), the class loader uses the C ++ language (HotSpot only) , it is part of the virtual machine itself; the other is for all other class loader, the class loader by the Java language, independent of external virtual machine, and all derived from the abstract class java.lang.ClassLoader.

From the perspective of Java developer's perspective, the class loader can also be divided into more detail some of the vast majority of Java programmers will use the following three class loader provided by the system:

Boot class loader (Bootstrap ClassLoader): This complex class loader will be stored in JAVA_HOME / lib directory, specified by the parameter or are -Xbootclasspath path thereof, and is identified by a virtual machine (identified only as a file name, such as rt.jar, even if the name does not comply with the library on the lib directory or reload).
Extension class loader (Extension ClassLoader): This class loader is implemented by sun.misc.Launcher $ ExtClassLoader, which is responsible for the inclusion JAVA_HOME lib / / ext directory or the system variables thereof java.ext.dirs path specified All library. Developers can directly use the extension class loader.
Application class loader (Application ClassLoader): This class loader is implemented by sun.misc.Launcher $ AppClassLoader. Since the class loader is the return value of the method getSystemClassLoader ClassLoader species, so it became the system class loader. It is responsible for loading the user class path (ClassPath) specified library. Developers can directly use this class loader, if the application had not defined its own class loaders, in general, this program is the default class loader.
The relationship between these classes generally loader as shown below:


FIG relationship between respective class loader becomes the parent class loader delegation model (Parents Dlegation Mode). In addition to the parent delegation model requires top-level boot loader class, the rest of the class loader should be loaded by its parent class loader, where parent-child relationship between the class loader will not inherit the general relationship to achieve, and use a combination of a multiplexed code in relation to the parent loader.

All Java class loader's parent delegation model was introduced and was widely used during the JDK1.2 later, but he was not a mandatory constraint model, but the Java designers recommend to developers a class loader It is implementation.

Parent delegation model is working process: If a class loader loads the class received a request, he first does not own to try to load this class, but the request to delegate the parent class loader to complete. Each level of the class loader is true, so all requests ultimate load should be transferred to the top of the boot class loader, loader feedback only when the parent they can not complete the request (his search does not find when the class), sub-loader will try to load your own.

Why do you do that?
If no parent delegation model, by each class loader to load their own words, if you have written a class called java.lang.Object and put ClassPath program, then the system will appear more different Object class, Java type system, the most basic of behavior can not be guaranteed. The application will also become a mess.

Parents how to implement the model when making appointments?
Very simple: all the code in the java.lang.ClassLoader loadClass method, as follows:


Lucid logic: if the check has been loaded before, if there is no load loadClass method is called the parent of the loader, such as the parent blank loader is used as the parent class loader boot loader default. If the parent fails to load, throw a ClassNotFoundException, then call their findClass method for loading.

3. How undermine parents appointment model?
We just said that parents appoint model is not a mandatory constraint model, but rather a suggestion type of class loader implementations. In the world of Java class loader most who follow the model, but there are exceptions, so far, parents delegate model has had three cases of large-scale "destroyed" in.
First: Before parents delegation model appears - that is, before the release of the JDK1.2.
Second: it is this model's own defects. We say, parents delegation model good solution to the harmonization of the base class of each class loader (the base of the upper classes loaded by the loader), the base class is called "foundation" because they always API is called as a user code, but not absolute, if you call the base class code the user will be how to do it?

This is not impossible. A typical example is the JNDI service, JNDI is now standard Java service, its code from the boot class loader to load (when JDK1.3 to go into rt.jar), but it needs to call realized by independent vendors and JNDI provider Interface (SPI, Service provider Interface) is deployed in ClassPath application code, but the boot class loader can not "know" the code ah. Because these classes are not rt.jar in, but they need to start the class loader to load. How to do it?

To solve this problem, Java design team had introduced a less elegant design: the thread context class loader (Thread Context ClassLoader). This class loader can be set by a method setContextClassLoader java.lang.Thread class. If you have not set a thread is created, it will inherit from a parent thread, if within the global scope of the application are not set too much, then the class loader by default even if the application class loader.

Hey, have a thread context loader, JNDI service uses this thread context loader to load SPI code is required, which is the parent class loader request child class loader to complete the action class loading, this behavior is actually open up the parents appoint a hierarchical model to reverse the use of the class loader has actually violated the general principles parent delegation model. But helpless, Java are all involved in the loading operation basic SPI wins have adopted this approach. For example JNDI, JDBC, JCE, JAXB, JBI like.

Third: In order to achieve hot swap, hot deployment, modular, meaning to add a feature or a minus function without restarting, you only need to replace these modules are connected together to achieve the same thermal loader replacement code.

The book also said:

Java programs have a basic consensus: worth learning when using OSGI class loader, understand the implementation of OSGI, it can be mastered the essence of the class loader.

Niubi ah! ! !

Now, we have to understand the basic default Java class loading effect principle, also know parents delegation model. Having said that, our tomcat almost forgotten, our topic is contrary to the parents why Tomcat loader delegation model? Here's good to talk about our tomcat class loader.

4. Tomcat class loader is how design?
First, let's ask a question:

If you use the default Tomcat class loading mechanism okay?
We think about: Tomcat is a web container, then it should solve the problem:
1. a web container may need to deploy two applications, different applications may rely on different versions of the same third-party libraries, you can not ask the same library on the same server only one copy, so make sure each application is independent libraries, to ensure mutual isolation.
2. Deploy the same in the same web container versions of the same library can be shared. Otherwise, if the server has 10 applications, then the library should have the same 10 parts loaded into a virtual machine, which is nonsense.
3. web container dependent libraries also have their own, can not be confused with the application library. Libraries based on security considerations, should allow container libraries and programs to isolate.
4. web container to support the modification jsp, we know that, ultimately jsp files to be compiled into class files to run in a virtual machine, but has been modified to run after jsp is a common occurrence, or should you use? So, web containers do not need to restart after the jsp support modification.

Look at our question: Tomcat If you use the default class loading mechanism okay?
The answer is not acceptable. why? We look at the first question, if you use the default class loader mechanism, it is unable to load two different versions of the same library, default accumulator is no matter what version you only care about your fully qualified class name, and only one. The second issue, the default class loader is able to achieve, because his duty is to ensure uniqueness. The third question and the first question the same. We look at fourth question, we want to modify how we want to achieve thermal jsp files (from the landlord's name), in fact, it is jsp file class file, if modified, but the name is still the same class, the class loader directly the method of taking the area already, jsp modified is not reloaded. So how to do it? We can unload directly off the class loader that jsp files, so you should think of each jsp file corresponds to a unique class loader, jsp when a file is modified, directly unload the jsp class loader. Re-create the class loader to reload jsp files.

Tomcat how to achieve their own unique class loading mechanism?
So, Tomcat is how to achieve it? Niubi Tomcat team has a good design. We look at their design:

 

 

 

We see, in front of three default class loading and consistent, CommonClassLoader, CatalinaClassLoader, SharedClassLoader and WebappClassLoader Tomcat class loader is its own definition, they are loaded / common / *, / server / *, / shared / * (in tomcat after 6 have been merged into the lib directory under the root directory) and / WebApp / WEB-INF / * in the Java class libraries. Wherein WebApp class loader and class loader Jsp there are often multiple instances, each corresponding to a Web application class loader WebApp, each corresponding to a JSP file Jsp class loader.

commonLoader: Tomcat basic class loader, class loading path Tomcat container itself may be accessed as well as individual Webapp;
catalinaLoader: Tomcat container private class loader, class loading path is invisible to the Webapp;
sharedLoader: each shared Webapp class loader, loading path class for all Webapp visible, but for Tomcat container is not visible;
WebappClassLoader: each Webapp private class loader, loading path class visible only to the current Webapp;
delegation relationship from the figure the As can be seen:

CommonClassLoader class can be loaded and Catalina ClassLoader SharedClassLoader used to achieve a common public library, but they can CatalinaClassLoader and Shared ClassLoader loaded classes are isolated from each other with each other.

WebAppClassLoader SharedClassLoader be used to load the class, but the isolation between respective WebAppClassLoader instances.

The load range JasperLoader only the JSP file compiled out of that .Class a file, the purpose of which is to be discarded appear: When the Web container detects JSP file is modified, will replace the current instance JasperLoader and HotSwap function to achieve the JSP file by re-establishing a new Jsp class loader.

Well, now, we already know tomcat why such a design, as well as how to design, then, java tomcat contrary to the recommendation of the parents delegated model yet? The answer is: contrary. As we have said:

In addition to the parent delegation model requires top-level boot loader class, the rest of the class loader should be loaded by its parent class loader.

Obviously, tomcat is not achieved, tomcat in order to achieve isolation, did not comply with this agreement, each webappClassLoader loading class files in its own directory and will not be passed to the parent class loader.

We extend a question: If the tomcat Common ClassLoader want to load WebApp ClassLoader of the class, how to do?
Read about the destruction of the parents in front of the delegated content model, we know the answer, we can use the thread context class loader implementations, using thread context loader that allows the parent class loader request child class loader class loader to complete action . Niubi it.

Summary
Well, finally, we understand why parents Tomcat contrary delegation model, but also know how tomcat class loader is designed. Incidentally, I brushed up the default Java class loader mechanism, but also know how to destroy the class loading mechanism of Java. This time, learned a lot Oh! ! ! Hey.

Well, stop here today.
----------------
Original link: https: //blog.csdn.net/qq_38182963/article/details/78660779

Guess you like

Origin www.cnblogs.com/jay-wu/p/11590571.html