| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
* Iso8601: |
| 6 |
* Initial Developer: Robert Rathsack (firstName dot lastName at gmx dot de) |
| 7 |
*/ |
| 8 |
package org.h2.util; |
| 9 |
|
| 10 |
import java.io.PrintWriter; |
| 11 |
import java.io.StringWriter; |
| 12 |
import java.lang.ref.PhantomReference; |
| 13 |
import java.lang.ref.ReferenceQueue; |
| 14 |
import java.util.Collections; |
| 15 |
import java.util.HashSet; |
| 16 |
import java.util.Set; |
| 17 |
|
| 18 |
/** |
| 19 |
* A phantom reference to watch for unclosed objects. |
| 20 |
*/ |
| 21 |
public class CloseWatcher extends PhantomReference<Object> { |
| 22 |
|
| 23 |
/** |
| 24 |
* The queue (might be set to null at any time). |
| 25 |
*/ |
| 26 |
private static final ReferenceQueue<Object> queue = new ReferenceQueue<>(); |
| 27 |
|
| 28 |
/** |
| 29 |
* The reference set. Must keep it, otherwise the references are garbage |
| 30 |
* collected first and thus never enqueued. |
| 31 |
*/ |
| 32 |
private static final Set<CloseWatcher> refs = Collections.synchronizedSet(new HashSet<>()); |
| 33 |
|
| 34 |
/** |
| 35 |
* The stack trace of when the object was created. It is converted to a |
| 36 |
* string early on to avoid classloader problems (a classloader can't be |
| 37 |
* garbage collected if there is a static reference to one of its classes). |
| 38 |
*/ |
| 39 |
private String openStackTrace; |
| 40 |
|
| 41 |
/** |
| 42 |
* The closeable object. |
| 43 |
*/ |
| 44 |
private AutoCloseable closeable; |
| 45 |
|
| 46 |
public CloseWatcher(Object referent, ReferenceQueue<Object> q, |
| 47 |
AutoCloseable closeable) { |
| 48 |
super(referent, q); |
| 49 |
this.closeable = closeable; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Check for an collected object. |
| 54 |
* |
| 55 |
* @return the first watcher |
| 56 |
*/ |
| 57 |
public static CloseWatcher pollUnclosed() { |
| 58 |
while (true) { |
| 59 |
CloseWatcher cw = (CloseWatcher) queue.poll(); |
| 60 |
if (cw == null) { |
| 61 |
return null; |
| 62 |
} |
| 63 |
if (refs != null) { |
| 64 |
refs.remove(cw); |
| 65 |
} |
| 66 |
if (cw.closeable != null) { |
| 67 |
return cw; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Register an object. Before calling this method, pollUnclosed() should be |
| 74 |
* called in a loop to remove old references. |
| 75 |
* |
| 76 |
* @param o the object |
| 77 |
* @param closeable the object to close |
| 78 |
* @param stackTrace whether the stack trace should be registered (this is |
| 79 |
* relatively slow) |
| 80 |
* @return the close watcher |
| 81 |
*/ |
| 82 |
public static CloseWatcher register(Object o, AutoCloseable closeable, boolean stackTrace) { |
| 83 |
CloseWatcher cw = new CloseWatcher(o, queue, closeable); |
| 84 |
if (stackTrace) { |
| 85 |
Exception e = new Exception("Open Stack Trace"); |
| 86 |
StringWriter s = new StringWriter(); |
| 87 |
e.printStackTrace(new PrintWriter(s)); |
| 88 |
cw.openStackTrace = s.toString(); |
| 89 |
} |
| 90 |
refs.add(cw); |
| 91 |
return cw; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Unregister an object, so it is no longer tracked. |
| 96 |
* |
| 97 |
* @param w the reference |
| 98 |
*/ |
| 99 |
public static void unregister(CloseWatcher w) { |
| 100 |
w.closeable = null; |
| 101 |
refs.remove(w); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get the open stack trace or null if none. |
| 106 |
* |
| 107 |
* @return the open stack trace |
| 108 |
*/ |
| 109 |
public String getOpenStackTrace() { |
| 110 |
return openStackTrace; |
| 111 |
} |
| 112 |
|
| 113 |
public AutoCloseable getCloseable() { |
| 114 |
return closeable; |
| 115 |
} |
| 116 |
|
| 117 |
} |