org.entityfs.util.lang
Class EntityClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by org.entityfs.util.lang.EntityClassLoader
Direct Known Subclasses:
TaskPackageClassLoader

public class EntityClassLoader
extends ClassLoader

This is a class loader that reads classes from Directory:s and Zip files. It does not support resolving resources to URL:s (there is no way to create generic URL:s that point to EntityFS entities), but it supports getResourceAsStream().

To release all resources when this class loader should not be used anymore, call close on it.

Since:
1.0
Author:
Karl Gustafsson

Constructor Summary
EntityClassLoader(LogAdapterHolder lah)
          Create an entity class loader with an empty class path.
EntityClassLoader(LogAdapterHolder lah, ClassLoader parent)
          Create an entity class loader with an empty class path.
EntityClassLoader(LogAdapterHolder lah, ClassLoader parent, Collection<? extends EntityView> classpath)
          Create an entity class loader with a class path that consists of the entities returned from the collection, in that order.
EntityClassLoader(LogAdapterHolder lah, Collection<? extends EntityView> classpath)
          Create an entity class loader with a class path that consists of the entities returned from the collection, in that order.
 
Method Summary
 EntityClassLoader addToClasspath(EntityView ev)
          Add a directory or a Zip (or Jar) file to the classpath.
 void close()
          Close this class loader and release all its resources.
protected  Class<?> defineClass(String name, EFile f)
          Define the class found in the provided file.
protected  Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase)
           
protected  void finalize()
           
protected  Class<?> findClass(String name)
           
protected  EFile findFileInClasspath(RelativeLocation rel)
          This is used by all methods to find a file in the class path.
 Class<?> getClassOrNull(String name)
          Get the class definition if it is loaded by this class loader.
protected  List<DirectoryView> getClasspath()
          Subclasses call this to get the root directories of this class loader's class path.
protected  LogAdapter getLogAdapter()
          Get the current log adapter, or null if no log adapter holder is set.
protected  Package getPackage(String name)
           
protected  Package[] getPackages()
           
 InputStream getResourceAsStream(String name)
          Return an open InputStream for the contents of a resource in the classpath.
protected  boolean isDebugLoggingEnabled()
          Check if a log adapter is set and it will accept debug logs.
protected  boolean isFinerLoggingEnabled()
          Check if a log adapter is set and it will accept finer logs.
protected  boolean isTraceLoggingEnabled()
          Check if a log adapter is set and it will accept trace logs.
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getParent, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EntityClassLoader

public EntityClassLoader(LogAdapterHolder lah)
Create an entity class loader with an empty class path. The class loader will get the system class loader as its parent.

Parameters:
lah - An adapter for logging. Set to null to disable logging from this class loader.

EntityClassLoader

public EntityClassLoader(LogAdapterHolder lah,
                         Collection<? extends EntityView> classpath)
                  throws UnsupportedEntityTypeException
Create an entity class loader with a class path that consists of the entities returned from the collection, in that order. The class loader will get the system class loader as its parent.

Parameters:
classpath - The collection of initial class path entries.
lah - An adapter for logging. Set to null to disable logging from this class loader.
Throws:
UnsupportedEntityTypeException - If the entity collection contains an entity that is not a directory or a Zip (or Jar) file.

EntityClassLoader

public EntityClassLoader(LogAdapterHolder lah,
                         ClassLoader parent)
Create an entity class loader with an empty class path. The provided class loader will be the class loader's parent.

Parameters:
lah - An adapter for logging. Set to null to disable logging from this class loader.
parent - The parent class loader.

EntityClassLoader

public EntityClassLoader(LogAdapterHolder lah,
                         ClassLoader parent,
                         Collection<? extends EntityView> classpath)
                  throws UnsupportedEntityTypeException
Create an entity class loader with a class path that consists of the entities returned from the collection, in that order. The class loader will get the provided parent class loader as its parent.

Parameters:
lah - An adapter for logging. Set to null to disable logging from this class loader.
parent - The parent class loader.
classpath - The collection of initial class path entries.
Throws:
UnsupportedEntityTypeException - If the entity collection contains an entity that is not a directory or a Zip (or Jar) file.
Method Detail

isDebugLoggingEnabled

protected boolean isDebugLoggingEnabled()
Check if a log adapter is set and it will accept debug logs.

Returns:
true if a log adapter is set and it will accept debug logs.

isFinerLoggingEnabled

protected boolean isFinerLoggingEnabled()
Check if a log adapter is set and it will accept finer logs.

Returns:
true if a log adapter is set and it will accept finer logs.

isTraceLoggingEnabled

protected boolean isTraceLoggingEnabled()
Check if a log adapter is set and it will accept trace logs.

Returns:
true if a log adapter is set and it will accept trace logs.

getLogAdapter

protected LogAdapter getLogAdapter()
Get the current log adapter, or null if no log adapter holder is set.


getClasspath

protected List<DirectoryView> getClasspath()
Subclasses call this to get the root directories of this class loader's class path.

Returns:
The class path.

addToClasspath

public EntityClassLoader addToClasspath(EntityView ev)
                                 throws UnsupportedEntityTypeException
Add a directory or a Zip (or Jar) file to the classpath.

Parameters:
ev - The entity to add to the end of the classpath.
Returns:
This class loader
Throws:
UnsupportedEntityTypeException - If the entity type is not supported.

close

public void close()
Close this class loader and release all its resources. This can safely be called several times.


defineClass

protected Class<?> defineClass(String name,
                               EFile f)
Define the class found in the provided file. Subclasses may override this to define the class in their own way (use a custom ProtectionDomain, for instance).

Parameters:
name - The fully qualified name of the class.
f - The class file.
Returns:
The defined class.

getClassOrNull

public Class<?> getClassOrNull(String name)
Get the class definition if it is loaded by this class loader. If it is not loaded, try to define the class if this class loader itself can find the class definition.

This can be used by other class loaders that want to be able to delegate to several different EntityClassLoader:s.

A typical usage (in that class loader's findClass(String) method) may be: m_entityClassLoaders) Class c = cl.getClassOrNull(name); if (c != null) return c; } throw new ClassNotFoundException(…);

Parameters:
name - The binary name of the class.
Returns:
The class object if it could be loaded by this class loader (not any of its parents), null otherwise.

findFileInClasspath

protected EFile findFileInClasspath(RelativeLocation rel)
This is used by all methods to find a file in the class path.

Parameters:
rel - The location of the file relative to the class path root directory.
Returns:
A file, or null if the file cannot be found.

findClass

protected Class<?> findClass(String name)
                      throws ClassNotFoundException
Overrides:
findClass in class ClassLoader
Throws:
ClassNotFoundException

getResourceAsStream

public InputStream getResourceAsStream(String name)
Return an open InputStream for the contents of a resource in the classpath. The resource is resolved as described in the java.lang.ClassLoader Javadocs.

If the returned stream is for a resource in this class loader's classpath, the resource is locked for reading and the returned stream is lock aware. The lock on the resource will be released when the stream is closed.

Note: This method does not look up package-local resources. See this JavaWorld article.

Overrides:
getResourceAsStream in class ClassLoader
Returns:
An InputStream for the resource's contents (lock aware if the resource is an EntityFS entity (file)), or null if the resource cannot be found.

getPackage

protected Package getPackage(String name)
Overrides:
getPackage in class ClassLoader

getPackages

protected Package[] getPackages()
Overrides:
getPackages in class ClassLoader

definePackage

protected Package definePackage(String name,
                                String specTitle,
                                String specVersion,
                                String specVendor,
                                String implTitle,
                                String implVersion,
                                String implVendor,
                                URL sealBase)
Overrides:
definePackage in class ClassLoader

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable