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 *
030 * @version $Id: FileCleanerCleanup.java 1564788 2014-02-05 14:36:41Z markt $
031 */
032public class FileCleanerCleanup implements ServletContextListener {
033
034    /**
035     * Attribute name, which is used for storing an instance of
036     * {@link FileCleaningTracker} in the web application.
037     */
038    public static final String FILE_CLEANING_TRACKER_ATTRIBUTE
039        = FileCleanerCleanup.class.getName() + ".FileCleaningTracker";
040
041    /**
042     * Returns the instance of {@link FileCleaningTracker}, which is
043     * associated with the given {@link ServletContext}.
044     *
045     * @param pServletContext The servlet context to query
046     * @return The contexts tracker
047     */
048    public static FileCleaningTracker
049            getFileCleaningTracker(ServletContext pServletContext) {
050        return (FileCleaningTracker)
051            pServletContext.getAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE);
052    }
053
054    /**
055     * Sets the instance of {@link FileCleaningTracker}, which is
056     * associated with the given {@link ServletContext}.
057     *
058     * @param pServletContext The servlet context to modify
059     * @param pTracker The tracker to set
060     */
061    public static void setFileCleaningTracker(ServletContext pServletContext,
062            FileCleaningTracker pTracker) {
063        pServletContext.setAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE, pTracker);
064    }
065
066    /**
067     * Called when the web application is initialized. Does
068     * nothing.
069     *
070     * @param sce The servlet context, used for calling
071     *   {@link #setFileCleaningTracker(ServletContext, FileCleaningTracker)}.
072     */
073    public void contextInitialized(ServletContextEvent sce) {
074        setFileCleaningTracker(sce.getServletContext(),
075                new FileCleaningTracker());
076    }
077
078    /**
079     * Called when the web application is being destroyed.
080     * Calls {@link FileCleaningTracker#exitWhenFinished()}.
081     *
082     * @param sce The servlet context, used for calling
083     *     {@link #getFileCleaningTracker(ServletContext)}.
084     */
085    public void contextDestroyed(ServletContextEvent sce) {
086        getFileCleaningTracker(sce.getServletContext()).exitWhenFinished();
087    }
088
089}