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

The following examples show how to use org.apache.catalina.startup.Tomcat#destroy() . 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: DubboApplicationContextInitializerTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpringContextLoaderListenerInWebXml() throws Exception {
    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir("target/test-classes");
    tomcat.setPort(12345);
    StandardContext context = new StandardContext();
    context.setName("test");
    context.setDocBase("test");
    context.setPath("/test");
    context.addLifecycleListener(new ContextConfig());
    tomcat.getHost().addChild(context);
    tomcat.start();
    // there should be 1 application listener
    Assert.assertEquals(1, context.getApplicationLifecycleListeners().length);
    // the first one should be Spring's built in ContextLoaderListener.
    Assert.assertTrue(context.getApplicationLifecycleListeners()[0] instanceof ContextLoaderListener);
    tomcat.stop();
    tomcat.destroy();
}
 
Example 2
Source File: DubboApplicationContextInitializerTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoListenerInWebXml() throws Exception {
    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir("target/test-classes");
    tomcat.setPort(12345);
    StandardContext context = new StandardContext();
    context.setName("test2");
    context.setDocBase("test2");
    context.setPath("/test2");
    context.addLifecycleListener(new ContextConfig());
    tomcat.getHost().addChild(context);
    tomcat.start();
    // there should be 1 application listener
    Assert.assertEquals(1, context.getApplicationLifecycleListeners().length);
    // the first one should be Spring's built in ContextLoaderListener.
    Assert.assertTrue(context.getApplicationLifecycleListeners()[0] instanceof ContextLoaderListener);
    tomcat.stop();
    tomcat.destroy();
}
 
Example 3
Source File: DubboApplicationContextInitializerTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetadataComplete() throws Exception {
    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir("target/test-classes");
    tomcat.setPort(12345);
    StandardContext context = new StandardContext();
    context.setName("test3");
    context.setDocBase("test3");
    context.setPath("/test3");
    context.addLifecycleListener(new ContextConfig());
    tomcat.getHost().addChild(context);
    tomcat.start();
    // there should be no application listeners
    Assert.assertEquals(0, context.getApplicationLifecycleListeners().length);
    tomcat.stop();
    tomcat.destroy();
}
 
Example 4
Source File: TomcatTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
private static void test(final Tomcat tomcatServer) throws IOException, LifecycleException {
  tomcatServer.start();
  logger.info("Tomcat server: http://" + tomcatServer.getHost().getName() + ":" + serverPort + "/");

  final URL url = new URL("http://localhost:" + serverPort + "/test");
  final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setRequestMethod("GET");
  final int responseCode = connection.getResponseCode();

  if (HttpServletResponse.SC_OK != responseCode)
    throw new AssertionError("ERROR: response: " + responseCode);

  tomcatServer.stop();
  tomcatServer.destroy();
  TestUtil.checkSpan(true, new ComponentSpanCount("java-web-servlet", 1), new ComponentSpanCount("http-url-connection", 1));
}
 
Example 5
Source File: JsfRenderIT.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@Override
public void executeApp() throws Exception {
    int port = getAvailablePort();
    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir("target/tomcat");
    tomcat.setPort(port);
    Context context =
            tomcat.addWebapp("", new File("src/test/resources").getAbsolutePath());

    WebappLoader webappLoader = new WebappLoader(RenderJsfInTomcat.class.getClassLoader());
    context.setLoader(webappLoader);

    tomcat.start();

    doTest(port);

    tomcat.stop();
    tomcat.destroy();
}
 
Example 6
Source File: GrailsIT.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@Override
public void executeApp() throws Exception {
    int port = getAvailablePort();
    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir("target/tomcat");
    tomcat.setPort(port);
    Context context =
            tomcat.addWebapp("", new File("src/test/resources").getAbsolutePath());

    WebappLoader webappLoader = new WebappLoader(RenderInTomcat.class.getClassLoader());
    context.setLoader(webappLoader);

    // this is needed in order for Tomcat to find annotated classes
    VirtualDirContext resources = new VirtualDirContext();
    resources.setExtraResourcePaths("/WEB-INF/classes=target/test-classes");
    context.setResources(resources);

    tomcat.start();

    doTest(port);

    tomcat.stop();
    tomcat.destroy();
}
 
