Class ServiceLoader<T>

  • Type Parameters:
    T - type of the service interface to be loaded by this ServiceLoader
    All Implemented Interfaces:
    java.lang.Iterable<T>

    public final class ServiceLoader<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    A simple extension of a ServiceLoader. This class provides the same methods and some additional functionality.
    Author:
    agz
    See Also:
    ServiceLoader
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Collection<T> getAllProviders()
      Loads all providers the same way as a call to iterator() would do.
      T getFirstProvider()
      Loads and returns the first provider the same way as a call to iterator() would do.
      T getProvider​(java.lang.String className)
      Returns the provider with the specified class name.
      java.lang.Class<T> getService()
      Returns the service interface of this loader.
      java.util.Iterator<T> iterator()
      See ServiceLoader.iterator()
      static <T> ServiceLoader<T> load​(java.lang.Class<T> service)
      Creates a new ServiceLoader for the specified service interface, using the current thread's context class loader.
      static <T> ServiceLoader<T> load​(java.lang.Class<T> service, java.lang.ClassLoader loader)
      Creates a new ServiceLoader for the specified service interface and class loader.
      static <T> ServiceLoader<T> loadInstalled​(java.lang.Class<T> service)
      Creates a new ServiceLoader for the specified service type, using the extension class loader.
      void reload()
      See ServiceLoader.reload().
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Method Detail

      • load

        public static <T> ServiceLoader<T> load​(java.lang.Class<T> service,
                                                java.lang.ClassLoader loader)
        Creates a new ServiceLoader for the specified service interface and class loader.
        Type Parameters:
        T - type of the service interface to be loaded by this ServiceLoader
        Parameters:
        service - the interface or abstract class representing the service
        loader - the class loader to be used to load provider-configuration files and provider classes, or null if the system class loader (or, failing that, the bootstrap class loader) is to be used
        Returns:
        a new ServiceLoader
        See Also:
        ServiceLoader.load(Class, ClassLoader)
      • load

        public static <T> ServiceLoader<T> load​(java.lang.Class<T> service)
        Creates a new ServiceLoader for the specified service interface, using the current thread's context class loader.
        Type Parameters:
        T - type of the service interface to be loaded by this ServiceLoader
        Parameters:
        service - the interface or abstract class representing the service
        Returns:
        a new ServiceLoader
        See Also:
        ServiceLoader.load(Class)
      • loadInstalled

        public static <T> ServiceLoader<T> loadInstalled​(java.lang.Class<T> service)
        Creates a new ServiceLoader for the specified service type, using the extension class loader.

        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.

        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.

        Type Parameters:
        T - type of the service interface to be loaded by this ServiceLoader
        Parameters:
        service - the interface or abstract class representing the service
        Returns:
        a new ServiceLoader
        See Also:
        ServiceLoader.loadInstalled(Class)
      • getService

        public java.lang.Class<T> getService()
        Returns the service interface of this loader.
        Returns:
        the service interface of this loader
      • reload

        public void reload()
        See ServiceLoader.reload().
      • iterator

        public java.util.Iterator<T> iterator()
        See ServiceLoader.iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T>
      • getFirstProvider

        public T getFirstProvider()
        Loads and returns the first provider the same way as a call to iterator() would do.
        Returns:
        the first provider
        Throws:
        java.util.ServiceConfigurationError - on errors getting the first provider
      • getProvider

        public T getProvider​(java.lang.String className)
        Returns the provider with the specified class name.

        Throws an ServiceConfigurationError if no such provider is found.

        Loads providers the same way as a call to iterator() would do until it finds the provider with the specified name or until all providers are iterated through.

        Parameters:
        className - class name of the provider to find
        Returns:
        the provider with the specified class name
        Throws:
        java.util.ServiceConfigurationError - on errors getting the provider with the specified class name
      • getAllProviders

        public java.util.Collection<T> getAllProviders()
        Loads all providers the same way as a call to iterator() would do.

        Returns a Collection of these providers.

        Returns an empty Collection if no provider is configured.

        Returns:
        Collection of all providers
        Throws:
        java.util.ServiceConfigurationError - on errors getting the providers
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object