Class ServiceLoaderUtils

java.lang.Object
de.iip_ecosphere.platform.support.jsl.ServiceLoaderUtils

public class ServiceLoaderUtils extends Object
Helper functions for Java Service Loading.
Author:
Holger Eichelberger, SSE
  • Constructor Details

    • ServiceLoaderUtils

      public ServiceLoaderUtils()
  • Method Details

    • filterExcluded

      public static <D> Optional<D> filterExcluded(Class<D> descriptorClass)
      Filters excluded descriptors, i.e., descriptors marked with ExcludeFirst. If selection by filtering does not work, the first descriptor is returned.
      Type Parameters:
      D - the descriptor type
      Parameters:
      descriptorClass - the descriptor class
      Returns:
      the found descriptor, may be an empty optional instance
      See Also:
    • findFirst

      public static <D> Optional<D> findFirst(Class<D> descriptorClass)
      Convenience method for findFirst(ServiceLoader) based on default loading of the service descriptors.
      Type Parameters:
      D - the descriptor type
      Parameters:
      descriptorClass - the descriptor class
      Returns:
      the first service provider
      See Also:
    • findFirst

      public static <D> Optional<D> findFirst(ServiceLoader<D> loader)
      Load the first available service provider of the given loader's service. [JDK 1.8 compatibility]
      Type Parameters:
      D - the service descriptor type
      Parameters:
      loader - the loader instance
      Returns:
      the first service provider
    • stream

      public static <D> Stream<D> stream(ServiceLoader<D> loader)
      Turns the service loader into a stream. [JDK 1.8 compatibility]
      Type Parameters:
      D - the service descriptor type
      Parameters:
      loader - the loader instance
      Returns:
      the stream of descriptor
    • hasExcludeFirst

      public static boolean hasExcludeFirst(Object instance)
      Returns whether an instance is tagged with the ExcludeFirst annotation.
      Parameters:
      instance - the instance to check (may be null)
      Returns:
      true if the annotation is present, false else
    • load

      public static <D> ServiceLoader<D> load(Class<D> cls)
      Creates a service loader for the given cls using PluginSetup.getClassLoader() as class loader.
      Type Parameters:
      D - the descriptor type
      Parameters:
      cls - the class of the descriptor type
      Returns:
      the service loader