353095 - maven-jetty-plugin: PermGen leak due to javax.el.BeanELResolver

Also found that java beans can be leaked via java.beans.Introspector so fixed that too.
This commit is contained in:
Jan Bartel 2011-07-29 11:05:45 +10:00
parent 1a832ef21e
commit fb8fb0e2f8
4 changed files with 177 additions and 0 deletions

View File

@ -0,0 +1,120 @@
// ========================================================================
// Copyright (c) 2011 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.servlet.listener;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.log.Log;
/**
* ELContextCleaner
*
* Clean up BeanELResolver when the context is going out
* of service:
*
* See http://java.net/jira/browse/GLASSFISH-1649
* See https://bugs.eclipse.org/bugs/show_bug.cgi?id=353095
*/
public class ELContextCleaner implements ServletContextListener
{
public void contextInitialized(ServletContextEvent sce)
{
}
public void contextDestroyed(ServletContextEvent sce)
{
try
{
//Check that the BeanELResolver class is on the classpath
Class beanELResolver = Loader.loadClass(this.getClass(), "javax.el.BeanELResolver");
//Get a reference via reflection to the properties field which is holding class references
Field field = getField(beanELResolver);
//Get rid of references
purgeEntries(field);
Log.info("javax.el.BeanELResolver purged");
}
catch (ClassNotFoundException e)
{
//BeanELResolver not on classpath, ignore
}
catch (SecurityException e)
{
Log.warn("Cannot purge classes from javax.el.BeanELResolver", e);
}
catch (IllegalArgumentException e)
{
Log.warn("Cannot purge classes from javax.el.BeanELResolver", e);
}
catch (IllegalAccessException e)
{
Log.warn("Cannot purge classes from javax.el.BeanELResolver", e);
}
catch (NoSuchFieldException e)
{
Log.warn("Cannot purge classes from javax.el.BeanELResolver", e);
}
}
protected Field getField (Class beanELResolver)
throws SecurityException, NoSuchFieldException
{
if (beanELResolver == null)
return null;
return beanELResolver.getDeclaredField("properties");
}
protected void purgeEntries (Field properties)
throws IllegalArgumentException, IllegalAccessException
{
if (properties == null)
return;
if (!properties.isAccessible())
properties.setAccessible(true);
ConcurrentHashMap map = (ConcurrentHashMap) properties.get(null);
if (map == null)
return;
Iterator<Class> itor = map.keySet().iterator();
while (itor.hasNext())
{
Class clazz = itor.next();
Log.info("Clazz: "+clazz+" loaded by "+clazz.getClassLoader());
if (Thread.currentThread().getContextClassLoader().equals(clazz.getClassLoader()))
{
itor.remove();
Log.info("removed");
}
else
Log.info("not removed: "+"contextclassloader="+Thread.currentThread().getContextClassLoader()+"clazz's classloader="+clazz.getClassLoader());
}
}
}

View File

@ -0,0 +1,40 @@
// ========================================================================
// Copyright (c) 2011 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.servlet.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* IntrospectorCleaner
*
* Cleans a static cache of Methods held by java.beans.Introspector
* class when a context is undeployed.
*
* @see java.beans.Introspector
*/
public class IntrospectorCleaner implements ServletContextListener
{
public void contextInitialized(ServletContextEvent sce)
{
}
public void contextDestroyed(ServletContextEvent sce)
{
java.beans.Introspector.flushCaches();
}
}

View File

@ -30,6 +30,22 @@
This file is applied to a Web application before it's own WEB_INF/web.xml file This file is applied to a Web application before it's own WEB_INF/web.xml file
</description> </description>
<!-- ==================================================================== -->
<!-- Removes static references to beans from javax.el.BeanELResolver to -->
<!-- ensure webapp classloader can be released on undeploy -->
<!-- ==================================================================== -->
<listener>
<listener-class>org.eclipse.jetty.servlet.listener.ELContextCleaner</listener-class>
</listener>
<!-- ==================================================================== -->
<!-- Removes static cache of Methods from java.beans.Introspector to -->
<!-- ensure webapp classloader can be released on undeploy -->
<!-- ==================================================================== -->
<listener>
<listener-class>org.eclipse.jetty.servlet.listener.IntrospectorCleaner</listener-class>
</listener>
<!-- ==================================================================== --> <!-- ==================================================================== -->
<!-- Context params to control Session Cookies --> <!-- Context params to control Session Cookies -->

View File

@ -111,6 +111,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
"-org.eclipse.jetty.plus.jaas.", // don't hide jaas classes "-org.eclipse.jetty.plus.jaas.", // don't hide jaas classes
"-org.eclipse.jetty.websocket.", // don't hide websocket extension "-org.eclipse.jetty.websocket.", // don't hide websocket extension
"-org.eclipse.jetty.servlet.DefaultServlet", // don't hide default servlet "-org.eclipse.jetty.servlet.DefaultServlet", // don't hide default servlet
"-org.eclipse.jetty.servlet.listener.", //don't hide useful listeners
"org.eclipse.jetty." // hide other jetty classes "org.eclipse.jetty." // hide other jetty classes
} ; } ;