Classes are shared by the bootstrap class loader internally
in the JVM, but any other Java™ class
loader must use the Java Helper
API to find and store classes in the shared class cache.
The Helper API provides a set of flexible Java interfaces that enable Java class loaders to exploit the shared classes
features in the JVM. The java.net.URLClassLoader shipped
with the SDK has been modified to use a SharedClassURLClasspathHelper and
any class loaders that extend java.net.URLClassLoader inherit
this behavior. Custom class loaders that do not extend URLClassLoader but
want to share classes must use the Java Helper
API. This section contains a summary on the different types of Helper
API available and how to use them.
The Helper API classes are contained in the com.ibm.oti.shared package
and Javadoc information for these classes is shipped with the SDK
(some of which is reproduced here).
- com.ibm.oti.shared.Shared
- The Shared class contains static utility methods: getSharedClassHelperFactory() and isSharingEnabled().
If -Xshareclasses is specified on the command
line and sharing has been successfully initialized, isSharingEnabled() returns
true. If sharing is enabled, getSharedClassHelperFactory() will
return a com.ibm.oti.shared.SharedClassHelperFactory.
The helper factories are singleton factories that manage the Helper
APIs. To use the Helper APIs, you must get a Factory.
- com.ibm.oti.shared.SharedClassHelperFactory
- SharedClassHelperFactory provides an interface
used to create various types of SharedClassHelper for
class loaders. Class loaders and SharedClassHelpers have
a one-to-one relationship. Any attempts to get a helper for a class
loader that already has a different type of helper causes a HelperAlreadyDefinedException.
Because class loaders and SharedClassHelpers have
a one-to-one relationship, calling findHelperForClassLoader() returns
a Helper for a given class loader if one exists.
- com.ibm.oti.shared.SharedClassHelper
- There are three different types of SharedClassHelper:
- SharedClassTokenHelper. Use this Helper to
store and find classes using a String token generated by the class
loader. Typically used by class loaders that require complete control
over cache contents.
- SharedClassURLHelper. Store and find classes
using a file system location represented as a URL. For use by class
loaders that do not have the concept of a classpath, that load classes
from multiple locations.
- SharedClassURLClasspathHelper. Store and find
classes using a classpath of URLs. For use by class loaders that load
classes using a URL class path
Compatibility between Helpers is as follows: Classes stored by SharedClassURLHelper can
be found using a SharedClassURLClasspathHelper and
the opposite also applies. However, classes stored using a SharedClassTokenHelper can
be found only by using a SharedClassTokenHelper.
Note also that classes stored using the URL Helpers are updated
dynamically by the cache (see Understanding dynamic updates) but classes stored
by the SharedClassTokenHelper are not updated by
the cache because the Tokens are meaningless Strings, so it has no
way of obtaining version information.
You
can control the classes a URL Helper will find and store in the cache
using a SharedClassURLFilter. An object implementing
this interface can be passed to the SharedClassURLHelper when
it is constructed or after it has been created. The filter is then
used to decide which classes to find and store in the cache. See SharedClassHelper API for more information.
For
a detailed description of each helper and how to use it, see the Javadoc
information shipped with the SDK.
- com.ibm.oti.shared.SharedClassStatistics
- The SharedClassStatistics class provides static
utilities that return the total cache size and the amount of free
bytes in the cache.