Saturday, January 15, 2011

WebLogic's Classloading Framework


Updated (09/12/2014):

The FilteringClassLoader provides a mechanism for you to configure deployment descriptors to explicitly specify that certain packages should always be loaded from the application, rather than being loaded by the system classloader. To understand the default class loader structure in WLS 12.1.3, read here.

System (D)
  |
  FilteringClassLoader (filterList := x.y.*) (C)
   |
  App (B)
   |
  Web (A)

Running any application on a JVM or an application server (e.g., WebLogic Server), the main question a designer faces is:
  • Which class is getting loaded from which source
A classloader is used by the JVM to locate and load Java classes into memory at runtime. Java classloaders define a hierarchy, a tree-like collection of parent and child classloaders.

In this article, we will look at two classloader hierarchies:
  • Java Classloader Hierarchy
    • Applied to regular Java applications running from command line
  • Application Classloader Hierarchy
    • We will look at one such hierarchy implemented by WebLogic's Classloading Framework

Three Principles of Java Classloader Operation

Classloader hierarchy plays an important role when locating and loading classes into memory.  There are only three basic principles to understand:
  1. Delegation Principle
    • If a class is not loaded already, the classloaders delegate the request to load that class to their parent classloaders (see also [11]).
      • In other words, a child classloader loads a class only if its parent fails to load it
      • See customization section for an exception that is supported by WebLogic Server to override this default behavior by setting the prefer-web-inf-classes element to true in the weblogic.xml descriptor file.
  2. Visibility Principle
    • Classes loaded by parent classloaders are visible to child classloaders but not vice versa
    • A classloader cannot access any classes loaded by a sibling classloader.
  3. Uniqueness Principle
    • When a classloader loads a class, the child classloaders in the hierarchy will never reload that class.
The application that triggered the request to load a class receives a ClassNotFoundException or NoClassDefFoundError [7]if neither the classloader nor any of its ancestors can locate the class.

Java Classloader Hierarchy


Regular Java applications running from command line involve three classloaders:
  1. Bootstrap classloader (root)
    • Created by the JVM for loading its internal classes and the java.* packages (i.e., core Java libraries under /lib directory) included within the JVM
    • Written in native code
    • Endorsed-standards override mechanism allows a jar file containing a newer implementation of an endorsed standard or standalone API be installed into a run-time image by placing it in one of the directories named by the system property (i.e., java.endorsed.dirs), or by placing it in the default lib/endorsed directory if the system property is not defined. 
      • Such jar files are prepended to the JVM's bootstrap class path at run time, thereby overriding any definitions stored in the run-time system itself.
  2. Extensions classloader (child of bootstrap classloader)
    • Loads any JARs placed in the extensions directory (/lib/ext or any other directory specified by the java.ext.dirs system property) of the JDK
    • Implemented by the sun.misc.Launcher$ExtClassLoader class
    • Extension classes cannot override the JDK classes loaded by the bootstrap loader but they are loaded in preference to classes defined by the system loader and its descendants.
  3. System classloader (child of extensions classloader)
    • Loads code found on java.class.path, which maps to the system CLASSPATH variable.
    • Implemented by the sun.misc.Launcher$AppClassLoader class
    • Any custom classloader created by an application, including WebLogic's classloaders, are all descendants of this system classpath classloader
    • Can be programmatically accessible as ClassLoader.getSystemClassLoader()
    • Is also known as application class loader
In J2EE, each application is packaged as an Enterprise ARchive (EAR) and each EAR gets its own classloader. The J2EE classloader hierarchy supported by a specific framework could include the above-mentioned classloaders and more[6]. J2EE application servers utilize sophisticated classloader hierarchies for features like Java Naming and Directory Interface (JNDI), thread pooling, component hot redeployment, and so on.  In this article, we will introduce one such Application Classloader Hierarchy implemented by WebLogic's classloading framework.

WebLogic's Classloading Framework

WebLogic's standard classloading framework needs to achieve two main goals:
  1. Maintain application independence
    • Classes used by application A must never come in conflict with any classes used by application B
    • Redeploying application A must have no effect on classes used by application B
  2. Hot-deploy or hot-redeploy
    • Within an application, it must allow you to redeploy web applications without having to redeploy the EJBs
    • It is more common to change JSP files and servlets than to change the EJB tier. With proper design, a separate classloader can be created for each servlet and JSP page. This allows you to reload individual servlets and JSPs easily, without the need for redeploying the web application or affecting any of the EJBs.
WebLogic achieves the first goals by creating a separate classloader hierarchy for each application deployed to the server. The parent of this hierarchy is the system classpath classloader. By creating a separate classloader hierarchy for each application, classloaders associated with one application cannot see the classloaders or classes of another application, and because sibling classloaders are isolated from each other, this also isolates the applications.

Java classloaders do not have any standard mechanism to undeploy or unload a set of classes, nor can they load new versions of classes. To achieve the second goal, each application in WebLogic Server has a hierarchy of classloaders (see the Figure below) that are offspring of the system classloader. These hierarchies allow applications or parts of applications to be individually reloaded without affecting the rest of the system. To find out more details on this, read WebLogic Server Application Classloading.


Customization

Even with good support from either Java classloading framework or WebLogic's application classloading framework, it often comes times that you need to have better control over which modules are reloadable, which classes are visible between modules, etc.

There are multiple solutions to your customization needs:
  1. You can configure a web application classloader so that it doesn't use the default parent delegation scheme by setting the prefer-web-inf-classes element to true in the weblogic.xml descriptor file. See details here.
  2. The FilteringClassLoader provides a mechanism for you to configure deployment descriptors to explicitly specify that certain packages should always be loaded from the application, rather than being loaded by the system classloader. See details here.
  3. You can create custom classloader hierarchies for an application allowing for better control over class visibility and reloadability. You achieve this by defining a classloader-structure element in the weblogic-application.xml deployment descriptor file. See details here.

Wrap-up

More often than not, you want class definitions (which are stable) shared across applications. To facilitate sharing, you would place them at higher level of the classloading hierarchy (for example, getting loaded at system classloader instead of at application classloader).

If it is common to change some modules, a separate classloader can be created for them and place them at the tip of the classloading tree. This allows you to reload individual modules easily, without the need for redeploying their parent applications.

When loading a resource dynamically, you can choose from at least three classloaders: the system classloader, the current classloader, and the current thread context classloader. Which classloader is the right one?  You can read [8] to find the answer.

References

  1. Classloader
  2. JRebel
  3. Understanding WebLogic Server Application Classloading
  4. WebLogic: The Definitive Guide
  5. WebLogic Server 11g for ADF/Forms Developers
  6. Classloaders and J2EE (good) 
  7. 3 ways to resolve NoClassDefFoundError in Java
  8. Find a way out of the ClassLoader maze 
  9. Professional Oracle WebLogic Server by Robert Patrick, Gregory Nyberg, and Philip Aston
  10. VM Class Loading
  11. Using 3rd party JDBC Jar Files
    • The general precedence of jar files in the classpath is the following:
    • Note that the above doesn't consider library-ref or prefer-application-packages (see article for details).
  12. WebLogic class loader - analysis and configuration options  (good)
  13. JEP 220: Modular Run-Time Images

1 comment:

Blogger said...

If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (no matter why you broke up) you gotta watch this video
right away...

(VIDEO) Get your ex CRAWLING back to you...?