Issue #3341 - change HttpClientProvider to interface

- HttpClientProvider is now an interface which defines a default method
newHttpClient, its static get() method get will attempt to use the
XmlHttpClientProvider to create a client, and if this fails to give a
non null client it will be created with the default newHttpClient method

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2019-02-14 13:37:18 +11:00
parent c6fd7a616f
commit 149620598d
7 changed files with 28 additions and 63 deletions

View File

@ -27,7 +27,4 @@ module org.eclipse.jetty.websocket.jetty.client
requires org.eclipse.jetty.websocket.jetty.api;
requires org.eclipse.jetty.websocket.core;
requires org.eclipse.jetty.websocket.jetty.common;
// Only required if using XmlBasedHttpClientProvider.
requires static org.eclipse.jetty.xml;
}

View File

@ -46,7 +46,7 @@ public class HttpClientInitTest
assertThat("Executor exists", executor, notNullValue());
assertThat("Executor instanceof", executor, instanceOf(QueuedThreadPool.class));
QueuedThreadPool threadPool = (QueuedThreadPool)executor;
assertThat("QueuedThreadPool.name", threadPool.getName(), startsWith("Jetty-WebSocketClient@"));
assertThat("QueuedThreadPool.name", threadPool.getName(), startsWith("WebSocketClient@"));
}
finally
{

View File

@ -37,9 +37,11 @@ module org.eclipse.jetty.websocket.core
requires org.eclipse.jetty.io;
requires org.eclipse.jetty.http;
requires org.eclipse.jetty.server;
requires static org.eclipse.jetty.xml;
requires org.eclipse.jetty.util;
// Only required if using XmlBasedHttpClientProvider.
requires static org.eclipse.jetty.xml;
uses Extension;
provides Extension with

View File

@ -1,39 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.core.client;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
class DefaultHttpClientProvider
{
public static HttpClient newHttpClient()
{
HttpClient client = new HttpClient(new SslContextFactory());
client.getSslContextFactory().setEndpointIdentificationAlgorithm("HTTPS");
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setName("WebSocketClient@" + client.hashCode());
threadPool.setDaemon(true);
client.setExecutor(threadPool);
return client;
}
}

View File

@ -18,33 +18,36 @@
package org.eclipse.jetty.websocket.core.client;
import java.lang.reflect.Method;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
public final class HttpClientProvider
public interface HttpClientProvider
{
public static HttpClient get()
static HttpClient get()
{
try
{
if (Class.forName("org.eclipse.jetty.xml.XmlConfiguration") != null)
{
Class<?> xmlClazz = Class.forName("org.eclipse.jetty.websocket.core.client.XmlBasedHttpClientProvider");
Method getMethod = xmlClazz.getMethod("get");
Object ret = getMethod.invoke(null);
if ((ret != null) && (ret instanceof HttpClient))
{
return (HttpClient)ret;
}
}
HttpClientProvider xmlProvider = new XmlHttpClientProvider();
HttpClient client = xmlProvider.newHttpClient();
if (client != null)
return client;
}
catch (Throwable ignore)
{
Log.getLogger(HttpClientProvider.class).ignore(ignore);
}
return DefaultHttpClientProvider.newHttpClient();
return new HttpClientProvider(){}.newHttpClient();
}
default HttpClient newHttpClient()
{
HttpClient client = new HttpClient(new SslContextFactory());
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setName("WebSocketClient@" + client.hashCode());
client.setExecutor(threadPool);
return client;
}
}

View File

@ -20,6 +20,7 @@ package org.eclipse.jetty.websocket.core.client;
import java.io.IOException;
import java.net.URI;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import org.eclipse.jetty.client.HttpClient;
@ -61,7 +62,7 @@ public class WebSocketCoreClient extends ContainerLifeCycle implements FrameHand
public WebSocketCoreClient(HttpClient httpClient, FrameHandler.Customizer customizer)
{
if (httpClient == null)
httpClient = HttpClientProvider.get();
httpClient = Objects.requireNonNull(HttpClientProvider.get());
this.httpClient = httpClient;
this.extensionRegistry = new WebSocketExtensionRegistry();

View File

@ -25,9 +25,10 @@ import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.xml.XmlConfiguration;
class XmlBasedHttpClientProvider
class XmlHttpClientProvider implements HttpClientProvider
{
public static HttpClient get()
@Override
public HttpClient newHttpClient()
{
URL resource = Thread.currentThread().getContextClassLoader().getResource("jetty-websocket-httpclient.xml");
if (resource == null)
@ -42,7 +43,7 @@ class XmlBasedHttpClientProvider
}
catch (Throwable t)
{
Log.getLogger(XmlBasedHttpClientProvider.class).warn("Unable to load: " + resource, t);
Log.getLogger(XmlHttpClientProvider.class).warn("Unable to load: " + resource, t);
}
return null;