T - a resource type that implements the
Closeable interface.public class ResourceManager<T extends Closeable> extends Object
close() method ensures that all managed resources
will have their close() method called. Any exceptions thrown by
resources are caught, wrapped in a ResourceManagerCheckedException or
a ResourceManagerRuntimeException and re-thrown only after all
resources have been closed.
When throwing exceptions runtime exceptions take precedence and a
ResourceManagerRuntimeException will be thrown even if some resources threw
checked exceptions. If any resources did throw checked exceptions they will
be available with
ResourceManagerRuntimeException.getCheckedExceptions().
There are three flavors of the close method:
close() : will re-throw runtime and checked exceptions.
closeQuietly() : will discard any checked exceptions and re-throw
runtime exceptions.
closeAbruptly() : ignores all exceptions.
close*() methods catch any exceptions thrown and
only propagate the exceptions after all resources have had their
close() methods called.
EXAMPLE
ResourceManager manager = new ResourceManager();
try
{
InputStream istream = manager.add(new FileInputStream(...));
OutputStream ostream = manager.add(new FileOutputStream(...));
Closeable dbConnection = manager.add(openDatabaseConnection());
...
}
finally
{
manager.closeQuietly();
}
| Modifier and Type | Field and Description |
|---|---|
protected LinkedList<T> |
resources |
| Constructor and Description |
|---|
ResourceManager() |
| Modifier and Type | Method and Description |
|---|---|
T |
add(T resource)
Add a resource to the resource manager.
|
void |
close() |
void |
closeAbruptly()
All resources are closed and all exceptions are ignored.
|
void |
closeQuietly()
Close all resources.
|
protected LinkedList<T extends Closeable> resources
public void close()
throws ResourceManagerCheckedException
ResourceManagerCheckedExceptionpublic void closeQuietly()
public void closeAbruptly()
Copyright © 2016 The American National Corpus. All rights reserved.