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.

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

    Author:
    harald
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.util.Map<java.lang.String,​java.lang.String> createNameMap​(java.lang.String serviceName)
      Creates a map of names to mapped services.
      java.util.Map<java.lang.String,​java.util.Set<java.lang.String>> createNameMapToAll​(java.lang.String serviceName)
      Creates a map of names to all mapped services.
      java.util.Map.Entry<java.lang.String,​java.net.URL> findFirstServiceConfiguration​(java.lang.String serviceName)
      Finds the first service configurations by service name.
      If similar configurations appear more than once on the classpath, the first is returned.
      <T> java.lang.Class<T> findFirstServiceProvider​(java.lang.Class<T> service)
      Finds the first service provider by service name.
      If similar configurations appear more than once on the classpath, the first is returned.
      java.util.Map<java.lang.String,​java.net.URL> findServiceConfigurations​(java.lang.String serviceName)
      Finds service configurations by service name.
      Iterations over the returned map are ordered by discovery along the classpath.
      <T> java.util.Collection<java.lang.Class<T>> findServiceProviders​(java.lang.Class<T> service)
      Finds the service providers by service names.
      java.util.Collection<java.net.URL> findServiceURLs​(java.lang.String serviceName)
      Finds URLs of a service by name.
      java.util.Map.Entry<java.lang.String,​java.net.URL> findUniqueServiceConfiguration​(java.lang.String serviceName)
      Finds a unique service configurations by service name.
      It is checked that the service is defined exactly once.
      <T> java.lang.Class<T> findUniqueServiceProvider​(java.lang.Class<T> service)
      Finds the unique service provider by service name.
      java.lang.ClassLoader getClassLoader()
      Gets the default classloader used by this finder.
      java.lang.String getServicePath()
      Gets the finder' service path it is responsible for.
    • Method Detail

      • getClassLoader

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

        java.lang.String getServicePath()
        Gets the finder' service path it is responsible for.
        Returns:
        the service path
      • findFirstServiceConfiguration

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

        <T> java.lang.Class<T> findFirstServiceProvider​(java.lang.Class<T> service)
                                                 throws java.lang.ClassNotFoundException
        Finds the first service provider by service name.
        If similar configurations appear more than once on the classpath, the first is returned. Useful to override service default implementations.
        Type Parameters:
        T - the service type
        Parameters:
        service - the service class
        Returns:
        the provider class
        Throws:
        java.lang.ClassNotFoundException - if some provider could not be loaded
      • findServiceConfigurations

        java.util.Map<java.lang.String,​java.net.URL> findServiceConfigurations​(java.lang.String serviceName)
        Finds service configurations by service name.
        Iterations over the returned map are ordered by discovery along the classpath.
        Parameters:
        serviceName - the service name, usually a classname
        Returns:
        a map of the configured services and their corresponding URLs
      • findServiceProviders

        <T> java.util.Collection<java.lang.Class<T>> findServiceProviders​(java.lang.Class<T> service)
                                                                   throws java.lang.ClassNotFoundException
        Finds the service providers by service names.
        Type Parameters:
        T - the service type
        Parameters:
        service - the service class
        Returns:
        the classes providing this service
        Throws:
        java.lang.ClassNotFoundException - if some provider could not be loaded
      • findServiceURLs

        java.util.Collection<java.net.URL> findServiceURLs​(java.lang.String serviceName)
        Finds URLs of a service by name.
        Parameters:
        serviceName - the service name, usually a classname
        Returns:
        the service's resource urls
      • findUniqueServiceConfiguration

        java.util.Map.Entry<java.lang.String,​java.net.URL> findUniqueServiceConfiguration​(java.lang.String serviceName)
        Finds a unique service configurations 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 configuation entry
      • findUniqueServiceProvider

        <T> java.lang.Class<T> findUniqueServiceProvider​(java.lang.Class<T> service)
                                                  throws java.lang.ClassNotFoundException
        Finds the unique service provider by service name.
        Type Parameters:
        T - the service type
        Parameters:
        service - the service class
        Returns:
        the provider class
        Throws:
        java.lang.ClassNotFoundException - if some provider could not be loaded
      • createNameMap

        java.util.Map<java.lang.String,​java.lang.String> createNameMap​(java.lang.String serviceName)
        Creates a map of names to mapped services.

        They key of the map is the service name. The value is the name providing the service which comes first in the classpath.

        Parameters:
        serviceName - the service class
        Returns:
        the map
      • createNameMapToAll

        java.util.Map<java.lang.String,​java.util.Set<java.lang.String>> createNameMapToAll​(java.lang.String serviceName)
        Creates a map of names to all mapped services.

        They key of the map is the service name. The value is a list of names providing the service ordered by discovery along the classpath.

        Parameters:
        serviceName - the service class
        Returns:
        the map