Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
java.util.ServiceLoader<S>
In terms of loading a service, the service is defined by a single interface or abstract class which the provider implements. This may not constitute the entire service, but is simply a mechanism by which a provider of the service can be loaded and its capabilities determined. The variety of possible services means that no more requirements are made of the service provider other than that it must have an accessible zero argument constructor in order to allow an instance to be created.
Service providers are listed in a file named after the
service type in the directory META-INF/services
.
The file contains a list of classes, and must be encoded
using UTF-8. Whitespace is ignored. Comments can be
included by using a '#'
prefix; anything occurring
on the same line after this symbol is ignored. Duplicate classes
are ignored.
The classes are loaded using the same classloader that was queried in order to locate the configuration file. As a result, the providers do not need to reside in the same JAR file as the resource; they merely have to be accessible to this classloader, which may differ from the one that loaded the file itself.
Providers are located and instantiated lazily, as calls to the
iterator()
are made. Providers are cached, and those in
the cache are returned first. The cache may be cleared by calling
reload()
. Service loaders always execute in the security
context of the caller, so ideally calls should be made from a trusted
source.
Note that this class is not thread-safe, and that strange errors may occur as the result of the use of remote URLs occurring on the classpath, which lead to erroneous web pages.
Method Summary | |
static |
|
static |
|
static |
|
Iterator |
|
void |
|
String |
|
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
public staticServiceLoaderload(Classservice)
Creates a new service loader for the given service, using the context class loader of the current thread. This is equivalent to callingServiceLoader.load(service, Thread.currentThread().getContextClassLoader())
.
Parameters:service
- the interface or abstract class that represents the service.
Returns:a newServiceLoader
instance.
public staticServiceLoaderload(Classservice, ClassLoader loader)
Creates a new service loader for the given service, using the specified class loader. The class loader is used to access the configuration file and the service provider instances themselves. If the loader isnull
, the system class loader (or, if this is alsonull
, the bootstrap class loader).
Parameters:service
- the interface or abstract class that represents the service.loader
- the class loader used to load the configuration file and service providers.
Returns:a newServiceLoader
instance.
public staticServiceLoaderloadInstalled(Classservice)
Creates a new service loader for the given service, using the extension class loader. If the extension class loader can not be found, the system class loader is used (or, if this isnull
, the bootstrap class loader). The primary use of this method is to only obtain installed services, ignoring any which may appear on the classpath. This is equivalent to callingload(service, extClassLoader)
whereextClassLoader
is the extension class loader (ornull
if this is unavailable).
Parameters:service
- the interface or abstract class that represents the service.
Returns:a newServiceLoader
instance.
public Iteratoriterator()
Lazily loads the available providers. The iterator first returns providers from the cache, in instantiation order, followed by any remaining providers, which are added to the cache after loading. The actual loading and parsing of the configuration file takes place in theIterator.hasNext()
andIterator.next()
methods, which means that they may result in aServiceConfigurationError
being thrown. If such an error does occur, subsequent invocations will attempt to recover. Theremove()
method is not supported and instead throws anUnsupportedOperationException
.
Specified by:iterator in interface Iterable<E>
Returns:an iterator that lazily loads service providers.
public void reload()
Clears the cache of the provider, so that all providers are again read from the configuration file and instantiated.