Class DefaultServiceFinder

    • Constructor Detail

      • DefaultServiceFinder

        public DefaultServiceFinder​(ClassLoader loader,
                                    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​(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 ClassLoader getClassLoader()
        Gets the classloader used by this finder.
        Specified by:
        getClassLoader in interface ServiceFinder
        Returns:
        the classloader, never null
      • getServicePath

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

        public Collection<URL> findServiceURLs​(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 Map<String,​URL> findServiceConfigurations​(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 Map.Entry<String,​URL> findFirstServiceConfiguration​(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 Map.Entry<String,​URL> findUniqueServiceConfiguration​(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
      • findFirstServiceProvider

        public <T> Class<T> findFirstServiceProvider​(Class<T> service)
                                              throws 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:
        ClassNotFoundException - if some provider could not be loaded
      • createNameMap

        public Map<String,​String> createNameMap​(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 Map<String,​Set<String>> createNameMapToAll​(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 ClassLoader getClassLoader​(String serviceName)
        Gets the classloader for the given service name.
        Parameters:
        serviceName - the service name
        Returns:
        the loader, never null
      • parseURL

        protected void parseURL​(URL url,
                                String serviceName,
                                Map<String,​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​(Collection<URL> urls,
                                 String serviceName,
                                 Map<String,​URL> services)
        Parses an enumeration of URLs.
        Parameters:
        urls - the URLs
        serviceName - the service name
        services - the destination map
      • normalizeLine

        protected String normalizeLine​(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