Java Code Examples for org.apache.catalina.Context#stop()

The following examples show how to use org.apache.catalina.Context#stop() . 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: TestStandardContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTldListener() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    File docBase = new File("test/webapp-3.0");
    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());

    // Start the context
    tomcat.start();

    // Stop the context
    ctx.stop();

    String log = TesterTldListener.getLog();
    Assert.assertTrue(log, log.contains("PASS-01"));
    Assert.assertTrue(log, log.contains("PASS-02"));
    Assert.assertFalse(log, log.contains("FAIL"));
}
 
Example 2
Source File: TestStandardContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void doTestBug51376(boolean loadOnStartUp) throws Exception {
    // Test that for a servlet that was added programmatically its
    // loadOnStartup property is honored and its init() and destroy()
    // methods are called.

    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add ServletContainerInitializer
    Bug51376SCI sci = new Bug51376SCI(loadOnStartUp);
    ctx.addServletContainerInitializer(sci, null);

    // Start the context
    tomcat.start();

    // Stop the context
    ctx.stop();

    // Make sure that init() and destroy() were called correctly
    assertTrue(sci.getServlet().isOk());
    assertTrue(loadOnStartUp == sci.getServlet().isInitCalled());
}
 
Example 3
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestBug51376(boolean loadOnStartUp) throws Exception {
    // Test that for a servlet that was added programmatically its
    // loadOnStartup property is honored and its init() and destroy()
    // methods are called.

    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add ServletContainerInitializer
    Bug51376SCI sci = new Bug51376SCI(loadOnStartUp);
    ctx.addServletContainerInitializer(sci, null);

    // Start the context
    tomcat.start();

    // Stop the context
    ctx.stop();

    // Make sure that init() and destroy() were called correctly
    Assert.assertTrue(sci.getServlet().isOk());
    Assert.assertTrue(loadOnStartUp == sci.getServlet().isInitCalled());
}
 
Example 4
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testTldListener() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    File docBase = new File("test/webapp-3.0");
    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());
    ctx.addServletContainerInitializer(new JasperInitializer(), null);

    // Start the context
    tomcat.start();

    // Stop the context
    ctx.stop();

    String log = TesterTldListener.getLog();
    Assert.assertTrue(log, log.contains("PASS-01"));
    Assert.assertTrue(log, log.contains("PASS-02"));
    Assert.assertFalse(log, log.contains("FAIL"));
}
 
Example 5
Source File: TestStandardContext.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testTldListener() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    File docBase = new File("test/webapp-3.0");
    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());

    // Start the context
    tomcat.start();

    // Stop the context
    ctx.stop();

    String log = TesterTldListener.getLog();
    Assert.assertTrue(log, log.contains("PASS-01"));
    Assert.assertTrue(log, log.contains("PASS-02"));
    Assert.assertFalse(log, log.contains("FAIL"));
}
 
Example 6
Source File: ManagerServlet.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Stop the web application at the specified context path.
 *
 * @param writer Writer to render to
 * @param cn Name of the application to be stopped
 */
protected void stop(PrintWriter writer, ContextName cn,
        StringManager smClient) {

    if (debug >= 1)
        log("stop: Stopping web application '" + cn + "'");

    if (!validateContextName(cn, writer, smClient)) {
        return;
    }

    String displayPath = cn.getDisplayName();

    try {
        Context context = (Context) host.findChild(cn.getName());
        if (context == null) {
            writer.println(smClient.getString("managerServlet.noContext",
                    RequestUtil.filter(displayPath)));
            return;
        }
        // It isn't possible for the manager to stop itself
        if (context.getName().equals(this.context.getName())) {
            writer.println(smClient.getString("managerServlet.noSelf"));
            return;
        }
        context.stop();
        writer.println(smClient.getString(
                "managerServlet.stopped", displayPath));
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log("ManagerServlet.stop[" + displayPath + "]", t);
        writer.println(smClient.getString("managerServlet.exception",
                t.toString()));
    }

}
 
Example 7
Source File: ManagerServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Stop the web application at the specified context path.
 *
 * @param writer Writer to render to
 * @param cn Name of the application to be stopped
 */
protected void stop(PrintWriter writer, ContextName cn,
        StringManager smClient) {

    if (debug >= 1)
        log("stop: Stopping web application '" + cn + "'");

    if (!validateContextName(cn, writer, smClient)) {
        return;
    }

    String displayPath = cn.getDisplayName();

    try {
        Context context = (Context) host.findChild(cn.getName());
        if (context == null) {
            writer.println(smClient.getString("managerServlet.noContext",
                    RequestUtil.filter(displayPath)));
            return;
        }
        // It isn't possible for the manager to stop itself
        if (context.getName().equals(this.context.getName())) {
            writer.println(smClient.getString("managerServlet.noSelf"));
            return;
        }
        context.stop();
        writer.println(smClient.getString(
                "managerServlet.stopped", displayPath));
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log("ManagerServlet.stop[" + displayPath + "]", t);
        writer.println(smClient.getString("managerServlet.exception",
                t.toString()));
    }

}
 
Example 8
Source File: FarmWarDeployer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Invoke the remove method on the deployer.
 */
protected void remove(String contextName) throws Exception {
    // TODO Handle remove also work dir content !
    // Stop the context first to be nicer
    Context context = (Context) host.findChild(contextName);
    if (context != null) {
        if(log.isDebugEnabled())
            log.debug(sm.getString("farmWarDeployer.undeployLocal",
                    contextName));
        context.stop();
        String baseName = context.getBaseName();
        File war = new File(getAppBase(), baseName + ".war");
        File dir = new File(getAppBase(), baseName);
        File xml = new File(configBase, baseName + ".xml");
        if (war.exists()) {
            if (!war.delete()) {
                log.error(sm.getString("farmWarDeployer.deleteFail", war));
            }
        } else if (dir.exists()) {
            undeployDir(dir);
        } else {
            if (!xml.delete()) {
                log.error(sm.getString("farmWarDeployer.deleteFail", xml));
            }
        }
        // Perform new deployment and remove internal HostConfig state
        check(contextName);
    }

}
 
Example 9
Source File: VirtualHostClusterUtil.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public static void removeVirtualHost(String hostName) {
    Engine engine = DataHolder.getInstance().getCarbonTomcatService().getTomcat().getEngine();
    Host host = (Host) engine.findChild(hostName);
    Context context = (Context) host.findChild("/");
    try {
        if (host.getState().isAvailable()) {
            if (context != null && context.getAvailable()) {
                context.setRealm(null);
                context.stop();
                context.destroy();
                log.info("Unloaded webapp from the host: " + host
                        + " as the context of: " + context);
            }
            host.removeChild(context);
            host.setRealm(null);
            host.stop();
            host.destroy();
            engine.removeChild(host);
        }
    }catch (LifecycleException e) {
        log.error("error while removing host from tomcat", e);
    }
    URLMappingHolder.getInstance().removeUrlMappingMap(
            host.getName());
    log.info("Unloaded host from the engine: " + host);

}
 
Example 10
Source File: TestWebappClassLoaderMemoryLeak.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimerThreadLeak() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    if (ctx instanceof StandardContext) {
        ((StandardContext) ctx).setClearReferencesStopTimerThreads(true);
    }

    Tomcat.addServlet(ctx, "taskServlet", new TaskServlet());
    ctx.addServletMapping("/", "taskServlet");

    tomcat.start();

    // This will trigger the timer & thread creation
    getUrl("http://localhost:" + getPort() + "/");

    // Stop the context
    ctx.stop();

    Thread[] threads = getThreads();
    for (Thread thread : threads) {
        if (thread != null && thread.isAlive() &&
                TaskServlet.TIMER_THREAD_NAME.equals(thread.getName())) {
            thread.join(5000);
            if (thread.isAlive()) {
                fail("Timer thread still running");
            }
        }
    }
}
 
