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