`
Tyrion
  • 浏览: 261024 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Find a way out of the ClassLoader maze

阅读更多

Find a way out of the ClassLoader maze

System, current, context? Which ClassLoader should you use?

June 6, 2003

QWhen should I use
Thread.getContextClassLoader()?

AAlthough not frequently asked, this question is rather tough to correctly answer. It usually comes up during framework programming, when a good deal of dynamic class and resource loading goes on. In general, when loading a resource dynamically, you can choose from at least three classloaders: the system (also referred to as the application) classloader, the current classloader, and the current thread context classloader. The question above refers to the latter. Which classloader is the right one?

One choice I dismiss easily: the system classloader. This classloader handles -classpath and is programmatically accessible as ClassLoader.getSystemClassLoader(). All ClassLoader.getSystemXXX() API methods are also routed through this classloader. You should rarely write code that explicitly uses any of the previous methods and instead let other classloaders delegate to the system one. Otherwise, your code will only work in simple command-line applications, when the system classloader is the last classloader created in the JVM. As soon as you move your code into an Enterprise JavaBean, a Web application, or a Java Web Start application, things are guaranteed to break.

So, now we are down to two choices: current and context classloaders. By definition, a current classloader loads and defines the class to which your current method belongs. This classloader is implied when dynamic links between classes resolve at runtime, and when you use the one-argument version of Class.forName(),Class.getResource(), and similar methods. It is also used by syntactic constructs like X.class class literals (see "Get a Load of That Name!" for more details).

Thread context classloaders were introduced in Java 2 Platform, Standard Edition (J2SE). Every Thread has a context classloader associated with it (unless it was created by native code). It is set via the Thread.setContextClassLoader() method. If you don't invoke this method following a Thread's construction, the thread will inherit its context classloader from its parent Thread. If you don't do anything at all in the entire application, all Threads will end up with the system classloader as their context classloader. It is important to understand that nowadays this is rarely the case since Web and Java 2 Platform, Enterprise Edition (J2EE) application servers utilize sophisticated classloader hierarchies for features like Java Naming and Directory Interface (JNDI), thread pooling, component hot redeployment, and so on.

Why do thread context classloaders exist in the first place? They were introduced in J2SE without much fanfare. A certain lack of proper guidance and documentation from Sun Microsystems likely explains why many developers find them confusing.

In truth, context classloaders provide a back door around the classloading delegation scheme also introduced in J2SE. Normally, all classloaders in a JVM are organized in a hierarchy such that every classloader (except for the primordial classloader that bootstraps the entire JVM) has a single parent. When asked to load a class, every compliant classloader is expected to delegate loading to its parent first and attempt to define the class only if the parent fails.

Sometimes this orderly arrangement does not work, usually when some JVM core code must dynamically load resources provided by application developers. Take JNDI for instance: its guts are implemented by bootstrap classes in rt.jar (starting with J2SE 1.3), but these core JNDI classes may load JNDI providers implemented by independent vendors and potentially deployed in the application's -classpath. This scenario calls for a parent classloader (the primordial one in this case) to load a class visible to one of its child classloaders (the system one, for example). Normal J2SE delegation does not work, and the workaround is to make the core JNDI classes use thread context loaders, thus effectively "tunneling" through the classloader hierarchy in the direction opposite to the proper delegation.

By the way, the previous paragraph may have reminded you of something else: Java API for XML Parsing (JAXP). Yes, when JAXP was just a J2SE extension, the XML parser factories used the current classloader approach for bootstrapping parser implementations. When JAXP was made part of the J2SE 1.4 core, the classloading changed to use thread context classloaders, in complete analogy with JNDI (and confusing many programmers along the way). See what I mean by lack of guidance from Sun?

After this introduction, I have come to the crux of the matter: neither of the remaining two choices is the right one under all circumstances. Some believe that thread context classloaders should become the new standard strategy. This, however, creates a very messy classloading picture if various JVM threads communicate via shared data, unless all of them use the same context loader instance. Furthermore, delegating to the current classloader is already a legacy rule in some existing situations like class literals or explicit calls to Class.forName() (which is why, by the way, I recommend (again, see "Get a Load of That Name!") avoiding the one-argument version of this method). Even if you make an explicit effort to use only context loaders whenever you can, there will always be some code not under your control that delegates to the current loader. This uncontrolled mixing of delegation strategies sounds rather dangerous.

To make matters worse, certain application servers set context and current classloaders to different ClassLoader instances that have thesame classpaths and yet are not related as a delegation parent and child. Take a second to think about why this is particularly horrendous. Remember that the classloader that loads and defines a class is part of the internal JVM's ID for that class. If the current classloader loads a class X that subsequently executes, say, a JNDI lookup for some data of type Y, the context loader could load and define Y. This Y definition will differ from the one by the same name but seen by the current loader. Enter obscure class cast and loader constraint violation exceptions.

This confusion will probably stay with Java for some time. Take any J2SE API with dynamic resource loading of any kind and try to guess which loading strategy it uses. Here is a sampling:

  • JNDI uses context classloaders
  • Class.getResource() and Class.forName() use the current classloader
  • JAXP uses context classloaders (as of J2SE 1.4)
  • java.util.ResourceBundle uses the caller's current classloader
  • URL protocol handlers specified via java.protocol.handler.pkgs system property are looked up in the bootstrap and system classloaders only
  • Java Serialization API uses the caller's current classloader by default

Those class and resource loading strategies must be the most poorly documented and least specified area of J2SE.

What is a Java programmer to do?

If your implementation is confined to a certain framework with articulated resource loading rules, stick to them. Hopefully, the burden of making them work will be on whoever has to implement the framework (such as an application server vendor, although they don't always get it right either). For example, always useClass.getResource() in a Web application or an Enterprise JavaBean.

In other situations, you might consider using a solution I have found useful in personal work. The following class serves as a global decision point for acquiring the best classloader to use at any given time in the application (all classes shown in this article are available with the download):

Java代码
  1. public abstract class ClassLoaderResolver  
  2. {  
  3.     /** 
  4.      * This method selects the best classloader instance to be used for 
  5.      * class/resource loading by whoever calls this method. The decision 
  6.      * typically involves choosing between the caller's current, thread context, 
  7.      * system, and other classloaders in the JVM and is made by the {@link IClassLoadStrategy} 
  8.      * instance established by the last call to {@link #setStrategy}. 
  9.      *  
  10.      * @return classloader to be used by the caller ['null' indicates the 
  11.      * primordial loader]    
  12.      */  
  13.     public static synchronized ClassLoader getClassLoader ()  
  14.     {  
  15.         final Class caller = getCallerClass (0);  
  16.         final ClassLoadContext ctx = new ClassLoadContext (caller);  
  17.           
  18.         return s_strategy.getClassLoader (ctx);   
  19.     }  
  20.     public static synchronized IClassLoadStrategy getStrategy ()  
  21.     {  
  22.         return s_strategy;  
  23.     }  
  24.     public static synchronized IClassLoadStrategy setStrategy (final IClassLoadStrategy strategy)  
  25.     {  
  26.         final IClassLoadStrategy old = s_strategy;  
  27.         s_strategy = strategy;  
  28.           
  29.         return old;  
  30.     }  
  31.           
  32.     /** 
  33.      * A helper class to get the call context. It subclasses SecurityManager 
  34.      * to make getClassContext() accessible. An instance of CallerResolver 
  35.      * only needs to be created, not installed as an actual security 
  36.      * manager. 
  37.      */  
  38.     private static final class CallerResolver extends SecurityManager  
  39.     {  
  40.         protected Class [] getClassContext ()  
  41.         {  
  42.             return super.getClassContext ();  
  43.         }  
  44.           
  45.     } // End of nested class   
  46.       
  47.       
  48.     /* 
  49.      * Indexes into the current method call context with a given 
  50.      * offset. 
  51.      */  
  52.     private static Class getCallerClass (final int callerOffset)  
  53.     {          
  54.         return CALLER_RESOLVER.getClassContext () [CALL_CONTEXT_OFFSET +  
  55.             callerOffset];  
  56.     }  
  57.       
  58.     private static IClassLoadStrategy s_strategy; // initialized in <clinit>  
  59.       
  60.     private static final int CALL_CONTEXT_OFFSET = 3// may need to change if this class is redesigned  
  61.     private static final CallerResolver CALLER_RESOLVER; // set in <clinit>  
  62.       
  63.     static  
  64.     {  
  65.         try  
  66.         {  
  67.             // This can fail if the current SecurityManager does not allow  
  68.             // RuntimePermission ("createSecurityManager"):  
  69.               
  70.             CALLER_RESOLVER = new CallerResolver ();  
  71.         }  
  72.         catch (SecurityException se)  
  73.         {  
  74.             throw new RuntimeException ("ClassLoaderResolver: could not create CallerResolver: " + se);  
  75.         }  
  76.           
  77.         s_strategy = new DefaultClassLoadStrategy ();  
  78.     }  
  79. // End of class.  

You acquire a classloader reference by calling the ClassLoaderResolver.getClassLoader() static method and use the result to load classes and resources via the normal java.lang.ClassLoader API. Alternatively, you can use this ResourceLoader API as a drop-in replacement for java.lang.ClassLoader:

Java代码
  1. public abstract class ResourceLoader  
  2. {  
  3.     /** 
  4.      * @see java.lang.ClassLoader#loadClass(java.lang.String) 
  5.      */  
  6.     public static Class loadClass (final String name)  
  7.         throws ClassNotFoundException  
  8.     {  
  9.         final ClassLoader loader = ClassLoaderResolver.getClassLoader (1);  
  10.           
  11.         return Class.forName (name, false, loader);  
  12.     }  
  13.     /** 
  14.      * @see java.lang.ClassLoader#getResource(java.lang.String) 
  15.      */      
  16.     public static URL getResource (final String name)  
  17.     {  
  18.         final ClassLoader loader = ClassLoaderResolver.getClassLoader (1);  
  19.           
  20.         if (loader != null)  
  21.             return loader.getResource (name);  
  22.         else  
  23.             return ClassLoader.getSystemResource (name);  
  24.     }  
  25.     ... more methods ...  
  26. // End of class  

The decision of what constitutes the best classloader to use is factored out into a pluggable component implementing theIClassLoadStrategy interface:

Java代码
  1. public interface IClassLoadStrategy  
  2. {  
  3.     ClassLoader getClassLoader (ClassLoadContext ctx);  
  4. // End of interface  

To help IClassLoadStrategy make its decision, it is given a ClassLoadContext object:

Java代码
  1. public class ClassLoadContext  
  2. {  
  3.     public final Class getCallerClass ()  
  4.     {  
  5.         return m_caller;  
  6.     }  
  7.       
  8.     ClassLoadContext (final Class caller)  
  9.     {  
  10.         m_caller = caller;  
  11.     }  
  12.       
  13.     private final Class m_caller;  
  14. // End of class  

ClassLoadContext.getCallerClass() returns the class whose code calls into ClassLoaderResolver or ResourceLoader. This is so that the strategy implementation can figure out the caller's classloader (the context loader is always available asThread.currentThread().getContextClassLoader()). Note that the caller is determined statically; thus, my API does not require existing business methods to be augmented with extra Class parameters and is suitable for static methods and initializers as well. You can augment this context object with other attributes that make sense in your deployment situation.

All of this should look like a familiar Strategy design pattern to you. The idea is that decisions like "always context loader" or "always current loader" get separated from the rest of your implementation logic. It is hard to know ahead of time which strategy will be the right one, and with this design, you can always change the decision later.

I have a default strategy implementation that should work correctly in 95 percent of real-life situations:

Java代码
  1. public class DefaultClassLoadStrategy implements IClassLoadStrategy  
  2. {  
  3.     public ClassLoader getClassLoader (final ClassLoadContext ctx)  
  4.     {  
  5.         final ClassLoader callerLoader = ctx.getCallerClass ().getClassLoader ();  
  6.         final ClassLoader contextLoader = Thread.currentThread ().getContextClassLoader ();  
  7.           
  8.         ClassLoader result;  
  9.           
  10.         // If 'callerLoader' and 'contextLoader' are in a parent-child  
  11.         // relationship, always choose the child:  
  12.           
  13.         if (isChild (contextLoader, callerLoader))  
  14.             result = callerLoader;  
  15.         else if (isChild (callerLoader, contextLoader))  
  16.             result = contextLoader;  
  17.         else  
  18.         {  
  19.             // This else branch could be merged into the previous one,  
  20.             // but I show it here to emphasize the ambiguous case:  
  21.             result = contextLoader;  
  22.         }  
  23.           
  24.         final ClassLoader systemLoader = ClassLoader.getSystemClassLoader ();  
  25.           
  26.         // Precaution for when deployed as a bootstrap or extension class:  
  27.         if (isChild (result, systemLoader))  
  28.             result = systemLoader;  
  29.           
  30.         return result;  
  31.     }  
  32.       
  33.     ... more methods ...  
  34. // End of class  

The logic above should be easy to follow. If the caller's current and context classloaders are in a parent-child relationship, I always choose the child. The set of resources visible to a child loader is normally a superset of classes visible to its parent, so this feels like the right decision as long as everybody plays by J2SE delegation rules.

It is when the current and the context classloaders are siblings that the right decision is impossible. Ideally, no Java runtime should ever create this ambiguity. When it happens, my code chooses the context loader: a decision based on personal experience of when things work correctly most of the time. Feel free to change that code branch to suit your taste. It is possible that the context loader is a better choice for framework components, and the current loader is better for business logic.

Finally, a simple check ensures that the selected classloader is not a parent of the system classloader. This is a good thing to do if you are developing code that might be deployed as an extension library.

Note that I intentionally do not look at the name of resources or classes that will be loaded. If nothing else, the experience with Java XML APIs becoming part of the J2SE core should have taught you that filtering by class names is a bad idea. Nor do I trial load classes to see which classloader succeeds first. Examining classloader parent-child relationships is a fundamentally better and more predictable approach.

Although Java resource loading remains an esoteric topic, J2SE relies on various load strategies more and more with every major platform upgrade. Java will be in serious trouble if this area is not given some significantly better design considerations. Whether you agree or not, I would appreciate your feedback and any interesting pointers from your personal design experience.

 

About the author

Vladimir Roubtsov has programmed in a variety of languages for more than 13 years, including Java since 1995. Currently, he develops enterprise software as a senior engineer for Trilogy in Austin, Texas.

分享到:
评论

相关推荐

    Understanding the Java ClassLoader

    - **加载类**:编译完成后,使用自定义ClassLoader的findClass方法加载编译后的类。 **3. 示例代码框架** ```java public class AutoCompileClassLoader extends ClassLoader { // 定义加载类的方法 @Override ...

    自定义classloader的使用

    创建自定义Classloader需要继承java.lang.ClassLoader类,并重写其关键方法,如`findClass(String name)`或`loadClass(String name)`。这两个方法分别用于查找指定类的字节码和实际加载类。在`findClass`中,我们...

    ClassLoader运行机制 自己写的

    1. 如果WebApp ClassLoader的缓存中没有类A,则会查找System ClassPath,未找到A。 2. 接下来查找Application Class Path,如果在其中找到了A(如在wsdl4j.jar中),则加载该类。 3. 如果Application Class Path也...

    ClassLoader

    ### Java虚拟机中ClassLoader概述与双亲委托机制详解 #### 一、ClassLoader概念与作用 在Java编程语言中,`ClassLoader`是一个非常重要的组件,它负责加载程序运行所需的类文件到Java虚拟机(JVM)中。`ClassLoader`...

    ClassLoader小例子

    - 自定义ClassLoader通常需要重写`loadClass()`方法,该方法在找不到类时调用`findClass()`进行实际的加载操作。 - 在`ClassLoaderDemo`这个例子中,可能就展示了如何创建一个自定义的ClassLoader,从非标准位置...

    classloader

    Java ClassLoader是Java运行时系统的关键但经常被忽视的组件,负责在运行时查找和加载类文件。通过创建自定义ClassLoader,你可以定制JVM,使类文件的引入方式完全重新定义,这提供了很多实用和有趣的可能。这篇教程...

    ClassLoader 案例

    在自定义ClassLoader时,主要需要覆写两个关键方法:`findClass()` 和 `loadClass()`。`loadClass()` 方法通常用于委托父类加载器加载类,如果父类加载器无法加载,再由当前类加载器尝试加载。`findClass()` 方法则...

    Java ClassLoader定制实例

    在Java编程语言中,ClassLoader是一个至关重要的组成部分,它负责加载类到JVM(Java虚拟机)中。理解ClassLoader的工作原理以及如何定制它,对于深入学习Java的运行机制和进行高级应用开发具有重要意义。本篇文章将...

    ClassLoader 详解.doc

    自定义ClassLoader通常需要重写findClass()或loadClass()方法,以控制类的加载行为。 理解ClassLoader的工作原理对于排查类冲突、处理依赖关系以及优化大型J2EE应用的性能具有重要意义。开发者可以通过日志输出、...

    Understanding the Java ClassLoader.pdf

    本文档提到的“编译时类加载器”(The Compiling ClassLoader)实际上是指在加载类的同时执行编译操作的类加载器。这种类型的类加载器可以实现在运行时自动编译源代码,并将生成的字节码加载到JVM中。这种能力对于...

    理解Java ClassLoader机制

    自定义ClassLoader需要继承`java.lang.ClassLoader`类,并重写`findClass()`或`loadClass()`方法。通过这两个方法,你可以控制类的加载来源和方式。 在实际开发中,理解ClassLoader机制可以帮助解决一些问题,例如...

    深入java虚拟机(inside the java virtual machine)

    java虚拟机的运行机理的详细介绍 Inside the Java Virtual Machine Bill Venners $39.95 0-07-913248-0 Inside the Java Virtual ... Slices of Pi: A Simulation of the Java Virtual Machine Index About the Author

    深入理解ClassLoader工作机制.docx

    《深入理解ClassLoader工作机制》 Java虚拟机(JVM)中的ClassLoader是负责加载类到内存中的核心组件。它不仅承担着将字节码转换为可执行对象的重任,还参与了类生命周期的各个阶段,包括加载、验证、准备、解析、...

    JVM ClassLoader简析

    首先,ClassLoader可以分为三种基本类型:Bootstrap ClassLoader、Extension ClassLoader和Application ClassLoader。Bootstrap ClassLoader是JVM启动时的第一个ClassLoader,负责加载JDK的`&lt;JAVA_HOME&gt;\lib`目录下...

    java ClassLoader机制及其在OSGi中的应用

    Java ClassLoader机制是Java虚拟机(JVM)中一个至关重要的组成部分,它的主要任务是将类的.class文件加载到JVM中,使得程序能够运行。ClassLoader不仅负责类的加载,还涉及类的验证、初始化等一系列过程。理解...

    ClassLoader类加载机制和原理详解

    在Java编程语言中,ClassLoader是核心组件之一,它负责加载类到JVM(Java虚拟机)中执行。本文将深入探讨ClassLoader的工作原理和类加载机制,帮助开发者理解这个至关重要的概念。 1. 类加载机制概述 Java的类加载...

Global site tag (gtag.js) - Google Analytics