Java Code Examples for org.apache.catalina.startup.Tomcat#getHost()

The following examples show how to use org.apache.catalina.startup.Tomcat#getHost() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestReplicatedContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug57425() throws LifecycleException, IOException {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
    }

    File root = new File("test/webapp");
    Context context = tomcat.addWebapp(host, "", root.getAbsolutePath());

    Tomcat.addServlet(context, "test", new AccessContextServlet());
    context.addServletMappingDecoded("/access", "test");

    tomcat.start();

    ByteChunk result = getUrl("http://localhost:" + getPort() + "/access");

    Assert.assertEquals("OK", result.toString());

}
 
Example 2
Source File: TestReplicatedContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug57425() throws LifecycleException, IOException {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
    }

    File root = new File("test/webapp-3.0");
    Context context = tomcat.addWebapp(host, "", root.getAbsolutePath());

    Tomcat.addServlet(context, "test", new AccessContextServlet());
    context.addServletMapping("/access", "test");

    tomcat.start();

    ByteChunk result = getUrl("http://localhost:" + getPort() + "/access");

    Assert.assertEquals("OK", result.toString());

}
 
Example 3
Source File: TestReplicatedContext.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug57425() throws LifecycleException, IOException {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
    }

    File root = new File("test/webapp-3.0");
    Context context = tomcat.addWebapp(host, "", root.getAbsolutePath());

    Tomcat.addServlet(context, "test", new AccessContextServlet());
    context.addServletMapping("/access", "test");

    tomcat.start();

    ByteChunk result = getUrl("http://localhost:" + getPort() + "/access");

    Assert.assertEquals("OK", result.toString());

}
 
Example 4
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testFlagFailCtxIfServletStartFails() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File docBase = new File(System.getProperty("java.io.tmpdir"));
    StandardContext context = (StandardContext) tomcat.addContext("",
            docBase.getAbsolutePath());

    // first we test the flag itself, which can be set on the Host and
    // Context
    Assert.assertFalse(context.getComputedFailCtxIfServletStartFails());

    StandardHost host = (StandardHost) tomcat.getHost();
    host.setFailCtxIfServletStartFails(true);
    Assert.assertTrue(context.getComputedFailCtxIfServletStartFails());
    context.setFailCtxIfServletStartFails(Boolean.FALSE);
    Assert.assertFalse("flag on Context should override Host config",
            context.getComputedFailCtxIfServletStartFails());

    // second, we test the actual effect of the flag on the startup
    Wrapper servlet = Tomcat.addServlet(context, "myservlet",
            new FailingStartupServlet());
    servlet.setLoadOnStartup(1);

    tomcat.start();
    Assert.assertTrue("flag false should not fail deployment", context.getState()
            .isAvailable());

    tomcat.stop();
    Assert.assertFalse(context.getState().isAvailable());

    host.removeChild(context);
    context = (StandardContext) tomcat.addContext("",
            docBase.getAbsolutePath());
    servlet = Tomcat.addServlet(context, "myservlet",
            new FailingStartupServlet());
    servlet.setLoadOnStartup(1);
    tomcat.start();
    Assert.assertFalse("flag true should fail deployment", context.getState()
            .isAvailable());
}
 
Example 5
Source File: TestStandardContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlagFailCtxIfServletStartFails() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File docBase = new File(System.getProperty("java.io.tmpdir"));
    StandardContext context = (StandardContext) tomcat.addContext("",
            docBase.getAbsolutePath());

    // first we test the flag itself, which can be set on the Host and
    // Context
    assertFalse(context.getComputedFailCtxIfServletStartFails());

    StandardHost host = (StandardHost) tomcat.getHost();
    host.setFailCtxIfServletStartFails(true);
    assertTrue(context.getComputedFailCtxIfServletStartFails());
    context.setFailCtxIfServletStartFails(Boolean.FALSE);
    assertFalse("flag on Context should override Host config",
            context.getComputedFailCtxIfServletStartFails());

    // second, we test the actual effect of the flag on the startup
    Wrapper servlet = Tomcat.addServlet(context, "myservlet",
            new FailingStartupServlet());
    servlet.setLoadOnStartup(1);

    tomcat.start();
    assertTrue("flag false should not fail deployment", context.getState()
            .isAvailable());

    tomcat.stop();
    assertFalse(context.getState().isAvailable());

    host.removeChild(context);
    context = (StandardContext) tomcat.addContext("",
            docBase.getAbsolutePath());
    servlet = Tomcat.addServlet(context, "myservlet",
            new FailingStartupServlet());
    servlet.setLoadOnStartup(1);
    tomcat.start();
    assertFalse("flag true should fail deployment", context.getState()
            .isAvailable());
}
 
Example 6
Source File: Main.java    From executable-embeded-tomcat-sample with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws ServletException, LifecycleException, IOException {

		String hostName = "localhost";
		int port = 8080;
		String contextPath = "";

		String tomcatBaseDir = TomcatUtil.createTempDir("tomcat", port).getAbsolutePath();
		String contextDocBase = TomcatUtil.createTempDir("tomcat-docBase", port).getAbsolutePath();

		Tomcat tomcat = new Tomcat();
		tomcat.setBaseDir(tomcatBaseDir);

		tomcat.setPort(port);
		tomcat.setHostname(hostName);

		Host host = tomcat.getHost();
		Context context = tomcat.addWebapp(host, contextPath, contextDocBase, new EmbededContextConfig());

		context.setJarScanner(new EmbededStandardJarScanner());

		ClassLoader classLoader = Main.class.getClassLoader();
		context.setParentClassLoader(classLoader);

		// context load WEB-INF/web.xml from classpath
		context.addLifecycleListener(new WebXmlMountListener());

		tomcat.start();
		tomcat.getServer().await();
	}
 
Example 7
Source File: TestStandardContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlagFailCtxIfServletStartFails() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File docBase = new File(System.getProperty("java.io.tmpdir"));
    StandardContext context = (StandardContext) tomcat.addContext("",
            docBase.getAbsolutePath());

    // first we test the flag itself, which can be set on the Host and
    // Context
    assertFalse(context.getComputedFailCtxIfServletStartFails());

    StandardHost host = (StandardHost) tomcat.getHost();
    host.setFailCtxIfServletStartFails(true);
    assertTrue(context.getComputedFailCtxIfServletStartFails());
    context.setFailCtxIfServletStartFails(Boolean.FALSE);
    assertFalse("flag on Context should override Host config",
            context.getComputedFailCtxIfServletStartFails());

    // second, we test the actual effect of the flag on the startup
    Wrapper servlet = Tomcat.addServlet(context, "myservlet",
            new FailingStartupServlet());
    servlet.setLoadOnStartup(1);

    tomcat.start();
    assertTrue("flag false should not fail deployment", context.getState()
            .isAvailable());

    tomcat.stop();
    assertFalse(context.getState().isAvailable());

    host.removeChild(context);
    context = (StandardContext) tomcat.addContext("",
            docBase.getAbsolutePath());
    servlet = Tomcat.addServlet(context, "myservlet",
            new FailingStartupServlet());
    servlet.setLoadOnStartup(1);
    tomcat.start();
    assertFalse("flag true should fail deployment", context.getState()
            .isAvailable());
}