org.jboss.seam.solder.util.service
Class ServiceLoader<S>

java.lang.Object
  extended by org.jboss.seam.solder.util.service.ServiceLoader<S>
All Implemented Interfaces:
Iterable<S>

public class ServiceLoader<S>
extends Object
implements Iterable<S>

This class handles looking up service providers on the class path. It implements the Service Provider section of the JAR File Specification. The Service Provider programmatic lookup was not specified prior to Java 6 so this interface allows use of the specification prior to Java 6. The API is copied from java.util.ServiceLoader

Author:
Pete Muir, Avalon Development Team, Nicklas Karlsson

Method Summary
 Iterator<S> iterator()
          Lazily loads the available providers of this loader's service.
static
<S> ServiceLoader<S>
load(Class<S> service)
          Creates a new service loader for the given service type, using the current thread's context class loader.
static
<S> ServiceLoader<S>
load(Class<S> service, ClassLoader loader)
          Creates a new service loader for the given service type and class loader.
static
<S> ServiceLoader<S>
loadInstalled(Class<S> service)
          Creates a new service loader for the given service type, using the extension class loader.
 void reload()
          Clear this loader's provider cache so that all providers will be reloaded.
 String toString()
          Returns a string describing this service.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

load

public static <S> ServiceLoader<S> load(Class<S> service)
Creates a new service loader for the given service type, using the current thread's context class loader. An invocation of this convenience method of the form ServiceLoader.load(service)</code> is equivalent to ServiceLoader.load(service, Thread.currentThread().getContextClassLoader())

Parameters:
service - The interface or abstract class representing the service
Returns:
A new service loader

load

public static <S> ServiceLoader<S> load(Class<S> service,
                                        ClassLoader loader)
Creates a new service loader for the given service type and class loader.

Parameters:
service - The interface or abstract class representing the service
loader - The class loader to be used to load provider-configuration files and provider classes, or null if the system class loader (or, failing that, the bootstrap class loader) is to be used
Returns:
A new service loader

loadInstalled

public static <S> ServiceLoader<S> loadInstalled(Class<S> service)
Creates a new service loader for the given service type, using the extension class loader. This convenience method simply locates the extension class loader, call it extClassLoader, and then returns ServiceLoader.load(service, extClassLoader) If the extension class loader cannot be found then the system class loader is used; if there is no system class loader then the bootstrap class loader is used. This method is intended for use when only installed providers are desired. The resulting service will only find and load providers that have been installed into the current Java virtual machine; providers on the application's class path will be ignored.

Parameters:
service - The interface or abstract class representing the service
Returns:
A new service loader

reload

public void reload()
Clear this loader's provider cache so that all providers will be reloaded. After invoking this method, subsequent invocations of the iterator method will lazily look up and instantiate providers from scratch, just as is done by a newly-created loader. This method is intended for use in situations in which new providers can be installed into a running Java virtual machine.


iterator

public Iterator<S> iterator()
Lazily loads the available providers of this loader's service. The iterator returned by this method first yields all of the elements of the provider cache, in instantiation order. It then lazily loads and instantiates any remaining providers, adding each one to the cache in turn. To achieve laziness the actual work of parsing the available provider-configuration files and instantiating providers must be done by the iterator itself. Its hasNext and next methods can therefore throw a ServiceConfigurationError if a provider-configuration file violates the specified format, or if it names a provider class that cannot be found and instantiated, or if the result of instantiating the class is not assignable to the service type, or if any other kind of exception or error is thrown as the next provider is located and instantiated. To write robust code it is only necessary to catch ServiceConfigurationError when using a service iterator. If such an error is thrown then subsequent invocations of the iterator will make a best effort to locate and instantiate the next available provider, but in general such recovery cannot be guaranteed. Design Note Throwing an error in these cases may seem extreme. The rationale for this behavior is that a malformed provider-configuration file, like a malformed class file, indicates a serious problem with the way the Java virtual machine is configured or is being used. As such it is preferable to throw an error rather than try to recover or, even worse, fail silently. The iterator returned by this method does not support removal. Invoking its remove method will cause an UnsupportedOperationException to be thrown.

Specified by:
iterator in interface Iterable<S>
Returns:
An iterator that lazily loads providers for this loader's service

toString

public String toString()
Returns a string describing this service.

Overrides:
toString in class Object
Returns:
A descriptive string


Copyright © 2008-2010 Seam Framework. All Rights Reserved.