Change the WSServer to allow deployment of multiple webapps

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-04-24 10:18:24 +10:00
parent 53246f977c
commit 7c2d804ce9
10 changed files with 137 additions and 167 deletions

View File

@ -20,7 +20,6 @@ package org.eclipse.jetty.websocket.javax.tests;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
@ -29,7 +28,6 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.IO;
import org.eclipse.jetty.toolchain.test.JAR;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.resource.PathResource;
@ -39,7 +37,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
@ -50,22 +47,77 @@ import static org.hamcrest.Matchers.notNullValue;
public class WSServer extends LocalServer implements LocalFuzzer.Provider
{
private static final Logger LOG = LoggerFactory.getLogger(WSServer.class);
private final Path contextDir;
private final String contextPath;
private ContextHandlerCollection contexts;
private Path webinf;
private Path classesDir;
private final Path testDir;
private ContextHandlerCollection contexts = new ContextHandlerCollection();
public WSServer(File testdir, String contextName)
public WSServer(Path testDir)
{
this(testdir.toPath(), contextName);
this.testDir = testDir;
}
public WSServer(Path testdir, String contextName)
public WebApp createWebApp(String contextName)
{
this.contextDir = testdir.resolve(contextName);
this.contextPath = "/" + contextName;
return new WebApp(contextName);
}
@Override
protected Handler createRootHandler(Server server)
{
return contexts;
}
public class WebApp
{
private final WebAppContext context;
private final Path contextDir;
private Path classesDir;
private final Path webInf;
private WebApp(String contextName)
{
// Ensure context directory.
contextDir = testDir.resolve(contextName);
FS.ensureEmpty(contextDir);
// Ensure WEB-INF.
webInf = contextDir.resolve("WEB-INF");
FS.ensureDirExists(webInf);
classesDir = webInf.resolve("classes");
FS.ensureDirExists(classesDir);
// Configure the WebAppContext.
context = new WebAppContext();
context.setContextPath("/" + contextName);
context.setBaseResource(new PathResource(contextDir));
context.setAttribute("org.eclipse.jetty.websocket.javax", Boolean.TRUE);
context.addConfiguration(new JavaxWebSocketConfiguration());
}
public WebAppContext getWebAppContext()
{
return context;
}
public String getContextPath()
{
return context.getContextPath();
}
public Path getContextDir()
{
return contextDir;
}
public void createWebInf() throws IOException
{
copyWebInf("empty-web.xml");
}
public void copyWebInf(String testResourceName) throws IOException
{
File testWebXml = MavenTestingUtils.getTestResourceFile(testResourceName);
Path webXml = webInf.resolve("web.xml");
IO.copy(testWebXml, webXml.toFile());
}
public void copyClass(Class<?> clazz) throws Exception
@ -80,82 +132,13 @@ public class WSServer extends LocalServer implements LocalFuzzer.Provider
IO.copy(srcFile, destFile.toFile());
}
public void copyEndpoint(Class<?> endpointClass) throws Exception
public void deploy()
{
copyClass(endpointClass);
}
public void copyLib(Class<?> clazz, String jarFileName) throws URISyntaxException, IOException
{
webinf = contextDir.resolve("WEB-INF");
FS.ensureDirExists(webinf);
Path libDir = webinf.resolve("lib");
FS.ensureDirExists(libDir);
Path jarFile = libDir.resolve(jarFileName);
URL codeSourceURL = clazz.getProtectionDomain().getCodeSource().getLocation();
assertThat("Class CodeSource URL is file scheme", codeSourceURL.getProtocol(), is("file"));
File sourceCodeSourceFile = new File(codeSourceURL.toURI());
if (sourceCodeSourceFile.isDirectory())
{
LOG.info("Creating " + jarFile + " from " + sourceCodeSourceFile);
JAR.create(sourceCodeSourceFile, jarFile.toFile());
}
else
{
LOG.info("Copying " + sourceCodeSourceFile + " to " + jarFile);
IO.copy(sourceCodeSourceFile, jarFile.toFile());
}
}
public void copyWebInf(String testResourceName) throws IOException
{
webinf = contextDir.resolve("WEB-INF");
FS.ensureDirExists(webinf);
classesDir = webinf.resolve("classes");
FS.ensureDirExists(classesDir);
Path webxml = webinf.resolve("web.xml");
File testWebXml = MavenTestingUtils.getTestResourceFile(testResourceName);
IO.copy(testWebXml, webxml.toFile());
}
public WebAppContext createWebAppContext() throws IOException
{
WebAppContext context = new WebAppContext();
context.setContextPath(this.contextPath);
context.setBaseResource(new PathResource(this.contextDir));
context.setAttribute("org.eclipse.jetty.websocket.javax", Boolean.TRUE);
context.addConfiguration(new JavaxWebSocketConfiguration());
return context;
}
public void createWebInf() throws IOException
{
copyWebInf("empty-web.xml");
}
public void deployWebapp(WebAppContext webapp) throws Exception
{
contexts.addHandler(webapp);
contexts.manage(webapp);
webapp.setThrowUnavailableOnStartupException(true);
webapp.start();
contexts.addHandler(context);
contexts.manage(context);
context.setThrowUnavailableOnStartupException(true);
if (LOG.isDebugEnabled())
{
LOG.debug("{}", webapp.dump());
LOG.debug("{}", context.dump());
}
}
public Path getWebAppDir()
{
return this.contextDir;
}
@Override
protected Handler createRootHandler(Server server) throws Exception
{
contexts = new ContextHandlerCollection();
return contexts;
}
}

