1 16 package org.apache.commons.io; 17 18 import java.io.File ; 19 import java.lang.ref.PhantomReference ; 20 import java.lang.ref.ReferenceQueue ; 21 import java.util.Collection ; 22 import java.util.Vector ; 23 24 33 public class FileCleaner { 34 35 38 private static ReferenceQueue q = new ReferenceQueue (); 39 40 43 private static Collection trackers = new Vector (); 44 45 48 private static Thread reaper = new Thread ("File Reaper") { 49 50 54 public void run() { 55 for (;;) { 56 Tracker tracker = null; 57 try { 58 tracker = (Tracker) q.remove(); 60 } catch(Exception _) { 61 continue; 62 } 63 64 tracker.delete(); 65 tracker.clear(); 66 trackers.remove(tracker); 67 } 68 } 69 }; 70 71 74 static { 75 reaper.setPriority(Thread.MAX_PRIORITY); 76 reaper.setDaemon(true); 77 reaper.start(); 78 } 79 80 87 public static void track(File file, Object marker) { 88 trackers.add(new Tracker(file, marker, q)); 89 } 90 91 98 public static void track(String path, Object marker) { 99 trackers.add(new Tracker(path, marker, q)); 100 } 101 102 108 public static int getTrackCount() { 109 return trackers.size(); 110 } 111 112 115 private static class Tracker extends PhantomReference { 116 117 120 private String path; 121 122 129 public Tracker(File file, Object marker, ReferenceQueue q) { 130 this(file.getPath(), marker, q); 131 } 132 133 140 public Tracker(String path, Object marker, ReferenceQueue q) { 141 super(marker, q); 142 this.path = path; 143 } 144 145 151 public boolean delete() { 152 return new File (path).delete(); 153 } 154 } 155 } | Popular Tags |