| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FileCleaner |
|
| 1.0;1 |
| 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 | ||
| 19 | import java.io.File; | |
| 20 | ||
| 21 | /** | |
| 22 | * Keeps track of files awaiting deletion, and deletes them when an associated | |
| 23 | * marker object is reclaimed by the garbage collector. | |
| 24 | * <p> | |
| 25 | * This utility creates a background thread to handle file deletion. | |
| 26 | * Each file to be deleted is registered with a handler object. | |
| 27 | * When the handler object is garbage collected, the file is deleted. | |
| 28 | * <p> | |
| 29 | * In an environment with multiple class loaders (a servlet container, for | |
| 30 | * example), you should consider stopping the background thread if it is no | |
| 31 | * longer needed. This is done by invoking the method | |
| 32 | * {@link #exitWhenFinished}, typically in | |
| 33 | * {@link javax.servlet.ServletContextListener#contextDestroyed} or similar. | |
| 34 | * | |
| 35 | * @version $Id: FileCleaner.java 1415850 2012-11-30 20:51:39Z ggregory $ | |
| 36 | * @deprecated Use {@link FileCleaningTracker} | |
| 37 | */ | |
| 38 | @Deprecated | |
| 39 | 0 | public class FileCleaner { |
| 40 | /** | |
| 41 | * The instance to use for the deprecated, static methods. | |
| 42 | */ | |
| 43 | 1 | static final FileCleaningTracker theInstance = new FileCleaningTracker(); |
| 44 | ||
| 45 | //----------------------------------------------------------------------- | |
| 46 | /** | |
| 47 | * Track the specified file, using the provided marker, deleting the file | |
| 48 | * when the marker instance is garbage collected. | |
| 49 | * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. | |
| 50 | * | |
| 51 | * @param file the file to be tracked, not null | |
| 52 | * @param marker the marker object used to track the file, not null | |
| 53 | * @throws NullPointerException if the file is null | |
| 54 | * @deprecated Use {@link FileCleaningTracker#track(File, Object)}. | |
| 55 | */ | |
| 56 | @Deprecated | |
| 57 | public static void track(final File file, final Object marker) { | |
| 58 | 0 | theInstance.track(file, marker); |
| 59 | 0 | } |
| 60 | ||
| 61 | /** | |
| 62 | * Track the specified file, using the provided marker, deleting the file | |
| 63 | * when the marker instance is garbage collected. | |
| 64 | * The speified deletion strategy is used. | |
| 65 | * | |
| 66 | * @param file the file to be tracked, not null | |
| 67 | * @param marker the marker object used to track the file, not null | |
| 68 | * @param deleteStrategy the strategy to delete the file, null means normal | |
| 69 | * @throws NullPointerException if the file is null | |
| 70 | * @deprecated Use {@link FileCleaningTracker#track(File, Object, FileDeleteStrategy)}. | |
| 71 | */ | |
| 72 | @Deprecated | |
| 73 | public static void track(final File file, final Object marker, final FileDeleteStrategy deleteStrategy) { | |
| 74 | 0 | theInstance.track(file, marker, deleteStrategy); |
| 75 | 0 | } |
| 76 | ||
| 77 | /** | |
| 78 | * Track the specified file, using the provided marker, deleting the file | |
| 79 | * when the marker instance is garbage collected. | |
| 80 | * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. | |
| 81 | * | |
| 82 | * @param path the full path to the file to be tracked, not null | |
| 83 | * @param marker the marker object used to track the file, not null | |
| 84 | * @throws NullPointerException if the path is null | |
| 85 | * @deprecated Use {@link FileCleaningTracker#track(String, Object)}. | |
| 86 | */ | |
| 87 | @Deprecated | |
| 88 | public static void track(final String path, final Object marker) { | |
| 89 | 0 | theInstance.track(path, marker); |
| 90 | 0 | } |
| 91 | ||
| 92 | /** | |
| 93 | * Track the specified file, using the provided marker, deleting the file | |
| 94 | * when the marker instance is garbage collected. | |
| 95 | * The speified deletion strategy is used. | |
| 96 | * | |
| 97 | * @param path the full path to the file to be tracked, not null | |
| 98 | * @param marker the marker object used to track the file, not null | |
| 99 | * @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 | 0 | theInstance.track(path, marker, deleteStrategy); |
| 106 | 0 | } |
| 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 | 0 | 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 {@link javax.servlet.ServletContextListener#contextDestroyed}. | |
| 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 | 0 | theInstance.exitWhenFinished(); |
| 146 | 0 | } |
| 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 | 9 | return theInstance; |
| 158 | } | |
| 159 | } |