/// <summary> /// Creates a new service loader for the given service type, using the /// extension class loader. /// /// <para> 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> /// /// </para> /// <para> 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. /// /// </para> /// <para> 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. /// /// </para> /// </summary> /// @param <S> the class of the service type /// </param> /// <param name="service"> /// The interface or abstract class representing the service /// </param> /// <returns> A new service loader </returns> public static ServiceLoader <S> loadInstalled <S>(Class service) { ClassLoader cl = ClassLoader.SystemClassLoader; ClassLoader prev = null; while (cl != null) { prev = cl; cl = cl.Parent; } return(ServiceLoader.Load(service, prev)); }
/// <summary> /// Creates a new service loader for the given service type, using the /// current thread's {@link java.lang.Thread#getContextClassLoader /// context class loader}. /// /// <para> 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> /// /// </para> /// </summary> /// @param <S> the class of the service type /// </param> /// <param name="service"> /// The interface or abstract class representing the service /// </param> /// <returns> A new service loader </returns> public static ServiceLoader <S> load <S>(Class service) { ClassLoader cl = Thread.CurrentThread.ContextClassLoader; return(ServiceLoader.Load(service, cl)); }