Skip to content

Commit

Permalink
Issue #4340 - shortcut method for the serviceStream
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Mar 15, 2020
1 parent a6b2b3f commit 7af220d
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ALPNClientConnectionFactory(Executor executor, ClientConnectionFactory co
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ALPNServerConnectionFactory(@Name("protocols") String... protocols)

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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,7 @@ public List<ServletContainerInitializer> getNonExcludedInitializers(WebAppContex
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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
14 changes: 13 additions & 1 deletion jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,18 @@ public static <T> Stream<T> providerMap(ServiceLoader.Provider<T> provider)
}
}

/**
* 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
Expand All @@ -788,7 +800,7 @@ public static <T> Stream<T> providerMap(ServiceLoader.Provider<T> provider)
* @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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static synchronized List<Configuration> getKnown()
{
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())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
{
Expand Down

0 comments on commit 7af220d

Please sign in to comment.