Package com.identityworksllc.iiq.common
Class Closer
- java.lang.Object
-
- com.identityworksllc.iiq.common.Closer
-
- All Implemented Interfaces:
AutoCloseable
public class Closer extends Object implements AutoCloseable
A container that will auto-close its contents when it is itself closed.Closeable items are stored in a
Stackand will be closed in the reverse order of being added. Errors will be logged and ignored.Items implementing
AutoCloseable(most of them) and items implementingCloseableIteratorare stored separately.Example in Beanshell (with dummy methods):
try { Connection dbConnection = closer.add(getDatabaseConnection()); PreparedStatement stmt = closer.add(dbConnection.prepareStatement("select id from spt_identity where name = ?")); stmt.setString(1, someName); ResultSet results = closer.add(stmt.executeQuery()); while(results.next()) { // do stuff } } finally { // All three objects are closed here, in reverse order closer.close(); }- See Also:
AutoCloseable
-
-
Constructor Summary
Constructors Constructor Description Closer()Creates a new Closer container with an empty set of itemsCloser(AutoCloseable cl1)Creates a new Closer containing the given itemCloser(AutoCloseable cl1, AutoCloseable cl2)Creates a new Closer containing the given itemsCloser(AutoCloseable cl1, AutoCloseable cl2, AutoCloseable cl3)Creates a new Closer containing the given itemsCloser(AutoCloseable cl1, AutoCloseable cl2, AutoCloseable cl3, AutoCloseable cl4)Creates a new Closer containing the given items
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description sailpoint.tools.CloseableIterator<?>add(sailpoint.tools.CloseableIterator<?> ci)Adds theCloseableIteratoritem to the stack to be closed when this container is closed<T extends AutoCloseable>
Tadd(T ac)Adds theAutoCloseableitem to the stack to be closed when this container is closedvoidclose()Pops each item off the stack and calls close() on it.<T extends AutoCloseable>
Optional<T>get(Class<T> requestedType)Extracts the managed object of the given type.
-
-
-
Constructor Detail
-
Closer
public Closer()
Creates a new Closer container with an empty set of items
-
Closer
public Closer(AutoCloseable cl1)
Creates a new Closer containing the given item- Parameters:
cl1- The item to add
-
Closer
public Closer(AutoCloseable cl1, AutoCloseable cl2)
Creates a new Closer containing the given items- Parameters:
cl1- The item to addcl2- The item to add
-
Closer
public Closer(AutoCloseable cl1, AutoCloseable cl2, AutoCloseable cl3)
Creates a new Closer containing the given items- Parameters:
cl1- The item to addcl2- The item to addcl3- The item to add
-
Closer
public Closer(AutoCloseable cl1, AutoCloseable cl2, AutoCloseable cl3, AutoCloseable cl4)
Creates a new Closer containing the given items- Parameters:
cl1- The item to addcl2- The item to addcl3- The item to addcl4- The item to add
-
-
Method Detail
-
add
public <T extends AutoCloseable> T add(T ac)
Adds theAutoCloseableitem to the stack to be closed when this container is closed- Type Parameters:
T- Some object extending AutoCloseable- Parameters:
ac- The auto-closeable item to add to the stack- Returns:
- The passed object, allowing you to open and add in the same line of code
-
add
public sailpoint.tools.CloseableIterator<?> add(sailpoint.tools.CloseableIterator<?> ci)
Adds theCloseableIteratoritem to the stack to be closed when this container is closed- Parameters:
ci- The CloseableIterator item to add to the stack- Returns:
- The passed object, allowing you to open and add in the same line of code
-
close
public void close() throws Exception
Pops each item off the stack and calls close() on it.Any errors will be logged and ignored. The list will be empty after this method is finished, allowing those objects to be garbage-collected.
- Specified by:
closein interfaceAutoCloseable- Throws:
Exception- If anything uncatchable fails
-
get
public <T extends AutoCloseable> Optional<T> get(Class<T> requestedType)
Extracts the managed object of the given type.For example, assuming that you passed a
Connectioninto aCloser:Connection conn = closer.get(Connection.class);- Type Parameters:
T- The output object- Parameters:
requestedType- The type of the requested class- Returns:
- An optional containing the object, if one matches
-
-