Class ResourceUtils
java.lang.Object
org.apache.commons.discovery.tools.ResourceUtils
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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
getPackageName
(Class<?> clazz) Get package name.static Resource
getResource
(Class<?> spi, String resourceName, ClassLoaders loaders) Load the resourceresourceName
.static Properties
loadProperties
(Class<?> spi, String propertiesFileName, ClassLoaders classLoaders) Load named property file, optionally qualified by spi's package name as per Class.getResource.
-
Constructor Details
-
ResourceUtils
public ResourceUtils()
-
-
Method Details
-
getPackageName
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.- Parameters:
clazz
- The class from which the package has to be extracted- Returns:
- The string representation of the input class package
-
getResource
public static Resource getResource(Class<?> spi, String resourceName, ClassLoaders loaders) throws DiscoveryException Load the resourceresourceName
. Try each classloader in succession, until first succeeds, or all fail. If all fail andresouceName
is not absolute (doesn't start with '/' character), then retry withpackageName/resourceName
after changing all '.' to '/'.- Parameters:
spi
- The SPI typeresourceName
- The name of the resource to load.loaders
- the class loaders holder- Returns:
- The discovered
Resource
instance - Throws:
DiscoveryException
- if the class implementing the SPI cannot be found, cannot be loaded and instantiated, or if the resulting class does not implement (or extend) the SPI
-
loadProperties
public static Properties loadProperties(Class<?> spi, String propertiesFileName, ClassLoaders classLoaders) throws DiscoveryException Load named property file, optionally qualified 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:
spi
- The SPI typepropertiesFileName
- The property file name.classLoaders
- The class loaders holder- Returns:
- The loaded named property file, in
Properties
format - 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.
-