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