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.ServletContextEvent; 021import javax.servlet.ServletContextListener; 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 servletContext The servlet context to query 044 * @return The contexts tracker 045 */ 046 public static FileCleaningTracker getFileCleaningTracker(final ServletContext servletContext) { 047 return (FileCleaningTracker) servletContext.getAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE); 048 } 049 050 /** 051 * Sets the instance of {@link FileCleaningTracker}, which is 052 * associated with the given {@link ServletContext}. 053 * 054 * @param servletContext The servlet context to modify 055 * @param tracker The tracker to set 056 */ 057 public static void setFileCleaningTracker(final ServletContext servletContext, final FileCleaningTracker tracker) { 058 servletContext.setAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE, tracker); 059 } 060 061 /** 062 * Constructs a new instance. 063 */ 064 public FileCleanerCleanup() { 065 // empty 066 } 067 068 /** 069 * Called when the web application is being destroyed. 070 * Calls {@link FileCleaningTracker#exitWhenFinished()}. 071 * 072 * @param sce The servlet context, used for calling 073 * {@link #getFileCleaningTracker(ServletContext)}. 074 */ 075 @Override 076 public void contextDestroyed(final ServletContextEvent sce) { 077 getFileCleaningTracker(sce.getServletContext()).exitWhenFinished(); 078 } 079 080 /** 081 * Called when the web application is initialized. Does 082 * nothing. 083 * 084 * @param sce The servlet context, used for calling 085 * {@link #setFileCleaningTracker(ServletContext, FileCleaningTracker)}. 086 */ 087 @Override 088 public void contextInitialized(final ServletContextEvent sce) { 089 setFileCleaningTracker(sce.getServletContext(), 090 new FileCleaningTracker()); 091 } 092 093}