Issue #4340 - Change usages to more closely follow previous behaviours

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-03-20 17:02:49 +11:00
parent 9363a3c39a
commit 04cecaaaef
9 changed files with 96 additions and 34 deletions

View File

@ -54,8 +54,21 @@ 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.serviceStream(ServiceLoader.load(Client.class)).forEach((processor) ->
TypeUtil.serviceProviderStream(ServiceLoader.load(Client.class)).forEach(provider ->
{
Client processor;
try
{
processor = provider.get();
}
catch (Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug("Unable to load client processor", x);
failure.addSuppressed(x);
return;
}
try
{
processor.init();

View File

@ -51,8 +51,22 @@ 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.serviceStream(ServiceLoader.load(Server.class)).forEach((processor) ->
TypeUtil.serviceProviderStream(ServiceLoader.load(Server.class)).forEach(provider ->
{
Server processor;
try
{
processor = provider.get();
}
catch (Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug(x.getMessage(), x);
if (x != failure)
failure.addSuppressed(x);
return;
}
try
{
processor.init();

View File

@ -39,6 +39,7 @@ import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.annotation.HandlesTypes;
@ -815,8 +816,23 @@ public class AnnotationConfiguration extends AbstractConfiguration
long start = 0;
if (LOG.isDebugEnabled())
start = System.nanoTime();
List<ServletContainerInitializer> scis = TypeUtil.serviceStream(ServiceLoader.load(ServletContainerInitializer.class))
.collect(Collectors.toList());
List<ServletContainerInitializer> scis = TypeUtil.serviceProviderStream(ServiceLoader.load(ServletContainerInitializer.class)).flatMap(provider ->
{
try
{
return Stream.of(provider.get());
}
catch (Error e)
{
// Probably a SCI discovered on the system classpath that is hidden by the context classloader
if (LOG.isDebugEnabled())
LOG.debug("Error: {} for {}", e.getMessage(), context, e);
else
LOG.info("Error: {} for {}", e.getMessage(), context);
return Stream.of();
}
}).collect(Collectors.toList());
if (LOG.isDebugEnabled())
LOG.debug("Service loaders found in {}ms", (TimeUnit.MILLISECONDS.convert((System.nanoTime() - start), TimeUnit.NANOSECONDS)));

View File

@ -19,9 +19,9 @@
package org.eclipse.jetty.http;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import org.eclipse.jetty.util.TypeUtil;
import org.slf4j.Logger;
@ -43,9 +43,20 @@ public class PreEncodedHttpField extends HttpField
static
{
List<HttpFieldPreEncoder> encoders = TypeUtil.serviceStream(ServiceLoader.load(HttpFieldPreEncoder.class))
.filter(encoder -> index(encoder.getHttpVersion()) >= 0)
.collect(Collectors.toList());
List<HttpFieldPreEncoder> encoders = new ArrayList<>();
TypeUtil.serviceProviderStream(ServiceLoader.load(HttpFieldPreEncoder.class)).forEach(provider ->
{
try
{
HttpFieldPreEncoder encoder = provider.get();
if (index(encoder.getHttpVersion()) >= 0)
encoders.add(encoder);
}
catch (Error | RuntimeException e)
{
LOG.debug("Unable to add HttpFieldPreEncoder", e);
}
});
LOG.debug("HttpField encoders loaded: {}", encoders);
int size = encoders.size();

View File

@ -767,7 +767,7 @@ public class TypeUtil
* @param provider The service provider to instantiate.
* @return a stream of the loaded service providers.
*/
public static <T> Stream<T> mapToService(ServiceLoader.Provider<T> provider)
private static <T> Stream<T> mapToService(ServiceLoader.Provider<T> provider)
{
try
{
@ -781,11 +781,12 @@ public class TypeUtil
}
/**
* Shortcut method combining {@link #serviceProviderStream(ServiceLoader)} with
* with {@link #mapToService(ServiceLoader.Provider)} using {@link Stream#flatMap(Function)}.
* Utility method to provide a stream of the service type from a {@link ServiceLoader}.
* Log warnings will be given for any {@link ServiceConfigurationError}s which occur when loading or
* instantiating the services.
* @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}.
* @return a stream of the service type which will not throw {@link ServiceConfigurationError}.
*/
public static <T> Stream<T> serviceStream(ServiceLoader<T> serviceLoader)
{
@ -802,6 +803,6 @@ public class TypeUtil
*/
public static <T> Stream<ServiceLoader.Provider<T>> serviceProviderStream(ServiceLoader<T> serviceLoader)
{
return StreamSupport.stream(new ServiceLoaderSpliterator<T>(serviceLoader), false);
return StreamSupport.stream(new ServiceLoaderSpliterator<>(serviceLoader), false);
}
}

View File

@ -24,6 +24,7 @@ import java.security.MessageDigest;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jetty.util.TypeUtil;
import org.slf4j.Logger;
@ -44,7 +45,9 @@ public abstract class Credential implements Serializable
{
private static final long serialVersionUID = -7760551052768181572L;
private static final Logger LOG = LoggerFactory.getLogger(Credential.class);
private static final List<CredentialProvider> CREDENTIAL_PROVIDERS = TypeUtil.serviceStream(ServiceLoader.load(CredentialProvider.class)).collect(Collectors.toList());
private static final List<CredentialProvider> CREDENTIAL_PROVIDERS = TypeUtil.serviceProviderStream(ServiceLoader.load(CredentialProvider.class))
.flatMap(p -> Stream.of(p.get()))
.collect(Collectors.toList());
/**
* Check a credential

View File

@ -75,17 +75,25 @@ public class Configurations extends AbstractList<Configuration> implements Dumpa
{
if (__known.isEmpty())
{
TypeUtil.serviceStream(ServiceLoader.load(Configuration.class)).forEach(configuration ->
TypeUtil.serviceProviderStream(ServiceLoader.load(Configuration.class)).forEach(provider ->
{
if (!configuration.isAvailable())
try
{
if (LOG.isDebugEnabled())
LOG.debug("Configuration unavailable: " + configuration);
__unavailable.add(configuration);
return;
Configuration configuration = provider.get();
if (!configuration.isAvailable())
{
if (LOG.isDebugEnabled())
LOG.debug("Configuration unavailable: " + configuration);
__unavailable.add(configuration);
return;
}
__known.add(configuration);
__knownByClassName.add(configuration.getClass().getName());
}
catch (Throwable e)
{
LOG.warn("Unable to get known Configuration", e);
}
__known.add(configuration);
__knownByClassName.add(configuration.getClass().getName());
});
sort(__known);

View File

@ -26,23 +26,17 @@ import java.util.Set;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
public class WebSocketExtensionRegistry implements Iterable<Class<? extends Extension>>
{
private Map<String, Class<? extends Extension>> availableExtensions;
private Map<String, Class<? extends Extension>> availableExtensions = new HashMap<>();
public WebSocketExtensionRegistry()
{
// Load extensions from container loader
ServiceLoader<Extension> extensionLoader = ServiceLoader.load(Extension.class, this.getClass().getClassLoader());
availableExtensions = new HashMap<>();
for (Extension ext : extensionLoader)
{
if (ext != null)
{
availableExtensions.put(ext.getName(), ext.getClass());
}
}
// Load extensions from container loader.
TypeUtil.serviceStream(ServiceLoader.load(Extension.class, this.getClass().getClassLoader()))
.forEach(ext -> availableExtensions.put(ext.getName(), ext.getClass()));
}
public Map<String, Class<? extends Extension>> getAvailableExtensions()

View File

@ -53,6 +53,7 @@ import java.util.Queue;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.Loader;
@ -97,7 +98,8 @@ public class XmlConfiguration
{
ArrayList.class, HashSet.class, Queue.class, List.class, Set.class, Collection.class
};
private static final List<ConfigurationProcessorFactory> PROCESSOR_FACTORIES = TypeUtil.serviceStream(ServiceLoader.load(ConfigurationProcessorFactory.class))
private static final List<ConfigurationProcessorFactory> PROCESSOR_FACTORIES = TypeUtil.serviceProviderStream(ServiceLoader.load(ConfigurationProcessorFactory.class))
.flatMap(p -> Stream.of(p.get()))
.collect(Collectors.toList());
private static final XmlParser PARSER = initParser();
public static final Comparator<Executable> EXECUTABLE_COMPARATOR = (e1, e2) ->