blob: 285de0f303570febbbb92b9317d5e4dce0fca5f4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
// ========================================================================
// 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.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;
import org.eclipse.jetty.util.log.Logger;
/**
* 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
{
private static final Logger LOG = Log.getLogger(ELContextCleaner.class);
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());
}
}
}
|