📄 service.java
字号:
* If an I/O error occurs while reading from the given URL, or * if a configuration-file format error is detected */ private static Iterator parse(Class service, URL u, Set returned) throws ServiceConfigurationError { InputStream in = null; BufferedReader r = null; ArrayList names = new ArrayList(); try { in = u.openStream(); r = new BufferedReader(new InputStreamReader(in, "utf-8")); int lc = 1; while ((lc = parseLine(service, u, r, lc, names, returned)) >= 0); } catch (IOException x) { fail(service, ": " + x); } finally { try { if (r != null) r.close(); if (in != null) in.close(); } catch (IOException y) { fail(service, ": " + y); } } return names.iterator(); } /** * Private inner class implementing fully-lazy provider lookup */ private static class LazyIterator implements Iterator { Class service; ClassLoader loader; Enumeration configs = null; Iterator pending = null; Set returned = new TreeSet(); String nextName = null; private LazyIterator(Class service, ClassLoader loader) { this.service = service; this.loader = loader; } public boolean hasNext() throws ServiceConfigurationError { if (nextName != null) { return true; } if (configs == null) { try { String fullName = prefix + service.getName(); if (loader == null) configs = ClassLoader.getSystemResources(fullName); else configs = loader.getResources(fullName); } catch (IOException x) { fail(service, ": " + x); } } while ((pending == null) || !pending.hasNext()) { if (!configs.hasMoreElements()) { return false; } pending = parse(service, (URL) configs.nextElement(), returned); } nextName = (String) pending.next(); return true; } public Object next() throws ServiceConfigurationError { if (!hasNext()) { throw new NoSuchElementException(); } String cn = nextName; nextName = null; try { return Class.forName(cn, true, loader).newInstance(); } catch (ClassNotFoundException x) { fail(service, "Provider " + cn + " not found"); } catch (Exception x) { fail(service, "Provider " + cn + " could not be instantiated: " + x); } return null; /* This cannot happen */ } public void remove() { throw new UnsupportedOperationException(); } } /** * Locates and incrementally instantiates the available providers of a * given service using the given class loader. * * <p> This method transforms the name of the given service class into a * provider-configuration filename as described above and then uses the * <tt>getResources</tt> 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. * * <p> 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. <p> * * @param service * The service's abstract service class * * @param loader * The class loader to be used to load provider-configuration files * and instantiate provider classes, or <tt>null</tt> if the system * class loader (or, failing that the bootstrap class loader) is to * be used * * @return An <tt>Iterator</tt> that yields provider objects for the given * service, in some arbitrary order. The iterator will throw a * <tt>ServiceConfigurationError</tt> if a provider-configuration * file violates the specified format or if a provider class cannot * be found and instantiated. * * @throws ServiceConfigurationError * If a provider-configuration file violates the specified format * or names a provider class that cannot be found and instantiated * * @see #providers(java.lang.Class) * @see #installedProviders(java.lang.Class) */ public static Iterator providers(Class service, ClassLoader loader) throws ServiceConfigurationError { return new LazyIterator(service, loader); } /** * Locates and incrementally instantiates the available providers of a * given service using the context class loader. This convenience method * is equivalent to * * <pre> * ClassLoader cl = Thread.currentThread().getContextClassLoader(); * return Service.providers(service, cl); * </pre> * * @param service * The service's abstract service class * * @return An <tt>Iterator</tt> that yields provider objects for the given * service, in some arbitrary order. The iterator will throw a * <tt>ServiceConfigurationError</tt> if a provider-configuration * file violates the specified format or if a provider class cannot * be found and instantiated. * * @throws ServiceConfigurationError * If a provider-configuration file violates the specified format * or names a provider class that cannot be found and instantiated * * @see #providers(java.lang.Class, java.lang.ClassLoader) */ public static Iterator providers(Class service) throws ServiceConfigurationError { ClassLoader cl = Thread.currentThread().getContextClassLoader(); return Service.providers(service, cl); } /** * Locates and incrementally instantiates the available providers of a * given service using the extension class loader. This convenience method * simply locates the extension class loader, call it * <tt>extClassLoader</tt>, and then does * * <pre> * return Service.providers(service, extClassLoader); * </pre> * * 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. * * @param service * The service's abstract service class * * @return An <tt>Iterator</tt> that yields provider objects for the given * service, in some arbitrary order. The iterator will throw a * <tt>ServiceConfigurationError</tt> if a provider-configuration * file violates the specified format or if a provider class cannot * be found and instantiated. * * @throws ServiceConfigurationError * If a provider-configuration file violates the specified format * or names a provider class that cannot be found and instantiated * * @see #providers(java.lang.Class, java.lang.ClassLoader) */ public static Iterator installedProviders(Class service) throws ServiceConfigurationError { ClassLoader cl = ClassLoader.getSystemClassLoader(); ClassLoader prev = null; while (cl != null) { prev = cl; cl = cl.getParent(); } return Service.providers(service, prev); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -