Package weka.core

Class WekaPackageClassLoaderManager


  • public class WekaPackageClassLoaderManager
    extends java.lang.Object
    Class that manages classloaders from individual Weka plugin packages. Maintains a collection of WekaPackageLibIsolatingClassLoaders - 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

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

      • getWekaPackageClassLoaderManager

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

        public static java.lang.Object objectForName​(java.lang.String className)
                                              throws java.lang.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:
        java.lang.Exception - if the class cannot be found, or a problem occurs during instantiation
      • forName

        public static java.lang.Class<?> forName​(java.lang.String className)
                                          throws java.lang.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:
        java.lang.ClassNotFoundException - if the named class cannot be found.
      • forName

        public static java.lang.Class<?> forName​(java.lang.String className,
                                                 boolean initialize)
                                          throws java.lang.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:
        java.lang.ClassNotFoundException - if the named class cannot be found.
      • getPathToWekaJarFile

        public java.io.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 java.net.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 java.util.Set<java.lang.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​(java.lang.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 java.lang.ClassLoader addPackageToClassLoader​(java.io.File packageDir)
                                                      throws java.lang.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:
        java.lang.Exception - if a problem occurs
      • getLoaderForClass

        public java.lang.ClassLoader getLoaderForClass​(java.lang.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​(java.lang.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 java.net.URL findResource​(java.lang.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 java.lang.ClassLoader findClassloaderForResource​(java.lang.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 java.util.Enumeration<java.net.URL> findResources​(java.lang.String name)
                                                          throws java.io.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:
        java.io.IOException