Interface ServiceFinder

All Known Implementing Classes:
DefaultServiceFinder

public interface ServiceFinder
Finds service configurations.

Very similar to ServiceLoader but provides additional methods to load configurations without being forced to load the classes.
Furthermore, it is not restricted to META-INF/services and the contents of the configuration file. Supports dedicated classloaders for services as well.

The configuration files are read in the order determined by the ModuleSorter: named modules according to their dependencies (the topmost module first), followed by the classpath and automatic modules (unless the service is annotated with ClasspathFirst). Throughout this interface, this is referred to as the "module order".

Applications should use the ServiceFactory to allow replacement of the finder (see Service-annotation).

Author:
harald
  • Method Details

    • getClassLoader

      ClassLoader getClassLoader()
      Gets the default classloader used by this finder.
      Returns:
      the classloader, never null
    • getModuleLayer

      default ModuleLayer getModuleLayer()
      Gets the module layer this finder looks up services in, if any.
      A non-null layer means getClassLoader() is the layer's own classloader (see ModuleLayerBuilder), and module ordering for this finder's service lookups is based on that layer's own module graph rather than the one visible from the context classloader.
      Returns:
      the module layer, null if this finder is not layer-based
    • getServicePath

      String getServicePath()
      Gets the finder's service path it is responsible for.
      The path is normalized: it never starts with a slash and always ends with one, for example "META-INF/services/".
      Returns:
      the service path
    • findFirstServiceConfiguration

      Map.Entry<String,URL> findFirstServiceConfiguration(String serviceName)
      Finds the first service configuration by service name.
      If similar configurations appear more than once, the first in module order is returned. Useful to override service default implementations.
      Parameters:
      serviceName - the service name, usually a classname
      Returns:
      the service configuration entry, never null
      Throws:
      TentackleRuntimeException - if no such service found
    • findFirstServiceProvider

      <T> Class<T> findFirstServiceProvider(Class<T> service) throws ClassNotFoundException
      Finds the first service provider by service class.
      If similar configurations appear more than once, the first in module order is returned. Useful to override service default implementations.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      Returns:
      the provider class
      Throws:
      ClassNotFoundException - if some provider could not be loaded
      TentackleRuntimeException - if no such service found or the provider does not implement the service
    • findServiceConfigurations

      Map<String,URL> findServiceConfigurations(String serviceName)
      Finds service configurations by service name.
      Iterations over the returned map are ordered by discovery in module order. The keys are the configuration lines, the values the URLs of the files they were found in. The same configuration line in two different files is considered an error, since it usually indicates a misconfiguration.
      Parameters:
      serviceName - the service name, usually a classname
      Returns:
      a map of the configured services and their corresponding URLs
      Throws:
      TentackleRuntimeException - if a configuration could not be read or is defined more than once
    • findServiceProviders

      <T> Collection<Class<T>> findServiceProviders(Class<T> service) throws ClassNotFoundException
      Finds the service providers by service class.
      The providers are ordered by discovery in module order.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      Returns:
      the classes providing this service
      Throws:
      ClassNotFoundException - if some provider could not be loaded
      TentackleRuntimeException - if some provider does not implement the service
    • findServiceURLs

      Collection<URL> findServiceURLs(String serviceName)
      Finds URLs of a service by name.
      The URLs are sorted in module order.
      Parameters:
      serviceName - the service name, usually a classname
      Returns:
      the service's resource URLs
      Throws:
      TentackleRuntimeException - if the resources could not be determined
    • findUniqueServiceConfiguration

      Map.Entry<String,URL> findUniqueServiceConfiguration(String serviceName)
      Finds a unique service configuration by service name.
      It is checked that the service is defined exactly once. Useful for factories, singletons and alike.
      Parameters:
      serviceName - the service name, usually a classname
      Returns:
      the service configuration entry
      Throws:
      TentackleRuntimeException - if no such service found or the service is defined more than once
    • findUniqueServiceProvider

      <T> Class<T> findUniqueServiceProvider(Class<T> service) throws ClassNotFoundException
      Finds the unique service provider by service class.
      It is checked that the service is defined exactly once.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      Returns:
      the provider class
      Throws:
      ClassNotFoundException - if some provider could not be loaded
      TentackleRuntimeException - if no such service found, the service is defined more than once, or the provider does not implement the service
    • createNameMap

      Map<String,String> createNameMap(String serviceName)
      Creates a map of names to mapped services.
      The configurations are taken from Constants.MAPPED_SERVICE_PATH and have the form provider = name (or provider : name), as generated for MappedServices.

      The key of the map is the mapped name (the right side, unquoted if quoted). The value is the name of the provider (the left side) that comes first in module order.

      Parameters:
      serviceName - the service name, usually the classname of the mapped service
      Returns:
      the map
      Throws:
      TentackleRuntimeException - if some configuration is malformed
    • createNameMapToAll

      Map<String, Set<String>> createNameMapToAll(String serviceName)
      Creates a map of names to all mapped services.
      Same as createNameMap(String), but maps each name to all of its providers.

      The key of the map is the mapped name (the right side). The value is the unordered set of names of all providers (the left side).

      Parameters:
      serviceName - the service name, usually the classname of the mapped service
      Returns:
      the map
      Throws:
      TentackleRuntimeException - if some configuration is malformed