|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.ServiceLoader<S>
public final class ServiceLoader<S>
Facilities for loading service providers. A service is defined by a set of interfaces or abstract classes, and a service provider gives a concrete implementation of this. Service providers may be installed as part of the runtime environment using JAR files in the extension directories, or may be simply supplied on the classpath.
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 | ||
---|---|---|
Iterator<S> |
iterator()
Lazily loads the available providers. |
|
static
|
load(Class<S> service)
Creates a new service loader for the given service, using the context class loader of the current thread. |
|
static
|
load(Class<S> service,
ClassLoader loader)
Creates a new service loader for the given service, using the specified class loader. |
|
static
|
loadInstalled(Class<S> service)
Creates a new service loader for the given service, using the extension class loader. |
|
void |
reload()
Clears the cache of the provider, so that all providers are again read from the configuration file and instantiated. |
|
String |
toString()
Returns a textual representation of this ServiceLoader . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Method Detail |
---|
public Iterator<S> iterator()
Iterator.hasNext()
and Iterator.next()
methods, which means that they may result in a
ServiceConfigurationError
being thrown. If such an error
does occur, subsequent invocations will attempt to recover.
The remove()
method is not supported and instead throws
an UnsupportedOperationException
.
iterator
in interface Iterable<S>
public static <S> ServiceLoader<S> load(Class<S> service)
ServiceLoader.load(service,
Thread.currentThread().getContextClassLoader())
.
service
- the interface or abstract class that represents
the service.
ServiceLoader
instance.public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader)
null
, the system class loader (or, if
this is also null
, the bootstrap class
loader).
service
- the interface or abstract class that represents
the service.loader
- the class loader used to load the configuration
file and service providers.
ServiceLoader
instance.public static <S> ServiceLoader<S> loadInstalled(Class<S> service)
null
, 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 calling
load(service, extClassLoader)
where
extClassLoader
is the extension class loader
(or null
if this is unavailable).
service
- the interface or abstract class that represents
the service.
ServiceLoader
instance.public void reload()
public String toString()
ServiceLoader
.
toString
in class Object
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |