In this lookup mechanism a service is represented by an interface or an abstract class. (A concrete class may be used, but this is not recommended.) A provider of a given service contains one or more concrete classes that extend this service class with data and code specific to the provider. This provider class will typically not be the entire provider itself but rather a proxy that contains enough information to decide whether the provider is able to satisfy a particular request together with code that can create the actual provider on demand. The details of provider classes tend to be highly service-specific; no single class or interface could possibly unify them, so no such class has been defined. The only requirement enforced here is that provider classes must have a zero-argument constructor so that they may be instantiated during lookup.
A service provider identifies itself by placing a provider-configuration file in the resource directory META-INF/services. The file's name should consist of the fully-qualified name of the abstract service class. The file should contain a list of fully-qualified concrete provider-class names, one per line. Space and tab characters surrounding each name, as well as blank lines, are ignored. The comment character is '#' (0x23); on each line all characters following the first comment character are ignored. The file must be encoded in UTF-8.
If a particular concrete provider class is named in more than one configuration file, or is named in the same configuration file more than once, then the duplicates will be ignored. The configuration file naming a particular provider need not be in the same jar file or other distribution unit as the provider itself. The provider must be accessible from the same class loader that was initially queried to locate the configuration file; note that this is not necessarily the class loader that found the file.
Example: Suppose we have a service class named java.io.spi.CharCodec. It has two abstract methods:
public abstract CharEncoder getEncoder(String encodingName); public abstract CharDecoder getDecoder(String encodingName);
Each method returns an appropriate object or null if it cannot translate the given encoding. Typical CharCodec providers will support more than one encoding.
If glassfish.io.StandardCodec is a provider of the CharCodec service then its jar file would contain the file META-INF/services/java.io.spi.CharCodec. This file would contain the single line:
glassfish.io.StandardCodec # Standard codecs for the platform
To locate an codec for a given encoding name, the internal I/O code would do something like this:
CharEncoder getEncoder(String encodingName) {
for (CharCodec cc : ServiceFinder.find(CharCodec.class)) {
CharEncoder ce = cc.getEncoder(encodingName);
if (ce != null)
return ce;
}
return null;
}
The provider-lookup mechanism always executes in the security context of the caller. Trusted system code should typically invoke the methods in this class from within a privileged security context.
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final ClassLoader | |
| private static final String | |
| private final Class |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static void | |
| private static void | |
| private static void | |
| public static <T> ServiceFinder | find(Class<T>
The service's abstract service class service, ClassLoader The class loader to be used to load provider-configuration files and instantiate provider classes, or
null if the system class loader (or, failing that the bootstrap class loader) is to be used loader)Locates and incrementally instantiates the available providers of a given service using the given class loader. |
| public static <T> ServiceFinder | |
| public Iterator | Returns: An Iterator that yields provider objects for the given service, in some arbitrary order. The iterator will throw a ServiceConfigurationError if a provider-configuration file violates the specified format or if a provider class cannot be found and instantiated.Implements java. Returns discovered objects incrementally. |
| private static Iterator | Returns: A (possibly empty) Iterator that will yield the provider-class names in the given configuration file that are not yet members of the returned setThe service class for which providers are being sought; used to construct error detail strings service, URL The URL naming the configuration file to be parsed u, Set<String> A Set containing the names of provider classes that have already been returned. This set will be
updated to contain the names that will be yielded from the returned Iterator. returned)Parse the content of the given URL as a provider-configuration file. |
| private static int | |
| public T[] |
| classLoader | back to summary |
|---|---|
| private final ClassLoader classLoader | |
| prefix | back to summary |
|---|---|
| private static final String prefix | |
| serviceClass | back to summary |
|---|---|
| private final Class<T> serviceClass | |
| ServiceFinder | back to summary |
|---|---|
| private ServiceFinder(Class<T> service, ClassLoader loader) | |
| fail | back to summary |
|---|---|
| private static void fail(Class<T> service, String msg, Throwable cause) throws ServiceConfigurationError | |
| fail | back to summary |
|---|---|
| private static void fail(Class<T> service, String msg) throws ServiceConfigurationError | |
| fail | back to summary |
|---|---|
| private static void fail(Class<T> service, URL u, int line, String msg) throws ServiceConfigurationError | |
| find | back to summary |
|---|---|
| public static <T> ServiceFinder Locates and incrementally instantiates the available providers of a given service using the given class loader.
This method transforms the name of the given service class into a provider-configuration filename as described above and then uses the getResources method of the given class loader to find all available files with that name. These files are then read and parsed to produce a list of provider-class names. The iterator that is returned uses the given class loader to lookup and then instantiate each element of the list.
Because it is possible for extensions to be installed into a running Java virtual machine, this method may return different results each time it is invoked.
| |
| find | back to summary |
|---|---|
| public static <T> ServiceFinder Locates and incrementally instantiates the available providers of a given service using the context class loader. This convenience method is equivalent to
ClassLoader cl = Thread.currentThread().getContextClassLoader(); return Service.providers(service, cl);
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements java. Returns discovered objects incrementally.
| |
| parse | back to summary |
|---|---|
| private static Iterator Parse the content of the given URL as a provider-configuration file.
| |
| parseLine | back to summary |
|---|---|
| private static int parseLine(Class<T> service, URL u, BufferedReader r, int lc, List<String> names, Set<String> returned) throws IOException, ServiceConfigurationError Parse a single line from the given configuration file, adding the name on the line to both the names list and the returned set iff the name is not already a member of the returned set. | |
| toArray | back to summary |
|---|---|
| public T[] toArray() Returns discovered objects all at once.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv Enumeration | |
| pack-priv URL | |
| pack-priv final ClassLoader | |
| pack-priv String | |
| pack-priv Iterator | |
| pack-priv final Set | |
| pack-priv final Class |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public T | |
| public void | remove()
Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
| configs | back to summary |
|---|---|
| pack-priv Enumeration<URL> configs | |
| currentConfig | back to summary |
|---|---|
| pack-priv URL currentConfig | |
| loader | back to summary |
|---|---|
| pack-priv final ClassLoader loader | |
| nextName | back to summary |
|---|---|
| pack-priv String nextName | |
| pending | back to summary |
|---|---|
| pack-priv Iterator<String> pending | |
| returned | back to summary |
|---|---|
| pack-priv final Set<String> returned | |
| service | back to summary |
|---|---|
| pack-priv final Class<T> service | |
| LazyIterator | back to summary |
|---|---|
| private LazyIterator(Class<T> service, ClassLoader loader) | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() throws ServiceConfigurationError Implements java. Doc from java. Returns
| |
| next | back to summary |
|---|---|
| public T next() throws ServiceConfigurationError Implements java. Doc from java. Returns the next element in the iteration.
| |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the
| |