To enable the default configuration two system properties must be set:
"-Daj.weaving.cache.enabled=true" "-Daj.weaving.cache.dir=/some/directory"
The class cache is often something that application developers or containers would like to manage, so there are a few interfaces for overriding the default behavior and performing other management functions.
CacheBacking
Provides an interface for implementing a custom backing store
for the cache; The default implementation in DefaultFileCacheBacking
provides a naive file-backed cache. An alternate implementation may ignore
caching until signaled explicitly by the application, or only cache files
for a specific duration. This class delegates the locking and synchronization
requirements to the CacheBacking implementation.
CacheKeyResolver
Provides methods for creating keys from classes to be cached and for
creating the "scope" of the cache itself for a given classloader and aspect
list. The default implementation is provided by DefaultCacheKeyResolver
but an alternate implementation may want to associate a cache with a particular
application running underneath a container.
This naive cache does not normally invalidate *any* classes; the interfaces above must be used to implement more intelligent behavior. Cache invalidation problems may occur in at least three scenarios:
| Modifier and Type | Field and Description |
|---|---|
| private final CacheBacking | |
| public static final String | |
| private static final List | |
| private final GeneratedCachedClassHandler | |
| private static CacheFactory | |
| private final IMessageHandler | |
| private final String | |
| private final CacheKeyResolver | |
| private final CacheStatistics | |
| public static final String | |
| public static final byte[] |
| Access | Constructor and Description |
|---|---|
| protected | WeavedClassCache(GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler, String name, CacheBacking backing, CacheKeyResolver resolver)
|
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public static WeavedClassCache | createCache(ClassLoader
classloader for this cache loader, List<String> list of aspects used by the WeavingAdapter aspects, GeneratedClassHandler the existing GeneratedClassHandler used by the weaver existingClassHandler, IMessageHandler the existing messageHandler used by the weaver messageHandler)Creates a new cache using the resolver and backing returned by the DefaultCacheFactory. |
| public CachedClassReference | Returns: a cache key, or null if no caching should be performedClassName, e.g. "com.foo.Bar" className, byte[] Original byte array of the class originalBytes)Create a key for a normal weaved class |
| public CachedClassReference | Returns: the cache key, or null if no caching should be performedClassName, e.g. "com.foo.Bar" className)Created a key for a generated class |
| protected void | |
| protected void | |
| public CachedClassEntry | Returns: the CacheEntry, or null if no entry exists in the cachereference to the cache entry, created through createCacheKey ref, byte[] Pre-weaving class bytes - required to ensure that
cached aspects refer to an unchanged original class classBytes)Get a cache value |
| public static List | |
| public GeneratedClassHandler | getCachingClassHandler()
Returns a generated class handler which wraps the handler this cache was initialized with; this handler should be used to make sure that generated classes are added to the cache |
| public String | |
| public CacheStatistics | getStats()
Get the statistics associated with this cache, or null if statistics have not been enabled. |
| public void | ignore(CachedClassReference
The cache reference ref, byte[] The un-weaved class bytes classBytes)Put a cache entry to indicate that the class should not be weaved; the original bytes of the class should be used. |
| protected void | |
| public static boolean | Returns: true if caching is enabledHas caching been enabled through the System property, WEAVED_CLASS_CACHE_ENABLED |
| public void | put(CachedClassReference
reference to the entry, as created through createCacheKey ref, byte[] pre-weaving class bytes classBytes, byte[] bytes to cache weavedBytes)Put a class in the cache |
| public void | |
| public static void | setDefaultCacheFactory(CacheFactory factory)
The Cache and be extended in two ways, through a specialized CacheKeyResolver and a specialized CacheBacking. |
| backing | back to summary |
|---|---|
| private final CacheBacking backing | |
| CACHE_IMPL | back to summary |
|---|---|
| public static final String CACHE_IMPL | |
| cacheRegistry | back to summary |
|---|---|
| private static final List<WeavedClassCache> cacheRegistry | |
| cachingClassHandler | back to summary |
|---|---|
| private final GeneratedCachedClassHandler cachingClassHandler | |
| DEFAULT_FACTORY | back to summary |
|---|---|
| private static CacheFactory DEFAULT_FACTORY | |
| messageHandler | back to summary |
|---|---|
| private final IMessageHandler messageHandler | |
| name | back to summary |
|---|---|
| private final String name | |
| resolver | back to summary |
|---|---|
| private final CacheKeyResolver resolver | |
| stats | back to summary |
|---|---|
| private final CacheStatistics stats | |
| WEAVED_CLASS_CACHE_ENABLED | back to summary |
|---|---|
| public static final String WEAVED_CLASS_CACHE_ENABLED | |
| ZERO_BYTES | back to summary |
|---|---|
| public static final byte[] ZERO_BYTES | |
| WeavedClassCache | back to summary |
|---|---|
| protected WeavedClassCache(GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler, String name, CacheBacking backing, CacheKeyResolver resolver) | |
| clear | back to summary |
|---|---|
| public void clear() Clear the entire cache | |
| createCache | back to summary |
|---|---|
| public static WeavedClassCache createCache(ClassLoader loader, List<String> aspects, GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler) Creates a new cache using the resolver and backing returned by the DefaultCacheFactory.
| |
| createCacheKey | back to summary |
|---|---|
| public CachedClassReference createCacheKey(String className, byte[] originalBytes) Create a key for a normal weaved class
| |
| createGeneratedCacheKey | back to summary |
|---|---|
| public CachedClassReference createGeneratedCacheKey(String className) Created a key for a generated class
| |
| error | back to summary |
|---|---|
| protected void error(String message, Throwable th) | |
| error | back to summary |
|---|---|
| protected void error(String message) | |
| get | back to summary |
|---|---|
| public CachedClassEntry get(CachedClassReference ref, byte[] classBytes) Get a cache value
| |
| getCaches | back to summary |
|---|---|
| public static List Return a list of all WeavedClassCaches which have been initialized | |
| getCachingClassHandler | back to summary |
|---|---|
| public GeneratedClassHandler getCachingClassHandler() Returns a generated class handler which wraps the handler this cache was initialized with; this handler should be used to make sure that generated classes are added to the cache | |
| getName | back to summary |
|---|---|
| public String getName() | |
| getStats | back to summary |
|---|---|
| public CacheStatistics getStats() Get the statistics associated with this cache, or null if statistics have not been enabled. | |
| ignore | back to summary |
|---|---|
| public void ignore(CachedClassReference ref, byte[] classBytes) Put a cache entry to indicate that the class should not be weaved; the original bytes of the class should be used.
| |
| info | back to summary |
|---|---|
| protected void info(String message) | |
| isEnabled | back to summary |
|---|---|
| public static boolean isEnabled() Has caching been enabled through the System property, WEAVED_CLASS_CACHE_ENABLED
| |
| put | back to summary |
|---|---|
| public void put(CachedClassReference ref, byte[] classBytes, byte[] weavedBytes) Put a class in the cache
| |
| remove | back to summary |
|---|---|
| public void remove(CachedClassReference ref) Invalidate a cache entry | |
| setDefaultCacheFactory | back to summary |
|---|---|
| public static void setDefaultCacheFactory(CacheFactory factory) The Cache and be extended in two ways, through a specialized CacheKeyResolver and a specialized CacheBacking. The default factory used to create these classes can be set with this method. Since each weaver will create a cache, this method must be called before the weaver is first initialized. | |