Class Cleanup<EX extends Exception>
- java.lang.Object
-
- de.aristaflow.adept2.util.Cleanup<EX>
-
- Type Parameters:
EX
- The checked exception type for transparent pass-through orRuntimeException
. This is the one that will be forwarded when explicitly called. We cannot ensure that all clean-up task provide the very same exception type or a subclass. So all checked exceptions thrown by clean-up tasks incompatible with this type will be wrapped in aUndeclaredThrowableException
.
public class Cleanup<EX extends Exception> extends Object
Cleanup
aims to complement the functionality ofCleaner
and provide some answers to questions thatCleaner
left open. As withCleaner
the main purpose is to release resources when the objects holding those resources aren't reachable anymore. This is only meant as a fail-safe mechanism; resources should always be properly released once they are no longer needed.Cleanup
allows to register multiple clean-up tasks and they can be added either at the start or the end of the queue. This enables subclasses to share the sameCleanup
and decide whether their tasks need to be executed before or after those of the superclass(es). Every clean-up tasks is guaranteed to be executed even in case of exceptions. The first encountered exception is passed through, with all others being attached as suppressed.- Transparent pass-through of checked exceptions:
Cleaner.Cleanable.clean()
cannot pass through checked exceptions, like the typicalIOException
. They would need to be manually wrapped and maybe unwrapped again.Cleanup
can handle this transparently for one checked exception type specified during creation. This allows to use the clean-up for an explicitly called clean-up, e. g.AutoCloseable.close()
with normal exception handling. - Exceptions occurring post-mortem (i. e. during implicit clean-up) are logged as they cannot be passed through to a caller.
CleanupTask
must fulfil certain requirements aimed at preventing accidental references to the monitored object. The implementation- must be
final
, - must NOT be a lambda,
- must NOT be an anonymous class,
- and must NOT have any non-static inner classes in its type hierarchy.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Cleanup.Builder
A builder forCleanup
that provides some additional options and generally easier usage than complex constructors.static class
Cleanup.CollectionCloseTask
A cleanup task for cleaning up a collection of objects possibly beingAutoCloseable
.
-
Field Summary
Fields Modifier and Type Field Description protected static Cleaner
DEFAULT_CLEANER
The defaultCleaner
used when no other is specified via theCleanup.Builder
.protected static Logger
DEFAULT_LOGGER
The defaultLogger
used when no other is specified via theCleanup.Builder
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Cleanup<EX>
addTask(CleanupTask<? extends Exception> task)
Adds the given clean-up task to the end of the list of registered clean-up tasks.Cleanup<EX>
addTask(AutoCloseable... autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks.Cleanup<EX>
addTask(String descriptor, AutoCloseable... autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks.Cleanup<EX>
addTask(String descriptor, Collection<? extends AutoCloseable> autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks.Cleanup<EX>
addTask(String descriptor, Supplier<? extends AutoCloseable> autoCloseable)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks.Cleanup<EX>
addTask(Collection<? extends AutoCloseable> autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks.Cleanup<EX>
addTask(Supplier<? extends AutoCloseable> autoCloseable)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
to the end of the list of registered clean-up tasks.Cleanup<EX>
addTaskAsFirst(CleanupTask<? extends Exception> task)
Adds the given clean-up task to the first position of the list of registered clean-up tasks.Cleanup<EX>
addTaskAsFirst(AutoCloseable... autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks.Cleanup<EX>
addTaskAsFirst(String descriptor, AutoCloseable... autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks.Cleanup<EX>
addTaskAsFirst(String descriptor, Collection<? extends AutoCloseable> autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks.Cleanup<EX>
addTaskAsFirst(String descriptor, Supplier<AutoCloseable> autoCloseable)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks.Cleanup<EX>
addTaskAsFirst(Collection<? extends AutoCloseable> autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks.Cleanup<EX>
addTaskAsFirst(Supplier<AutoCloseable> autoCloseable)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
to the first position of the list of registered clean-up tasks.static Cleanup.Builder
builder()
Starts building aCleanup
.void
clean()
Triggers the clean-up and invokes all registered clean-up tasks.static Cleanup<RuntimeException>
register(Object object)
Creates aCleanup
as post-mortem action for the designated object.static <EX extends Exception>
Cleanup<EX>register(Object object, Class<EX> checkedExceptionType)
Creates aCleanup
as post-mortem action for the designated object.static Cleanup<RuntimeException>
register(Object object, String descriptor)
Creates aCleanup
as post-mortem action for the designated object with the designated descriptor.static <EX extends Exception>
Cleanup<EX>register(Object object, String descriptor, Class<EX> checkedExceptionType)
Creates aCleanup
as post-mortem action for the designated object with the designated descriptor.protected void
runTasks()
Executes all registered clean-up tasks.protected static void
runTasks(Collection<CleanupTask<? extends Exception>> tasks, boolean explicitlyCleaned, String objectDescriptor, Logger logger)
Executes the designated tasks by callingCleanupTask.cleanup(boolean)
and collecting the corresponding exceptions.
-
-
-
Field Detail
-
DEFAULT_CLEANER
protected static final Cleaner DEFAULT_CLEANER
The defaultCleaner
used when no other is specified via theCleanup.Builder
.
-
DEFAULT_LOGGER
protected static final Logger DEFAULT_LOGGER
The defaultLogger
used when no other is specified via theCleanup.Builder
.
-
-
Method Detail
-
register
public static Cleanup<RuntimeException> register(Object object)
Creates aCleanup
as post-mortem action for the designated object. This only registers the notification mechanism, actual clean-up tasks need to be registered yet. No exception will be expected by any clean-up task.- Parameters:
object
- The object for which to register post-mortem clean-up.- Returns:
- A new
Cleanup
registered for as post-mortem action for the designated object. - See Also:
builder()
-
register
public static Cleanup<RuntimeException> register(Object object, String descriptor)
Creates aCleanup
as post-mortem action for the designated object with the designated descriptor. This only registers the notification mechanism, actual clean-up tasks need to be registered yet. No exception will be expected by any clean-up task.- Parameters:
object
- The object for which to register post-mortem clean-up.descriptor
- A string that meaningfully describes this clean-up when used for logging etc. ornull
to use the class name of the designated object.- Returns:
- A new
Cleanup
registered for as post-mortem action for the designated object. - See Also:
builder()
-
register
public static <EX extends Exception> Cleanup<EX> register(Object object, Class<EX> checkedExceptionType)
Creates aCleanup
as post-mortem action for the designated object. This only registers the notification mechanism, actual clean-up tasks need to be registered yet. There will be transparent pass-through for the provided checked exception type inclean()
.- Parameters:
object
- The object for which to register post-mortem clean-up.checkedExceptionType
- The exception that is expected by the clean-up. This will be forwarded when manually triggering clean-up.- Returns:
- A new
Cleanup
registered for as post-mortem action for the designated object. - See Also:
builder()
-
register
public static <EX extends Exception> Cleanup<EX> register(Object object, String descriptor, Class<EX> checkedExceptionType)
Creates aCleanup
as post-mortem action for the designated object with the designated descriptor. This only registers the notification mechanism, actual clean-up tasks need to be registered yet. There will be transparent pass-through for the provided checked exception type inclean()
.- Parameters:
object
- The object for which to register post-mortem clean-up.descriptor
- A string that meaningfully describes this clean-up when used for logging etc. ornull
to use the class name of the designated object.checkedExceptionType
- The exception that is expected by the clean-up. This will be forwarded when manually triggering clean-up.- Returns:
- A new
Cleanup
registered for as post-mortem action for the designated object. - See Also:
builder()
-
builder
public static Cleanup.Builder builder()
Starts building aCleanup
.- Returns:
- The
Builder
for building aCleanup
.
-
addTask
public Cleanup<EX> addTask(CleanupTask<? extends Exception> task)
Adds the given clean-up task to the end of the list of registered clean-up tasks.If an exception thrown by the
task
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
task
- The clean-up task to be executed as post-mortem or when explicitly triggered. For proper/normal exception handling, let its exception beEX
or a subclass thereof.- Returns:
- This
Cleanup
.
-
addTaskAsFirst
public Cleanup<EX> addTaskAsFirst(CleanupTask<? extends Exception> task)
Adds the given clean-up task to the first position of the list of registered clean-up tasks.If an exception thrown by the
task
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
task
- The clean-up task to be executed as post-mortem or when explicitly triggered. For proper/normal exception handling, let its exception beEX
or a subclass thereof.- Returns:
- This
Cleanup
.
-
addTask
public Cleanup<EX> addTask(Supplier<? extends AutoCloseable> autoCloseable)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
to the end of the list of registered clean-up tasks. Uses the class name(s) of theSupplier
to generate a descriptor for the task.If an exception thrown by the
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
autoCloseable
- TheSupplier
to get theAutoCloseable
to close when cleaning-up. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTask
public Cleanup<EX> addTask(AutoCloseable... autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks. Uses the class name(s) of theAutoCloseable
(s) to generate a descriptor for the task(s).If an exception thrown by an
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
autoCloseables
- TheAutoCloseable
s to be added as clean-up task. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTask
public Cleanup<EX> addTask(Collection<? extends AutoCloseable> autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks. Uses the class name(s) of theAutoCloseable
(s) to generate a descriptor for the task(s).If an exception thrown by an
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
autoCloseables
- TheAutoCloseable
s to be added as clean-up task. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTask
public Cleanup<EX> addTask(String descriptor, Supplier<? extends AutoCloseable> autoCloseable)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks.If an exception thrown by the
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
descriptor
- A string that meaningfully describes the clean-up task ornull
for the class name(s) of theAutoCloseable
(s).autoCloseable
- TheSupplier
to get theAutoCloseable
to close when cleaning-up. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTask
public Cleanup<EX> addTask(String descriptor, AutoCloseable... autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks.If an exception thrown by an
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
descriptor
- A string that meaningfully describes the clean-up task ornull
for the class name(s) of theAutoCloseable
(s).autoCloseables
- TheAutoCloseable
s to be added as clean-up task. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTask
public Cleanup<EX> addTask(String descriptor, Collection<? extends AutoCloseable> autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the end of the list of registered clean-up tasks.If an exception thrown by an
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
descriptor
- A string that meaningfully describes the clean-up task ornull
for the class name(s) of theAutoCloseable
(s).autoCloseables
- TheAutoCloseable
s to be added as clean-up task. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTaskAsFirst
public Cleanup<EX> addTaskAsFirst(Supplier<AutoCloseable> autoCloseable)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
to the first position of the list of registered clean-up tasks. Uses the class name(s) of theSupplier
to generate a descriptor for the task.If an exception thrown by the
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
autoCloseable
- TheSupplier
to get theAutoCloseable
to close when cleaning-up. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTaskAsFirst
public Cleanup<EX> addTaskAsFirst(AutoCloseable... autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks. Uses the class name(s) of theSupplier
(s) to generate a descriptor for the task(s).If an exception thrown by the
AutoCloseable
is not aRuntimeException
and is not compatible with thisCleanup's
pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
autoCloseables
- TheAutoCloseable
s to be added as clean-up task. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTaskAsFirst
public Cleanup<EX> addTaskAsFirst(Collection<? extends AutoCloseable> autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks. Uses the class name(s) of theAutoCloseable
(s) to generate a descriptor for the task(s).If an exception thrown by the
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
autoCloseables
- TheAutoCloseable
s to be added as clean-up task. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTaskAsFirst
public Cleanup<EX> addTaskAsFirst(String descriptor, Supplier<AutoCloseable> autoCloseable)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks.If an exception thrown by the
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
descriptor
- A string that meaningfully describes the clean-up task ornull
for the class name(s) of theAutoCloseable
(s).autoCloseable
- TheSupplier
to get theAutoCloseable
to close when cleaning-up. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTaskAsFirst
public Cleanup<EX> addTaskAsFirst(String descriptor, AutoCloseable... autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks.If an exception thrown by an
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
descriptor
- A string that meaningfully describes the clean-up task ornull
for the class name(s) of theAutoCloseable
(s).autoCloseables
- TheAutoCloseable
s to be added as clean-up task. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
addTaskAsFirst
public Cleanup<EX> addTaskAsFirst(String descriptor, Collection<? extends AutoCloseable> autoCloseables)
Adds anCleanup.AutoCloseableCleanupTask
for the designatedAutoCloseable
s to the first position of the list of registered clean-up tasks.If an exception thrown by an
AutoCloseable
is not aRuntimeException
and is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException
.- Parameters:
descriptor
- A string that meaningfully describes the clean-up task ornull
for the class name(s) of theAutoCloseable
(s).autoCloseables
- TheAutoCloseable
s to be added as clean-up task. For proper/normal exception handling, make sureEX
isIOException
(orException
).- Returns:
- This
Cleanup
.
-
runTasks
protected void runTasks()
Executes all registered clean-up tasks. SeeCleanup
for details on the behaviour.
-
runTasks
protected static void runTasks(Collection<CleanupTask<? extends Exception>> tasks, boolean explicitlyCleaned, String objectDescriptor, Logger logger)
Executes the designated tasks by callingCleanupTask.cleanup(boolean)
and collecting the corresponding exceptions. SeeCleanup
for details on the behaviour.
If one or more exceptions occur, the most critical one will be selected having all others as suppressed ones. In an explicit clean it will be thrown, otherwise it will be logged together with the corresponding description.- Parameters:
tasks
- The tasks to be run.explicitlyCleaned
- Whether the tasks are run due to an explicit call toclean()
. In this case it is not called as post-mortem action of the monitored object.objectDescriptor
- A string that meaningfully describes the monitored object for logging purpose.logger
- The logger for messages and/or problems, e. g. unexpected exceptions.
-
clean
public void clean() throws EX extends Exception
Triggers the clean-up and invokes all registered clean-up tasks. These tasks are invoked at most once regardless of the number of calls toclean()
. You may call it for clean-up normally, that is, before post-mortem which will deregister it from post-mortem cleaning.This method is the replacement of
Cleaner.Cleanable.clean()
when using the vanillaCleaner
.
-
-