Package weka.core

# Class WekaPackageClassLoaderManager

java.lang.Object
weka.core.WekaPackageClassLoaderManager

public class WekaPackageClassLoaderManager extends Object
Class that manages classloaders from individual Weka plugin packages. Maintains a collection of `WekaPackageLibIsolatingClassLoader`s - one for each package. `Utils.forName()` and `weka.Run` use this classloader to find/instantiate schemes exposed in top-level package jar files. Client code in a package should do the same, unless directly referring to classes in other packages, in which case the other packages should be explicit dependencies. This classloader will not find classes in third-party libraries inside a package's lib directory.
Classes are searched for first in the parent classloader and then in the top-level package jar files.

Version:
  $Revision: $
Author:
  Mark Hall (mhall{[at]}pentaho{[dot]}com)
See Also:
  - `WekaPackageLibIsolatingClassLoader`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `ClassLoader`
  `addPackageToClassLoader(File packageDir)`
  Create a class loader for the given package directory
  `ClassLoader`
  `findClassloaderForResource(String name)`
  Get the classloader that covers the jar that contains the named resource.
  `URL`
  `findResource(String name)`
  Find a named resource.
  `Enumeration<URL>`
  `findResources(String name)`
  Find a named resource.
  `static Class<?>`
  `forName(String className)`
  Return the class object for the supplied class name.
  `static Class<?>`
  `forName(String className, boolean initialize)`
  Return the class object for the supplied class name.
  `ClassLoader`
  `getLoaderForClass(String className)`
  Attempts to locate a classloader for the named class.
  `WekaPackageLibIsolatingClassLoader`
  `getPackageClassLoader(String packageName)`
  Get the classloader for the named package
  `Set<String>`
  `getPackageJarFileClasses()`
  Get a set of all classes contained in all top-level jar files from Weka packages.
  `File`
  `getPathToWekaJarFile()`
  Return the path to the weka.jar file (if found) on the classpath.
  `URL[]`
  `getWekaClassloaderClasspathEntries()`
  Get the entries in the Weka class loader (i.e.
  `static WekaPackageClassLoaderManager`
  `getWekaPackageClassLoaderManager()`
  Gets the singleton instance of the WekaPackageClassLoaderManager
  `static Object`
  `objectForName(String className)`
  Return an instantiated instance of the supplied class name.
  `void`
  `removeClassLoaderForPackage(String packageName)`
  Removes the named package classloader from those managed by this class.

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getWekaPackageClassLoaderManager

    public static WekaPackageClassLoaderManager getWekaPackageClassLoaderManager()
    Gets the singleton instance of the WekaPackageClassLoaderManager
    Returns:
      the singleton instance of hte WekaPackageClassLoaderManager

  - ### objectForName

    public static Object objectForName(String className) throws Exception
    Return an instantiated instance of the supplied class name. This method will attempt to find a package that owns the named class first, before falling back on the current and then parent class loader. Use this method instead of Class.forName().newInstance().
    Parameters:
      `className` - the name of the class to get an instance of
    Returns:
      an instantiated object
    Throws:
      `Exception` - if the class cannot be found, or a problem occurs during instantiation

  - ### forName

    public static Class<?> forName(String className) throws ClassNotFoundException
    Return the class object for the supplied class name. This method will attempt to find a package that owns the named class first, before falling back on the current, and then parent, class loader. Use this method instead of Class.forName().
    Parameters:
      `className` - the name of hte class to get an instance of
    Returns:
      a class object
    Throws:
      `ClassNotFoundException` - if the named class cannot be found.

  - ### forName

    public static Class<?> forName(String className, boolean initialize) throws ClassNotFoundException
    Return the class object for the supplied class name. This method will attempt to find a package that owns the named class first, before falling back on the current, and then parent, class loader. Use this method instead of Class.forName().
    Parameters:
      `className` - the name of hte class to get an instance of
      `initialize` - true if the class should be initialized
    Returns:
      a class object
    Throws:
      `ClassNotFoundException` - if the named class cannot be found.

  - ### getPathToWekaJarFile

    public File getPathToWekaJarFile()
    Return the path to the weka.jar file (if found) on the classpath.
    Returns:
      the path to the weka.jar file on the classpath, or null if no weka.jar file was found.

  - ### getWekaClassloaderClasspathEntries

    public URL[] getWekaClassloaderClasspathEntries()
    Get the entries in the Weka class loader (i.e. the class loader that loads the core weka classes) as an array of URLs. This is primarily used by Weka's dynamic class discovery mechanism, so that all Weka schemes on the classpath can be discovered. If the Weka class loader is an instance of URLClassLoader then the URLs encapsulated within are returned. Otherwise, if the system class loader is the classloader that loads Weka then the entries from java.class.path are returned. Failing that, we assume that Weka and any other supporting classes (not including packages) can be found in the WEKA_CLASSPATH environment variable. This latter case can be used to handle the situation where the weka.jar is loaded by a custom (non-URLClassLoader) withn an app server (for example).
    Returns:
      an array of URLs containing the entries available to the classloader that loads the core weka classes

  - ### getPackageJarFileClasses

    public Set<String> getPackageJarFileClasses()
    Get a set of all classes contained in all top-level jar files from Weka packages. These classes are globally visible across all packages.
    Returns:
      a set of all classes in all top-level package jar files

  - ### removeClassLoaderForPackage

    public void removeClassLoaderForPackage(String packageName)
    Removes the named package classloader from those managed by this class. Attempts to close the classloader and remove any file locks (under Windows) that it might be holding
    Parameters:
      `packageName` - the name of the package to remove the classloader for

  - ### addPackageToClassLoader

    public ClassLoader addPackageToClassLoader(File packageDir) throws Exception
    Create a class loader for the given package directory
    Parameters:
      `packageDir` - the directory of a Weka package to create a class loader for
    Throws:
      `Exception` - if a problem occurs

  - ### getLoaderForClass

    public ClassLoader getLoaderForClass(String className)
    Attempts to locate a classloader for the named class. Tries the Weka classloader and globally visible package classes first. Note that this also finds classes that are contained in a package's lib directory. General code should not be looking directly for such third-party classes. Instead, if they require third-party classes they should reference them directly (and compile against the libraries in question), and then either include the third party libraries in their own lib directory or declare a dependency on the package that contains them. This method is used by Weka's deserialization routines (in SerializationHelper and SerializedObject) that need to locate classes (including third-party library ones) that might have been serialized when saving a learning scheme.
    Parameters:
      `className` - the name of the class to locate a classloader for
    Returns:
      a classloader

  - ### getPackageClassLoader

    public WekaPackageLibIsolatingClassLoader getPackageClassLoader(String packageName)
    Get the classloader for the named package
    Parameters:
      `packageName` - the name of the package to get the classloader for
    Returns:
      the package's classloader, or null if the package is not known (or perhaps was not loaded for some reason)

  - ### findResource

    public URL findResource(String name)
    Find a named resource. This searches the Weka classloader and all package classloaders. Note that it will only find resources contained in a package's top-level jar file(s).
    Parameters:
      `name` - the name of the resource to find
    Returns:
      a URL to the resource, or null if the resource could not be located

  - ### findClassloaderForResource

    public ClassLoader findClassloaderForResource(String name)
    Get the classloader that covers the jar that contains the named resource. Note that this considers the Weka classloader and package classloaders (with respect to resources contained in their top-level jar file(s))
    Parameters:
      `name` - the name of the resource to get the owning classloader for
    Returns:
      the classloader that "owns" the resource

  - ### findResources

    public Enumeration<URL> findResources(String name) throws IOException
    Find a named resource. This searches the Weka classloader and all package classloaders. Note that it will only find resources contained in a package's top-level jar file(s).
    Parameters:
      `name` - the name of the resource to find
    Returns:
      an enumeration of URLs to the resource, or null if the resource could not be located
    Throws:
      `IOException`