use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.
the class TestJNDI method testThreadContextClassloaderAndCurrentContext.
@Test
public void testThreadContextClassloaderAndCurrentContext() throws Exception {
//create a jetty context, and start it so that its classloader it created
//and it is the current context
ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
ContextHandler ch = new ContextHandler();
URLClassLoader chLoader = new URLClassLoader(new URL[0], currentLoader);
ch.setClassLoader(chLoader);
Server server = new Server();
HandlerList hl = new HandlerList();
server.setHandler(hl);
hl.addHandler(ch);
//Create another one
ContextHandler ch2 = new ContextHandler();
URLClassLoader ch2Loader = new URLClassLoader(new URL[0], currentLoader);
ch2.setClassLoader(ch2Loader);
hl.addHandler(ch2);
try {
ch.setContextPath("/ch");
ch.addEventListener(new ServletContextListener() {
private Context comp;
private Object testObj = new Object();
public void contextInitialized(ServletContextEvent sce) {
try {
InitialContext initCtx = new InitialContext();
Context java = (Context) initCtx.lookup("java:");
assertNotNull(java);
comp = (Context) initCtx.lookup("java:comp");
assertNotNull(comp);
Context env = ((Context) comp).createSubcontext("env");
assertNotNull(env);
env.bind("ch", testObj);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public void contextDestroyed(ServletContextEvent sce) {
try {
assertNotNull(comp);
assertEquals(testObj, comp.lookup("env/ch"));
comp.destroySubcontext("env");
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
});
//Starting the context makes it current and creates a classloader for it
ch.start();
ch2.setContextPath("/ch2");
ch2.addEventListener(new ServletContextListener() {
private Context comp;
private Object testObj = new Object();
public void contextInitialized(ServletContextEvent sce) {
try {
InitialContext initCtx = new InitialContext();
comp = (Context) initCtx.lookup("java:comp");
assertNotNull(comp);
//another context's bindings should not be visible
Context env = ((Context) comp).createSubcontext("env");
try {
env.lookup("ch");
fail("java:comp/env visible from another context!");
} catch (NameNotFoundException e) {
//expected
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public void contextDestroyed(ServletContextEvent sce) {
try {
assertNotNull(comp);
comp.destroySubcontext("env");
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
});
//make the new context the current one
ch2.start();
} finally {
ch.stop();
ch2.stop();
Thread.currentThread().setContextClassLoader(currentLoader);
}
}
use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.
the class ServerConnectorTest method testReuseAddress_Default.
@Test
public void testReuseAddress_Default() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.addConnector(connector);
HandlerList handlers = new HandlerList();
handlers.addHandler(new ReuseInfoHandler());
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);
try {
server.start();
URI uri = toServerURI(connector);
String response = getResponse(uri);
assertThat("Response", response, containsString("connector.getReuseAddress() = true"));
assertThat("Response", response, containsString("connector._reuseAddress() = true"));
// Java on Windows is incapable of propagating reuse-address this to the opened socket.
if (!OS.IS_WINDOWS) {
assertThat("Response", response, containsString("socket.getReuseAddress() = true"));
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.
the class ServerConnectorTest method testReuseAddress_True.
@Test
public void testReuseAddress_True() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
connector.setReuseAddress(true);
server.addConnector(connector);
HandlerList handlers = new HandlerList();
handlers.addHandler(new ReuseInfoHandler());
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);
try {
server.start();
URI uri = toServerURI(connector);
String response = getResponse(uri);
assertThat("Response", response, containsString("connector.getReuseAddress() = true"));
assertThat("Response", response, containsString("connector._reuseAddress() = true"));
// Java on Windows is incapable of propagating reuse-address this to the opened socket.
if (!OS.IS_WINDOWS) {
assertThat("Response", response, containsString("socket.getReuseAddress() = true"));
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.
the class ServerConnectorTest method testReuseAddress_False.
@Test
public void testReuseAddress_False() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
connector.setReuseAddress(false);
server.addConnector(connector);
HandlerList handlers = new HandlerList();
handlers.addHandler(new ReuseInfoHandler());
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);
try {
server.start();
URI uri = toServerURI(connector);
String response = getResponse(uri);
assertThat("Response", response, containsString("connector.getReuseAddress() = false"));
assertThat("Response", response, containsString("connector._reuseAddress() = false"));
// Java on Windows is incapable of propagating reuse-address this to the opened socket.
if (!OS.IS_WINDOWS) {
assertThat("Response", response, containsString("socket.getReuseAddress() = false"));
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.
the class WebAppContextTest method testNullPath.
@Test
public void testNullPath() throws Exception {
Server server = new Server(0);
HandlerList handlers = new HandlerList();
ContextHandlerCollection contexts = new ContextHandlerCollection();
WebAppContext context = new WebAppContext();
context.setBaseResource(Resource.newResource("./src/test/webapp"));
context.setContextPath("/");
server.setHandler(handlers);
handlers.addHandler(contexts);
contexts.addHandler(context);
LocalConnector connector = new LocalConnector(server);
server.addConnector(connector);
server.start();
try {
String response = connector.getResponses("GET http://localhost:8080 HTTP/1.1\r\nHost: localhost:8080\r\nConnection: close\r\n\r\n");
Assert.assertTrue(response.indexOf("200 OK") >= 0);
} finally {
server.stop();
}
}
Aggregations