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