]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/servlet/listener/ELContextCleaner.java
Merge "Update notes about password security"
[gigi.git] / lib / jetty / org / eclipse / jetty / servlet / listener / ELContextCleaner.java
1 //
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.
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.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
28
29 import org.eclipse.jetty.util.Loader;
30 import org.eclipse.jetty.util.log.Log;
31 import org.eclipse.jetty.util.log.Logger;
32
33 /**
34  * ELContextCleaner
35  *
36  * Clean up BeanELResolver when the context is going out
37  * of service:
38  *
39  * See http://java.net/jira/browse/GLASSFISH-1649
40  * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=353095
41  */
42 public class ELContextCleaner implements ServletContextListener
43 {
44     private static final Logger LOG = Log.getLogger(ELContextCleaner.class);
45
46
47     public void contextInitialized(ServletContextEvent sce)
48     {
49     }
50
51     public void contextDestroyed(ServletContextEvent sce)
52     {
53         try
54         {
55             //Check that the BeanELResolver class is on the classpath
56             Class<?> beanELResolver = Loader.loadClass(this.getClass(), "javax.el.BeanELResolver");
57
58             //Get a reference via reflection to the properties field which is holding class references
59             Field field = getField(beanELResolver);
60
61             //Get rid of references
62             purgeEntries(field);
63
64             if (LOG.isDebugEnabled())
65                 LOG.debug("javax.el.BeanELResolver purged");
66         }
67
68         catch (ClassNotFoundException e)
69         {
70             //BeanELResolver not on classpath, ignore
71         }
72         catch (SecurityException | IllegalArgumentException | IllegalAccessException e)
73         {
74             LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
75         }
76         catch (NoSuchFieldException e)
77         {
78             LOG.debug("Not cleaning cached beans: no such field javax.el.BeanELResolver.properties");
79         }
80
81     }
82
83
84     protected Field getField (Class<?> beanELResolver)
85     throws SecurityException, NoSuchFieldException
86     {
87         if (beanELResolver == null)
88             return  null;
89
90         return beanELResolver.getDeclaredField("properties");
91     }
92
93     protected void purgeEntries (Field properties)
94     throws IllegalArgumentException, IllegalAccessException
95     {
96         if (properties == null)
97             return;
98
99         if (!properties.isAccessible())
100             properties.setAccessible(true);
101
102         Map map = (Map) properties.get(null);
103         if (map == null)
104             return;
105
106         Iterator<Class<?>> itor = map.keySet().iterator();
107         while (itor.hasNext())
108         {
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()))
113             {
114                 itor.remove();
115                 if (LOG.isDebugEnabled())
116                     LOG.debug("removed");
117             }
118             else
119             {
120                 if (LOG.isDebugEnabled())
121                     LOG.debug("not removed: "+"contextclassloader="+Thread.currentThread().getContextClassLoader()+"clazz's classloader="+clazz.getClassLoader());
122             }
123         }
124     }
125 }