ServletContextCleaner.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.logging.impl;

  18. import java.lang.reflect.InvocationTargetException;
  19. import java.lang.reflect.Method;

  20. import javax.servlet.ServletContextEvent;
  21. import javax.servlet.ServletContextListener;

  22. import org.apache.commons.logging.LogFactory;

  23. /**
  24.  * This class is capable of receiving notifications about the undeployment of
  25.  * a webapp, and responds by ensuring that commons-logging releases all
  26.  * memory associated with the undeployed webapp.
  27.  * <p>
  28.  * In general, the WeakHashtable support added in commons-logging release 1.1
  29.  * ensures that logging classes do not hold references that prevent an
  30.  * undeployed webapp's memory from being garbage-collected even when multiple
  31.  * copies of commons-logging are deployed via multiple class loaders (a
  32.  * situation that earlier versions had problems with). However there are
  33.  * some rare cases where the WeakHashtable approach does not work; in these
  34.  * situations specifying this class as a listener for the web application will
  35.  * ensure that all references held by commons-logging are fully released.
  36.  * <p>
  37.  * To use this class, configure the webapp deployment descriptor to call
  38.  * this class on webapp undeploy; the contextDestroyed method will tell
  39.  * every accessible LogFactory class that the entry in its map for the
  40.  * current webapp's context class loader should be cleared.
  41.  *
  42.  * @since 1.1
  43.  */
  44. public class ServletContextCleaner implements ServletContextListener {

  45.     private static final Class<?>[] RELEASE_SIGNATURE = { ClassLoader.class };

  46.     /**
  47.      * Constructs a new instance.
  48.      */
  49.     public ServletContextCleaner() {
  50.         // empty
  51.     }

  52.     /**
  53.      * Invoked when a webapp is undeployed, this tells the LogFactory
  54.      * class to release any logging information related to the current
  55.      * contextClassloader.
  56.      */
  57.     @Override
  58.     public void contextDestroyed(final ServletContextEvent sce) {
  59.         final ClassLoader tccl = Thread.currentThread().getContextClassLoader();

  60.         final Object[] params = new Object[1];
  61.         params[0] = tccl;

  62.         // Walk up the tree of class loaders, finding all the available
  63.         // LogFactory classes and releasing any objects associated with
  64.         // the tccl (ie the webapp).
  65.         //
  66.         // When there is only one LogFactory in the classpath, and it
  67.         // is within the webapp being undeployed then there is no problem;
  68.         // garbage collection works fine.
  69.         //
  70.         // When there are multiple LogFactory classes in the classpath but
  71.         // parent-first classloading is used everywhere, this loop is really
  72.         // short. The first instance of LogFactory found will
  73.         // be the highest in the classpath, and then no more will be found.
  74.         // This is ok, as with this setup this will be the only LogFactory
  75.         // holding any data associated with the tccl being released.
  76.         //
  77.         // When there are multiple LogFactory classes in the classpath and
  78.         // child-first classloading is used in any class loader, then multiple
  79.         // LogFactory instances may hold info about this TCCL; whenever the
  80.         // webapp makes a call into a class loaded via an ancestor class loader
  81.         // and that class calls LogFactory the tccl gets registered in
  82.         // the LogFactory instance that is visible from the ancestor
  83.         // class loader. However the concrete logging library it points
  84.         // to is expected to have been loaded via the TCCL, so the
  85.         // underlying logging lib is only initialized/configured once.
  86.         // These references from ancestor LogFactory classes down to
  87.         // TCCL class loaders are held via weak references and so should
  88.         // be released but there are circumstances where they may not.
  89.         // Walking up the class loader ancestry ladder releasing
  90.         // the current tccl at each level tree, though, will definitely
  91.         // clear any problem references.
  92.         ClassLoader loader = tccl;
  93.         while (loader != null) {
  94.             // Load via the current loader. Note that if the class is not accessible
  95.             // via this loader, but is accessible via some ancestor then that class
  96.             // will be returned.
  97.             try {
  98.                 @SuppressWarnings("unchecked")
  99.                 final Class<LogFactory> logFactoryClass = (Class<LogFactory>) loader.loadClass("org.apache.commons.logging.LogFactory");
  100.                 final Method releaseMethod = logFactoryClass.getMethod("release", RELEASE_SIGNATURE);
  101.                 releaseMethod.invoke(null, params);
  102.                 loader = logFactoryClass.getClassLoader().getParent();
  103.             } catch (final ClassNotFoundException ex) {
  104.                 // Neither the current class loader nor any of its ancestors could find
  105.                 // the LogFactory class, so we can stop now.
  106.                 loader = null;
  107.             } catch (final NoSuchMethodException ex) {
  108.                 // This is not expected; every version of JCL has this method
  109.                 System.err.println("LogFactory instance found which does not support release method!");
  110.                 loader = null;
  111.             } catch (final IllegalAccessException ex) {
  112.                 // This is not expected; every ancestor class should be accessible
  113.                 System.err.println("LogFactory instance found which is not accessible!");
  114.                 loader = null;
  115.             } catch (final InvocationTargetException ex) {
  116.                 // This is not expected
  117.                 System.err.println("LogFactory instance release method failed!");
  118.                 loader = null;
  119.             }
  120.         }

  121.         // Just to be sure, invoke release on the LogFactory that is visible from
  122.         // this ServletContextCleaner class too. This should already have been caught
  123.         // by the above loop but just in case...
  124.         LogFactory.release(tccl);
  125.     }

  126.     /**
  127.      * Invoked when a webapp is deployed. Nothing needs to be done here.
  128.      */
  129.     @Override
  130.     public void contextInitialized(final ServletContextEvent sce) {
  131.         // do nothing
  132.     }
  133. }