org.apache.commons.discovery.tools
Class DiscoverSingleton

java.lang.Object
  extended by org.apache.commons.discovery.tools.DiscoverSingleton

public class DiscoverSingleton
extends Object

Discover singleton service providers. This

DiscoverSingleton instances are cached by the Discovery service, keyed by a combination of

This DOES allow multiple instances of a given singleton class to exist for different class loaders and different group contexts.

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.

DiscoverSingleton provides the find methods for locating and instantiating a singleton instance of an implementation of a service (SPI). Each form of find varies slightly, but they all perform the same basic function. The simplest find methods are intended for direct use by components looking for a service. If you are not sure which finder(s) to use, you can narrow your search to one of these:

The DiscoverSingleton.find methods proceed as follows:

Variances for various forms of the find methods are discussed with each such method. Variances include the following concepts:

IMPLEMENTATION NOTE - This implementation is modelled after the SAXParserFactory and DocumentBuilderFactory implementations (corresponding to the JAXP pluggability APIs) found in Apache Xerces.

Version:
$Revision: 1089242 $ $Date: 2011-04-05 23:33:21 +0200 (Tue, 05 Apr 2011) $

Constructor Summary
DiscoverSingleton()
           
 
Method Summary
static
<T> T
find(Class<T> spiClass)
          Find implementation of SPI.
static
<T> T
find(Class<T> spiClass, Properties properties)
          Find implementation of SPI.
static
<T> T
find(Class<T> spiClass, Properties properties, String defaultImpl)
          Find implementation of SPI.
static
<T> T
find(Class<T> spiClass, String defaultImpl)
          Find implementation of SPI.
static
<T> T
find(Class<T> spiClass, String propertiesFileName, String defaultImpl)
          Find implementation of SPI.
static
<T> T
find(ClassLoaders loaders, SPInterface<T> spi, PropertiesHolder properties, DefaultClassHolder<T> defaultImpl)
          Find implementation of SPI.
static void release()
          Release all internal references to previously created service instances associated with the current thread context class loader.
static void release(Class<?> spiClass)
          Release any internal references to a previously created service instance associated with the current thread context class loader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DiscoverSingleton

public DiscoverSingleton()
Method Detail

find

public static <T> T find(Class<T> spiClass)
              throws DiscoveryException
Find implementation of SPI.

Type Parameters:
T - Service Provider Interface type.
Parameters:
spiClass - Service Provider Interface Class.
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.

find

public static <T> T find(Class<T> spiClass,
                         Properties properties)
              throws DiscoveryException
Find implementation of SPI.

Type Parameters:
T - Service Provider Interface type
Parameters:
spiClass - Service Provider Interface Class.
properties - Used to determine name of SPI implementation, and passed to implementation.init() method if implementation implements Service interface.
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.

find

public static <T> T find(Class<T> spiClass,
                         String defaultImpl)
              throws DiscoveryException
Find implementation of SPI.

Type Parameters:
T - Service Provider Interface type
Parameters:
spiClass - Service Provider Interface Class.
defaultImpl - Default implementation.
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.

find

public static <T> T find(Class<T> spiClass,
                         Properties properties,
                         String defaultImpl)
              throws DiscoveryException
Find implementation of SPI.

Type Parameters:
T - Service Provider Interface type
Parameters:
spiClass - 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.
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.

find

public static <T> T find(Class<T> spiClass,
                         String propertiesFileName,
                         String defaultImpl)
              throws DiscoveryException
Find implementation of SPI.

Type Parameters:
T - Service Provider Interface type
Parameters:
spiClass - 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.
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.

find

public static <T> T find(ClassLoaders loaders,
                         SPInterface<T> spi,
                         PropertiesHolder properties,
                         DefaultClassHolder<T> defaultImpl)
              throws DiscoveryException
Find implementation of SPI.

Type Parameters:
T - Service Provider Interface type
Parameters:
loaders - The ClassLoader holder
spi - 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.
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.

release

public static void release()
Release all internal references to previously created service instances associated with the current thread context class loader. The release() method is called for service instances that implement the Service interface. This is useful in environments like servlet containers, which implement application reloading by throwing away a ClassLoader. Dangling references to objects in that class loader would prevent garbage collection.


release

public static void release(Class<?> spiClass)
Release any internal references to a previously created service instance associated with the current thread context class loader. If the SPI instance implements Service, then call release().

Parameters:
spiClass - The previously created service


Copyright © 2002-2011 The Apache Software Foundation. All Rights Reserved.