Issue #2376 - relax ContextHandler requirements

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2018-03-28 13:39:07 -05:00
parent 464b74ed60
commit cf5b473571
1 changed files with 72 additions and 13 deletions

View File

@ -49,11 +49,13 @@ import org.eclipse.jetty.server.HttpConnection;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.DeprecationWarning;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.websocket.api.InvalidWebSocketException;
@ -100,6 +102,14 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
private DecoratedObjectFactory objectFactory;
private WebSocketCreator creator;
/**
* Entry point for Spring Boot's MockMVC framework
*/
public WebSocketServerFactory()
{
this(WebSocketPolicy.newServerPolicy(), null, new MappedByteBufferPool());
}
public WebSocketServerFactory(ServletContext context)
{
this(context, WebSocketPolicy.newServerPolicy(), new MappedByteBufferPool());
@ -303,19 +313,14 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
@Override
protected void doStart() throws Exception
{
if(this.objectFactory == null && context != null)
{
this.objectFactory = (DecoratedObjectFactory) context.getAttribute(DecoratedObjectFactory.ATTR);
if(this.objectFactory == null)
{
throw new IllegalStateException("Unable to find required ServletContext attribute: " + DecoratedObjectFactory.ATTR);
}
this.objectFactory = findDecorator();
}
if(this.executor == null && context != null)
if(this.executor == null)
{
ContextHandler contextHandler = ContextHandler.getContextHandler(context);
this.executor = contextHandler.getServer().getThreadPool();
this.executor = findExecutor();
}
Objects.requireNonNull(this.objectFactory, DecoratedObjectFactory.class.getName());
@ -324,6 +329,60 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
super.doStart();
}
private DecoratedObjectFactory findDecorator()
{
DecoratedObjectFactory objectFactory;
if (context != null)
{
objectFactory = (DecoratedObjectFactory) context.getAttribute(DecoratedObjectFactory.ATTR);
if (objectFactory != null)
{
return objectFactory;
}
}
objectFactory = new DecoratedObjectFactory();
objectFactory.addDecorator(new DeprecationWarning());
LOG.info("No DecoratedObjectFactory provided, using new {}", objectFactory);
return objectFactory;
}
/**
* Attempt to find the Executor that should be used.
* @return the Executor that should be used.
*/
private Executor findExecutor()
{
if(context != null)
{
// Attempt to pull Executor from ServletContext attribute
Executor contextExecutor = (Executor) context.getAttribute("org.eclipse.jetty.server.Executor");
if(contextExecutor != null)
{
return contextExecutor;
}
// Attempt to pull Executor from Jetty Server, via ContextHandler
ContextHandler contextHandler = ContextHandler.getContextHandler(context);
if (contextHandler != null)
{
contextExecutor = contextHandler.getServer().getThreadPool();
if(contextExecutor != null)
{
return contextExecutor;
}
}
}
// Create a new one
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setName("WebSocketServerFactory");
addManaged(threadPool);
LOG.info("No Executor provided, using new {}", threadPool);
return threadPool;
}
@Override
public ByteBufferPool getBufferPool()
{