1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.servlet.listener;
20
21 import java.lang.reflect.Field;
22 import java.util.Iterator;
23 import java.util.concurrent.ConcurrentHashMap;
24
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
27
28 import org.eclipse.jetty.util.Loader;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.log.Logger;
31
32
33
34
35
36
37
38
39
40
41 public class ELContextCleaner implements ServletContextListener
42 {
43 private static final Logger LOG = Log.getLogger(ELContextCleaner.class);
44
45
46 public void contextInitialized(ServletContextEvent sce)
47 {
48 }
49
50 public void contextDestroyed(ServletContextEvent sce)
51 {
52 try
53 {
54
55 Class<?> beanELResolver = Loader.loadClass(this.getClass(), "javax.el.BeanELResolver");
56
57
58 Field field = getField(beanELResolver);
59
60
61 purgeEntries(field);
62
63 if (LOG.isDebugEnabled())
64 LOG.debug("javax.el.BeanELResolver purged");
65 }
66
67 catch (ClassNotFoundException e)
68 {
69
70 }
71 catch (SecurityException | IllegalArgumentException | IllegalAccessException e)
72 {
73 LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
74 }
75 catch (NoSuchFieldException e)
76 {
77 LOG.debug("Not cleaning cached beans: no such field javax.el.BeanELResolver.properties");
78 }
79
80 }
81
82
83 protected Field getField (Class<?> beanELResolver)
84 throws SecurityException, NoSuchFieldException
85 {
86 if (beanELResolver == null)
87 return null;
88
89 return beanELResolver.getDeclaredField("properties");
90 }
91
92 protected void purgeEntries (Field properties)
93 throws IllegalArgumentException, IllegalAccessException
94 {
95 if (properties == null)
96 return;
97
98 if (!properties.isAccessible())
99 properties.setAccessible(true);
100
101 ConcurrentHashMap<Class<?>, Object> map = (ConcurrentHashMap<Class<?>, Object>) properties.get(null);
102 if (map == null)
103 return;
104
105 Iterator<Class<?>> itor = map.keySet().iterator();
106 while (itor.hasNext())
107 {
108 Class<?> clazz = itor.next();
109 if (LOG.isDebugEnabled())
110 LOG.debug("Clazz: "+clazz+" loaded by "+clazz.getClassLoader());
111 if (Thread.currentThread().getContextClassLoader().equals(clazz.getClassLoader()))
112 {
113 itor.remove();
114 if (LOG.isDebugEnabled())
115 LOG.debug("removed");
116 }
117 else
118 {
119 if (LOG.isDebugEnabled())
120 LOG.debug("not removed: "+"contextclassloader="+Thread.currentThread().getContextClassLoader()+"clazz's classloader="+clazz.getClassLoader());
121 }
122 }
123 }
124 }