View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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              if (LOG.isDebugEnabled())
64                  LOG.debug("javax.el.BeanELResolver purged");
65          }
66  
67          catch (ClassNotFoundException e)
68          {
69              //BeanELResolver not on classpath, ignore
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 }