1 *java.util.ServiceLoader* *ServiceLoader* A simple service-provider loading faci
3 public final class ServiceLoader<S>
4 extends |java.lang.Object|
5 implements |java.lang.Iterable|
7 |java.util.ServiceLoader_Description|
8 |java.util.ServiceLoader_Fields|
9 |java.util.ServiceLoader_Constructors|
10 |java.util.ServiceLoader_Methods|
12 ================================================================================
14 *java.util.ServiceLoader_Methods*
15 |java.util.ServiceLoader.iterator()|Lazily loads the available providers of thi
16 |java.util.ServiceLoader.load(Class<S>)|Creates a new service loader for the gi
17 |java.util.ServiceLoader.load(Class<S>,ClassLoader)|Creates a new service loade
18 |java.util.ServiceLoader.loadInstalled(Class<S>)|Creates a new service loader f
19 |java.util.ServiceLoader.reload()|Clear this loader's provider cache so that al
20 |java.util.ServiceLoader.toString()|Returns a string describing this service.
22 *java.util.ServiceLoader_Description*
24 A simple service-provider loading facility.
26 A service is a well-known set of interfaces and (usually abstract) classes. A
27 service provider is a specific implementation of a service. The classes in a
28 provider typically implement the interfaces and subclass the classes defined in
29 the service itself. Service providers can be installed in an implementation of
30 the Java platform in the form of extensions, that is, jar files placed into any
31 of the usual extension directories. Providers can also be made available by
32 adding them to the application's class path or by some other platform-specific
35 For the purpose of loading, a service is represented by a single type, that is,
36 a single interface or abstract class. (A concrete class can be used, but this
37 is not recommended.) A provider of a given service contains one or more
38 concrete classes that extend this service type with data and code specific to
39 the provider. The provider class is typically not the entire provider itself
40 but rather a proxy which contains enough information to decide whether the
41 provider is able to satisfy a particular request together with code that can
42 create the actual provider on demand. The details of provider classes tend to
43 be highly service-specific; no single class or interface could possibly unify
44 them, so no such type is defined here. The only requirement enforced by this
45 facility is that provider classes must have a zero-argument constructor so that
46 they can be instantiated during loading.
48 A service provider is identified by placing a provider-configuration file in
49 the resource directory META-INF/services. The file's name is the
50 fully-qualified binary name of the service's type. The file contains a list of
51 fully-qualified binary names of concrete provider classes, one per line. Space
52 and tab characters surrounding each name, as well as blank lines, are ignored.
53 The comment character is '#' ('u0023', NUMBER SIGN); on each line all
54 characters following the first comment character are ignored. The file must be
57 If a particular concrete provider class is named in more than one configuration
58 file, or is named in the same configuration file more than once, then the
59 duplicates are ignored. The configuration file naming a particular provider
60 need not be in the same jar file or other distribution unit as the provider
61 itself. The provider must be accessible from the same class loader that was
62 initially queried to locate the configuration file; note that this is not
63 necessarily the class loader from which the file was actually loaded.
65 Providers are located and instantiated lazily, that is, on demand. A service
66 loader maintains a cache of the providers that have been loaded so far. Each
67 invocation of the iterator(|java.util.ServiceLoader|) method returns an
68 iterator that first yields all of the elements of the cache, in instantiation
69 order, and then lazily locates and instantiates any remaining providers, adding
70 each one to the cache in turn. The cache can be cleared via the
71 reload(|java.util.ServiceLoader|) method.
73 Service loaders always execute in the security context of the caller. Trusted
74 system code should typically invoke the methods in this class, and the methods
75 of the iterators which they return, from within a privileged security context.
77 Instances of this class are not safe for use by multiple concurrent threads.
79 Unless otherwise specified, passing a null argument to any method in this class
80 will cause a (|java.lang.NullPointerException|) to be thrown.
82 Example Suppose we have a service type com.example.CodecSet which is intended
83 to represent sets of encoder/decoder pairs for some protocol. In this case it
84 is an abstract class with two abstract methods:
88 public abstract Encoder getEncoder(String encodingName); public abstract
89 Decoder getDecoder(String encodingName);
91 Each method returns an appropriate object or null if the provider does not
92 support the given encoding. Typical providers support more than one encoding.
94 If com.example.impl.StandardCodecs is an implementation of the CodecSet service
95 then its jar file also contains a file named
99 META-INF/services/com.example.CodecSet
101 This file contains the single line:
105 com.example.impl.StandardCodecs # Standard codecs
107 The CodecSet class creates and saves a single service instance at
112 private static ServiceLoader<CodecSet> codecSetLoader =
113 ServiceLoader.load(CodecSet.class);
115 To locate an encoder for a given encoding name it defines a static factory
116 method which iterates through the known and available providers, returning only
117 when it has located a suitable encoder or has run out of providers.
121 public static Encoder getEncoder(String encodingName) { for (CodecSet cp :
122 codecSetLoader) { Encoder enc = cp.getEncoder(encodingName); if (enc != null)
123 return enc; } return null; }
125 A getDecoder method is defined similarly.
127 Usage Note If the class path of a class loader that is used for provider
128 loading includes remote network URLs then those URLs will be dereferenced in
129 the process of searching for provider-configuration files.
131 This activity is normal, although it may cause puzzling entries to be created
132 in web-server logs. If a web server is not configured correctly, however, then
133 this activity may cause the provider-loading algorithm to fail spuriously.
135 A web server should return an HTTP 404 (Not Found) response when a requested
136 resource does not exist. Sometimes, however, web servers are erroneously
137 configured to return an HTTP 200 (OK) response along with a helpful HTML error
138 page in such cases. This will cause a (|java.util.ServiceConfigurationError|)
139 to be thrown when this class attempts to parse the HTML page as a
140 provider-configuration file. The best solution to this problem is to fix the
141 misconfigured web server to return the correct response code (HTTP 404) along
142 with the HTML error page.
146 *java.util.ServiceLoader.iterator()*
148 public |java.util.Iterator|<S> iterator()
150 Lazily loads the available providers of this loader's service.
152 The iterator returned by this method first yields all of the elements of the
153 provider cache, in instantiation order. It then lazily loads and instantiates
154 any remaining providers, adding each one to the cache in turn.
156 To achieve laziness the actual work of parsing the available
157 provider-configuration files and instantiating providers must be done by the
158 iterator itself. Its hasNext(|java.util.Iterator|) and
159 next(|java.util.Iterator|) methods can therefore throw a
160 (|java.util.ServiceConfigurationError|) if a provider-configuration file
161 violates the specified format, or if it names a provider class that cannot be
162 found and instantiated, or if the result of instantiating the class is not
163 assignable to the service type, or if any other kind of exception or error is
164 thrown as the next provider is located and instantiated. To write robust code
165 it is only necessary to catch (|java.util.ServiceConfigurationError|) when
166 using a service iterator.
168 If such an error is thrown then subsequent invocations of the iterator will
169 make a best effort to locate and instantiate the next available provider, but
170 in general such recovery cannot be guaranteed.
172 Design Note Throwing an error in these cases may seem extreme. The rationale
173 for this behavior is that a malformed provider-configuration file, like a
174 malformed class file, indicates a serious problem with the way the Java virtual
175 machine is configured or is being used. As such it is preferable to throw an
176 error rather than try to recover or, even worse, fail silently.
178 The iterator returned by this method does not support removal. Invoking its
179 remove(|java.util.Iterator|) method will cause an
180 (|java.lang.UnsupportedOperationException|) to be thrown.
184 Returns: An iterator that lazily loads providers for this loader's service
186 *java.util.ServiceLoader.load(Class<S>)*
188 public static |java.util.ServiceLoader|<S> load(java.lang.Class<S> service)
190 Creates a new service loader for the given service type, using the current
191 thread's context class loader(|java.lang.Thread|) .
193 An invocation of this convenience method of the form
197 ServiceLoader.load(service)
203 ServiceLoader.load(service, Thread.currentThread().getContextClassLoader())
206 service - The interface or abstract class representing the service
208 Returns: A new service loader
210 *java.util.ServiceLoader.load(Class<S>,ClassLoader)*
212 public static |java.util.ServiceLoader|<S> load(
213 java.lang.Class<S> service,
214 java.lang.ClassLoader loader)
216 Creates a new service loader for the given service type and class loader.
219 service - The interface or abstract class representing the service
220 loader - The class loader to be used to load provider-configuration files and provider
221 classes, or null if the system class loader (or, failing that, the
222 bootstrap class loader) is to be used
224 Returns: A new service loader
226 *java.util.ServiceLoader.loadInstalled(Class<S>)*
228 public static |java.util.ServiceLoader|<S> loadInstalled(java.lang.Class<S> service)
230 Creates a new service loader for the given service type, using the extension
233 This convenience method simply locates the extension class loader, call it
234 extClassLoader, and then returns
238 ServiceLoader.load(service, extClassLoader)
240 If the extension class loader cannot be found then the system class loader is
241 used; if there is no system class loader then the bootstrap class loader is
244 This method is intended for use when only installed providers are desired. The
245 resulting service will only find and load providers that have been installed
246 into the current Java virtual machine; providers on the application's class
247 path will be ignored.
250 service - The interface or abstract class representing the service
252 Returns: A new service loader
254 *java.util.ServiceLoader.reload()*
258 Clear this loader's provider cache so that all providers will be reloaded.
260 After invoking this method, subsequent invocations of the
261 iterator(|java.util.ServiceLoader|) method will lazily look up and instantiate
262 providers from scratch, just as is done by a newly-created loader.
264 This method is intended for use in situations in which new providers can be
265 installed into a running Java virtual machine.
269 *java.util.ServiceLoader.toString()*
271 public |java.lang.String| toString()
273 Returns a string describing this service.
277 Returns: A descriptive string