Skip to content

Commit

Permalink
Issue #3341 - change HttpClientProvider to interface
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
lachlan-roberts committed Feb 14, 2019
1 parent c6fd7a6 commit b0d9a36
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,4 @@
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.net.URI;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

import org.eclipse.jetty.client.HttpClient;
Expand Down Expand Up @@ -61,7 +62,7 @@ public WebSocketCoreClient(HttpClient httpClient)
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
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)
Expand All @@ -42,7 +43,7 @@ public static HttpClient get()
}
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;
Expand Down

0 comments on commit b0d9a36

Please sign in to comment.