Issue #4340 - remove TypeUtil.load and use flatMap() on streams instead

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-02-29 11:06:32 +11:00
parent 6832912d49
commit 97c55e27cc
9 changed files with 25 additions and 26 deletions

View File

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

View File

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

View File

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

View File

@ -44,7 +44,8 @@ public class PreEncodedHttpField extends HttpField
static static
{ {
List<HttpFieldPreEncoder> encoders = new ArrayList<>(); List<HttpFieldPreEncoder> encoders = new ArrayList<>();
TypeUtil.load(ServiceLoader.load(HttpFieldPreEncoder.class)) ServiceLoader.load(HttpFieldPreEncoder.class).stream()
.flatMap(TypeUtil::providerMap)
.filter(encoder -> index(encoder.getHttpVersion()) >= 0) .filter(encoder -> index(encoder.getHttpVersion()) >= 0)
.forEach(encoders::add); .forEach(encoders::add);

View File

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

View File

@ -41,10 +41,10 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.ServiceConfigurationError; import java.util.ServiceConfigurationError;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.function.Function;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
@ -757,27 +757,23 @@ public class TypeUtil
} }
/** /**
* Uses the {@link ServiceLoader} to return a Stream of the service providers which does not throw. * Used on a {@link ServiceLoader#stream()} with {@link Stream#flatMap(Function)},
* If loading a service type throws {@link ServiceConfigurationError}, * so that in the case a {@link ServiceConfigurationError} is thrown it warns and
* it warns and continues iterating through the service loader. * continues iterating through the service loader.
* @param <T> The class of the service type. * @param <T> The class of the service type.
* @param serviceLoader The service loader to use. * @param provider The service provider to instantiate.
* @return a stream of the loaded service providers. * @return a stream of the loaded service providers.
*/ */
public static <T> Stream<T> load(ServiceLoader<T> serviceLoader) public static <T> Stream<T> providerMap(ServiceLoader.Provider<T> provider)
{
return serviceLoader.stream().map((provider) ->
{ {
try try
{ {
// Attempt to load service, will either return a service, throw an error, or return null. return Stream.of(provider.get());
return provider.get();
} }
catch (ServiceConfigurationError error) catch (ServiceConfigurationError error)
{ {
LOG.warn("Service Provider failed to load", error); LOG.warn("Service Provider failed to load", error);
return Stream.empty();
} }
return null;
}).filter(Objects::nonNull);
} }
} }

View File

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

View File

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

View File

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