    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.logging.jakarta;
    019
    020import java.lang.reflect.InvocationTargetException;
    021import java.lang.reflect.Method;
    022
    023import jakarta.servlet.ServletContextEvent;
    024import jakarta.servlet.ServletContextListener;
    025
    026import org.apache.commons.logging.LogFactory;
    027
    028/**
    029 * This class is capable of receiving notifications about the undeployment of a webapp, and responds by ensuring that commons-logging releases all memory
    030 * associated with the undeployed webapp.
    031 * <p>
    032 * In general, we ensurs that logging classes do not hold references that prevent an undeployed webapp's memory from being garbage-collected even when multiple
    033 * copies of commons-logging are deployed via multiple class loaders (a situation that earlier versions had problems with). However there are some rare cases
    034 * where the this approach does not work; in these situations specifying this class as a listener for the web application will ensure that all
    035 * references held by commons-logging are fully released.
    036 * </p>
    037 * <p>
    038 * To use this class, configure the webapp deployment descriptor to call this class on webapp undeploy; the contextDestroyed method will tell every accessible
    039 * LogFactory class that the entry in its map for the current webapp's context class loader should be cleared.
    040 * </p>
    041 *
    042 * @since 1.4.0
    043 */
    044public class ServletContextCleaner implements ServletContextListener {
    045
    046    private static final Class<?>[] RELEASE_SIGNATURE = { ClassLoader.class };
    047
    048    /**
    049     * Constructs a new instance.
    050     */
    051    public ServletContextCleaner() {
    052        // empty
    053    }
    054
    055    /**
    056     * Invoked when a webapp is undeployed, this tells the LogFactory class to release any logging information related to the current contextClassloader.
    057     */
    058    @Override
    059    public void contextDestroyed(final ServletContextEvent sce) {
    060        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    061        final Object[] params = new Object[1];
    062        params[0] = tccl;
    063        // Walk up the tree of class loaders, finding all the available
    064        // LogFactory classes and releasing any objects associated with
    065        // the tccl (ie the webapp).
    066        //
    067        // When there is only one LogFactory in the classpath, and it
    068        // is within the webapp being undeployed then there is no problem;
    069        // garbage collection works fine.
    070        //
    071        // When there are multiple LogFactory classes in the classpath but
    072        // parent-first classloading is used everywhere, this loop is really
    073        // short. The first instance of LogFactory found will
    074        // be the highest in the classpath, and then no more will be found.
    075        // This is ok, as with this setup this will be the only LogFactory
    076        // holding any data associated with the tccl being released.
    077        //
    078        // When there are multiple LogFactory classes in the classpath and
    079        // child-first classloading is used in any class loader, then multiple
    080        // LogFactory instances may hold info about this TCCL; whenever the
    081        // webapp makes a call into a class loaded via an ancestor class loader
    082        // and that class calls LogFactory the tccl gets registered in
    083        // the LogFactory instance that is visible from the ancestor
    084        // class loader. However the concrete logging library it points
    085        // to is expected to have been loaded via the TCCL, so the7
    086        // underlying logging lib is only initialized/configured once.
    087        // These references from ancestor LogFactory classes down to
    088        // TCCL class loaders are held via weak references and so should
    089        // be released but there are circumstances where they may not.
    090        // Walking up the class loader ancestry ladder releasing
    091        // the current tccl at each level tree, though, will definitely
    092        // clear any problem references.
    093        ClassLoader loader = tccl;
    094        while (loader != null) {
    095            // Load via the current loader. Note that if the class is not accessible
    096            // via this loader, but is accessible via some ancestor then that class
    097            // will be returned.
    098            try {
    099                @SuppressWarnings("unchecked")
    100                final Class<LogFactory> logFactoryClass = (Class<LogFactory>) loader.loadClass("org.apache.commons.logging.LogFactory");
    101                final Method releaseMethod = logFactoryClass.getMethod("release", RELEASE_SIGNATURE);
    102                releaseMethod.invoke(null, params);
    103                loader = logFactoryClass.getClassLoader().getParent();
    104            } catch (final ClassNotFoundException ex) {
    105                // Neither the current class loader nor any of its ancestors could find
    106                // the LogFactory class, so we can stop now.
    107                loader = null;
    108            } catch (final NoSuchMethodException ex) {
    109                // This is not expected; every version of JCL has this method
    110                System.err.println("LogFactory instance found which does not support release method!");
    111                loader = null;
    112            } catch (final IllegalAccessException ex) {
    113                // This is not expected; every ancestor class should be accessible
    114                System.err.println("LogFactory instance found which is not accessible!");
    115                loader = null;
    116            } catch (final InvocationTargetException ex) {
    117                // This is not expected
    118                System.err.println("LogFactory instance release method failed!");
    119                loader = null;
    120            }
    121        }
    122        // Just to be sure, invoke release on the LogFactory that is visible from
    123        // this ServletContextCleaner class too. This should already have been caught
    124        // by the above loop but just in case...
    125        LogFactory.release(tccl);
    126    }
    127
    128    /**
    129     * Invoked when a webapp is deployed. Nothing needs to be done here.
    130     */
    131    @Override
    132    public void contextInitialized(final ServletContextEvent sce) {
    133        // do nothing
    134    }
    135}