Package io.micronaut.core.io.service
Class SoftServiceLoader<S>
- java.lang.Object
-
- io.micronaut.core.io.service.SoftServiceLoader<S>
-
- Type Parameters:
S
- The service type
- All Implemented Interfaces:
java.lang.Iterable<ServiceDefinition<S>>
public final class SoftServiceLoader<S> extends java.lang.Object implements java.lang.Iterable<ServiceDefinition<S>>
Variation of
ServiceLoader
that allows soft loading and conditional loading of META-INF/services classes.- Since:
- 1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SoftServiceLoader.Optimizations
Static optimizations for service loaders.static interface
SoftServiceLoader.ServiceCollector<S>
Service collector for loading services of the given type.static class
SoftServiceLoader.StaticDefinition<S>
AServiceDefinition
implementation that uses aMethodHandles.Lookup
object to find a public constructor.static interface
SoftServiceLoader.StaticServiceLoader<S>
Service loader that usesSoftServiceLoader.StaticDefinition
.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
META_INF_SERVICES
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.List<S>
collectAll()
Collects all initialized instances.void
collectAll(java.util.Collection<S> values)
Collects all initialized instances.void
collectAll(java.util.Collection<S> values, java.util.function.Predicate<S> predicate)
Collects all initialized instances.java.util.List<S>
collectAll(java.util.function.Predicate<S> predicate)
Collects all initialized instances.SoftServiceLoader<S>
disableFork()
java.util.Optional<ServiceDefinition<S>>
first()
java.util.Optional<S>
firstAvailable()
java.util.Optional<ServiceDefinition<S>>
firstOr(java.lang.String alternative, java.lang.ClassLoader classLoader)
java.util.Iterator<ServiceDefinition<S>>
iterator()
static <S> SoftServiceLoader<S>
load(java.lang.Class<S> service)
Creates a newSoftServiceLoader
using the thread context loader by default.static <S> SoftServiceLoader<S>
load(java.lang.Class<S> service, java.lang.ClassLoader loader)
Creates a newSoftServiceLoader
using the given type and class loader.static <S> SoftServiceLoader<S>
load(java.lang.Class<S> service, java.lang.ClassLoader loader, java.util.function.Predicate<java.lang.String> condition)
Creates a newSoftServiceLoader
using the given type and class loader.static <S> SoftServiceLoader.ServiceCollector<S>
newCollector(java.lang.String serviceName, java.util.function.Predicate<java.lang.String> lineCondition, java.lang.ClassLoader classLoader, java.util.function.Function<java.lang.String,S> transformer)
protected ServiceDefinition<S>
newService(java.lang.String name, java.util.Optional<java.lang.Class> loadedClass)
Deprecated.No longer used
-
-
-
Field Detail
-
META_INF_SERVICES
public static final java.lang.String META_INF_SERVICES
- See Also:
- Constant Field Values
-
-
Method Detail
-
load
public static <S> SoftServiceLoader<S> load(java.lang.Class<S> service)
Creates a newSoftServiceLoader
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(java.lang.Class<S> service, java.lang.ClassLoader loader)
Creates a newSoftServiceLoader
using the given type and class loader.- Type Parameters:
S
- The service generic type- Parameters:
service
- The service typeloader
- The class loader- Returns:
- A new service loader
-
load
public static <S> SoftServiceLoader<S> load(java.lang.Class<S> service, java.lang.ClassLoader loader, java.util.function.Predicate<java.lang.String> condition)
Creates a newSoftServiceLoader
using the given type and class loader.- Type Parameters:
S
- The service generic type- Parameters:
service
- The service typeloader
- The class loader to usecondition
- APredicate
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 java.util.Optional<ServiceDefinition<S>> first()
- Returns:
- Return the first such instance
-
firstAvailable
public java.util.Optional<S> firstAvailable()
- 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 java.util.Optional<ServiceDefinition<S>> firstOr(java.lang.String alternative, java.lang.ClassLoader classLoader)
- Parameters:
alternative
- An alternative type to use if the this type is not presentclassLoader
- The classloader- Returns:
- Return the first such instance
-
collectAll
public void collectAll(@NonNull java.util.Collection<S> values, @Nullable java.util.function.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 java.util.Collection<S> values)
Collects all initialized instances.- Parameters:
values
- The collection to be populated.
-
collectAll
public java.util.List<S> collectAll()
Collects all initialized instances.- Returns:
- The instances of this service.
-
collectAll
public java.util.List<S> collectAll(java.util.function.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
@NonNull public java.util.Iterator<ServiceDefinition<S>> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<S>
- Returns:
- The iterator
-
newService
@Deprecated protected ServiceDefinition<S> newService(java.lang.String name, java.util.Optional<java.lang.Class> loadedClass)
Deprecated.No longer used- Parameters:
name
- The nameloadedClass
- The loaded class- Returns:
- The service definition
-
newCollector
public static <S> SoftServiceLoader.ServiceCollector<S> newCollector(java.lang.String serviceName, java.util.function.Predicate<java.lang.String> lineCondition, java.lang.ClassLoader classLoader, java.util.function.Function<java.lang.String,S> transformer)
-
-