Issue #4340 - shortcut method for the serviceStream

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-03-16 09:42:32 +11:00
parent a6b2b3ff98
commit 7af220dade
9 changed files with 22 additions and 13 deletions

View File

@ -54,7 +54,7 @@ public class ALPNClientConnectionFactory extends NegotiatingClientConnectionFact
IllegalStateException failure = new IllegalStateException("No Client ALPNProcessors!");
// Use a for loop on iterator so load exceptions can be caught and ignored
TypeUtil.serviceLoaderStream(ServiceLoader.load(Client.class)).flatMap(TypeUtil::providerMap).forEach((processor) ->
TypeUtil.serviceStream(ServiceLoader.load(Client.class)).forEach((processor) ->
{
try
{

View File

@ -51,7 +51,7 @@ public class ALPNServerConnectionFactory extends NegotiatingServerConnectionFact
IllegalStateException failure = new IllegalStateException("No Server ALPNProcessors!");
// Use a for loop on iterator so load exceptions can be caught and ignored
TypeUtil.serviceLoaderStream(ServiceLoader.load(Server.class)).flatMap(TypeUtil::providerMap).forEach((processor) ->
TypeUtil.serviceStream(ServiceLoader.load(Server.class)).forEach((processor) ->
{
try
{

View File

@ -815,8 +815,7 @@ public class AnnotationConfiguration extends AbstractConfiguration
long start = 0;
if (LOG.isDebugEnabled())
start = System.nanoTime();
List<ServletContainerInitializer> scis = TypeUtil.serviceLoaderStream(ServiceLoader.load(ServletContainerInitializer.class))
.flatMap(TypeUtil::providerMap)
List<ServletContainerInitializer> scis = TypeUtil.serviceStream(ServiceLoader.load(ServletContainerInitializer.class))
.collect(Collectors.toList());
if (LOG.isDebugEnabled())
LOG.debug("Service loaders found in {}ms", (TimeUnit.MILLISECONDS.convert((System.nanoTime() - start), TimeUnit.NANOSECONDS)));

View File

@ -43,8 +43,7 @@ public class PreEncodedHttpField extends HttpField
static
{
List<HttpFieldPreEncoder> encoders = TypeUtil.serviceLoaderStream(ServiceLoader.load(HttpFieldPreEncoder.class))
.flatMap(TypeUtil::providerMap)
List<HttpFieldPreEncoder> encoders = TypeUtil.serviceStream(ServiceLoader.load(HttpFieldPreEncoder.class))
.filter(encoder -> index(encoder.getHttpVersion()) >= 0)
.collect(Collectors.toList());

View File

@ -77,8 +77,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
static
{
TypeUtil.serviceLoaderStream(ServiceLoader.load(Authenticator.Factory.class))
.flatMap(TypeUtil::providerMap)
TypeUtil.serviceStream(ServiceLoader.load(Authenticator.Factory.class))
.forEach(__knownAuthenticatorFactories::add);
__knownAuthenticatorFactories.add(new DefaultAuthenticatorFactory());
}

View File

@ -780,6 +780,18 @@ public class TypeUtil
}
}
/**
* Shortcut method combining {@link #serviceProviderStream(ServiceLoader)} with
* with {@link #providerMap(ServiceLoader.Provider)} using {@link Stream#flatMap(Function)}.
* @param serviceLoader the ServiceLoader instance to use.
* @param <T> the type of the service to load.
* @return a stream of the service provider type which will not throw {@link ServiceConfigurationError}.
*/
public static <T> Stream<T> serviceStream(ServiceLoader<T> serviceLoader)
{
return serviceProviderStream(serviceLoader).flatMap(TypeUtil::providerMap);
}
/**
* Utility to create a stream which provides the same functionality as {@link ServiceLoader#stream()}.
* However this also guards the case in which {@link Iterator#hasNext()} throws. Any exceptions
@ -788,7 +800,7 @@ public class TypeUtil
* @param <T> the type of the service to load.
* @return A stream that lazily loads providers for this loader's service
*/
public static <T> Stream<ServiceLoader.Provider<T>> serviceLoaderStream(ServiceLoader<T> serviceLoader)
public static <T> Stream<ServiceLoader.Provider<T>> serviceProviderStream(ServiceLoader<T> serviceLoader)
{
return StreamSupport.stream(new ServiceLoaderSpliterator<T>(serviceLoader), false);
}

View File

@ -44,7 +44,7 @@ public abstract class Credential implements Serializable
{
private static final long serialVersionUID = -7760551052768181572L;
private static final Logger LOG = Log.getLogger(Credential.class);
private static final List<CredentialProvider> CREDENTIAL_PROVIDERS = TypeUtil.serviceLoaderStream(ServiceLoader.load(CredentialProvider.class)).flatMap(TypeUtil::providerMap).collect(Collectors.toList());
private static final List<CredentialProvider> CREDENTIAL_PROVIDERS = TypeUtil.serviceStream(ServiceLoader.load(CredentialProvider.class)).collect(Collectors.toList());
/**
* Check a credential

View File

@ -75,7 +75,7 @@ public class Configurations extends AbstractList<Configuration> implements Dumpa
{
if (__known.isEmpty())
{
TypeUtil.serviceLoaderStream(ServiceLoader.load(Configuration.class)).flatMap(TypeUtil::providerMap).forEach(configuration ->
TypeUtil.serviceStream(ServiceLoader.load(Configuration.class)).forEach(configuration ->
{
if (!configuration.isAvailable())
{

View File

@ -97,8 +97,8 @@ public class XmlConfiguration
{
ArrayList.class, HashSet.class, Queue.class, List.class, Set.class, Collection.class
};
private static final List<ConfigurationProcessorFactory> PROCESSOR_FACTORIES = TypeUtil.serviceLoaderStream(ServiceLoader.load(ConfigurationProcessorFactory.class))
.flatMap(TypeUtil::providerMap).collect(Collectors.toList());
private static final List<ConfigurationProcessorFactory> PROCESSOR_FACTORIES = TypeUtil.serviceStream(ServiceLoader.load(ConfigurationProcessorFactory.class))
.collect(Collectors.toList());
private static final XmlParser PARSER = initParser();
public static final Comparator<Executable> EXECUTABLE_COMPARATOR = (e1, e2) ->
{