java.util

Class ServiceLoader<S>

Implemented Interfaces:
Iterable<E>

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

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.

Since:
1.6

Method Summary

static
ServiceLoader load(Class service)
Creates a new service loader for the given service, using the context class loader of the current thread.
static
ServiceLoader load(Class service, ClassLoader loader)
Creates a new service loader for the given service, using the specified class loader.
static
ServiceLoader loadInstalled(Class service)
Creates a new service loader for the given service, using the extension class loader.
Iterator
iterator()
Lazily loads the available providers.
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, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Method Details

ServiceLoader load

public static  ServiceLoader load(Class service)
Creates a new service loader for the given service, using the context class loader of the current thread. This is equivalent to calling ServiceLoader.load(service, Thread.currentThread().getContextClassLoader()).
Parameters:
service - the interface or abstract class that represents the service.
Returns:
a new ServiceLoader instance.

ServiceLoader load

public static  ServiceLoader load(Class service,
                                        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 is null, the system class loader (or, if this is also null, 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 new ServiceLoader instance.

ServiceLoader loadInstalled

public static  ServiceLoader loadInstalled(Class service)
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 is 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).
Parameters:
service - the interface or abstract class that represents the service.
Returns:
a new ServiceLoader instance.

iterator

public Iterator iterator()
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 the 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.
Specified by:
iterator in interface Iterable<E>
Returns:
an iterator that lazily loads service providers.

reload

public void reload()
Clears the cache of the provider, so that all providers are again read from the configuration file and instantiated.

toString

public String toString()
Returns a textual representation of this ServiceLoader.
Overrides:
toString in interface Object
Returns:
a textual representation of the service loader.

ServiceLoader.java -- Allows loading of plug-in services. Copyright (C) 2006, 2007 Free Software Foundation This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.