org.apache.sling.jcr.classloader.internal
Class DynamicRepositoryClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by org.apache.sling.jcr.classloader.internal.DynamicRepositoryClassLoader
All Implemented Interfaces:
javax.jcr.observation.EventListener, DynamicClassLoader

public final class DynamicRepositoryClassLoader
extends SecureClassLoader
implements javax.jcr.observation.EventListener, DynamicClassLoader

The DynamicRepositoryClassLoader class provides the functionality to load classes and resources from the JCR Repository. Additionally, this class supports the notion of getting 'dirty', which means, that if a resource loaded through this class loader has been modified in the Repository, this class loader marks itself dirty, which flag can get retrieved. This helps the user of this class loader to decide on whether to reinstantiate it or continue using this class loader.

When a user of the class loader recognizes an instance to be dirty, it can easily be reinstantiated with the reinstantiate(javax.jcr.Session, java.lang.ClassLoader) method. This reinstantiation will also rebuild the internal real class path from the same list of path patterns as was used to create the internal class path for the original class loader. The resulting internal class path need not be the same, though.


Nested Class Summary
protected static class DynamicRepositoryClassLoader.ProxyEventListener
           
 
Constructor Summary
DynamicRepositoryClassLoader(javax.jcr.Session session, String[] classPath, ClassLoader parent)
          Creates a DynamicRepositoryClassLoader from a list of item path strings containing globbing pattens for the paths defining the class path.
 
Method Summary
 void destroy()
          Destroys this class loader.
protected  Class<?> findClass(String name)
          Finds and loads the class with the specified name from the class path.
 URL findResource(String name)
          Finds the resource with the specified name on the search path.
 Enumeration<URL> findResources(String name)
          Returns an Enumeration of URLs representing all of the resources on the search path having the specified name.
 boolean isDirty()
          Returns whether the class loader is dirty.
 boolean isLive()
          Is this class loader still alive/valid?
 void onEvent(javax.jcr.observation.EventIterator events)
          Handles a repository item modifcation events checking whether a class needs to be expired.
 DynamicRepositoryClassLoader reinstantiate(javax.jcr.Session session, ClassLoader parent)
          Reinstantiates this class loader.
 String toString()
          Returns a string representation of this class loader.
 
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass, getPermissions
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DynamicRepositoryClassLoader

public DynamicRepositoryClassLoader(javax.jcr.Session session,
                                    String[] classPath,
                                    ClassLoader parent)
Creates a DynamicRepositoryClassLoader from a list of item path strings containing globbing pattens for the paths defining the class path.

Parameters:
session - The Session to use to access the class items.
classPath - The list of path strings making up the (initial) class path of this class loader. The strings may contain globbing characters which will be resolved to build the actual class path.
parent - The parent ClassLoader, which may be null.
Throws:
NullPointerException - if either the session or the handles list is null.
Method Detail

destroy

public void destroy()
Destroys this class loader. This process encompasses all steps needed to remove as much references to this class loader as possible.

NOTE: This method just clears all internal fields and especially the class path to render this class loader unusable.

This implementation does not throw any exceptions.


findClass

protected Class<?> findClass(String name)
                      throws ClassNotFoundException
Finds and loads the class with the specified name from the class path.

Overrides:
findClass in class ClassLoader
Parameters:
name - the name of the class
Returns:
the resulting class
Throws:
ClassNotFoundException - If the named class could not be found or if this class loader has already been destroyed.

findResource

public URL findResource(String name)
Finds the resource with the specified name on the search path.

Overrides:
findResource in class ClassLoader
Parameters:
name - the name of the resource
Returns:
a URL for the resource, or null if the resource could not be found or if the class loader has already been destroyed.

findResources

public Enumeration<URL> findResources(String name)
Returns an Enumeration of URLs representing all of the resources on the search path having the specified name.

Overrides:
findResources in class ClassLoader
Parameters:
name - the resource name
Returns:
an Enumeration of URLs. This is an empty enumeration if no resources are found by this class loader or if this class loader has already been destroyed.

isDirty

public boolean isDirty()
Returns whether the class loader is dirty. This can be the case if any of the loaded class has been expired through the observation.

This method may also return true if the Session associated with this class loader is not valid anymore.

Finally the method always returns true if the class loader has already been destroyed. Note, however, that a destroyed class loader cannot be reinstantiated. See reinstantiate(Session, ClassLoader).

If the class loader is dirty, it should be reinstantiated through the reinstantiate(javax.jcr.Session, java.lang.ClassLoader) method.

Returns:
true if the class loader is dirty and needs reinstantiation.

isLive

public boolean isLive()
Description copied from interface: DynamicClassLoader
Is this class loader still alive/valid?

Specified by:
isLive in interface DynamicClassLoader
See Also:
DynamicClassLoader.isLive()

reinstantiate

public DynamicRepositoryClassLoader reinstantiate(javax.jcr.Session session,
                                                  ClassLoader parent)
Reinstantiates this class loader. That is, a new ClassLoader with no loaded class is created with the same configuration as this class loader.

When the new class loader is returned, this class loader has been destroyed and may not be used any more.

Parameters:
parent - The parent ClassLoader for the reinstantiated DynamicRepositoryClassLoader, which may be null.
Returns:
a new instance with the same configuration as this class loader.
Throws:
IllegalStateException - if this DynamicRepositoryClassLoader has already been destroyed through the destroy() method.

onEvent

public void onEvent(javax.jcr.observation.EventIterator events)
Handles a repository item modifcation events checking whether a class needs to be expired. As a side effect, this method sets the class loader dirty if a loaded class has been modified in the repository.

Specified by:
onEvent in interface javax.jcr.observation.EventListener
Parameters:
events - The iterator of repository events to be handled.

toString

public String toString()
Returns a string representation of this class loader.

Overrides:
toString in class Object


Copyright © 2007-2013 The Apache Software Foundation. All Rights Reserved.