Example 11
Source File: TomcatWsRegistry.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void removeWsContainer(String path, final String moduleId) {
    if (path == null) {
        return;
    }

    // assure context root with a leading slash
    if (!path.startsWith("/")) {
        path = "/" + path;
    }

    if (TomcatHelper.isStopping()) {
        return;
    }

    Context context = webserviceContexts.remove(new Key(path, moduleId));
    if (context == null) { // fake
        context = webserviceContexts.remove(new Key(path, null));
    }

    Integer refs = 1; // > 0 to avoid to destroy the context if not mandatory
    if (context != null) {
        final String name = context.getName();
        refs = fakeContextReferences.remove(name);
        if (refs != null && refs > 0) {
            fakeContextReferences.put(name, refs - 1);
        }
    }

    if ((WEBSERVICE_OLDCONTEXT_ACTIVE || (refs != null && refs == 0)) && context != null) {
        try {
            context.stop();
            context.destroy();
        } catch (final Exception e) {
            throw new TomEERuntimeException(e);
        }
        final Host host = (Host) context.getParent();
        host.removeChild(context);
    } // else let tomcat manages its context
}
 
Example 12
Source File: TestWebappClassLoaderMemoryLeak.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testTimerThreadLeak() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    if (ctx instanceof StandardContext) {
        ((StandardContext) ctx).setClearReferencesStopTimerThreads(true);
    }

    Tomcat.addServlet(ctx, "taskServlet", new TaskServlet());
    ctx.addServletMappingDecoded("/", "taskServlet");

    tomcat.start();

    // This will trigger the timer & thread creation
    getUrl("http://localhost:" + getPort() + "/");

    // Stop the context
    ctx.stop();

    Thread[] threads = getThreads();
    for (Thread thread : threads) {
        if (thread != null && thread.isAlive() &&
                TaskServlet.TIMER_THREAD_NAME.equals(thread.getName())) {
            thread.join(5000);
            if (thread.isAlive()) {
                Assert.fail("Timer thread still running");
            }
        }
    }
}
 
Example 13
Source File: TestWebappClassLoaderExecutorMemoryLeak.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testTimerThreadLeak() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    if (ctx instanceof StandardContext) {
        ((StandardContext) ctx).setClearReferencesStopThreads(true);
    }

    ExecutorServlet executorServlet = new ExecutorServlet();
    Tomcat.addServlet(ctx, "taskServlet", executorServlet);
    ctx.addServletMappingDecoded("/", "taskServlet");

    tomcat.start();

    // This will trigger the timer & thread creation
    getUrl("http://localhost:" + getPort() + "/");

    // Stop the context
    ctx.stop();

    // Should be shutdown once the stop() method above exists
    Assert.assertTrue(executorServlet.tpe.isShutdown());

    // The time taken to shutdown the executor can vary between systems. Try
    // to avoid false test failures due to timing issues. Give the executor
    // upto 10 seconds to close down.
    int count = 0;
    while (count < 100 && !executorServlet.tpe.isTerminated()) {
        count++;
        Thread.sleep(100);
    }

    // If the executor has not terminated, there is a thread/memory leak
    Assert.assertTrue(executorServlet.tpe.isTerminated());
}
 
Example 14
Source File: ManagerServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Stop the web application at the specified context path.
 *
 * @param writer Writer to render to
 * @param cn Name of the application to be stopped
 * @param smClient i18n support for current client's locale
 */
protected void stop(PrintWriter writer, ContextName cn,
        StringManager smClient) {

    if (debug >= 1)
        log("stop: Stopping web application '" + cn + "'");

    if (!validateContextName(cn, writer, smClient)) {
        return;
    }

    String displayPath = cn.getDisplayName();

    try {
        Context context = (Context) host.findChild(cn.getName());
        if (context == null) {
            writer.println(smClient.getString("managerServlet.noContext",
                    Escape.htmlElementContent(displayPath)));
            return;
        }
        // It isn't possible for the manager to stop itself
        if (context.getName().equals(this.context.getName())) {
            writer.println(smClient.getString("managerServlet.noSelf"));
            return;
        }
        context.stop();
        writer.println(smClient.getString(
                "managerServlet.stopped", displayPath));
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log("ManagerServlet.stop[" + displayPath + "]", t);
        writer.println(smClient.getString("managerServlet.exception",
                t.toString()));
    }

}
 