Example 7
Source File: InvokeJaxwsWebServiceInTomcat.java    From glowroot with Apache License 2.0 6 votes vote down vote up
public void executeApp(String webapp, String contextPath, String url) throws Exception {
    int port = getAvailablePort();
    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir("target/tomcat");
    tomcat.setPort(port);
    Context context = tomcat.addWebapp(contextPath,
            new File("src/test/resources/" + webapp).getAbsolutePath());

    WebappLoader webappLoader =
            new WebappLoader(InvokeJaxwsWebServiceInTomcat.class.getClassLoader());
    context.setLoader(webappLoader);

    tomcat.start();

    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(ForBothHelloAndRootService.class);
    factory.setAddress("http://localhost:" + port + contextPath + url);
    ForBothHelloAndRootService client = (ForBothHelloAndRootService) factory.create();
    client.echo("abc");

    checkForRequestThreads(webappLoader);
    tomcat.stop();
    tomcat.destroy();
}
 
Example 8
Source File: InvokeJaxrsResourceInTomcat.java    From glowroot with Apache License 2.0 6 votes vote down vote up
public void executeApp(String webapp, String contextPath, String url) throws Exception {
    int port = getAvailablePort();
    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir("target/tomcat");
    tomcat.setPort(port);
    Context context = tomcat.addWebapp(contextPath,
            new File("src/test/resources/" + webapp).getAbsolutePath());

    WebappLoader webappLoader =
            new WebappLoader(InvokeJaxrsResourceInTomcat.class.getClassLoader());
    context.setLoader(webappLoader);

    tomcat.start();
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + contextPath + url)
            .execute().get().getStatusCode();
    asyncHttpClient.close();
    if (statusCode != 200) {
        throw new IllegalStateException("Unexpected status code: " + statusCode);
    }

    tomcat.stop();
    tomcat.destroy();
}
 
Example 9
Source File: TokenExpiryTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: AbstractOIDCTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) throws LifecycleException {
    if (server != null && server.getServer() != null
        && server.getServer().getState() != LifecycleState.DESTROYED) {
        if (server.getServer().getState() != LifecycleState.STOPPED) {
            server.stop();
        }
        server.destroy();
    }
}
 
Example 11
Source File: TokenExpiryTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 12
Source File: SpringTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 13
Source File: InvokeSpringControllerInTomcat.java    From glowroot with Apache License 2.0 5 votes vote down vote up
public void executeApp(String webapp, String contextPath, RunnableWithPort runnable)
        throws Exception {
    int port = getAvailablePort();
    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir("target/tomcat");
    tomcat.setPort(port);
    Context context = tomcat.addWebapp(contextPath,
            new File("src/test/resources/" + webapp).getAbsolutePath());

    WebappLoader webappLoader =
            new WebappLoader(InvokeSpringControllerInTomcat.class.getClassLoader());
    context.setLoader(webappLoader);

    tomcat.start();

    runnable.run(port);

    // spring still does a bit of work after the response is concluded,
    // see org.springframework.web.servlet.FrameworkServlet.publishRequestHandledEvent(),
    // so give a bit of time here, otherwise end up with sporadic test failures due to
    // ERROR logged by org.apache.catalina.loader.WebappClassLoaderBase, e.g.
    // "The web application [] is still processing a request that has yet to finish"
    MILLISECONDS.sleep(200);
    checkForRequestThreads(webappLoader);
    tomcat.stop();
    tomcat.destroy();
}
 
Example 14
Source File: WSFedTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: WSFedTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 16
Source File: TomcatLauncher.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) throws LifecycleException {
    if (server != null && server.getServer() != null
        && server.getServer().getState() != LifecycleState.DESTROYED) {
        if (server.getServer().getState() != LifecycleState.STOPPED) {
            server.stop();
        }
        server.destroy();
    }
}
 
Example 17
Source File: CXFTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 18
Source File: KerberosTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 19
Source File: JCacheFilterTest.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
private void stop(final Tomcat tomcat) throws LifecycleException {
    if (LifecycleState.STARTED.equals(tomcat.getServer().getState())) {
        tomcat.stop();
        tomcat.destroy();
    }
}
 
Example 20
Source File: JspRenderIT.java    From glowroot with Apache License 2.0 3 votes vote down vote up
@Override
public void executeApp() throws Exception {
    int port = getAvailablePort();
    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir("target/tomcat");
    tomcat.setPort(port);
    Context context =
            tomcat.addContext("", new File("src/test/resources").getAbsolutePath());

    WebappLoader webappLoader = new WebappLoader(RenderJspInTomcat.class.getClassLoader());
    context.setLoader(webappLoader);

    Tomcat.addServlet(context, "hello", new ForwardingServlet());
    context.addServletMapping("/hello", "hello");
    Tomcat.addServlet(context, "jsp", new JspServlet());
    context.addServletMapping("*.jsp", "jsp");

    tomcat.start();
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + "/hello")
            .execute().get().getStatusCode();
    asyncHttpClient.close();
    if (statusCode != 200) {
        throw new IllegalStateException("Unexpected status code: " + statusCode);
    }
    tomcat.stop();
    tomcat.destroy();
}