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