View Javadoc

1   // ========================================================================
2   // Copyright (c) 2011 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  
14  package org.eclipse.jetty.servlet.listener;
15  
16  import java.lang.reflect.Field;
17  import java.lang.reflect.InvocationTargetException;
18  import java.lang.reflect.Method;
19  import java.util.Iterator;
20  import java.util.concurrent.ConcurrentHashMap;
21  
22  import javax.servlet.ServletContextEvent;
23  import javax.servlet.ServletContextListener;
24  
25  import org.eclipse.jetty.util.Loader;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.util.log.Logger;
28  
29  /**
30   * ELContextCleaner
31   *
32   * Clean up BeanELResolver when the context is going out
33   * of service:
34   * 
35   * See http://java.net/jira/browse/GLASSFISH-1649
36   * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=353095
37   */
38  public class ELContextCleaner implements ServletContextListener
39  {
40      private static final Logger LOG = Log.getLogger(ELContextCleaner.class);
41  
42  
43      public void contextInitialized(ServletContextEvent sce)
44      {
45      }
46  
47      public void contextDestroyed(ServletContextEvent sce)
48      {
49          try
50          {
51              //Check that the BeanELResolver class is on the classpath
52              Class beanELResolver = Loader.loadClass(this.getClass(), "javax.el.BeanELResolver");
53  
54              //Get a reference via reflection to the properties field which is holding class references
55              Field field = getField(beanELResolver);
56  
57              //Get rid of references
58              purgeEntries(field);
59              
60              LOG.info("javax.el.BeanELResolver purged");
61          }
62          
63          catch (ClassNotFoundException e)
64          {
65              //BeanELResolver not on classpath, ignore
66          }
67          catch (SecurityException e)
68          {
69              LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
70          }
71          catch (IllegalArgumentException e)
72          {
73              LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
74          }
75          catch (IllegalAccessException e)
76          {
77              LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
78          }
79          catch (NoSuchFieldException e)
80          {
81              LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
82          }
83         
84      }
85  
86  
87      protected Field getField (Class beanELResolver) 
88      throws SecurityException, NoSuchFieldException
89      {
90          if (beanELResolver == null)
91              return  null;
92  
93          return beanELResolver.getDeclaredField("properties");
94      }
95  
96      protected void purgeEntries (Field properties) 
97      throws IllegalArgumentException, IllegalAccessException
98      {
99          if (properties == null)
100             return;
101 
102         if (!properties.isAccessible())
103             properties.setAccessible(true);
104 
105         ConcurrentHashMap map = (ConcurrentHashMap) properties.get(null);
106         if (map == null)
107             return;
108         
109         Iterator<Class> itor = map.keySet().iterator();
110         while (itor.hasNext()) 
111         {
112             Class clazz = itor.next();
113             LOG.info("Clazz: "+clazz+" loaded by "+clazz.getClassLoader());
114             if (Thread.currentThread().getContextClassLoader().equals(clazz.getClassLoader()))
115             {
116                 itor.remove();  
117                 LOG.info("removed");
118             }
119             else
120                 LOG.info("not removed: "+"contextclassloader="+Thread.currentThread().getContextClassLoader()+"clazz's classloader="+clazz.getClassLoader());
121         }
122     }
123 }