janino.net

org.codehaus.janino
Class CachingJavaSourceClassLoader

  extended by 
      extended by org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
          extended by org.codehaus.janino.JavaSourceClassLoader
              extended by org.codehaus.janino.CachingJavaSourceClassLoader

public class CachingJavaSourceClassLoader
extends JavaSourceClassLoader

A JavaSourceClassLoader that uses a resource storage provided by the application to cache compiled classes and thus saving unnecessary recompilations.

The application provides access to the resource storeage through a pair of a ResourceFinder and a ResourceCreator (see CachingJavaSourceClassLoader(ClassLoader, ResourceFinder, String, ResourceFinder, ResourceCreator).

See AbstractJavaSourceClassLoader.main(String[]) for an example how to use this class.

Notice: You must NOT rely on that this class stores some particular data in some particular resources through the given classFileCacheResourceFinder/Creator! These serve only as a means for the CachingJavaSourceClassLoader to persistently cache some data between invocations. In other words: If you want to compile .java files into .class files, then don't use this class but Compiler instead!


Nested Class Summary
 
Nested classes/interfaces inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
AbstractJavaSourceClassLoader.ProtectionDomainFactory
 
Field Summary
 
Fields inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
optionalProtectionDomainFactory
 
Constructor Summary
CachingJavaSourceClassLoader( parentClassLoader, [] optionalSourcePath,  optionalCharacterEncoding,  cacheDirectory)
          See CachingJavaSourceClassLoader(ClassLoader, ResourceFinder, String, ResourceFinder, ResourceCreator).
CachingJavaSourceClassLoader( parentClassLoader, ResourceFinder sourceFinder,  optionalCharacterEncoding, ResourceFinder classFileCacheResourceFinder, ResourceCreator classFileCacheResourceCreator)
          Notice that this class is thread-safe if and only if the classFileCacheResourceCreator stores its data atomically, i.e. the classFileCacheResourceFinder sees the resource written by the classFileCacheResourceCreator only after the is closed.
 
Method Summary
protected  <,byte[]> generateBytecodes( className)
          Override JavaSourceClassLoader.generateBytecodes(String) to implement class file caching.
 
Methods inherited from class org.codehaus.janino.JavaSourceClassLoader
findClass, setCompileErrorHandler, setDebuggingInfo, setSourceFileCharacterEncoding, setSourcePath, setWarningHandler
 
Methods inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
main, setProtectionDomainFactory
 
Methods inherited from class java.lang.
, , , , , , , , , , , , , , , , , , , , , , , , , , ,
 
Methods inherited from class java.lang.
, , , , , , , , , ,
 

Constructor Detail

CachingJavaSourceClassLoader

public CachingJavaSourceClassLoader( parentClassLoader,
                                    [] optionalSourcePath,
                                     optionalCharacterEncoding,
                                     cacheDirectory)
See CachingJavaSourceClassLoader(ClassLoader, ResourceFinder, String, ResourceFinder, ResourceCreator).

Parameters:
optionalSourcePath - Directories to scan for source files
cacheDirectory - Directory to use for caching generated class files (see class description)

CachingJavaSourceClassLoader

public CachingJavaSourceClassLoader( parentClassLoader,
                                    ResourceFinder sourceFinder,
                                     optionalCharacterEncoding,
                                    ResourceFinder classFileCacheResourceFinder,
                                    ResourceCreator classFileCacheResourceCreator)
Notice that this class is thread-safe if and only if the classFileCacheResourceCreator stores its data atomically, i.e. the classFileCacheResourceFinder sees the resource written by the classFileCacheResourceCreator only after the is closed.

In order to make the caching scheme work, both the classFileCacheResourceFinder and the sourceFinder must support the Resource.lastModified() method, so that the modification time of the source and the class files can be compared.

Parameters:
parentClassLoader - Attempt to load classes through this one before looking for source files
sourceFinder - Finds Java™ source for class pkg.Cls in resource pkg/Cls.java
optionalCharacterEncoding - Encoding of Java™ source or null for platform default encoding
classFileCacheResourceFinder - Finds precompiled class pkg.Cls in resource pkg/Cls.class (see class description)
classFileCacheResourceCreator - Stores compiled class pkg.Cls in resource pkg/Cls.class (see class description)
Method Detail

generateBytecodes

protected <,byte[]> generateBytecodes( className)
                                        throws 
Override JavaSourceClassLoader.generateBytecodes(String) to implement class file caching.

Overrides:
generateBytecodes in class JavaSourceClassLoader
Returns:
String name => byte[] bytecode, or null if no source code could be found
Throws:
- Compilation problems or class file cache I/O problems

janino.net