Class ServiceLoader<T>

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

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

    • load

      public static <T> ServiceLoader<T> load(Class<T> service, 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:
    • load

      public static <T> ServiceLoader<T> load(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:
    • loadInstalled

      public static <T> ServiceLoader<T> loadInstalled(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:
    • getService

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

      public void reload()
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface 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:
      ServiceConfigurationError - on errors getting the first provider
    • getProvider

      public T getProvider(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:
      ServiceConfigurationError - on errors getting the provider with the specified class name
    • getAllProviders

      public 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:
      ServiceConfigurationError - on errors getting the providers
    • toString

      public String toString()
      Overrides:
      toString in class Object