class DefaultCloseableService extends java.lang.Object implements CloseableService
CloseableService
.
This implementation stores instances of Closeable
in an internal identity hash set and makes sure
that the close method is invoked at most once.Modifier and Type | Field and Description |
---|---|
private java.util.Set<java.io.Closeable> |
closeables |
private java.util.concurrent.atomic.AtomicBoolean |
closed |
private static java.util.logging.Logger |
LOGGER |
Constructor and Description |
---|
DefaultCloseableService() |
Modifier and Type | Method and Description |
---|---|
boolean |
add(java.io.Closeable closeable)
Register a new instance of
Closeable that is to be closed when the request goes out of scope. |
void |
close()
Invokes
Closeable#close() method on all instances of Closeable added by the #add(Closeable)
method. |
private static final java.util.logging.Logger LOGGER
private final java.util.concurrent.atomic.AtomicBoolean closed
private final java.util.Set<java.io.Closeable> closeables
public boolean add(java.io.Closeable closeable)
CloseableService
Closeable
that is to be closed when the request goes out of scope.
After CloseableService.close()
has been called, this method will not accept any new instance registrations and
will return false
instead.
add
in interface CloseableService
closeable
- the instance of Closeable
.true
if the closeable service has not been closed yet and the closeable instance was successfully
registered with the service, false
otherwise.public void close()
CloseableService
Closeable#close()
method on all instances of Closeable
added by the #add(Closeable)
method.
Subsequent calls of this method should not do anything.close
in interface CloseableService