Class ResourceUtils


  • public class ResourceUtils
    extends java.lang.Object
    Mechanisms to locate and load a class. The load methods locate a class only. The find methods locate a class and verify that the class implements an given interface or extends a given class.
    Author:
    Richard A. Sitze, Craig R. McClanahan, Costin Manolache
    • Constructor Summary

      Constructors 
      Constructor Description
      ResourceUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getPackageName​(java.lang.Class clazz)
      Get package name.
      static Resource getResource​(java.lang.Class spi, java.lang.String resourceName, ClassLoaders loaders)
      Load the resource resourceName.
      static java.util.Properties loadProperties​(java.lang.Class spi, java.lang.String propertiesFileName, ClassLoaders classLoaders)
      Load named property file, optionally qualifed by spi's package name as per Class.getResource.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ResourceUtils

        public ResourceUtils()
    • Method Detail

      • getPackageName

        public static java.lang.String getPackageName​(java.lang.Class clazz)
        Get package name. Not all class loaders 'keep' package information, in which case Class.getPackage() returns null. This means that calling Class.getPackage().getName() is unreliable at best.
      • getResource

        public static Resource getResource​(java.lang.Class spi,
                                           java.lang.String resourceName,
                                           ClassLoaders loaders)
                                    throws DiscoveryException
        Load the resource resourceName. Try each classloader in succession, until first succeeds, or all fail. If all fail and resouceName is not absolute (doesn't start with '/' character), then retry with packageName/resourceName after changing all '.' to '/'.
        Parameters:
        resourceName - The name of the resource to load.
        Throws:
        DiscoveryException
      • loadProperties

        public static java.util.Properties loadProperties​(java.lang.Class spi,
                                                          java.lang.String propertiesFileName,
                                                          ClassLoaders classLoaders)
                                                   throws DiscoveryException
        Load named property file, optionally qualifed by spi's package name as per Class.getResource. A property file is loaded using the following sequence of class loaders:
        • Thread Context Class Loader
        • DiscoverSingleton's Caller's Class Loader
        • SPI's Class Loader
        • DiscoverSingleton's (this class) Class Loader
        • System Class Loader
        Parameters:
        propertiesFileName - The property file name.
        Returns:
        Instance of a class implementing the SPI.
        Throws:
        DiscoveryException - Thrown if the name of a class implementing the SPI cannot be found, if the class cannot be loaded and instantiated, or if the resulting class does not implement (or extend) the SPI.