org.apache.catalina.loader.WebappClassLoaderBase Java Examples

The following examples show how to use org.apache.catalina.loader.WebappClassLoaderBase. 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: StandardHost.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Attempt to identify the contexts that have a class loader memory leak.
 * This is usually triggered on context reload. Note: This method attempts
 * to force a full garbage collection. This should be used with extreme
 * caution on a production system.
 *
 * @return a list of possibly leaking contexts
 */
public String[] findReloadedContextMemoryLeaks() {

    System.gc();

    List<String> result = new ArrayList<>();

    for (Map.Entry<ClassLoader, String> entry :
            childClassLoaders.entrySet()) {
        ClassLoader cl = entry.getKey();
        if (cl instanceof WebappClassLoaderBase) {
            if (!((WebappClassLoaderBase) cl).getState().isAvailable()) {
                result.add(entry.getValue());
            }
        }
    }

    return result.toArray(new String[result.size()]);
}
 
Example #2
Source File: TestTomcatClassLoader.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setContentType("text/plain");
    PrintWriter out = resp.getWriter();

    ClassLoader system = ClassLoader.getSystemClassLoader();

    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    while (cl != null) {
        if (system == cl) {
            out.print("SYSTEM,");
        } else if (custom == cl) {
            out.print("CUSTOM,");
        } else if (cl instanceof WebappClassLoaderBase) {
            out.print("WEBAPP,");
        } else {
            out.print("OTHER,");
        }
        cl = cl.getParent();
    }
}
 
Example #3
Source File: StandardHost.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to identify the contexts that have a class loader memory leak.
 * This is usually triggered on context reload. Note: This method attempts
 * to force a full garbage collection. This should be used with extreme
 * caution on a production system.
 */
public String[] findReloadedContextMemoryLeaks() {

    System.gc();

    List<String> result = new ArrayList<String>();

    for (Map.Entry<ClassLoader, String> entry :
            childClassLoaders.entrySet()) {
        ClassLoader cl = entry.getKey();
        if (cl instanceof WebappClassLoaderBase) {
            if (!((WebappClassLoaderBase) cl).isStarted()) {
                result.add(entry.getValue());
            }
        }
    }

    return result.toArray(new String[result.size()]);
}
 
Example #4
Source File: TestTomcatClassLoader.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setContentType("text/plain");
    PrintWriter out = resp.getWriter();

    ClassLoader system = ClassLoader.getSystemClassLoader();

    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    while (cl != null) {
        if (system == cl) {
            out.print("SYSTEM,");
        } else if (custom == cl) {
            out.print("CUSTOM,");
        } else if (cl instanceof WebappClassLoaderBase) {
            out.print("WEBAPP,");
        } else {
            out.print("OTHER,");
        }
        cl = cl.getParent();
    }
}
 
Example #5
Source File: StandardHost.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to identify the contexts that have a class loader memory leak.
 * This is usually triggered on context reload. Note: This method attempts
 * to force a full garbage collection. This should be used with extreme
 * caution on a production system.
 */
public String[] findReloadedContextMemoryLeaks() {

    System.gc();

    List<String> result = new ArrayList<String>();

    for (Map.Entry<ClassLoader, String> entry :
            childClassLoaders.entrySet()) {
        ClassLoader cl = entry.getKey();
        if (cl instanceof WebappClassLoaderBase) {
            if (!((WebappClassLoaderBase) cl).isStarted()) {
                result.add(entry.getValue());
            }
        }
    }

    return result.toArray(new String[result.size()]);
}
 
Example #6
Source File: TestTomcatClassLoader.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setContentType("text/plain");
    PrintWriter out = resp.getWriter();

    ClassLoader system = ClassLoader.getSystemClassLoader();

    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    while (cl != null) {
        if (system == cl) {
            out.print("SYSTEM,");
        } else if (custom == cl) {
            out.print("CUSTOM,");
        } else if (cl instanceof WebappClassLoaderBase) {
            out.print("WEBAPP,");
        } else {
            out.print("OTHER,");
        }
        cl = cl.getParent();
    }
}
 
Example #7
Source File: InvokeJaxwsWebServiceInTomcat.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private void checkForRequestThreads(WebappLoader webappLoader) throws Exception {
    Method getThreadsMethod = WebappClassLoaderBase.class.getDeclaredMethod("getThreads");
    getThreadsMethod.setAccessible(true);
    Method isRequestThreadMethod =
            WebappClassLoaderBase.class.getDeclaredMethod("isRequestThread", Thread.class);
    isRequestThreadMethod.setAccessible(true);
    ClassLoader webappClassLoader = webappLoader.getClassLoader();
    Thread[] threads = (Thread[]) getThreadsMethod.invoke(webappClassLoader);
    for (Thread thread : threads) {
        if (thread == null) {
            continue;
        }
        if ((boolean) isRequestThreadMethod.invoke(webappClassLoader, thread)) {
            StringBuilder sb = new StringBuilder();
            for (StackTraceElement element : thread.getStackTrace()) {
                sb.append(element);
                sb.append('\n');
            }
            logger.error("tomcat request thread \"{}\" is still active:\n{}", thread.getName(),
                    sb);
        }
    }
}
 
Example #8
Source File: InvokeSpringControllerInTomcat.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private void checkForRequestThreads(WebappLoader webappLoader) throws Exception {
    Method getThreadsMethod = WebappClassLoaderBase.class.getDeclaredMethod("getThreads");
    getThreadsMethod.setAccessible(true);
    Method isRequestThreadMethod =
            WebappClassLoaderBase.class.getDeclaredMethod("isRequestThread", Thread.class);
    isRequestThreadMethod.setAccessible(true);
    ClassLoader webappClassLoader = webappLoader.getClassLoader();
    Thread[] threads = (Thread[]) getThreadsMethod.invoke(webappClassLoader);
    for (Thread thread : threads) {
        if (thread == null) {
            continue;
        }
        if ((Boolean) isRequestThreadMethod.invoke(webappClassLoader, thread)) {
            StringBuilder sb = new StringBuilder();
            for (StackTraceElement element : thread.getStackTrace()) {
                sb.append(element);
                sb.append('\n');
            }
            logger.error("tomcat request thread \"{}\" is still active:\n{}", thread.getName(),
                    sb);
        }
    }
}
 
Example #9
Source File: TestClassLoader.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnOpen
public void onOpen(Session session) throws IOException {
    if (Thread.currentThread().getContextClassLoader() instanceof WebappClassLoaderBase) {
        session.getBasicRemote().sendText(PASS);
    } else {
        session.getBasicRemote().sendText(FAIL);
    }
}
 
Example #10
Source File: TestClassLoader.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public String onMessage(@SuppressWarnings("unused") String msg) {
    if (Thread.currentThread().getContextClassLoader() instanceof WebappClassLoaderBase) {
        return PASS;
    } else {
        return FAIL;
    }
}