// ServiceLoader实现了Iterable接口,可以遍历所有的服务实现者 public final classServiceLoader<S> implements Iterable<S> {
// 约定的配置文件的存放目录 private static final StringPREFIX = "META-INF/services/";
// The class or interface representing the service being loaded private final Class<S> service;
// The class loader used to locate, load, and instantiate providers private final ClassLoader loader;
// The access control context taken when the ServiceLoader is created private final AccessControlContext acc;
// Cached providers, in instantiation order private LinkedHashMap<String,S> providers = newLinkedHashMap<>();
// The current lazy-lookup iterator private LazyIterator lookupIterator;
/** * Clear this loader's provider cache so that all providers will be * reloaded. * * <p> After invoking this method, subsequent invocations of the {@link * #iterator() iterator} method will lazily look up and instantiate * providers from scratch, just as is done by a newly-created loader. * * <p> This method is intended for use in situations in which new providers * can be installed into a running Java virtual machine. */ public voidreload() { providers.clear(); lookupIterator = newLazyIterator(service, loader); }
private staticvoidfail(Class<?> service, URL u, int line, String msg) throws ServiceConfigurationError { fail(service, u + ":" + line + ": " + msg); }
// Parse a single line from the given configuration file, adding the name // on the line to the names list. // private int parseLine(Class<?> service, URL u, BufferedReader r, int lc, List<String> names) throws IOException, ServiceConfigurationError { String ln = r.readLine(); if (ln == null) { return -1; } int ci = ln.indexOf('#'); if (ci >= 0) ln = ln.substring(0, ci); ln = ln.trim(); int n = ln.length(); if (n != 0) { if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) fail(service, u, lc, "Illegal configuration-file syntax"); int cp = ln.codePointAt(0); if (!Character.isJavaIdentifierStart(cp)) fail(service, u, lc, "Illegal provider-class name: " + ln); for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) { cp = ln.codePointAt(i); if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) fail(service, u, lc, "Illegal provider-class name: " + ln); } if (!providers.containsKey(ln) && !names.contains(ln)) names.add(ln); } return lc + 1; }
// Parse the content of the given URL as a provider-configuration file. // // @param service // The service type for which providers are being sought; // used to construct error detail strings // // @param u // The URL naming the configuration file to be parsed // // @return A (possibly empty) iterator that will yield the provider-class // names in the given configuration file that are not yet members // of the returned set // // @throws ServiceConfigurationError // If an I/O error occurs while reading from the given URL, or // if a configuration-file format error is detected // private Iterator<String> parse(Class<?> service, URL u) throws ServiceConfigurationError { InputStreamin = null; BufferedReader r = null; ArrayList<String> names = newArrayList<>(); try { in = u.openStream(); r = newBufferedReader(newInputStreamReader(in, "utf-8")); int lc = 1; while ((lc = parseLine(service, u, r, lc, names)) >= 0); } catch (IOException x) { fail(service, "Error reading configuration file", x); } finally { try { if (r != null) r.close(); if (in != null) in.close(); } catch (IOException y) { fail(service, "Error closing configuration file", y); } } return names.iterator(); }
private S nextService() { if (!hasNextService()) thrownewNoSuchElementException(); String cn = nextName; nextName = null; Class<?> c = null; try { c = Class.forName(cn, false, loader); } catch (ClassNotFoundException x) { fail(service, "Provider " + cn + " not found"); } if (!service.isAssignableFrom(c)) { fail(service, "Provider " + cn + " not a subtype"); } try { // 用反射机制,创建接口实现对象 S p = service.cast(c.newInstance()); // 放进 ServiceLoader的providers容器里面 providers.put(cn, p); return p; } catch (Throwable x) { fail(service, "Provider " + cn + " could not be instantiated", x); } thrownewError(); // This cannot happen }
public boolean hasNext() { if (acc == null) { returnhasNextService(); } else { PrivilegedAction<Boolean> action = newPrivilegedAction<Boolean>() { public Booleanrun() { returnhasNextService(); } }; returnAccessController.doPrivileged(action, acc); } }
public S next() { if (acc == null) { returnnextService(); } else { PrivilegedAction<S> action = newPrivilegedAction<S>() { public S run() { returnnextService(); } }; returnAccessController.doPrivileged(action, acc); } }
public voidremove() { thrownewUnsupportedOperationException(); }
}
/** * Lazily loads the available providers of this loader's service. * * <p> The iterator returned by this method first yields all of the * elements of the provider cache, in instantiation order. It then lazily * loads and instantiates any remaining providers, adding each one to the * cache in turn. * * <p> To achieve laziness the actual work of parsing the available * provider-configuration files and instantiating providers must be done by * the iterator itself. Its {@link java.util.Iterator#hasNext hasNext} and * {@link java.util.Iterator#next next} methods can therefore throw a * {@link ServiceConfigurationError} if a provider-configuration file * violates the specified format, or if it names a provider class that * cannot be found and instantiated, or if the result of instantiating the * class is not assignable to the service type, or if any other kind of * exception or error is thrown as the next provider is located and * instantiated. To write robust code it is only necessary to catch {@link * ServiceConfigurationError} when using a service iterator. * * <p> If such an error is thrown then subsequent invocations of the * iterator will make a best effort to locate and instantiate the next * available provider, but in general such recovery cannot be guaranteed. * * <blockquote style="font-size: smaller; line-height: 1.2"><span * style="padding-right: 1em; font-weight: bold">Design Note</span> * Throwing an error in these cases may seem extreme. The rationale for * this behavior is that a malformed provider-configuration file, like a * malformed class file, indicates a serious problem with the way the Java * virtual machine is configured or is being used. As such it is * preferable to throw an error rather than try to recover or, even worse, * fail silently.</blockquote> * * <p> The iterator returned by this method does not support removal. * Invoking its {@link java.util.Iterator#remove() remove} method will * cause an {@link UnsupportedOperationException} to be thrown. * * @implNote When adding providers to the cache, the {@link #iterator * Iterator} processes resources in the order that the {@link * java.lang.ClassLoader#getResources(java.lang.String) * ClassLoader.getResources(String)} method finds the service configuration * files. * * @return An iterator that lazily loads providers for this loader's * service */ public Iterator<S> iterator() { returnnewIterator<S>() {
public boolean hasNext() { if (knownProviders.hasNext()) returntrue; return lookupIterator.hasNext(); }
public S next() { if (knownProviders.hasNext()) return knownProviders.next().getValue(); return lookupIterator.next(); }
public voidremove() { thrownewUnsupportedOperationException(); }
}; }
/** * Creates a new service loader for the given service type and class * loader. * * @param <S> the class of the service type * * @paramservice * The interface or abstract class representing the service * * @paramloader * The class loader to be used to load provider-configuration files * and provider classes, or <tt>null</tt> if the system class * loader (or, failing that, the bootstrap class loader) is to be * used * * @return A new service loader */ public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader) { returnnewServiceLoader<>(service, loader); }
/** * Creates a new service loader for the given service type, using the * current thread's {@linkplain java.lang.Thread#getContextClassLoader * context class loader}. * * <p> An invocation of this convenience method of the form * * <blockquote><pre> * ServiceLoader.load(<i>service</i>)</pre></blockquote> * * is equivalent to * * <blockquote><pre> * ServiceLoader.load(<i>service</i>, * Thread.currentThread().getContextClassLoader())</pre></blockquote> * * @param <S> the class of the service type * * @paramservice * The interface or abstract class representing the service * * @return A new service loader */ public static <S> ServiceLoader<S> load(Class<S> service) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); returnServiceLoader.load(service, cl); }
/** * Creates a new service loader for the given service type, using the * extension class loader. * * <p> This convenience method simply locates the extension class loader, * call it <tt><i>extClassLoader</i></tt>, and then returns * * <blockquote><pre> * ServiceLoader.load(<i>service</i>, <i>extClassLoader</i>)</pre></blockquote> * * <p> If the extension class loader cannot be found then the system class * loader is used; if there is no system class loader then the bootstrap * class loader is used. * * <p> This method is intended for use when only installed providers are * desired. The resulting service will only find and load providers that * have been installed into the current Java virtual machine; providers on * the application's class path will be ignored. * * @param <S> the class of the service type * * @paramservice * The interface or abstract class representing the service * * @return A new service loader */ public static <S> ServiceLoader<S> loadInstalled(Class<S> service) { ClassLoader cl = ClassLoader.getSystemClassLoader(); ClassLoader prev = null; while (cl != null) { prev = cl; cl = cl.getParent(); } returnServiceLoader.load(service, prev); }
/** * Returns a string describing this service. * * @return A descriptive string */ public StringtoString() { return"java.util.ServiceLoader[" + service.getName() + "]"; }