com.qrmedia.commons.multispi
Class MultiSpi

java.lang.Object
  extended by com.qrmedia.commons.multispi.MultiSpi

@ThreadSafe
public final class MultiSpi
extends Object

One-stop shop for users of MultiSpi. Instances should be created via dependency injection or using the MultiSpiBuilder.

Since:
4 Dec 2010
Author:
aphillips
See Also:
ServiceLoader

Constructor Summary
MultiSpi(Set<ServiceImplementationProvider> providers)
           
 
Method Summary
 boolean equals(Object obj)
           
 Set<String> findImplementationNames(Class<?> serviceClass)
          Shorthand for findImplementationNames(serviceClass, defaultClassLoader) where defaultClassLoader is Thread.currentThread().getContextClassLoader() if non-null ClassLoader.getSystemClassLoader() if non-null the bootstrap classloader otherwise
 Set<String> findImplementationNames(Class<?> serviceClass, ClassLoader classpathResourceLoader)
          Finds the names of classes implementing the requested service, as determined by the available providers.
<S> Set<Class<? extends S>>
findImplementations(Class<S> serviceClass)
          Shorthand for findImplementations(serviceClass, defaultClassLoader) where defaultClassLoader is Thread.currentThread().getContextClassLoader() if non-null ClassLoader.getSystemClassLoader() if non-null the bootstrap classloader otherwise
<S> Set<Class<? extends S>>
findImplementations(Class<S> serviceClass, ClassLoader implementationLoader)
          Finds the classes implementing the requested service, as determined by the available providers.
 int hashCode()
           
<S> Set<S>
loadImplementations(Class<S> serviceClass)
          Shorthand for loadImplementations(serviceClass, defaultClassLoader).
<S> Set<S>
loadImplementations(Class<S> serviceClass, ClassLoader implementationLoader)
          Finds the classes implementing the requested service, as determined by the available providers, and loads an instance of each implementation.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiSpi

@Inject
public MultiSpi(@Nonnull
                       Set<ServiceImplementationProvider> providers)
Method Detail

findImplementationNames

@Nonnull
public Set<String> findImplementationNames(@Nonnull
                                                   Class<?> serviceClass)
Shorthand for findImplementationNames(serviceClass, defaultClassLoader) where defaultClassLoader is

Parameters:
serviceClass - the class of the service
Returns:
a set of names of classes designated as implementing the requested service

findImplementationNames

@Nonnull
public Set<String> findImplementationNames(@Nonnull
                                                   Class<?> serviceClass,
                                                   @Nonnull
                                                   ClassLoader classpathResourceLoader)
Finds the names of classes implementing the requested service, as determined by the available providers.

Note that individual providers may or may not guarantee that the classes returned actually implement the interface!

Parameters:
serviceClass - the class of the service
classpathResourceLoader - the class loader to be used to locate classpath resources
Returns:
a set of names of classes designated as implementing the requested service

findImplementations

@Nonnull
public <S> Set<Class<? extends S>> findImplementations(@Nonnull
                                                               Class<S> serviceClass)
                                            throws ClassCastException,
                                                   ClassNotFoundException
Shorthand for findImplementations(serviceClass, defaultClassLoader) where defaultClassLoader is

Type Parameters:
S - the type of the service
Parameters:
serviceClass - the class of the service
Returns:
a set of classes of implementations of the service
Throws:
ClassCastException - if any of the classes are not actually implementations of the requested service
ClassNotFoundException - if any of the classes cannot be loaded
See Also:
findImplementations(Class, ClassLoader)

findImplementations

@Nonnull
public <S> Set<Class<? extends S>> findImplementations(@Nonnull
                                                               Class<S> serviceClass,
                                                               @Nonnull
                                                               ClassLoader implementationLoader)
                                            throws ClassCastException,
                                                   ClassNotFoundException
Finds the classes implementing the requested service, as determined by the available providers.

This method will check whether the classes found do, in fact, implement the requested service, and a ClassCastException will be thrown if any of the classes does not.

If more lenient behaviour is desired (e.g. skipping any classes that cannot be found, or do not actually implement the interface), use findImplementationNames(Class) instead.

Type Parameters:
S - the type of the service
Parameters:
serviceClass - the class of the service
implementationLoader - the class loader to be used to attempt to load the classes
Returns:
a set of instances of implementations of the service
Throws:
ClassCastException - if any of the classes are not actually implementations of the requested service
ClassNotFoundException - if any of the classes cannot be loaded
See Also:
findImplementations(Class)

loadImplementations

@Nonnull
public <S> Set<S> loadImplementations(@Nonnull
                                              Class<S> serviceClass)
                           throws ClassNotFoundException,
                                  InstantiationException
Shorthand for loadImplementations(serviceClass, defaultClassLoader). where defaultClassLoader is

Type Parameters:
S - the type of the service
Parameters:
serviceClass - the class of the service
Returns:
a set of instances of implementations of the service
Throws:
ClassNotFoundException - if any of the classes cannot be loaded
InstantiationException - if any of the classes are not actually implementations of the requested service, or cannot be instantiated using a public no-argument constructor
See Also:
loadImplementations(Class, ClassLoader)

loadImplementations

@Nonnull
public <S> Set<S> loadImplementations(@Nonnull
                                              Class<S> serviceClass,
                                              @Nonnull
                                              ClassLoader implementationLoader)
                           throws ClassNotFoundException,
                                  InstantiationException
Finds the classes implementing the requested service, as determined by the available providers, and loads an instance of each implementation.

This method will check whether the classes found do, in fact, implement the requested service, and an InstantiationException will be thrown if any of the classes does not.

Service classes are expected to provide a public, no-argument constructor, which will be invoked to create instances of the implementation classes found. An InstantiationException is thrown if the class cannot be instantiated.

If more lenient behaviour is desired (e.g. skipping any classes that cannot be found, or do not actually implement the interface), use findImplementationNames(Class) instead.

Type Parameters:
S - the type of the service
Parameters:
serviceClass - the class of the service
implementationLoader - the class loader to be used to attempt to load the classes
Returns:
a set of instances of implementations of the service
Throws:
ClassNotFoundException - if any of the classes cannot be loaded
InstantiationException - if any of the classes are not actually implementations of the requested service, or cannot be instantiated using a public no-argument constructor
See Also:
loadImplementations(Class)

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object


Copyright © 2011 qrmedia. All Rights Reserved.