View File

@ -24,7 +24,6 @@ import java.util.List;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
@ -50,22 +49,21 @@ public class AltFilterTest
@Test
public void testEcho() throws Exception
{
WSServer wsb = new WSServer(testdir.getPath(), "app");
wsb.copyWebInf("alt-filter-web.xml");
WSServer wsb = new WSServer(testdir.getPath());
WSServer.WebApp app = wsb.createWebApp("app");
app.copyWebInf("alt-filter-web.xml");
// the endpoint (extends javax.websocket.Endpoint)
wsb.copyClass(BasicEchoSocket.class);
app.copyClass(BasicEchoSocket.class);
app.deploy();
try
{
wsb.start();
WebAppContext webapp = wsb.createWebAppContext();
wsb.deployWebapp(webapp);
FilterHolder filterWebXml = webapp.getServletHandler().getFilter("wsuf-test");
FilterHolder filterWebXml = app.getWebAppContext().getServletHandler().getFilter("wsuf-test");
assertThat("Filter[wsuf-test]", filterWebXml, notNullValue());
FilterHolder filterSCI = webapp.getServletHandler().getFilter("Jetty_WebSocketUpgradeFilter");
FilterHolder filterSCI = app.getWebAppContext().getServletHandler().getFilter("Jetty_WebSocketUpgradeFilter");
assertThat("Filter[Jetty_WebSocketUpgradeFilter]", filterSCI, nullValue());
List<Frame> send = new ArrayList<>();

View File

@ -28,7 +28,6 @@ import javax.websocket.WebSocketContainer;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.javax.tests.EventSocket;
import org.eclipse.jetty.websocket.javax.tests.WSServer;
import org.junit.jupiter.api.AfterEach;
@ -61,12 +60,13 @@ public class ContainerProviderServerTest
public void startServer() throws Exception
{
Path testdir = MavenTestingUtils.getTargetTestingPath(ContainerProviderServerTest.class.getName());
server = new WSServer(testdir, "app");
server.createWebInf();
server.copyEndpoint(MySocket.class);
server = new WSServer(testdir);
WSServer.WebApp app = server.createWebApp("app");
app.createWebInf();
app.copyClass(MySocket.class);
app.deploy();
server.start();
WebAppContext webapp = server.createWebAppContext();
server.deployWebapp(webapp);
}
@AfterEach

View File

@ -27,7 +27,6 @@ import com.acme.websocket.BasicEchoEndpointConfigContextListener;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
@ -56,21 +55,20 @@ public class EndpointViaConfigTest
@Test
public void testEcho() throws Exception
{
WSServer wsb = new WSServer(testdir.getPath(), "app");
wsb.copyWebInf("basic-echo-endpoint-config-web.xml");
WSServer wsb = new WSServer(testdir.getPath());
WSServer.WebApp app = wsb.createWebApp("app");
app.copyWebInf("basic-echo-endpoint-config-web.xml");
// the endpoint (extends javax.websocket.Endpoint)
wsb.copyClass(BasicEchoEndpoint.class);
app.copyClass(BasicEchoEndpoint.class);
// the configuration (adds the endpoint)
wsb.copyClass(BasicEchoEndpointConfigContextListener.class);
app.copyClass(BasicEchoEndpointConfigContextListener.class);
app.deploy();
try
{
wsb.start();
URI uri = wsb.getWsUri();
WebAppContext webapp = wsb.createWebAppContext();
wsb.deployWebapp(webapp);
WebSocketCoreClient client = new WebSocketCoreClient();
try
{

View File

@ -28,7 +28,6 @@ import com.acme.websocket.IdleTimeoutOnOpenEndpoint;
import com.acme.websocket.IdleTimeoutOnOpenSocket;
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.core.CloseStatus;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
@ -51,19 +50,18 @@ public class IdleTimeoutTest
@BeforeAll
public static void setupServer() throws Exception
{
server = new WSServer(MavenTestingUtils.getTargetTestingPath(IdleTimeoutTest.class.getName()), "app");
server.copyWebInf("idle-timeout-config-web.xml");
server = new WSServer(MavenTestingUtils.getTargetTestingPath(IdleTimeoutTest.class.getName()));
WSServer.WebApp app = server.createWebApp("app");
app.copyWebInf("idle-timeout-config-web.xml");
// the endpoint (extends javax.websocket.Endpoint)
server.copyClass(IdleTimeoutOnOpenEndpoint.class);
app.copyClass(IdleTimeoutOnOpenEndpoint.class);
// the configuration that adds the endpoint
server.copyClass(IdleTimeoutContextListener.class);
app.copyClass(IdleTimeoutContextListener.class);
// the annotated socket
server.copyClass(IdleTimeoutOnOpenSocket.class);
app.copyClass(IdleTimeoutOnOpenSocket.class);
app.deploy();
server.start();
WebAppContext webapp = server.createWebAppContext();
server.deployWebapp(webapp);
}
@AfterAll

View File

@ -29,7 +29,6 @@ import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
@ -63,18 +62,17 @@ public class LargeAnnotatedTest
@Test
public void testEcho() throws Exception
{
WSServer wsb = new WSServer(testdir.getPath(), "app");
wsb.createWebInf();
wsb.copyEndpoint(LargeEchoConfiguredSocket.class);
WSServer wsb = new WSServer(testdir.getPath());
WSServer.WebApp app = wsb.createWebApp("app");
app.createWebInf();
app.copyClass(LargeEchoConfiguredSocket.class);
app.deploy();
try
{
wsb.start();
URI uri = wsb.getWsUri();
WebAppContext webapp = wsb.createWebAppContext();
wsb.deployWebapp(webapp);
WebSocketCoreClient client = new WebSocketCoreClient();
try
{

View File

@ -28,7 +28,6 @@ import com.acme.websocket.LargeEchoDefaultSocket;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
@ -53,18 +52,17 @@ public class LargeContainerTest
@Test
public void testEcho() throws Exception
{
WSServer wsb = new WSServer(testdir.getPath(), "app");
wsb.copyWebInf("large-echo-config-web.xml");
wsb.copyEndpoint(LargeEchoDefaultSocket.class);
WSServer wsb = new WSServer(testdir.getPath());
WSServer.WebApp app = wsb.createWebApp("app");
app.copyWebInf("large-echo-config-web.xml");
app.copyClass(LargeEchoDefaultSocket.class);
app.deploy();
try
{
wsb.start();
URI uri = wsb.getWsUri();
WebAppContext webapp = wsb.createWebAppContext();
wsb.deployWebapp(webapp);
WebSocketCoreClient client = new WebSocketCoreClient();
try
{

View File

@ -31,7 +31,6 @@ import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
@ -87,18 +86,17 @@ public class OnMessageReturnTest
@Test
public void testEchoReturn() throws Exception
{
WSServer wsb = new WSServer(testdir.getPath(), "app");
wsb.copyWebInf("empty-web.xml");
wsb.copyClass(EchoReturnEndpoint.class);
WSServer wsb = new WSServer(testdir.getPath());
WSServer.WebApp app = wsb.createWebApp("app");
app.copyWebInf("empty-web.xml");
app.copyClass(EchoReturnEndpoint.class);
app.deploy();
try
{
wsb.start();
URI uri = wsb.getWsUri();
WebAppContext webapp = wsb.createWebAppContext();
wsb.deployWebapp(webapp);
WebSocketCoreClient client = new WebSocketCoreClient();
try
{

View File

@ -30,7 +30,6 @@ import com.acme.websocket.PongMessageEndpoint;
import com.acme.websocket.PongSocket;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
@ -55,17 +54,16 @@ public class PingPongTest
public static void startServer() throws Exception
{
Path testdir = MavenTestingUtils.getTargetTestingPath(PingPongTest.class.getName());
server = new WSServer(testdir, "app");
server.copyWebInf("pong-config-web.xml");
server = new WSServer(testdir);
server.copyClass(PongContextListener.class);
server.copyClass(PongMessageEndpoint.class);
server.copyClass(PongSocket.class);
WSServer.WebApp app = server.createWebApp("app");
app.copyWebInf("pong-config-web.xml");
app.copyClass(PongContextListener.class);
app.copyClass(PongMessageEndpoint.class);
app.copyClass(PongSocket.class);
app.deploy();
server.start();
WebAppContext webapp = server.createWebAppContext();
server.deployWebapp(webapp);
}
@BeforeAll

View File

@ -93,12 +93,13 @@ public class WebAppClassLoaderTest
public void startServer() throws Exception
{
Path testdir = MavenTestingUtils.getTargetTestingPath(WebAppClassLoaderTest.class.getName());
server = new WSServer(testdir, "app");
server.createWebInf();
server.copyEndpoint(MySocket.class);
server = new WSServer(testdir);
WSServer.WebApp app = server.createWebApp("app");
app.createWebInf();
app.copyClass(MySocket.class);
app.deploy();
webapp = app.getWebAppContext();
server.start();
webapp = server.createWebAppContext();
server.deployWebapp(webapp);
}
@AfterEach