Class 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 Stack and will be closed in the reverse order of being added. Errors will be logged and ignored.

    Items implementing AutoCloseable (most of them) and items implementing CloseableIterator are 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 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 add
        cl2 - The item to add
    • Method Detail

      • add

        public <T extends AutoCloseable> T add​(T ac)
        Adds the AutoCloseable item 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 the CloseableIterator item 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:
        close in interface AutoCloseable
        Throws:
        Exception - If anything uncatchable fails
      • get

        public <T extends AutoCloseableOptional<T> get​(Class<T> requestedType)
        Extracts the managed object of the given type.

        For example, assuming that you passed a Connection into a Closer:

        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