Class DefaultServiceFinder

    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultServiceFinder()
      Creates a default service finder.
      The classloader used is Thread.currentThread().getContextClassLoader().
      The servicepath is "META_INF/services/".
      DefaultServiceFinder​(java.lang.ClassLoader loader, java.lang.String servicePath)
      Creates a service finder.
      DefaultServiceFinder​(java.lang.String servicePath)
      Creates a default service finder.
      The classloader used is Thread.currentThread().getContextClassLoader().
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Map<java.lang.String,​java.lang.String> createNameMap​(java.lang.String serviceName)
      Creates a map of classnames 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 class.
      java.lang.ClassLoader getClassLoader()
      Gets the classloader used by this finder.
      protected java.lang.ClassLoader getClassLoader​(java.lang.String serviceName)
      Gets the classloader for the given service name.
      java.lang.String getServicePath()
      Gets the finder' service path it is responsible for.
      protected java.lang.String normalizeLine​(java.lang.String line)
      Normalizes a line.
      Trailing and leading spaces are ignored.
      protected void parseURL​(java.net.URL url, java.lang.String serviceName, java.util.Map<java.lang.String,​java.net.URL> services)
      Parses a service URL.
      The implementation assumes the provider configuration file in property-file format.
      protected void parseURLs​(java.util.Collection<java.net.URL> urls, java.lang.String serviceName, java.util.Map<java.lang.String,​java.net.URL> services)
      Parses an enumeration of URLs.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DefaultServiceFinder

        public DefaultServiceFinder​(java.lang.ClassLoader loader,
                                    java.lang.String servicePath)
        Creates a service finder.
        Parameters:
        loader - optional classloader, null if use ClassLoader.getSystemClassLoader()
        servicePath - optional service path prefix, null if "META_INF/services/"
      • DefaultServiceFinder

        public DefaultServiceFinder​(java.lang.String servicePath)
        Creates a default service finder.
        The classloader used is Thread.currentThread().getContextClassLoader().
        Parameters:
        servicePath - optional service path prefix, null if "META_INF/services/"
      • DefaultServiceFinder

        public DefaultServiceFinder()
        Creates a default service finder.
        The classloader used is Thread.currentThread().getContextClassLoader().
        The servicepath is "META_INF/services/".
    • Method Detail

      • getClassLoader

        public java.lang.ClassLoader getClassLoader()
        Gets the classloader used by this finder.
        Specified by:
        getClassLoader in interface ServiceFinder
        Returns:
        the classloader, never null
      • getServicePath

        public java.lang.String getServicePath()
        Gets the finder' service path it is responsible for.
        Specified by:
        getServicePath in interface ServiceFinder
        Returns:
        the service path
      • findServiceURLs

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

        public 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.
        Specified by:
        findServiceConfigurations in interface ServiceFinder
        Parameters:
        serviceName - the service name, usually a classname
        Returns:
        a map of the configured services and their corresponding URLs
      • findFirstServiceConfiguration

        public 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.
        Specified by:
        findFirstServiceConfiguration in interface ServiceFinder
        Parameters:
        serviceName - the service name, usually a classname
        Returns:
        the service configuation entry
      • findUniqueServiceConfiguration

        public 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.
        Specified by:
        findUniqueServiceConfiguration in interface ServiceFinder
        Parameters:
        serviceName - the service name, usually a classname
        Returns:
        the service configuation entry
      • findServiceProviders

        public <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.
        Specified by:
        findServiceProviders in interface ServiceFinder
        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
      • findFirstServiceProvider

        public <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.
        Specified by:
        findFirstServiceProvider in interface ServiceFinder
        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
      • findUniqueServiceProvider

        public <T> java.lang.Class<T> findUniqueServiceProvider​(java.lang.Class<T> service)
                                                         throws java.lang.ClassNotFoundException
        Finds the unique service provider by service class.
        Specified by:
        findUniqueServiceProvider in interface ServiceFinder
        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

        public java.util.Map<java.lang.String,​java.lang.String> createNameMap​(java.lang.String serviceName)
        Creates a map of classnames 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.

        Specified by:
        createNameMap in interface ServiceFinder
        Parameters:
        serviceName - the service class
        Returns:
        the map
      • createNameMapToAll

        public 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.

        Specified by:
        createNameMapToAll in interface ServiceFinder
        Parameters:
        serviceName - the service class
        Returns:
        the map
      • getClassLoader

        protected java.lang.ClassLoader getClassLoader​(java.lang.String serviceName)
        Gets the classloader for the given service name.
        Parameters:
        serviceName - the service name
        Returns:
        the loader, never null
      • parseURL

        protected void parseURL​(java.net.URL url,
                                java.lang.String serviceName,
                                java.util.Map<java.lang.String,​java.net.URL> services)
        Parses a service URL.
        The implementation assumes the provider configuration file in property-file format. This includes the special case of key-only properties which is equivalent to the format used in ServiceLoader.
        Parameters:
        url - the url of the provider definition
        serviceName - the name of the service
        services - the implementing services map
      • parseURLs

        protected void parseURLs​(java.util.Collection<java.net.URL> urls,
                                 java.lang.String serviceName,
                                 java.util.Map<java.lang.String,​java.net.URL> services)
        Parses an enumeration of URLs.
        Parameters:
        urls - the URLs
        serviceName - the service name
        services - the destination map
      • normalizeLine

        protected java.lang.String normalizeLine​(java.lang.String line)
        Normalizes a line.
        Trailing and leading spaces are ignored. Comments starting with a hash # are stripped. A hash can be escaped by a backslash.
        Parameters:
        line - the source line
        Returns:
        the normalized line, null if line contains no information