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