Class VFSClassLoader

java.lang.Object
java.lang.ClassLoader
java.security.SecureClassLoader
org.apache.commons.vfs2.impl.VFSClassLoader

public class VFSClassLoader extends SecureClassLoader
A class loader that can load classes and resources from a search path.

The search path can consist of VFS FileObjects referring both to folders and JAR files. Any FileObject of type FileType.FILE is assumed 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.

See Also:
  • Field Details

  • Constructor Details

    • 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 - if an error occurs.
    • VFSClassLoader

      public VFSClassLoader(FileObject file, FileSystemManager manager, 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 - if an error occurs.
    • 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 - if an error occurs.
    • VFSClassLoader

      public VFSClassLoader(FileObject[] files, FileSystemManager manager, 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 - if an error occurs.
  • Method Details

    • getFileObjects

      public FileObject[] getFileObjects()
      Provide access to the file objects this class loader represents.
      Returns:
      An array of FileObjects.
      Since:
      2.0
    • 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:
      manager - The FileSystemManager.
      files - the FileObjects to append to the search path.
      Throws:
      FileSystemException - if an error occurs.
    • findClass

      protected Class<?> findClass(String name) throws ClassNotFoundException
      Finds and loads the class with the specified name from the search path.
      Overrides:
      findClass in class ClassLoader
      Throws:
      ClassNotFoundException - if the class is not found.
    • defineClass

      private Class<?> defineClass(String name, Resource res) throws IOException
      Loads and verifies the class with name and located with res.
      Throws:
      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 Package definePackage(String name, Resource res) throws FileSystemException
      Reads attributes for the package and defines it.
      Throws:
      FileSystemException
    • getPermissions

      protected PermissionCollection getPermissions(CodeSource cs)
      Calls super.getPermissions both for the code source and also adds the permissions granted to the parent layers.
      Overrides:
      getPermissions in class SecureClassLoader
      Parameters:
      cs - the CodeSource.
      Returns:
      The PermissionCollections.
    • copyPermissions

      protected void copyPermissions(PermissionCollection src, PermissionCollection dest)
      Copies the permissions from src to dest.
      Parameters:
      src - The source PermissionCollection.
      dest - The destination PermissionCollection.
    • lookupFileObject

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

      protected URL findResource(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 ClassLoader
      Parameters:
      name - The resource name.
      Returns:
      The URL that matches the resource.
    • findResources

      protected Enumeration<URL> findResources(String name) throws IOException
      Returns an Enumeration of all the resources in the search path with the specified name.

      Gets called from ClassLoader.getResources(String) after parent class loader was questioned.

      Overrides:
      findResources in class ClassLoader
      Parameters:
      name - The resources to find.
      Returns:
      An Enumeration of the resources associated with the name.
      Throws:
      FileSystemException - if an error occurs.
      IOException
    • loadResource

      private Resource loadResource(String name) throws FileSystemException
      Searches through the search path of for the first class or resource with specified name.
      Parameters:
      name - The resource to load.
      Returns:
      The Resource.
      Throws:
      FileSystemException - if an error occurs.