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 *      http://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 */
017package org.apache.commons.fileupload.servlet;
018
019import javax.servlet.ServletContext;
020import javax.servlet.ServletContextListener;
021import javax.servlet.ServletContextEvent;
022
023import org.apache.commons.io.FileCleaningTracker;
024
025/**
026 * A servlet context listener, which ensures that the
027 * {@link FileCleaningTracker}'s reaper thread is terminated,
028 * when the web application is destroyed.
029 */
030public class FileCleanerCleanup implements ServletContextListener {
031
032    /**
033     * Attribute name, which is used for storing an instance of
034     * {@link FileCleaningTracker} in the web application.
035     */
036    public static final String FILE_CLEANING_TRACKER_ATTRIBUTE
037        = FileCleanerCleanup.class.getName() + ".FileCleaningTracker";
038
039    /**
040     * Returns the instance of {@link FileCleaningTracker}, which is
041     * associated with the given {@link ServletContext}.
042     *
043     * @param pServletContext The servlet context to query
044     * @return The contexts tracker
045     */
046    public static FileCleaningTracker
047            getFileCleaningTracker(ServletContext pServletContext) {
048        return (FileCleaningTracker)
049            pServletContext.getAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE);
050    }
051
052    /**
053     * Sets the instance of {@link FileCleaningTracker}, which is
054     * associated with the given {@link ServletContext}.
055     *
056     * @param pServletContext The servlet context to modify
057     * @param pTracker The tracker to set
058     */
059    public static void setFileCleaningTracker(ServletContext pServletContext,
060            FileCleaningTracker pTracker) {
061        pServletContext.setAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE, pTracker);
062    }
063
064    /**
065     * Called when the web application is initialized. Does
066     * nothing.
067     *
068     * @param sce The servlet context, used for calling
069     *   {@link #setFileCleaningTracker(ServletContext, FileCleaningTracker)}.
070     */
071    public void contextInitialized(ServletContextEvent sce) {
072        setFileCleaningTracker(sce.getServletContext(),
073                new FileCleaningTracker());
074    }
075
076    /**
077     * Called when the web application is being destroyed.
078     * Calls {@link FileCleaningTracker#exitWhenFinished()}.
079     *
080     * @param sce The servlet context, used for calling
081     *     {@link #getFileCleaningTracker(ServletContext)}.
082     */
083    public void contextDestroyed(ServletContextEvent sce) {
084        getFileCleaningTracker(sce.getServletContext()).exitWhenFinished();
085    }
086
087}