Issue #1528 - Use WebSocketContainer out of current context

+ Interrogate ServletContext, ContextHandler, and Context
  for an active ServerContainer before returning a new ClientContainer
This commit is contained in:
Joakim Erdfelt 2017-05-19 13:50:40 -07:00
parent e001fe0355
commit e3bab3df22
4 changed files with 41 additions and 4 deletions

View File

@ -18,6 +18,8 @@
package org.eclipse.jetty.websocket.jsr356;
import java.lang.reflect.Method;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
@ -66,6 +68,20 @@ public class JettyClientContainerProvider extends ContainerProvider
{
synchronized (lock)
{
try
{
Class<?> clazzServerContainer = Class.forName("org.eclipse.jetty.websocket.jsr356.server.ServerContainer");
Method method = clazzServerContainer.getMethod("getWebSocketContainer");
WebSocketContainer container = (WebSocketContainer) method.invoke(null);
if (container != null)
{
return container;
}
}
catch (Throwable ignore)
{
}
if (INSTANCE == null)
{
INSTANCE = new ClientContainer();

View File

@ -50,6 +50,28 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
{
private static final Logger LOG = Log.getLogger(ServerContainer.class);
/**
* Get the WebSocketContainer out of the current ThreadLocal reference
* of the active ContextHandler.
*
* @return the WebSocketContainer if found, null if not found.
*/
public static WebSocketContainer getWebSocketContainer()
{
ContextHandler.Context context = ContextHandler.getCurrentContext();
if (context == null)
return null;
ContextHandler handler = ContextHandler.getContextHandler(context);
if (handler == null)
return null;
if (!(handler instanceof ServletContextHandler))
return null;
return (javax.websocket.WebSocketContainer) handler.getServletContext().getAttribute("javax.websocket.server.ServerContainer");
}
private final NativeWebSocketConfiguration configuration;
private List<Class<?>> deferredEndpointClasses;
private List<ServerEndpointConfig> deferredEndpointConfigs;

View File

@ -55,7 +55,6 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.websocket.api.util.WSURI;
import org.eclipse.jetty.websocket.jsr356.ClientContainer;
import org.eclipse.jetty.websocket.jsr356.JettyClientContainerProvider;
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
import org.junit.Before;
@ -254,7 +253,7 @@ public class DelayedStartClientOnServerTest
assertThat("Response", response, startsWith("Connected to ws://"));
List<String> threadNames = getThreadNames();
assertNoHttpClientPoolThreads(threadNames);
assertThat("Threads", threadNames, hasItem(containsString("WebSocketContainer@")));
assertThat("Threads", threadNames, hasItem(containsString("WebSocketClient@")));
}
finally
{
@ -301,7 +300,7 @@ public class DelayedStartClientOnServerTest
{
server.start();
String response = GET(server.getURI().resolve("/configure"));
assertThat("Response", response, startsWith("Configured " + ClientContainer.class.getName()));
assertThat("Response", response, startsWith("Configured " + ServerContainer.class.getName()));
List<String> threadNames = getThreadNames();
assertNoHttpClientPoolThreads(threadNames);
assertThat("Threads", threadNames, not(hasItem(containsString("WebSocketContainer@"))));

View File

@ -43,7 +43,7 @@ public final class HttpClientProvider
}
catch (Throwable ignore)
{
Log.getLogger(HttpClientProvider.class).warn(ignore);
Log.getLogger(HttpClientProvider.class).ignore(ignore);
}
return DefaultHttpClientProvider.newHttpClient(scope);