Example 15
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testThreadLocalLeak2() throws Exception {

    Tomcat tomcat = getTomcatInstance();
    // Need to make sure we see a leak for the right reasons
    tomcat.getServer().addLifecycleListener(
            new JreMemoryLeakPreventionListener());

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "leakServlet2",
            "org.apache.tomcat.unittest.TesterLeakingServlet2");
    ctx.addServletMapping("/leak2", "leakServlet2");

    tomcat.start();

    Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
    ((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);

    // Configure logging filter to check leak message appears
    LogValidationFilter f = new LogValidationFilter(
            "The web application [] created a ThreadLocal with key of");
    LogManager.getLogManager().getLogger(
            "org.apache.catalina.loader.WebappClassLoaderBase").setFilter(f);

    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterThreadScopedHolder",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet2",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());

    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak2",
            new ByteChunk(), null);

    // Make sure request is OK
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);

    // Destroy the context
    ctx.stop();
    tomcat.getHost().removeChild(ctx);
    ctx = null;

    // Make sure we have a memory leak
    String[] leaks = ((StandardHost) tomcat.getHost())
            .findReloadedContextMemoryLeaks();
    Assert.assertNotNull(leaks);
    Assert.assertTrue(leaks.length > 0);

    // Make sure the message was logged
    Assert.assertEquals(1, f.getMessageCount());
}
 
Example 16
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testThreadLocalLeak2() throws Exception {

    Tomcat tomcat = getTomcatInstance();
    // Need to make sure we see a leak for the right reasons
    tomcat.getServer().addLifecycleListener(
            new JreMemoryLeakPreventionListener());

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "leakServlet2",
            "org.apache.tomcat.unittest.TesterLeakingServlet2");
    ctx.addServletMappingDecoded("/leak2", "leakServlet2");

    tomcat.start();

    Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
    ((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);

    // Configure logging filter to check leak message appears
    TesterLogValidationFilter f = TesterLogValidationFilter.add(null,
            "The web application [ROOT] created a ThreadLocal with key of", null,
            "org.apache.catalina.loader.WebappClassLoaderBase");

    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterThreadScopedHolder",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet2",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());

    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak2",
            new ByteChunk(), null);

    // Make sure request is OK
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);

    // Destroy the context
    ctx.stop();
    tomcat.getHost().removeChild(ctx);
    ctx = null;

    // Make sure we have a memory leak
    String[] leaks = ((StandardHost) tomcat.getHost())
            .findReloadedContextMemoryLeaks();
    Assert.assertNotNull(leaks);
    Assert.assertTrue(leaks.length > 0);

    // Make sure the message was logged
    Assert.assertEquals(1, f.getMessageCount());
}
 
Example 17
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testThreadLocalLeak1() throws Exception {

    Tomcat tomcat = getTomcatInstance();
    // Need to make sure we see a leak for the right reasons
    tomcat.getServer().addLifecycleListener(
            new JreMemoryLeakPreventionListener());

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "leakServlet1",
            "org.apache.tomcat.unittest.TesterLeakingServlet1");
    ctx.addServletMapping("/leak1", "leakServlet1");

    tomcat.start();

    Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
    ((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);

    // Configure logging filter to check leak message appears
    LogValidationFilter f = new LogValidationFilter(
            "The web application [] created a ThreadLocal with key of");
    LogManager.getLogManager().getLogger(
            "org.apache.catalina.loader.WebappClassLoaderBase").setFilter(f);

    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet1",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());

    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak1",
            new ByteChunk(), null);

    // Make sure request is OK
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);

    // Destroy the context
    ctx.stop();
    tomcat.getHost().removeChild(ctx);
    ctx = null;

    // Make sure we have a memory leak
    String[] leaks = ((StandardHost) tomcat.getHost())
            .findReloadedContextMemoryLeaks();
    Assert.assertNotNull(leaks);
    Assert.assertTrue(leaks.length > 0);

    // Make sure the message was logged
    Assert.assertEquals(1, f.getMessageCount());
}
 
