|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.discovery.tools.DiscoverClass
public class DiscoverClass
Discover class that implements a given service interface, with discovery and configuration features similar to that employed by standard Java APIs such as JAXP.
In the context of this package, a service interface is defined by a Service Provider Interface (SPI). The SPI is expressed as a Java interface, abstract class, or (base) class that defines an expected programming interface.
DiscoverClass provides the find
methods for locating a
class that implements a service interface (SPI). Each form of
find
varies slightly, but they all perform the same basic
function.
The DiscoverClass.find
methods proceed as follows:
ScopedProperties
class provides a way to bind
properties by classloader, in a secure hierarchy similar in concept
to the way classloader find class and resource files.
See ScopedProperties
for more details.
If the ScopedProperties are not set by users, then behaviour
is equivalent to System.getProperty()
.
Properties properties
property, if provided
as a parameter, whose name is the same as the SPI's fully qualifed class
name (as given by SPI.class.getName()).
SPI.class.getName()
. This is implemented
internally, so there is not a dependency on JDK 1.3+.
defaultImpl
) is null,
then an exception is thrown.
defaultImpl
) is non-null,
then load the default implementation class. The class loaded is the
first class loaded by the following sequence of class loaders:
This limits the scope in which the default class loader can be found to the SPI, DiscoverSingleton, and System class loaders. The assumption here is that the default implementation is closely associated with the SPI or system, and is not defined in the user's application space.
An exception is thrown if the class cannot be loaded.
IMPLEMENTATION NOTE - This implementation is modelled after the SAXParserFactory and DocumentBuilderFactory implementations (corresponding to the JAXP pluggability APIs) found in Apache Xerces.
Field Summary | |
---|---|
static PropertiesHolder |
nullProperties
Readable placeholder for a null value. |
Constructor Summary | |
---|---|
DiscoverClass()
Create a class instance with dynamic environment (thread context class loader is determined on each call). |
|
DiscoverClass(ClassLoaders classLoaders)
Create a class instance with dynamic environment (thread context class loader is determined on each call). |
Method Summary | ||
---|---|---|
static
|
discoverClassNames(SPInterface<T> spi,
Properties properties)
Discover names of SPI implementation Classes from properties. |
|
|
find(Class<T> spiClass)
Find class implementing SPI. |
|
|
find(Class<T> spiClass,
Properties properties)
Find class implementing SPI. |
|
|
find(Class<T> spiClass,
Properties properties,
String defaultImpl)
Find class implementing SPI. |
|
|
find(Class<T> spiClass,
String defaultImpl)
Find class implementing SPI. |
|
|
find(Class<T> spiClass,
String propertiesFileName,
String defaultImpl)
Find class implementing SPI. |
|
static
|
find(ClassLoaders loaders,
SPInterface<T> spi,
PropertiesHolder properties,
DefaultClassHolder<T> defaultImpl)
Find class implementing SPI. |
|
ClassLoaders |
getClassLoaders(Class<?> spiClass)
Return the class loaders holder for the given SPI. |
|
static String |
getManagedProperty(String propertyName)
Load the class whose name is given by the value of a (Managed) System Property. |
|
|
newInstance(Class<T> spiClass)
Create new instance of class implementing SPI. |
|
|
newInstance(Class<T> spiClass,
Properties properties)
Create new instance of class implementing SPI. |
|
|
newInstance(Class<T> spiClass,
Properties properties,
String defaultImpl)
Create new instance of class implementing SPI. |
|
|
newInstance(Class<T> spiClass,
String defaultImpl)
Create new instance of class implementing SPI. |
|
|
newInstance(Class<T> spiClass,
String propertiesFileName,
String defaultImpl)
Create new instance of class implementing SPI. |
|
static
|
newInstance(ClassLoaders loaders,
SPInterface<T> spi,
PropertiesHolder properties,
DefaultClassHolder<T> defaultImpl)
Create new instance of class implementing SPI. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final PropertiesHolder nullProperties
Constructor Detail |
---|
public DiscoverClass()
public DiscoverClass(ClassLoaders classLoaders)
classLoaders
- The class loaders holderMethod Detail |
---|
public ClassLoaders getClassLoaders(Class<?> spiClass)
spiClass
- The SPI type
public <T,S extends T> Class<S> find(Class<T> spiClass) throws DiscoveryException
T
- The SPI typeS
- Any class extending TspiClass
- Service Provider Interface Class.
DiscoveryException
- Thrown if the name of a class implementing
the SPI cannot be found, if the class cannot be loaded, or if
the resulting class does not implement (or extend) the SPI.public <T,S extends T> Class<S> find(Class<T> spiClass, Properties properties) throws DiscoveryException
T
- The SPI typeS
- Any class extending TspiClass
- Service Provider Interface Class.properties
- Used to determine name of SPI implementation.
DiscoveryException
- Thrown if the name of a class implementing
the SPI cannot be found, if the class cannot be loaded, or if
the resulting class does not implement (or extend) the SPI.public <T,S extends T> Class<S> find(Class<T> spiClass, String defaultImpl) throws DiscoveryException
T
- The SPI typeS
- Any class extending TspiClass
- Service Provider Interface Class.defaultImpl
- Default implementation name.
DiscoveryException
- Thrown if the name of a class implementing
the SPI cannot be found, if the class cannot be loaded, or if
the resulting class does not implement (or extend) the SPI.public <T,S extends T> Class<S> find(Class<T> spiClass, Properties properties, String defaultImpl) throws DiscoveryException
T
- The SPI typeS
- Any class extending TspiClass
- Service Provider Interface Class.properties
- Used to determine name of SPI implementation,.defaultImpl
- Default implementation class.
DiscoveryException
- Thrown if the name of a class implementing
the SPI cannot be found, if the class cannot be loaded, or if
the resulting class does not implement (or extend) the SPI.public <T,S extends T> Class<S> find(Class<T> spiClass, String propertiesFileName, String defaultImpl) throws DiscoveryException
T
- The SPI typeS
- Any class extending TspiClass
- Service Provider Interface Class.propertiesFileName
- Used to determine name of SPI implementation,.defaultImpl
- Default implementation class.
DiscoveryException
- Thrown if the name of a class implementing
the SPI cannot be found, if the class cannot be loaded, or if
the resulting class does not implement (or extend) the SPI.public static <T,S extends T> Class<S> find(ClassLoaders loaders, SPInterface<T> spi, PropertiesHolder properties, DefaultClassHolder<T> defaultImpl) throws DiscoveryException
T
- The SPI typeS
- Any class extending Tloaders
- The class loaders holderspi
- Service Provider Interface Class.properties
- Used to determine name of SPI implementation,.defaultImpl
- Default implementation class.
DiscoveryException
- Thrown if the name of a class implementing
the SPI cannot be found, if the class cannot be loaded, or if
the resulting class does not implement (or extend) the SPI.public <T> T newInstance(Class<T> spiClass) throws DiscoveryException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
T
- The SPI typespiClass
- Service Provider Interface Class.
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.
InstantiationException
- see Class.newInstance()
IllegalAccessException
- see Class.newInstance()
NoSuchMethodException
- see Class.newInstance()
InvocationTargetException
- see Class.newInstance()
public <T> T newInstance(Class<T> spiClass, Properties properties) throws DiscoveryException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
T
- The SPI typespiClass
- Service Provider Interface Class.properties
- Used to determine name of SPI implementation,
and passed to implementation.init() method if
implementation implements Service interface.
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.
InstantiationException
- see Class.newInstance()
IllegalAccessException
- see Class.newInstance()
NoSuchMethodException
- see Class.newInstance()
InvocationTargetException
- see Class.newInstance()
public <T> T newInstance(Class<T> spiClass, String defaultImpl) throws DiscoveryException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
T
- The SPI typespiClass
- Service Provider Interface Class.defaultImpl
- Default implementation.
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.
InstantiationException
- see Class.newInstance()
IllegalAccessException
- see Class.newInstance()
NoSuchMethodException
- see Class.newInstance()
InvocationTargetException
- see Class.newInstance()
public <T> T newInstance(Class<T> spiClass, Properties properties, String defaultImpl) throws DiscoveryException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
T
- The SPI typespiClass
- Service Provider Interface Class.properties
- Used to determine name of SPI implementation,
and passed to implementation.init() method if
implementation implements Service interface.defaultImpl
- Default implementation.
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.
InstantiationException
- see Class.newInstance()
IllegalAccessException
- see Class.newInstance()
NoSuchMethodException
- see Class.newInstance()
InvocationTargetException
- see Class.newInstance()
public <T> T newInstance(Class<T> spiClass, String propertiesFileName, String defaultImpl) throws DiscoveryException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
T
- The SPI typespiClass
- Service Provider Interface Class.propertiesFileName
- Used to determine name of SPI implementation,
and passed to implementation.init() method if
implementation implements Service interface.defaultImpl
- Default implementation.
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.
InstantiationException
- see Class.newInstance()
IllegalAccessException
- see Class.newInstance()
NoSuchMethodException
- see Class.newInstance()
InvocationTargetException
- see Class.newInstance()
public static <T> T newInstance(ClassLoaders loaders, SPInterface<T> spi, PropertiesHolder properties, DefaultClassHolder<T> defaultImpl) throws DiscoveryException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
T
- The SPI typeloaders
- The class loaders holderspi
- Service Provider Interface Class.properties
- Used to determine name of SPI implementation,
and passed to implementation.init() method if
implementation implements Service interface.defaultImpl
- Default implementation.
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.
InstantiationException
- see Class.newInstance()
IllegalAccessException
- see Class.newInstance()
NoSuchMethodException
- see Class.newInstance()
InvocationTargetException
- see Class.newInstance()
public static <T> String[] discoverClassNames(SPInterface<T> spi, Properties properties)
Discover names of SPI implementation Classes from properties. The names are the non-null values, in order, obtained from the following resources:
T
- The SPI typespi
- The SPI representationproperties
- Properties that may define the implementation
class name(s).
DiscoveryException
- Thrown if the name of a class implementing
the SPI cannot be found.public static String getManagedProperty(String propertyName)
propertyName
- the name of the system property whose value is
the name of the class to load.
ManagedProperties
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |