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.io; 018 019import java.io.File; 020 021/** 022 * Keeps track of files awaiting deletion, and deletes them when an associated 023 * marker object is reclaimed by the garbage collector. 024 * <p> 025 * This utility creates a background thread to handle file deletion. 026 * Each file to be deleted is registered with a handler object. 027 * When the handler object is garbage collected, the file is deleted. 028 * <p> 029 * In an environment with multiple class loaders (a servlet container, for 030 * example), you should consider stopping the background thread if it is no 031 * longer needed. This is done by invoking the method 032 * {@link #exitWhenFinished}, typically in 033 * {@code javax.servlet.ServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent)} or similar. 034 * 035 * @version $Id: FileCleaner.java 1680650 2015-05-20 18:36:40Z britter $ 036 * @deprecated Use {@link FileCleaningTracker} 037 */ 038@Deprecated 039public class FileCleaner { 040 /** 041 * The instance to use for the deprecated, static methods. 042 */ 043 static final FileCleaningTracker theInstance = new FileCleaningTracker(); 044 045 //----------------------------------------------------------------------- 046 /** 047 * Track the specified file, using the provided marker, deleting the file 048 * when the marker instance is garbage collected. 049 * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. 050 * 051 * @param file the file to be tracked, not null 052 * @param marker the marker object used to track the file, not null 053 * @throws NullPointerException if the file is null 054 * @deprecated Use {@link FileCleaningTracker#track(File, Object)}. 055 */ 056 @Deprecated 057 public static void track(final File file, final Object marker) { 058 theInstance.track(file, marker); 059 } 060 061 /** 062 * Track the specified file, using the provided marker, deleting the file 063 * when the marker instance is garbage collected. 064 * The speified deletion strategy is used. 065 * 066 * @param file the file to be tracked, not null 067 * @param marker the marker object used to track the file, not null 068 * @param deleteStrategy the strategy to delete the file, null means normal 069 * @throws NullPointerException if the file is null 070 * @deprecated Use {@link FileCleaningTracker#track(File, Object, FileDeleteStrategy)}. 071 */ 072 @Deprecated 073 public static void track(final File file, final Object marker, final FileDeleteStrategy deleteStrategy) { 074 theInstance.track(file, marker, deleteStrategy); 075 } 076 077 /** 078 * Track the specified file, using the provided marker, deleting the file 079 * when the marker instance is garbage collected. 080 * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. 081 * 082 * @param path the full path to the file to be tracked, not null 083 * @param marker the marker object used to track the file, not null 084 * @throws NullPointerException if the path is null 085 * @deprecated Use {@link FileCleaningTracker#track(String, Object)}. 086 */ 087 @Deprecated 088 public static void track(final String path, final Object marker) { 089 theInstance.track(path, marker); 090 } 091 092 /** 093 * Track the specified file, using the provided marker, deleting the file 094 * when the marker instance is garbage collected. 095 * The speified deletion strategy is used. 096 * 097 * @param path the full path to the file to be tracked, not null 098 * @param marker the marker object used to track the file, not null 099 * @param deleteStrategy the strategy to delete the file, null means normal 100 * @throws NullPointerException if the path is null 101 * @deprecated Use {@link FileCleaningTracker#track(String, Object, FileDeleteStrategy)}. 102 */ 103 @Deprecated 104 public static void track(final String path, final Object marker, final FileDeleteStrategy deleteStrategy) { 105 theInstance.track(path, marker, deleteStrategy); 106 } 107 108 //----------------------------------------------------------------------- 109 /** 110 * Retrieve the number of files currently being tracked, and therefore 111 * awaiting deletion. 112 * 113 * @return the number of files being tracked 114 * @deprecated Use {@link FileCleaningTracker#getTrackCount()}. 115 */ 116 @Deprecated 117 public static int getTrackCount() { 118 return theInstance.getTrackCount(); 119 } 120 121 /** 122 * Call this method to cause the file cleaner thread to terminate when 123 * there are no more objects being tracked for deletion. 124 * <p> 125 * In a simple environment, you don't need this method as the file cleaner 126 * thread will simply exit when the JVM exits. In a more complex environment, 127 * with multiple class loaders (such as an application server), you should be 128 * aware that the file cleaner thread will continue running even if the class 129 * loader it was started from terminates. This can consitute a memory leak. 130 * <p> 131 * For example, suppose that you have developed a web application, which 132 * contains the commons-io jar file in your WEB-INF/lib directory. In other 133 * words, the FileCleaner class is loaded through the class loader of your 134 * web application. If the web application is terminated, but the servlet 135 * container is still running, then the file cleaner thread will still exist, 136 * posing a memory leak. 137 * <p> 138 * This method allows the thread to be terminated. Simply call this method 139 * in the resource cleanup code, such as 140 * {@code javax.servlet.ServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent)}. 141 * One called, no new objects can be tracked by the file cleaner. 142 * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}. 143 */ 144 @Deprecated 145 public static synchronized void exitWhenFinished() { 146 theInstance.exitWhenFinished(); 147 } 148 149 /** 150 * Returns the singleton instance, which is used by the deprecated, static methods. 151 * This is mainly useful for code, which wants to support the new 152 * {@link FileCleaningTracker} class while maintain compatibility with the 153 * deprecated {@link FileCleaner}. 154 * 155 * @return the singleton instance 156 */ 157 public static FileCleaningTracker getInstance() { 158 return theInstance; 159 } 160}