FileCleaner.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io;

  18. import java.io.File;

  19. /**
  20.  * Keeps track of files awaiting deletion, and deletes them when an associated
  21.  * marker object is reclaimed by the garbage collector.
  22.  * <p>
  23.  * This utility creates a background thread to handle file deletion.
  24.  * Each file to be deleted is registered with a handler object.
  25.  * When the handler object is garbage collected, the file is deleted.
  26.  * <p>
  27.  * In an environment with multiple class loaders (a servlet container, for
  28.  * example), you should consider stopping the background thread if it is no
  29.  * longer needed. This is done by invoking the method
  30.  * {@link #exitWhenFinished}, typically in
  31.  * {@code javax.servlet.ServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent)} or similar.
  32.  *
  33.  * @deprecated Use {@link FileCleaningTracker}
  34.  */
  35. @Deprecated
  36. public class FileCleaner {

  37.     /**
  38.      * The instance to use for the deprecated, static methods.
  39.      */
  40.     private static final FileCleaningTracker INSTANCE = new FileCleaningTracker();

  41.     /**
  42.      * Call this method to cause the file cleaner thread to terminate when
  43.      * there are no more objects being tracked for deletion.
  44.      * <p>
  45.      * In a simple environment, you don't need this method as the file cleaner
  46.      * thread will simply exit when the JVM exits. In a more complex environment,
  47.      * with multiple class loaders (such as an application server), you should be
  48.      * aware that the file cleaner thread will continue running even if the class
  49.      * loader it was started from terminates. This can constitute a memory leak.
  50.      * <p>
  51.      * For example, suppose that you have developed a web application, which
  52.      * contains the commons-io jar file in your WEB-INF/lib directory. In other
  53.      * words, the FileCleaner class is loaded through the class loader of your
  54.      * web application. If the web application is terminated, but the servlet
  55.      * container is still running, then the file cleaner thread will still exist,
  56.      * posing a memory leak.
  57.      * <p>
  58.      * This method allows the thread to be terminated. Simply call this method
  59.      * in the resource cleanup code, such as
  60.      * {@code javax.servlet.ServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent)}.
  61.      * One called, no new objects can be tracked by the file cleaner.
  62.      * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}.
  63.      */
  64.     @Deprecated
  65.     public static synchronized void exitWhenFinished() {
  66.         INSTANCE.exitWhenFinished();
  67.     }

  68.     /**
  69.      * Gets the singleton instance, which is used by the deprecated, static methods.
  70.      * This is mainly useful for code, which wants to support the new
  71.      * {@link FileCleaningTracker} class while maintain compatibility with the
  72.      * deprecated {@link FileCleaner}.
  73.      *
  74.      * @return the singleton instance
  75.      */
  76.     public static FileCleaningTracker getInstance() {
  77.         return INSTANCE;
  78.     }

  79.     /**
  80.      * Gets the number of files currently being tracked, and therefore
  81.      * awaiting deletion.
  82.      *
  83.      * @return the number of files being tracked
  84.      * @deprecated Use {@link FileCleaningTracker#getTrackCount()}.
  85.      */
  86.     @Deprecated
  87.     public static int getTrackCount() {
  88.         return INSTANCE.getTrackCount();
  89.     }

  90.     /**
  91.      * Track the specified file, using the provided marker, deleting the file
  92.      * when the marker instance is garbage collected.
  93.      * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used.
  94.      *
  95.      * @param file  the file to be tracked, not null
  96.      * @param marker  the marker object used to track the file, not null
  97.      * @throws NullPointerException if the file is null
  98.      * @deprecated Use {@link FileCleaningTracker#track(File, Object)}.
  99.      */
  100.     @Deprecated
  101.     public static void track(final File file, final Object marker) {
  102.         INSTANCE.track(file, marker);
  103.     }

  104.     /**
  105.      * Track the specified file, using the provided marker, deleting the file
  106.      * when the marker instance is garbage collected.
  107.      * The specified deletion strategy is used.
  108.      *
  109.      * @param file  the file to be tracked, not null
  110.      * @param marker  the marker object used to track the file, not null
  111.      * @param deleteStrategy  the strategy to delete the file, null means normal
  112.      * @throws NullPointerException if the file is null
  113.      * @deprecated Use {@link FileCleaningTracker#track(File, Object, FileDeleteStrategy)}.
  114.      */
  115.     @Deprecated
  116.     public static void track(final File file, final Object marker, final FileDeleteStrategy deleteStrategy) {
  117.         INSTANCE.track(file, marker, deleteStrategy);
  118.     }

  119.     /**
  120.      * Track the specified file, using the provided marker, deleting the file
  121.      * when the marker instance is garbage collected.
  122.      * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used.
  123.      *
  124.      * @param path  the full path to the file to be tracked, not null
  125.      * @param marker  the marker object used to track the file, not null
  126.      * @throws NullPointerException if the path is null
  127.      * @deprecated Use {@link FileCleaningTracker#track(String, Object)}.
  128.      */
  129.     @Deprecated
  130.     public static void track(final String path, final Object marker) {
  131.         INSTANCE.track(path, marker);
  132.     }

  133.     /**
  134.      * Track the specified file, using the provided marker, deleting the file
  135.      * when the marker instance is garbage collected.
  136.      * The specified deletion strategy is used.
  137.      *
  138.      * @param path  the full path to the file to be tracked, not null
  139.      * @param marker  the marker object used to track the file, not null
  140.      * @param deleteStrategy  the strategy to delete the file, null means normal
  141.      * @throws NullPointerException if the path is null
  142.      * @deprecated Use {@link FileCleaningTracker#track(String, Object, FileDeleteStrategy)}.
  143.      */
  144.     @Deprecated
  145.     public static void track(final String path, final Object marker, final FileDeleteStrategy deleteStrategy) {
  146.         INSTANCE.track(path, marker, deleteStrategy);
  147.     }

  148.     /**
  149.      * Construct a new instance.
  150.      */
  151.     public FileCleaner() {
  152.         // empty
  153.     }
  154. }