1 5 package org.h2.util; 6 7 import java.lang.ref.PhantomReference ; 8 import java.lang.ref.Reference ; 9 import java.lang.ref.ReferenceQueue ; 10 import java.util.HashMap ; 11 12 import org.h2.engine.Constants; 13 import org.h2.message.Message; 14 15 public class TempFileDeleter { 16 17 private static ReferenceQueue queue = new ReferenceQueue (); 18 private static HashMap refMap = new HashMap (); 19 20 public static synchronized Reference addFile(String fileName, Object file) { 21 PhantomReference ref = new PhantomReference (file, queue); 22 refMap.put(ref, fileName); 23 deleteUnused(); 24 return ref; 25 } 26 27 public static synchronized void deleteFile(Reference ref, String fileName) { 28 if(ref != null) { 29 String f2 = (String ) refMap.remove(ref); 30 if(Constants.CHECK && f2 != null && fileName != null && !f2.equals(fileName)) { 31 throw Message.getInternalError("f2:"+f2+" f:"+fileName); 32 } 33 } 34 if(fileName != null && FileUtils.exists(fileName)) { 35 try { 36 FileUtils.delete(fileName); 37 } catch(Exception e) { 38 } 40 } 41 deleteUnused(); 42 } 43 44 public static void deleteUnused() { 45 while(true) { 46 Reference ref = queue.poll(); 47 if(ref == null) { 48 break; 49 } 50 deleteFile(ref, null); 51 } 52 } 53 54 public static void stopAutoDelete(Reference ref, String fileName) { 55 if(ref != null) { 56 String f2 = (String ) refMap.remove(ref); 57 if(Constants.CHECK && (f2 == null || !f2.equals(fileName))) { 58 throw Message.getInternalError("f2:"+f2+" f:"+fileName); 59 } 60 } 61 deleteUnused(); 62 } 63 64 } 65 | Popular Tags |