From: markt Date: Fri, 12 Feb 2010 17:50:22 +0000 (+0000) Subject: Make class loader registration more robust X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=84fffede1c016b1dbd41cc4c6942663e627a49a6;p=tomcat7.0 Make class loader registration more robust git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@909526 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/core/StandardHost.java b/java/org/apache/catalina/core/StandardHost.java index 8e11dec18..870df745b 100644 --- a/java/org/apache/catalina/core/StandardHost.java +++ b/java/org/apache/catalina/core/StandardHost.java @@ -30,7 +30,10 @@ import javax.management.ObjectName; import org.apache.catalina.Container; import org.apache.catalina.Context; import org.apache.catalina.Host; +import org.apache.catalina.Lifecycle; +import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleException; +import org.apache.catalina.LifecycleListener; import org.apache.catalina.Valve; import org.apache.catalina.loader.WebappClassLoader; import org.apache.catalina.startup.HostConfig; @@ -579,17 +582,33 @@ public class StandardHost throw new IllegalArgumentException (sm.getString("standardHost.notContext")); super.addChild(child); - - // Record a reference to the context's class loader to aid memory leak - // detection - if (child.getLoader() != null) { - childClassLoaders.put(child.getLoader().getClassLoader(), - child.getName()); + + if (child instanceof Lifecycle) { + ((Lifecycle) child).addLifecycleListener( + new MemoryLeakTrackingListener()); } } /** + * Used to ensure the regardless of {@link Context} implementation, a record + * is kept of the class loader used every time a context starts. + */ + private class MemoryLeakTrackingListener implements LifecycleListener { + @Override + public void lifecycleEvent(LifecycleEvent event) { + if (event.getType().equals(Lifecycle.AFTER_START_EVENT)) { + if (event.getSource() instanceof Context) { + Context context = ((Context) event.getSource()); + childClassLoaders.put(context.getLoader().getClassLoader(), + context.getServletContext().getContextPath()); + } + } + } + } + + + /** * 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