2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.servlet.listener;
21 import java.lang.reflect.Field;
22 import java.util.Iterator;
24 import java.util.concurrent.ConcurrentHashMap;
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
29 import org.eclipse.jetty.util.Loader;
30 import org.eclipse.jetty.util.log.Log;
31 import org.eclipse.jetty.util.log.Logger;
36 * Clean up BeanELResolver when the context is going out
39 * See http://java.net/jira/browse/GLASSFISH-1649
40 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=353095
42 public class ELContextCleaner implements ServletContextListener
44 private static final Logger LOG = Log.getLogger(ELContextCleaner.class);
47 public void contextInitialized(ServletContextEvent sce)
51 public void contextDestroyed(ServletContextEvent sce)
55 //Check that the BeanELResolver class is on the classpath
56 Class<?> beanELResolver = Loader.loadClass(this.getClass(), "javax.el.BeanELResolver");
58 //Get a reference via reflection to the properties field which is holding class references
59 Field field = getField(beanELResolver);
61 //Get rid of references
64 if (LOG.isDebugEnabled())
65 LOG.debug("javax.el.BeanELResolver purged");
68 catch (ClassNotFoundException e)
70 //BeanELResolver not on classpath, ignore
72 catch (SecurityException | IllegalArgumentException | IllegalAccessException e)
74 LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
76 catch (NoSuchFieldException e)
78 LOG.debug("Not cleaning cached beans: no such field javax.el.BeanELResolver.properties");
84 protected Field getField (Class<?> beanELResolver)
85 throws SecurityException, NoSuchFieldException
87 if (beanELResolver == null)
90 return beanELResolver.getDeclaredField("properties");
93 protected void purgeEntries (Field properties)
94 throws IllegalArgumentException, IllegalAccessException
96 if (properties == null)
99 if (!properties.isAccessible())
100 properties.setAccessible(true);
102 Map map = (Map) properties.get(null);
106 Iterator<Class<?>> itor = map.keySet().iterator();
107 while (itor.hasNext())
109 Class<?> clazz = itor.next();
110 if (LOG.isDebugEnabled())
111 LOG.debug("Clazz: "+clazz+" loaded by "+clazz.getClassLoader());
112 if (Thread.currentThread().getContextClassLoader().equals(clazz.getClassLoader()))
115 if (LOG.isDebugEnabled())
116 LOG.debug("removed");
120 if (LOG.isDebugEnabled())
121 LOG.debug("not removed: "+"contextclassloader="+Thread.currentThread().getContextClassLoader()+"clazz's classloader="+clazz.getClassLoader());