]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/servlet/listener/ELContextCleaner.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / servlet / listener / ELContextCleaner.java
1 //
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.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
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  * ELContextCleaner
34  *
35  * Clean up BeanELResolver when the context is going out
36  * of service:
37  *
38  * See http://java.net/jira/browse/GLASSFISH-1649
39  * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=353095
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             //Check that the BeanELResolver class is on the classpath
55             Class beanELResolver = Loader.loadClass(this.getClass(), "javax.el.BeanELResolver");
56
57             //Get a reference via reflection to the properties field which is holding class references
58             Field field = getField(beanELResolver);
59
60             //Get rid of references
61             purgeEntries(field);
62
63             LOG.debug("javax.el.BeanELResolver purged");
64         }
65
66         catch (ClassNotFoundException e)
67         {
68             //BeanELResolver not on classpath, ignore
69         }
70         catch (SecurityException e)
71         {
72             LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
73         }
74         catch (IllegalArgumentException e)
75         {
76             LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
77         }
78         catch (IllegalAccessException e)
79         {
80             LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
81         }
82         catch (NoSuchFieldException e)
83         {
84             LOG.debug("Not cleaning cached beans: no such field javax.el.BeanELResolver.properties");
85         }
86
87     }
88
89
90     protected Field getField (Class beanELResolver)
91     throws SecurityException, NoSuchFieldException
92     {
93         if (beanELResolver == null)
94             return  null;
95
96         return beanELResolver.getDeclaredField("properties");
97     }
98
99     protected void purgeEntries (Field properties)
100     throws IllegalArgumentException, IllegalAccessException
101     {
102         if (properties == null)
103             return;
104
105         if (!properties.isAccessible())
106             properties.setAccessible(true);
107
108         ConcurrentHashMap map = (ConcurrentHashMap) properties.get(null);
109         if (map == null)
110             return;
111
112         Iterator<Class> itor = map.keySet().iterator();
113         while (itor.hasNext())
114         {
115             Class clazz = itor.next();
116             LOG.debug("Clazz: "+clazz+" loaded by "+clazz.getClassLoader());
117             if (Thread.currentThread().getContextClassLoader().equals(clazz.getClassLoader()))
118             {
119                 itor.remove();
120                 LOG.debug("removed");
121             }
122             else
123                 LOG.debug("not removed: "+"contextclassloader="+Thread.currentThread().getContextClassLoader()+"clazz's classloader="+clazz.getClassLoader());
124         }
125     }
126 }