此内容没有您所选择的语言版本。
Chapter 9. The ClassLoading Layer
JBoss has always had a unique way of dealing with classloading, and the new classloading layer that comes with the Microcontainer is no exception ClassLoading is an optional add-on that you can use when you want non-default classloading. With the rising demand for OSGi-style classloading, and a number of new Java classloading specifications on the horizon, the changes to the ClassLoading layer of EAP 5.1 are useful and timely.
The Microcontainer ClassLoading layer is an abstraction layer. Most of the details are hidden behind private and package-private methods, without compromising the extensibility and functionality available through public classes and methods that make the API. This means that you code against policy and not against classloader details.
The ClassLoader project is split into 3 sub-projects
- classloader
- classloading
- classloading-vfs
classloader
contains a custom java.lang.ClassLoader
extension without any specific classloading policy. A classloading policy includes knowledge of where to load from and how to load.
Classloading
is an extension of Microcontainer’s dependency mechanisms. Its VFS-backed implementation is classloading-vfs
. See Chapter 8, The Virtual File System for more information on VFS.
9.1. ClassLoader 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The
ClassLoader
implementation supports pluggable policies and is itself a final class, not meant to be altered. To write your own ClassLoader implementations, write a ClassLoaderPolicy
which provides a simpler API for locating classes and resources, and for specifying other rules associated with the classloader.
To customize classloading, instantiate a
ClassLoaderPolicy
and register it with a ClassLoaderSystem
to create a custom ClassLoader
. You can also create a ClassLoaderDomain
to partition the ClassLoaderSystem
.
The
ClassLoader
layer also includes the implementation of things like DelegateLoader model, classloading, resource filters, and parent-child delegation policies.
The run-time is JMX enabled to expose the policy used for each classloader. It also provides classloading statistics and debugging methods to help determine where things are loaded from.
Example 9.1. ClassLoaderPolicy
Class
The
ClassLoaderPolicy
controls the way your classloading works.
The following two examples of
ClassLoaderPolicy
. The first one retrieves resources based on regular expressions, while the second one handles encrypted resources.
Example 9.2. ClassLoaderPolicy with Regular Expression Support
RegexpClassLoaderPolicy
uses a simplistic mechanism to find matching resources. Real-world implementations would be more comprehensive and elegant.
The regexp service uses the regular expression pattern
config/[^.]+\\.[^.]{1,4}
to list resources under the config/
/ directory. The suffix length is limited, such that file names such as excluded.properties will be ignored.
Example 9.3. ClassLoaderPolicy with Encryption Support
Example 9.3, “ClassLoaderPolicy with Encryption Support” shows how to encrypt JARs. You can configure which resources to encrypt by specifying a proper filter. Here, everything is encrypted except for the contents of the
META-INF/
directory.
This service prints out the contents of two configuration files. It shows that decryption of any encrypted resources is hidden behind the classloading layer.
To properly test this, either encrypt the policy module yourself or use an existing encrypted one. To put this into action, you need to properly tie EncryptedService to
ClassLoaderSystem
and deployers.
Partitioning
ClassLoaderSystem
is discussed later in this chapter.