org.apache.commons.vfs.impl
Class VFSClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by org.apache.commons.vfs.impl.VFSClassLoader

public class VFSClassLoader
extends java.security.SecureClassLoader

A class loader that can load classes and resources from a search path VFS FileObjects refering both to folders and JAR files. Any FileObject of type FileType.FILE is asumed to be a JAR and is opened by creating a layered file system with the "jar" scheme.

TODO - Test this with signed Jars and a SecurityManager.

Version:
$Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
Author:
Brian Olsen
See Also:
FileSystemManager.createFileSystem(java.lang.String, org.apache.commons.vfs.FileObject)

Field Summary
private  java.util.ArrayList resources
           
 
Constructor Summary
VFSClassLoader(FileObject[] files, FileSystemManager manager)
          Constructors a new VFSClassLoader for the given files.
VFSClassLoader(FileObject[] files, FileSystemManager manager, java.lang.ClassLoader parent)
          Constructors a new VFSClassLoader for the given FileObjects.
VFSClassLoader(FileObject file, FileSystemManager manager)
          Constructors a new VFSClassLoader for the given file.
VFSClassLoader(FileObject file, FileSystemManager manager, java.lang.ClassLoader parent)
          Constructors a new VFSClassLoader for the given file.
 
Method Summary
private  void addFileObjects(FileSystemManager manager, FileObject[] files)
          Appends the specified FileObjects to the list of FileObjects to search for classes and resources.
protected  void copyPermissions(java.security.PermissionCollection src, java.security.PermissionCollection dest)
          Copies the permissions from src to dest.
private  java.lang.Class defineClass(java.lang.String name, Resource res)
          Loads and verifies the class with name and located with res.
private  java.lang.Package definePackage(java.lang.String name, Resource res)
          Reads attributes for the package and defines it.
protected  java.lang.Class findClass(java.lang.String name)
          Finds and loads the class with the specified name from the search path.
protected  java.net.URL findResource(java.lang.String name)
          Finds the resource with the specified name from the search path.
protected  java.util.Enumeration findResources(java.lang.String name)
          Returns an Enumeration of all the resources in the search path with the specified name.
protected  java.security.PermissionCollection getPermissions(java.security.CodeSource cs)
          Calls super.getPermissions both for the code source and also adds the permissions granted to the parent layers.
private  boolean isSealed(Resource res)
          Returns true if the we should seal the package where res resides.
private  Resource loadResource(java.lang.String name)
          Searches through the search path of for the first class or resource with specified name.
private  FileObject lookupFileObject(java.lang.String name)
          Does a reverse lookup to find the FileObject when we only have the URL.
 
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
 
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, toString, wait, wait, wait
 

Field Detail

resources

private final java.util.ArrayList resources
Constructor Detail

VFSClassLoader

public VFSClassLoader(FileObject file,
                      FileSystemManager manager)
               throws FileSystemException
Constructors a new VFSClassLoader for the given file.

Parameters:
file - the file to load the classes and resources from.
manager - the FileManager to use when trying create a layered Jar file system.
Throws:
FileSystemException

VFSClassLoader

public VFSClassLoader(FileObject file,
                      FileSystemManager manager,
                      java.lang.ClassLoader parent)
               throws FileSystemException
Constructors a new VFSClassLoader for the given file.

Parameters:
file - the file to load the classes and resources from.
manager - the FileManager to use when trying create a layered Jar file system.
parent - the parent class loader for delegation.
Throws:
FileSystemException

VFSClassLoader

public VFSClassLoader(FileObject[] files,
                      FileSystemManager manager)
               throws FileSystemException
Constructors a new VFSClassLoader for the given files. The files will be searched in the order specified.

Parameters:
files - the files to load the classes and resources from.
manager - the FileManager to use when trying create a layered Jar file system.
Throws:
FileSystemException

VFSClassLoader

public VFSClassLoader(FileObject[] files,
                      FileSystemManager manager,
                      java.lang.ClassLoader parent)
               throws FileSystemException
Constructors a new VFSClassLoader for the given FileObjects. The FileObjects will be searched in the order specified.

Parameters:
files - the FileObjects to load the classes and resources from.
manager - the FileManager to use when trying create a layered Jar file system.
parent - the parent class loader for delegation.
Throws:
FileSystemException
Method Detail

addFileObjects

private void addFileObjects(FileSystemManager manager,
                            FileObject[] files)
                     throws FileSystemException
Appends the specified FileObjects to the list of FileObjects to search for classes and resources.

Parameters:
files - the FileObjects to append to the search path.
Throws:
FileSystemException

findClass

protected java.lang.Class findClass(java.lang.String name)
                             throws java.lang.ClassNotFoundException
Finds and loads the class with the specified name from the search path.

Overrides:
findClass in class java.lang.ClassLoader
Throws:
java.lang.ClassNotFoundException - if the class is not found.

defineClass

private java.lang.Class defineClass(java.lang.String name,
                                    Resource res)
                             throws java.io.IOException
Loads and verifies the class with name and located with res.

Throws:
java.io.IOException

isSealed

private boolean isSealed(Resource res)
                  throws FileSystemException
Returns true if the we should seal the package where res resides.

Throws:
FileSystemException

definePackage

private java.lang.Package definePackage(java.lang.String name,
                                        Resource res)
                                 throws FileSystemException
Reads attributes for the package and defines it.

Throws:
FileSystemException

getPermissions

protected java.security.PermissionCollection getPermissions(java.security.CodeSource cs)
Calls super.getPermissions both for the code source and also adds the permissions granted to the parent layers.

Overrides:
getPermissions in class java.security.SecureClassLoader

copyPermissions

protected void copyPermissions(java.security.PermissionCollection src,
                               java.security.PermissionCollection dest)
Copies the permissions from src to dest.


lookupFileObject

private FileObject lookupFileObject(java.lang.String name)
Does a reverse lookup to find the FileObject when we only have the URL.


findResource

protected java.net.URL findResource(java.lang.String name)
Finds the resource with the specified name from the search path. This returns null if the resource is not found.

Overrides:
findResource in class java.lang.ClassLoader

findResources

protected java.util.Enumeration findResources(java.lang.String name)
Returns an Enumeration of all the resources in the search path with the specified name. TODO - Implement this.

Overrides:
findResources in class java.lang.ClassLoader

loadResource

private Resource loadResource(java.lang.String name)
                       throws FileSystemException
Searches through the search path of for the first class or resource with specified name.

Throws:
FileSystemException