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 /**
027 * A servlet context listener, which ensures that the
028 * {@link org.apache.commons.io.FileCleaner FileCleaner's}
029 * reaper thread is terminated,
030 * when the web application is destroyed.
031 */
032 public class FileCleanerCleanup implements ServletContextListener {
033 /**
034 * Attribute name, which is used for storing an instance of
035 * {@link FileCleaningTracker} in the web application.
036 */
037 public static final String FILE_CLEANING_TRACKER_ATTRIBUTE
038 = FileCleanerCleanup.class.getName() + ".FileCleaningTracker";
039
040 /**
041 * Returns the instance of {@link FileCleaningTracker}, which is
042 * associated with the given {@link ServletContext}.
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 * @param pServletContext The servlet context to modify
056 * @param pTracker The tracker to set
057 */
058 public static void setFileCleaningTracker(ServletContext pServletContext,
059 FileCleaningTracker pTracker) {
060 pServletContext.setAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE, pTracker);
061 }
062
063 /**
064 * Called when the web application is initialized. Does
065 * nothing.
066 * @param sce The servlet context, used for calling
067 * {@link #setFileCleaningTracker(ServletContext, FileCleaningTracker)}.
068 */
069 public void contextInitialized(ServletContextEvent sce) {
070 setFileCleaningTracker(sce.getServletContext(),
071 new FileCleaningTracker());
072 }
073
074 /**
075 * Called when the web application is being destroyed.
076 * Calls {@link FileCleaningTracker#exitWhenFinished()}.
077 * @param sce The servlet context, used for calling
078 * {@link #getFileCleaningTracker(ServletContext)}.
079 */
080 public void contextDestroyed(ServletContextEvent sce) {
081 getFileCleaningTracker(sce.getServletContext()).exitWhenFinished();
082 }
083 }