Package org.apache.logging.log4j.util
Class ServiceLoaderUtil
- java.lang.Object
-
- org.apache.logging.log4j.util.ServiceLoaderUtil
-
@InternalApi public final class ServiceLoaderUtil extends java.lang.Object
An utility class to retrieve services in a safe way.This class should be considered internal.
A common source of
ServiceLoaderfailures, when running in a multi-classloader environment, is the presence of multiple classes with the same class name in the same classloader hierarchy. SinceServiceLoaderretrieves services by class name, it is entirely possible that the registered services don't extend the required interface and cause an exception to be thrown byServiceLoader.The purpose of this class is to:
- skip faulty services, allowing for a partial retrieval of the good ones,
- allow to integrate other sources of services like OSGi services.
- Since:
- 2.18.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classServiceLoaderUtil.ServiceLoaderSpliterator<S>
-
Field Summary
Fields Modifier and Type Field Description private static intMAX_BROKEN_SERVICES
-
Constructor Summary
Constructors Modifier Constructor Description privateServiceLoaderUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <S> java.util.stream.Stream<S>safeStream(java.lang.Class<S> serviceType, java.util.ServiceLoader<? extends S> serviceLoader, Logger logger)Retrieves services registered withServiceLoader
-
-
-
Field Detail
-
MAX_BROKEN_SERVICES
private static final int MAX_BROKEN_SERVICES
- See Also:
- Constant Field Values
-
-
Method Detail
-
safeStream
public static <S> java.util.stream.Stream<S> safeStream(java.lang.Class<S> serviceType, java.util.ServiceLoader<? extends S> serviceLoader, Logger logger)Retrieves services registered withServiceLoaderIt ignores the most common service loading errors.
- Parameters:
serviceType- The service type to use for OSGi service retrieval.serviceLoader- The service loader instance to use.logger- The logger to use to report service failures.- Returns:
- A stream of all correctly loaded services.
- Since:
- 2.24.0
-
-