001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.context;
018
019import java.util.Enumeration;
020import javax.servlet.ServletContext;
021import javax.servlet.ServletContextEvent;
022import javax.servlet.ServletContextListener;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026
027import org.springframework.beans.factory.DisposableBean;
028
029/**
030 * Web application listener that cleans up remaining disposable attributes
031 * in the ServletContext, i.e. attributes which implement {@link DisposableBean}
032 * and haven't been removed before. This is typically used for destroying objects
033 * in "application" scope, for which the lifecycle implies destruction at the
034 * very end of the web application's shutdown phase.
035 *
036 * @author Juergen Hoeller
037 * @since 3.0
038 * @see org.springframework.web.context.support.ServletContextScope
039 * @see ContextLoaderListener
040 */
041public class ContextCleanupListener implements ServletContextListener {
042
043        private static final Log logger = LogFactory.getLog(ContextCleanupListener.class);
044
045
046        @Override
047        public void contextInitialized(ServletContextEvent event) {
048        }
049
050        @Override
051        public void contextDestroyed(ServletContextEvent event) {
052                cleanupAttributes(event.getServletContext());
053        }
054
055
056        /**
057         * Find all ServletContext attributes which implement {@link DisposableBean}
058         * and destroy them, removing all affected ServletContext attributes eventually.
059         * @param sc the ServletContext to check
060         */
061        static void cleanupAttributes(ServletContext sc) {
062                Enumeration<String> attrNames = sc.getAttributeNames();
063                while (attrNames.hasMoreElements()) {
064                        String attrName = attrNames.nextElement();
065                        if (attrName.startsWith("org.springframework.")) {
066                                Object attrValue = sc.getAttribute(attrName);
067                                if (attrValue instanceof DisposableBean) {
068                                        try {
069                                                ((DisposableBean) attrValue).destroy();
070                                        }
071                                        catch (Throwable ex) {
072                                                logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
073                                        }
074                                }
075                        }
076                }
077        }
078
079}