org.aspectj.weaver.tools.cache
Class WeavedClassCache

java.lang.Object
  extended by org.aspectj.weaver.tools.cache.WeavedClassCache

public class WeavedClassCache
extends java.lang.Object

Manages a cache of weaved and generated classes similar to Eclipse Equinox, except designed to operate across multiple restarts of the JVM and with one cache per classloader; allowing URLClassLoaders with the same set of URI paths to share the same cache (with the default configuration).

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:

    1. New aspects are added dynamically somewhere in the classloader hierarchy; affecting
       other classes elsewhere.
    2. Use of declare parent in aspects to change the type hierarchy; if the cache
       has not invalidated the right classes in the type hierarchy aspectj may not
       be reconstruct the class incorrectly.
    3. Similarly to (2), the addition of fields or methods on classes which have
       already been weaved and cached could have inter-type conflicts.
 


Field Summary
static java.lang.String CACHE_IMPL
           
static java.lang.String WEAVED_CLASS_CACHE_ENABLED
           
static byte[] ZERO_BYTES
           
 
Constructor Summary
protected WeavedClassCache(GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler, java.lang.String name, CacheBacking backing, CacheKeyResolver resolver)
           
 
Method Summary
 void clear()
          Clear the entire cache
static WeavedClassCache createCache(java.lang.ClassLoader loader, java.util.List<java.lang.String> aspects, GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler)
          Creates a new cache using the resolver and backing returned by the DefaultCacheFactory.
 CachedClassReference createCacheKey(java.lang.String className, byte[] originalBytes)
          Create a key for a normal weaved class
 CachedClassReference createGeneratedCacheKey(java.lang.String className)
          Created a key for a generated class
protected  void error(java.lang.String message)
           
protected  void error(java.lang.String message, java.lang.Throwable th)
           
 CachedClassEntry get(CachedClassReference ref, byte[] classBytes)
          Get a cache value
static java.util.List<WeavedClassCache> getCaches()
          Return a list of all WeavedClassCaches which have been initialized
 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
 java.lang.String getName()
           
 CacheStatistics getStats()
          Get the statistics associated with this cache, or null if statistics have not been enabled.
 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.
protected  void info(java.lang.String message)
           
static boolean isEnabled()
          Has caching been enabled through the System property, WEAVED_CLASS_CACHE_ENABLED
 void put(CachedClassReference ref, byte[] classBytes, byte[] weavedBytes)
          Put a class in the cache
 void remove(CachedClassReference ref)
          Invalidate a cache entry
static void setDefaultCacheFactory(CacheFactory factory)
          The Cache and be extended in two ways, through a specialized CacheKeyResolver and a specialized CacheBacking.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WEAVED_CLASS_CACHE_ENABLED

public static final java.lang.String WEAVED_CLASS_CACHE_ENABLED
See Also:
Constant Field Values

CACHE_IMPL

public static final java.lang.String CACHE_IMPL
See Also:
Constant Field Values

ZERO_BYTES

public static final byte[] ZERO_BYTES
Constructor Detail

WeavedClassCache

protected WeavedClassCache(GeneratedClassHandler existingClassHandler,
                           IMessageHandler messageHandler,
                           java.lang.String name,
                           CacheBacking backing,
                           CacheKeyResolver resolver)
Method Detail

createCache

public static WeavedClassCache createCache(java.lang.ClassLoader loader,
                                           java.util.List<java.lang.String> aspects,
                                           GeneratedClassHandler existingClassHandler,
                                           IMessageHandler messageHandler)
Creates a new cache using the resolver and backing returned by the DefaultCacheFactory.

Parameters:
loader - classloader for this cache
aspects - list of aspects used by the WeavingAdapter
existingClassHandler - the existing GeneratedClassHandler used by the weaver
messageHandler - the existing messageHandler used by the weaver
Returns:

getName

public java.lang.String getName()

setDefaultCacheFactory

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.

Parameters:
factory -

createGeneratedCacheKey

public CachedClassReference createGeneratedCacheKey(java.lang.String className)
Created a key for a generated class

Parameters:
className - ClassName, e.g. "com.foo.Bar"
Returns:
the cache key, or null if no caching should be performed

createCacheKey

public CachedClassReference createCacheKey(java.lang.String className,
                                           byte[] originalBytes)
Create a key for a normal weaved class

Parameters:
className - ClassName, e.g. "com.foo.Bar"
originalBytes - Original byte array of the class
Returns:
a cache key, or null if no caching should be performed

getCachingClassHandler

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


isEnabled

public static boolean isEnabled()
Has caching been enabled through the System property, WEAVED_CLASS_CACHE_ENABLED

Returns:
true if caching is enabled

put

public void put(CachedClassReference ref,
                byte[] classBytes,
                byte[] weavedBytes)
Put a class in the cache

Parameters:
ref - reference to the entry, as created through createCacheKey
classBytes - pre-weaving class bytes
weavedBytes - bytes to cache

get

public CachedClassEntry get(CachedClassReference ref,
                            byte[] classBytes)
Get a cache value

Parameters:
ref - reference to the cache entry, created through createCacheKey
classBytes - Pre-weaving class bytes - required to ensure that cached aspects refer to an unchanged original class
Returns:
the CacheEntry, or null if no entry exists in the cache

ignore

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.

Parameters:
ref - The cache reference
classBytes - The un-weaved class bytes

remove

public void remove(CachedClassReference ref)
Invalidate a cache entry

Parameters:
ref -

clear

public void clear()
Clear the entire cache


getStats

public CacheStatistics getStats()
Get the statistics associated with this cache, or null if statistics have not been enabled.

Returns:

getCaches

public static java.util.List<WeavedClassCache> getCaches()
Return a list of all WeavedClassCaches which have been initialized

Returns:

error

protected void error(java.lang.String message,
                     java.lang.Throwable th)

error

protected void error(java.lang.String message)

info

protected void info(java.lang.String message)