1717package org .apache .commons .io ;
1818
1919import java .io .File ;
20- import java .lang .ref .PhantomReference ;
21- import java .lang .ref .ReferenceQueue ;
22- import java .util .Collection ;
23- import java .util .Vector ;
2420
2521/**
2622 * Keeps track of files awaiting deletion, and deletes them when an associated
3935 * @author Noel Bergman
4036 * @author Martin Cooper
4137 * @version $Id$
38+ * @deprecated Use {@link FileCleaningTracker}
4239 */
4340public class FileCleaner {
44-
45- /**
46- * Queue of <code>Tracker</code> instances being watched.
47- */
48- static ReferenceQueue /* Tracker */ q = new ReferenceQueue ();
49- /**
50- * Collection of <code>Tracker</code> instances in existence.
51- */
52- static Collection /* Tracker */ trackers = new Vector (); // synchronized
53- /**
54- * Whether to terminate the thread when the tracking is complete.
55- */
56- static volatile boolean exitWhenFinished = false ;
5741 /**
58- * The thread that will clean up registered files .
42+ * The instance to use for the deprecated, static methods .
5943 */
60- static Thread reaper ;
44+ static final FileCleaningTracker theInstance = new FileCleaningTracker () ;
6145
6246 //-----------------------------------------------------------------------
6347 /**
@@ -68,9 +52,10 @@ public class FileCleaner {
6852 * @param file the file to be tracked, not null
6953 * @param marker the marker object used to track the file, not null
7054 * @throws NullPointerException if the file is null
55+ * @deprecated Use {@link FileCleaningTracker#track(File, Object)}.
7156 */
7257 public static void track (File file , Object marker ) {
73- track (file , marker , ( FileDeleteStrategy ) null );
58+ theInstance . track (file , marker );
7459 }
7560
7661 /**
@@ -82,12 +67,10 @@ public static void track(File file, Object marker) {
8267 * @param marker the marker object used to track the file, not null
8368 * @param deleteStrategy the strategy to delete the file, null means normal
8469 * @throws NullPointerException if the file is null
70+ * @deprecated Use {@link FileCleaningTracker#track(File, Object, FileDeleteStrategy)}.
8571 */
8672 public static void track (File file , Object marker , FileDeleteStrategy deleteStrategy ) {
87- if (file == null ) {
88- throw new NullPointerException ("The file must not be null" );
89- }
90- addTracker (file .getPath (), marker , deleteStrategy );
73+ theInstance .track (file , marker , deleteStrategy );
9174 }
9275
9376 /**
@@ -98,9 +81,10 @@ public static void track(File file, Object marker, FileDeleteStrategy deleteStra
9881 * @param path the full path to the file to be tracked, not null
9982 * @param marker the marker object used to track the file, not null
10083 * @throws NullPointerException if the path is null
84+ * @deprecated Use {@link FileCleaningTracker#track(String, Object)}.
10185 */
10286 public static void track (String path , Object marker ) {
103- track (path , marker , ( FileDeleteStrategy ) null );
87+ theInstance . track (path , marker );
10488 }
10589
10690 /**
@@ -112,31 +96,10 @@ public static void track(String path, Object marker) {
11296 * @param marker the marker object used to track the file, not null
11397 * @param deleteStrategy the strategy to delete the file, null means normal
11498 * @throws NullPointerException if the path is null
99+ * @deprecated Use {@link FileCleaningTracker#track(String, Object, FileDeleteStrategy)}.
115100 */
116101 public static void track (String path , Object marker , FileDeleteStrategy deleteStrategy ) {
117- if (path == null ) {
118- throw new NullPointerException ("The path must not be null" );
119- }
120- addTracker (path , marker , deleteStrategy );
121- }
122-
123- /**
124- * Adds a tracker to the list of trackers.
125- *
126- * @param path the full path to the file to be tracked, not null
127- * @param marker the marker object used to track the file, not null
128- * @param deleteStrategy the strategy to delete the file, null means normal
129- */
130- private static synchronized void addTracker (String path , Object marker , FileDeleteStrategy deleteStrategy ) {
131- // synchronized block protects reaper
132- if (exitWhenFinished ) {
133- throw new IllegalStateException ("No new trackers can be added once exitWhenFinished() is called" );
134- }
135- if (reaper == null ) {
136- reaper = new Reaper ();
137- reaper .start ();
138- }
139- trackers .add (new Tracker (path , deleteStrategy , marker , q ));
102+ theInstance .track (path , marker , deleteStrategy );
140103 }
141104
142105 //-----------------------------------------------------------------------
@@ -145,9 +108,10 @@ private static synchronized void addTracker(String path, Object marker, FileDele
145108 * awaiting deletion.
146109 *
147110 * @return the number of files being tracked
111+ * @deprecated Use {@link FileCleaningTracker#getTrackCount()}.
148112 */
149113 public static int getTrackCount () {
150- return trackers . size ();
114+ return theInstance . getTrackCount ();
151115 }
152116
153117 /**
@@ -170,90 +134,19 @@ public static int getTrackCount() {
170134 * This method allows the thread to be terminated. Simply call this method
171135 * in the resource cleanup code, such as {@link javax.servlet.ServletContextListener#contextDestroyed}.
172136 * One called, no new objects can be tracked by the file cleaner.
137+ * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}.
173138 */
174139 public static synchronized void exitWhenFinished () {
175- // synchronized block protects reaper
176- exitWhenFinished = true ;
177- if (reaper != null ) {
178- synchronized (reaper ) {
179- reaper .interrupt ();
180- }
181- }
182- }
183-
184- //-----------------------------------------------------------------------
185- /**
186- * The reaper thread.
187- */
188- static final class Reaper extends Thread {
189- /** Construct a new Reaper */
190- Reaper () {
191- super ("File Reaper" );
192- setPriority (Thread .MAX_PRIORITY );
193- setDaemon (true );
194- }
195-
196- /**
197- * Run the reaper thread that will delete files as their associated
198- * marker objects are reclaimed by the garbage collector.
199- */
200- public void run () {
201- // thread exits when exitWhenFinished is true and there are no more tracked objects
202- while (exitWhenFinished == false || trackers .size () > 0 ) {
203- Tracker tracker = null ;
204- try {
205- // Wait for a tracker to remove.
206- tracker = (Tracker ) q .remove ();
207- } catch (Exception e ) {
208- continue ;
209- }
210- if (tracker != null ) {
211- tracker .delete ();
212- tracker .clear ();
213- trackers .remove (tracker );
214- }
215- }
216- }
140+ theInstance .exitWhenFinished ();
217141 }
218142
219- //-----------------------------------------------------------------------
220143 /**
221- * Inner class which acts as the reference for a file pending deletion.
144+ * Returns the singleton instance, which is used by the deprecated, static methods.
145+ * This is mainly useful for code, which wants to support the new
146+ * {@link FileCleaningTracker} class while maintain compatibility with the
147+ * deprecated {@link FileCleaner}.
222148 */
223- static final class Tracker extends PhantomReference {
224-
225- /**
226- * The full path to the file being tracked.
227- */
228- private final String path ;
229- /**
230- * The strategy for deleting files.
231- */
232- private final FileDeleteStrategy deleteStrategy ;
233-
234- /**
235- * Constructs an instance of this class from the supplied parameters.
236- *
237- * @param path the full path to the file to be tracked, not null
238- * @param deleteStrategy the strategy to delete the file, null means normal
239- * @param marker the marker object used to track the file, not null
240- * @param queue the queue on to which the tracker will be pushed, not null
241- */
242- Tracker (String path , FileDeleteStrategy deleteStrategy , Object marker , ReferenceQueue queue ) {
243- super (marker , queue );
244- this .path = path ;
245- this .deleteStrategy = (deleteStrategy == null ? FileDeleteStrategy .NORMAL : deleteStrategy );
246- }
247-
248- /**
249- * Deletes the file associated with this tracker instance.
250- *
251- * @return <code>true</code> if the file was deleted successfully;
252- * <code>false</code> otherwise.
253- */
254- public boolean delete () {
255- return deleteStrategy .deleteQuietly (new File (path ));
256- }
149+ public static FileCleaningTracker getInstance () {
150+ return theInstance ;
257151 }
258-
259152}
0 commit comments