01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.helpers;
07:
08: import info.aduna.iteration.CloseableIteration;
09: import info.aduna.iteration.IterationWrapper;
10:
11: /**
12: * An iteration extension that keeps a reference to the SailConnectionBase from
13: * which it originates and signals when it is closed.
14: *
15: * @author jeen
16: */
17: class SailBaseIteration<T, E extends Exception> extends
18: IterationWrapper<T, E> {
19:
20: private SailConnectionBase connection;
21:
22: private Throwable creatorTrace;
23:
24: /**
25: * Creates a new memory-store specific iteration object.
26: *
27: * @param lock
28: * a query lock
29: * @param iter
30: * the wrapped iteration over sail objects.
31: * @param connection
32: * the connection from which this iteration originates.
33: */
34: public SailBaseIteration(
35: CloseableIteration<? extends T, ? extends E> iter,
36: SailConnectionBase connection) {
37: super (iter);
38: this .connection = connection;
39:
40: if (SailBase.debugEnabled()) {
41: creatorTrace = new Throwable();
42: }
43: }
44:
45: @Override
46: public boolean hasNext() throws E {
47: if (super .hasNext()) {
48: return true;
49: } else {
50: // auto-close when exhausted
51: close();
52: return false;
53: }
54: }
55:
56: @Override
57: protected void handleClose() throws E {
58: super .handleClose();
59: connection.iterationClosed(this );
60: }
61:
62: @Override
63: protected void finalize() throws Throwable {
64: if (!isClosed()) {
65: forceClose();
66: }
67:
68: super .finalize();
69: }
70:
71: protected void forceClose() throws E {
72: if (creatorTrace != null) {
73: logger
74: .warn(
75: "Forced closing of unclosed iteration that was created in:",
76: creatorTrace);
77: }
78:
79: close();
80: }
81: }
|