Module xaos.tools

Class ServiceLoaderUtilities


  • public class ServiceLoaderUtilities
    extends Object
    Provides few more methods to ServiceLoader.

    Note: when using ServiceLoaderUtilities the service provider interface type's package must be opened to the xaos.tools module, i.e. it must be included into an opens statement in the module-info class.

    Author:
    claudio.rosati@esss.se
    • Method Detail

      • classesOf

        public static <S> List<Class<? extends S>> classesOf​(ServiceLoader<S> loader)
        Returns the List of classes implementing the given service provider interface (spi). A ServiceLoader is created for the given service type, using the current thread's context class loader.

        This is equivalent to call

           ServiceLoaderUtilities.stream(loader)
             .map(Provider::type)
             .collect(Collectors.toList());
        Type Parameters:
        S - The service provider interface type.
        Parameters:
        loader - The ServiceLoader for which a sorted Stream must be returned.
        Returns:
        The List of found service provider classes.
      • findFirst

        public static <S> Optional<S> findFirst​(ServiceLoader<S> loader)
        Load the first available service provider of the given service provider interface (spi).

        This is equivalent to call

           ServiceLoaderUtilities.stream(loader)
             .map(Provider::get)
             .findFirst();

        The following example loads the first available service provider. If no service providers are located then it uses a default implementation.

           CodecFactory factory = ServiceLoaderUtilities.findFirst(CodecFactory.class)
             .orElse(DEFAULT_CODECSET_FACTORY);
        Type Parameters:
        S - The service provider interface type.
        Parameters:
        loader - The ServiceLoader for which a sorted Stream must be returned.
        Returns:
        The first service provider or empty Optional if no service providers are located.
      • of

        public static <S> List<S> of​(ServiceLoader<S> loader)
        Returns the List of implementers of the given service provider interface (spi).

        This is equivalent to call

           ServiceLoaderUtilities.stream(loader)
             .map(Provider::get)
             .collect(Collectors.toList());
        Type Parameters:
        S - The service provider interface type.
        Parameters:
        loader - The ServiceLoader for which a sorted Stream must be returned.
        Returns:
        The List of found service provider implementers.
      • stream

        public static <S> Stream<ServiceLoader.Provider<S>> stream​(ServiceLoader<S> loader)
        Returns a sorted stream to lazily load available providers of the given loader's service. The stream elements are of type ServiceLoader.Provider. The ServiceLoader.Provider's get method must be invoked to get or instantiate the provider.

        If the provider class is annotated with ServiceProvider, and a specific order number is given, then it will be used to sort the elements of the returned stream (low order first). Non annotated providers and annotated ones whose order is not explicitly set will be the last ones in the returned stream.

        This is equivalent to call

           loader
             .stream()
             .sorted(( p1, p2 ) -> {
               Class<? extends S> t1 = p1.type();
               int o1 = t1.isAnnotationPresent(ServiceProvider.class)
                      ? t1.getAnnotation(ServiceProvider.class).order()
                      : Integer.MAX_VALUE;
               Class<? extends S> t2 = p2.type();
               int o2 = t2.isAnnotationPresent(ServiceProvider.class)
                      ? t2.getAnnotation(ServiceProvider.class).order()
                      : Integer.MAX_VALUE;
             return o1 - o2;
           });
        Type Parameters:
        S - The service provider interface type.
        Parameters:
        loader - The ServiceLoader for which a sorted Stream must be returned.
        Returns:
        A sorted stream that lazily loads providers for this loader's service.
        See Also:
        ServiceLoader.stream()