File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\TempFileDeleter.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.util;
8
9 import java.lang.ref.PhantomReference;
10 import java.lang.ref.Reference;
11 import java.lang.ref.ReferenceQueue;
12 import java.util.HashMap;
13
14 import org.h2.constant.SysProperties;
15 import org.h2.message.DbException;
16 import org.h2.store.fs.FileUtils;
17
18 /**
19 * This class deletes temporary files when they are not used any longer.
20 */
21 public class TempFileDeleter {
22
23 private final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
24 private final HashMap<PhantomReference<?>, String> refMap = New.hashMap();
25
26 private TempFileDeleter() {
27 // utility class
28 }
29
30 public static TempFileDeleter getInstance() {
31 return new TempFileDeleter();
32 }
33
34 /**
35 * Add a file to the list of temp files to delete. The file is deleted once
36 * the file object is garbage collected.
37 *
38 * @param fileName the file name
39 * @param file the object to monitor
40 * @return the reference that can be used to stop deleting the file
41 */
42 public synchronized Reference<?> addFile(String fileName, Object file) {
43 IOUtils.trace("TempFileDeleter.addFile", fileName, file);
44 PhantomReference<?> ref = new PhantomReference<Object>(file, queue);
45 refMap.put(ref, fileName);
46 deleteUnused();
47 return ref;
48 }
49
50 /**
51 * Delete the given file now. This will remove the reference from the list.
52 *
53 * @param ref the reference as returned by addFile
54 * @param fileName the file name
55 */
56 public synchronized void deleteFile(Reference<?> ref, String fileName) {
57 if (ref != null) {
58 String f2 = refMap.remove(ref);
59 if (f2 != null) {
60 if (SysProperties.CHECK) {
61 if (fileName != null && !f2.equals(fileName)) {
62 DbException.throwInternalError("f2:" + f2 + " f:" + fileName);
63 }
64 }
65 fileName = f2;
66 }
67 }
68 if (fileName != null && FileUtils.exists(fileName)) {
69 try {
70 IOUtils.trace("TempFileDeleter.deleteFile", fileName, null);
71 FileUtils.tryDelete(fileName);
72 } catch (Exception e) {
73 // TODO log such errors?
74 }
75 }
76 }
77
78 /**
79 * Delete all registered temp files.
80 */
81 public void deleteAll() {
82 for (String tempFile : New.arrayList(refMap.values())) {
83 deleteFile(null, tempFile);
84 }
85 deleteUnused();
86 }
87
88 /**
89 * Delete all unused files now.
90 */
91 public void deleteUnused() {
92 while (queue != null) {
93 Reference<? extends Object> ref = queue.poll();
94 if (ref == null) {
95 break;
96 }
97 deleteFile(ref, null);
98 }
99 }
100
101 /**
102 * This method is called if a file should no longer be deleted if the object
103 * is garbage collected.
104 *
105 * @param ref the reference as returned by addFile
106 * @param fileName the file name
107 */
108 public void stopAutoDelete(Reference<?> ref, String fileName) {
109 IOUtils.trace("TempFileDeleter.stopAutoDelete", fileName, ref);
110 if (ref != null) {
111 String f2 = refMap.remove(ref);
112 if (SysProperties.CHECK) {
113 if (f2 == null || !f2.equals(fileName)) {
114 DbException.throwInternalError("f2:" + f2 + " " + (f2 == null ? "" : f2) + " f:" + fileName);
115 }
116 }
117 }
118 deleteUnused();
119 }
120
121 }
122