2 // ========================================================================
3 // Copyright (c) 1995-2014 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;
23 import java.util.concurrent.ConcurrentHashMap;
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
28 import org.eclipse.jetty.util.Loader;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.log.Logger;
35 * Clean up BeanELResolver when the context is going out
38 * See http://java.net/jira/browse/GLASSFISH-1649
39 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=353095
41 public class ELContextCleaner implements ServletContextListener
43 private static final Logger LOG = Log.getLogger(ELContextCleaner.class);
46 public void contextInitialized(ServletContextEvent sce)
50 public void contextDestroyed(ServletContextEvent sce)
54 //Check that the BeanELResolver class is on the classpath
55 Class beanELResolver = Loader.loadClass(this.getClass(), "javax.el.BeanELResolver");
57 //Get a reference via reflection to the properties field which is holding class references
58 Field field = getField(beanELResolver);
60 //Get rid of references
63 LOG.debug("javax.el.BeanELResolver purged");
66 catch (ClassNotFoundException e)
68 //BeanELResolver not on classpath, ignore
70 catch (SecurityException e)
72 LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
74 catch (IllegalArgumentException e)
76 LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
78 catch (IllegalAccessException e)
80 LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
82 catch (NoSuchFieldException e)
84 LOG.debug("Not cleaning cached beans: no such field javax.el.BeanELResolver.properties");
90 protected Field getField (Class beanELResolver)
91 throws SecurityException, NoSuchFieldException
93 if (beanELResolver == null)
96 return beanELResolver.getDeclaredField("properties");
99 protected void purgeEntries (Field properties)
100 throws IllegalArgumentException, IllegalAccessException
102 if (properties == null)
105 if (!properties.isAccessible())
106 properties.setAccessible(true);
108 ConcurrentHashMap map = (ConcurrentHashMap) properties.get(null);
112 Iterator<Class> itor = map.keySet().iterator();
113 while (itor.hasNext())
115 Class clazz = itor.next();
116 LOG.debug("Clazz: "+clazz+" loaded by "+clazz.getClassLoader());
117 if (Thread.currentThread().getContextClassLoader().equals(clazz.getClassLoader()))
120 LOG.debug("removed");
123 LOG.debug("not removed: "+"contextclassloader="+Thread.currentThread().getContextClassLoader()+"clazz's classloader="+clazz.getClassLoader());