Code cleanups.
This commit is contained in:
parent
da4a9e4cd8
commit
e578c79b41
|
@ -74,14 +74,13 @@ import org.eclipse.jetty.websocket.jsr356.metadata.EndpointMetadata;
|
|||
public class ClientContainer extends ContainerLifeCycle implements WebSocketContainer, WebSocketContainerScope
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(ClientContainer.class);
|
||||
|
||||
|
||||
/** The delegated Container Scope */
|
||||
private final WebSocketContainerScope scopeDelegate;
|
||||
/** Tracking all primitive decoders for the container */
|
||||
private final DecoderFactory decoderFactory;
|
||||
/** Tracking all primitive encoders for the container */
|
||||
private final EncoderFactory encoderFactory;
|
||||
|
||||
/** Tracking for all declared Client endpoints */
|
||||
private final Map<Class<?>, EndpointMetadata> endpointClientMetadataCache;
|
||||
/** Tracking for all open Sessions */
|
||||
|
@ -95,11 +94,11 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
|
|||
this(new SimpleContainerScope(WebSocketPolicy.newClientPolicy()));
|
||||
client.setDaemon(true);
|
||||
}
|
||||
|
||||
|
||||
public ClientContainer(WebSocketContainerScope scope)
|
||||
{
|
||||
boolean trustAll = Boolean.getBoolean("org.eclipse.jetty.websocket.jsr356.ssl-trust-all");
|
||||
|
||||
|
||||
this.scopeDelegate = scope;
|
||||
client = new WebSocketClient(scope, new SslContextFactory(trustAll));
|
||||
client.setEventDriverFactory(new JsrEventDriverFactory(client.getPolicy()));
|
||||
|
@ -112,7 +111,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
|
|||
|
||||
ShutdownThread.register(this);
|
||||
}
|
||||
|
||||
|
||||
private Session connect(EndpointInstance instance, URI path) throws IOException
|
||||
{
|
||||
Objects.requireNonNull(instance,"EndpointInstance cannot be null");
|
||||
|
@ -190,18 +189,18 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
|
|||
EndpointInstance instance = newClientEndpointInstance(endpoint,null);
|
||||
return connect(instance,path);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void doStart() throws Exception
|
||||
{
|
||||
super.doStart();
|
||||
|
||||
|
||||
// Initialize the default decoder / encoder factories
|
||||
EmptyClientEndpointConfig empty = new EmptyClientEndpointConfig();
|
||||
this.decoderFactory.init(empty);
|
||||
this.encoderFactory.init(empty);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void doStop() throws Exception
|
||||
{
|
||||
|
@ -223,11 +222,9 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
|
|||
|
||||
public EndpointMetadata getClientEndpointMetadata(Class<?> endpoint, EndpointConfig config)
|
||||
{
|
||||
EndpointMetadata metadata = null;
|
||||
|
||||
synchronized (endpointClientMetadataCache)
|
||||
{
|
||||
metadata = endpointClientMetadataCache.get(endpoint);
|
||||
EndpointMetadata metadata = endpointClientMetadataCache.get(endpoint);
|
||||
|
||||
if (metadata != null)
|
||||
{
|
||||
|
@ -257,7 +254,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
|
|||
err.append(endpoint.getName());
|
||||
err.append("] does not extend @").append(ClientEndpoint.class.getName());
|
||||
err.append(" or extend from ").append(Endpoint.class.getName());
|
||||
throw new InvalidWebSocketException("Unable to identify as valid Endpoint: " + endpoint);
|
||||
throw new InvalidWebSocketException(err.toString());
|
||||
}
|
||||
|
||||
endpointClientMetadataCache.put(endpoint,metadata);
|
||||
|
@ -339,7 +336,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
|
|||
{
|
||||
return scopeDelegate.getPolicy();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public SslContextFactory getSslContextFactory()
|
||||
{
|
||||
|
@ -403,7 +400,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
|
|||
Session.class.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setAsyncSendTimeout(long ms)
|
||||
{
|
||||
|
|
|
@ -60,7 +60,7 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
|
|||
this.webSocketServerFactory.addSessionFactory(new JsrSessionFactory(this));
|
||||
addBean(webSocketServerFactory);
|
||||
}
|
||||
|
||||
|
||||
public EndpointInstance newClientEndpointInstance(Object endpoint, ServerEndpointConfig config, String path)
|
||||
{
|
||||
EndpointMetadata metadata = getClientEndpointMetadata(endpoint.getClass(),config);
|
||||
|
@ -124,13 +124,13 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
|
|||
deferredEndpointConfigs.add(config);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void doStart() throws Exception
|
||||
{
|
||||
// Proceed with Normal Startup
|
||||
super.doStart();
|
||||
|
||||
|
||||
// Process Deferred Endpoints
|
||||
if (deferredEndpointClasses != null)
|
||||
{
|
||||
|
@ -140,7 +140,7 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
|
|||
}
|
||||
deferredEndpointClasses.clear();
|
||||
}
|
||||
|
||||
|
||||
if (deferredEndpointConfigs != null)
|
||||
{
|
||||
for (ServerEndpointConfig config : deferredEndpointConfigs)
|
||||
|
|
|
@ -474,7 +474,7 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
|
|||
{
|
||||
return new WebSocketRemoteEndpoint(connection,outgoingHandler,getBatchMode());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Open/Activate the session
|
||||
*/
|
||||
|
|
|
@ -120,7 +120,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
|
||||
addBean(scheduler);
|
||||
addBean(bufferPool);
|
||||
|
||||
|
||||
this.contextClassloader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
this.registeredSocketClasses = new ArrayList<>();
|
||||
|
@ -129,7 +129,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
this.eventDriverFactory = new EventDriverFactory(defaultPolicy);
|
||||
this.bufferPool = bufferPool;
|
||||
this.extensionFactory = new WebSocketExtensionFactory(this);
|
||||
|
||||
|
||||
this.sessionFactories = new ArrayList<>();
|
||||
this.sessionFactories.add(new WebSocketSessionFactory(this));
|
||||
this.creator = this;
|
||||
|
@ -166,7 +166,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
try
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(contextClassloader);
|
||||
|
||||
|
||||
// Create Servlet Specific Upgrade Request/Response objects
|
||||
ServletUpgradeRequest sockreq = new ServletUpgradeRequest(request);
|
||||
ServletUpgradeResponse sockresp = new ServletUpgradeResponse(response);
|
||||
|
@ -188,10 +188,10 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
|
||||
// Allow Decorators to do their thing
|
||||
websocketPojo = getObjectFactory().decorate(websocketPojo);
|
||||
|
||||
|
||||
// Get the original HTTPConnection
|
||||
HttpConnection connection = (HttpConnection)request.getAttribute("org.eclipse.jetty.server.HttpConnection");
|
||||
|
||||
|
||||
// Send the upgrade
|
||||
EventDriver driver = eventDriverFactory.wrap(websocketPojo);
|
||||
return upgrade(connection, sockreq, sockresp, driver);
|
||||
|
@ -199,13 +199,13 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
catch (URISyntaxException e)
|
||||
{
|
||||
throw new IOException("Unable to accept websocket due to mangled URI", e);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(old);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void addSessionFactory(SessionFactory sessionFactory)
|
||||
{
|
||||
if (sessionFactories.contains(sessionFactory))
|
||||
|
@ -285,7 +285,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
throw new WebSocketException("Unable to create instance of " + firstClass, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void doStart() throws Exception
|
||||
{
|
||||
|
@ -293,7 +293,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
{
|
||||
this.objectFactory = new DecoratedObjectFactory();
|
||||
}
|
||||
|
||||
|
||||
super.doStart();
|
||||
}
|
||||
|
||||
|
@ -308,7 +308,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
{
|
||||
return this.creator;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Executor getExecutor()
|
||||
{
|
||||
|
@ -330,7 +330,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
{
|
||||
return extensionFactory;
|
||||
}
|
||||
|
||||
|
||||
public Collection<WebSocketSession> getOpenSessions()
|
||||
{
|
||||
return getBeans(WebSocketSession.class);
|
||||
|
@ -341,12 +341,12 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
{
|
||||
return defaultPolicy;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public SslContextFactory getSslContextFactory()
|
||||
{
|
||||
/* Not relevant for a Server, as this is defined in the
|
||||
* Connector configuration
|
||||
* Connector configuration
|
||||
*/
|
||||
return null;
|
||||
}
|
||||
|
@ -358,10 +358,10 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
{
|
||||
this.objectFactory = new DecoratedObjectFactory();
|
||||
}
|
||||
|
||||
|
||||
this.executor = context.getServer().getThreadPool();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void init(ServletContext context) throws ServletException
|
||||
{
|
||||
|
@ -371,7 +371,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
{
|
||||
this.objectFactory = new DecoratedObjectFactory();
|
||||
}
|
||||
|
||||
|
||||
// Validate Environment
|
||||
ContextHandler handler = ContextHandler.getContextHandler(context);
|
||||
|
||||
|
@ -381,7 +381,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
}
|
||||
|
||||
this.executor = handler.getServer().getThreadPool();
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// start lifecycle
|
||||
|
@ -465,23 +465,6 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
addManaged(session);
|
||||
}
|
||||
|
||||
protected String[] parseProtocols(String protocol)
|
||||
{
|
||||
if (protocol == null)
|
||||
{
|
||||
return new String[]{null};
|
||||
}
|
||||
protocol = protocol.trim();
|
||||
if (protocol.length() == 0)
|
||||
{
|
||||
return new String[]{null};
|
||||
}
|
||||
String[] passed = protocol.split("\\s*,\\s*");
|
||||
String[] protocols = new String[passed.length + 1];
|
||||
System.arraycopy(passed, 0, protocols, 0, passed.length);
|
||||
return protocols;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(Class<?> websocketPojo)
|
||||
{
|
||||
|
@ -594,7 +577,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
WebSocketSession session = createSession(request.getRequestURI(), driver, wsConnection);
|
||||
session.setPolicy(driver.getPolicy());
|
||||
session.setUpgradeRequest(request);
|
||||
// set true negotiated extension list back to response
|
||||
// set true negotiated extension list back to response
|
||||
response.setExtensions(extensionStack.getNegotiatedExtensions());
|
||||
session.setUpgradeResponse(response);
|
||||
wsConnection.addListener(session);
|
||||
|
@ -610,7 +593,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
// Start Components
|
||||
session.addManaged(extensionStack);
|
||||
this.addManaged(session);
|
||||
|
||||
|
||||
if (session.isFailed())
|
||||
{
|
||||
throw new IOException("Session failed to start");
|
||||
|
@ -624,7 +607,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
|
||||
if (getSendServerVersion(connector))
|
||||
response.setHeader("Server",HttpConfiguration.SERVER_VERSION);
|
||||
|
||||
|
||||
// Process (version specific) handshake response
|
||||
handshaker.doHandshakeResponse(request, response);
|
||||
|
||||
|
|
Loading…
Reference in New Issue