Class SoftServiceLoader<S>

java.lang.Object
io.micronaut.core.io.service.SoftServiceLoader<S>
Type Parameters:
S - The service type
All Implemented Interfaces:
Iterable<ServiceDefinition<S>>

public final class SoftServiceLoader<S> extends Object implements Iterable<ServiceDefinition<S>>

Variation of ServiceLoader that allows soft loading and conditional loading of META-INF/services classes.

Since:
1.0
Author:
Graeme Rocher
  • Field Details

  • Method Details

    • load

      public static <S> SoftServiceLoader<S> load(Class<S> service)
      Creates a new SoftServiceLoader using the thread context loader by default.
      Type Parameters:
      S - The service generic type
      Parameters:
      service - The service type
      Returns:
      A new service loader
    • load

      public static <S> SoftServiceLoader<S> load(Class<S> service, ClassLoader loader)
      Creates a new SoftServiceLoader using the given type and class loader.
      Type Parameters:
      S - The service generic type
      Parameters:
      service - The service type
      loader - The class loader
      Returns:
      A new service loader
    • load

      public static <S> SoftServiceLoader<S> load(Class<S> service, ClassLoader loader, Predicate<String> condition)
      Creates a new SoftServiceLoader using the given type and class loader.
      Type Parameters:
      S - The service generic type
      Parameters:
      service - The service type
      loader - The class loader to use
      condition - A Predicate to use to conditionally load the service. The predicate is passed the service class name
      Returns:
      A new service loader
    • disableFork

      public SoftServiceLoader<S> disableFork()
    • first

      public Optional<ServiceDefinition<S>> first()
      Returns:
      Return the first such instance
    • firstAvailable

      public Optional<S> firstAvailable()
      Find the first service definition that is present, and then load it.
      Returns:
      Return the first such instance, or Optional.empty() if there is no definition or none of the definitions are present on the classpath.
    • firstOr

      public Optional<ServiceDefinition<S>> firstOr(String alternative, ClassLoader classLoader)
      Parameters:
      alternative - An alternative type to use if the this type is not present
      classLoader - The classloader
      Returns:
      Return the first such instance
    • collectAll

      public void collectAll(@NonNull @NonNull Collection<S> values, @Nullable @Nullable Predicate<S> predicate)
      Collects all initialized instances.
      Parameters:
      values - The collection to be populated.
      predicate - The predicated to filter the instances or null if not needed.
    • collectAll

      public void collectAll(@NonNull @NonNull Collection<S> values)
      Collects all initialized instances.
      Parameters:
      values - The collection to be populated.
    • collectAll

      public List<S> collectAll()
      Collects all initialized instances.
      Returns:
      The instances of this service.
    • collectAll

      public List<S> collectAll(Predicate<S> predicate)
      Collects all initialized instances.
      Parameters:
      predicate - The predicated to filter the instances or null if not needed.
      Returns:
      The instances of this service.
    • iterator

      Specified by:
      iterator in interface Iterable<S>
      Returns:
      The iterator
    • newCollector

      public static <S> SoftServiceLoader.ServiceCollector<S> newCollector(String serviceName, Predicate<String> lineCondition, ClassLoader classLoader, Function<String,S> transformer)