Example 18
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testThreadLocalLeak2() throws Exception {

    Tomcat tomcat = getTomcatInstance();
    // Need to make sure we see a leak for the right reasons
    tomcat.getServer().addLifecycleListener(
            new JreMemoryLeakPreventionListener());

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "leakServlet2",
            "org.apache.tomcat.unittest.TesterLeakingServlet2");
    ctx.addServletMapping("/leak2", "leakServlet2");

    tomcat.start();

    Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
    ((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);

    // Configure logging filter to check leak message appears
    LogValidationFilter f = new LogValidationFilter(
            "The web application [] created a ThreadLocal with key of");
    LogManager.getLogManager().getLogger(
            "org.apache.catalina.loader.WebappClassLoaderBase").setFilter(f);

    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterThreadScopedHolder",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet2",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());

    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak2",
            new ByteChunk(), null);

    // Make sure request is OK
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);

    // Destroy the context
    ctx.stop();
    tomcat.getHost().removeChild(ctx);
    ctx = null;

    // Make sure we have a memory leak
    String[] leaks = ((StandardHost) tomcat.getHost())
            .findReloadedContextMemoryLeaks();
    Assert.assertNotNull(leaks);
    Assert.assertTrue(leaks.length > 0);

    // Make sure the message was logged
    Assert.assertEquals(1, f.getMessageCount());
}
 
Example 19
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testThreadLocalLeak1() throws Exception {

    Tomcat tomcat = getTomcatInstance();
    // Need to make sure we see a leak for the right reasons
    tomcat.getServer().addLifecycleListener(
            new JreMemoryLeakPreventionListener());

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "leakServlet1",
            "org.apache.tomcat.unittest.TesterLeakingServlet1");
    ctx.addServletMapping("/leak1", "leakServlet1");

    tomcat.start();

    Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
    ((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);

    // Configure logging filter to check leak message appears
    LogValidationFilter f = new LogValidationFilter(
            "The web application [] created a ThreadLocal with key of");
    LogManager.getLogManager().getLogger(
            "org.apache.catalina.loader.WebappClassLoaderBase").setFilter(f);

    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet1",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());

    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak1",
            new ByteChunk(), null);

    // Make sure request is OK
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);

    // Destroy the context
    ctx.stop();
    tomcat.getHost().removeChild(ctx);
    ctx = null;

    // Make sure we have a memory leak
    String[] leaks = ((StandardHost) tomcat.getHost())
            .findReloadedContextMemoryLeaks();
    Assert.assertNotNull(leaks);
    Assert.assertTrue(leaks.length > 0);

    // Make sure the message was logged
    Assert.assertEquals(1, f.getMessageCount());
}
 
Example 20
Source File: TestELInterpreterFactory.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug54239() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    tomcat.start();

    ServletContext context = ctx.getServletContext();

    ELInterpreter interpreter =
            ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof DefaultELInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    context.setAttribute(ELInterpreter.class.getName(),
            SimpleELInterpreter.class.getName());
    interpreter = ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    SimpleELInterpreter simpleInterpreter = new SimpleELInterpreter();
    context.setAttribute(ELInterpreter.class.getName(), simpleInterpreter);
    interpreter = ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
    Assert.assertTrue(interpreter == simpleInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    ctx.stop();
    ctx.addApplicationListener(Bug54239Listener.class.getName());
    ctx.start();

    interpreter = ELInterpreterFactory.getELInterpreter(ctx.getServletContext());
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
}