Centralizing Resource Closing for Cleaner Code, Fun and Profit

Posted 2008-07-07 in Java by Johann.

Resources are openend and closed in every software. The opening is obviously different for different resources, but the closing isn’t.

99 % of the time, the closing code looks like this:

finally {
    if (xyz != null) {
        try {
            xyz.close();
        }
        catch (XyzException ex) {
            // Log it, ignore it or flip it and reverse it.
        }
    }
}

That’s a lot of code for such a simple task.

Centralizing Resource Closing

Since most resource classes have a close method, we can simplify this by

  • calling the close method via Reflection,
  • either logging or ignoring any exceptions,
  • using varargs to close multiple objects at once and
  • using static imports to reduce the amount of boilerplate code.

Using these techniques, all of the above code essentially becomes

finally {
    close(xyz);
}

Potential Problems

  1. Not all APIs use close. JSch for example uses disconnect.
  2. More than just close needs to be called in some APIs. For example, the grotesque JMS API forces you to call stop before close.

The Code

Download Resource.java and the test case.

Resource needs log4j in your classpath, the test case additionally needs EasyMock, the EasyMock Class Extension, the Code Generation Library and ASM.

Closing Resources

Import

Assuming you have downloaded the source file and put it in its com.eaio.util package, continue by adding a static import to your classes.

package xyz;

import static com.eaio.util.Resource.close;

Special close methods

Some classes have a special closing method. You can set them up by calling the insteadOfClose method.

Resource.insteadOfClose(MySupahClass.class, "selfDestruct");

The method scanning is recursive and takes interfaces into account so MySupahClass can be an interface or a common superclass.

Before-closing methods

Some classes require other methods to be called before they can be closed.

Resource.beforeClose(Keks.class, "sayGoodbye");

Any implementation of Keks will now have its sayGoodbye method called before close is called.

After-closing methods

Special methods can also be called after a resource has been closed.

Resource.afterClose(Keks.class, "kThxPlzBye");

Varargs

Resource.close will close all resources in the order they are given.

finally {
    close(inStream, outStream, rset, stmt, conn);
}

Any of the arguments can be null or throw an exception.

Logging

Resource (perhaps incorrectly) assumes that nobody is interested in exceptions that happen when resources are closed. To see them, set the logger of com.eaio.util.Resource to DEBUG or TRACE in log4j.properties.

log4j.logger.com.eaio.util.Resource=DEBUG

If the log level is DEBUG, any exceptions will be logged with WARN level in a short format. If the log level is TRACE, the full exception stack trace is logged.

Summary

It’s amazing how much code that does the same is replicated across larger projects. By relying on reflection, a lot of essentialy duplicate code can be removed.

Subscribe

RSS 2.0, Atom or subscribe by Email.

Top Posts

  1. DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
  2. 6 fast jQuery Tips: More basic Snippets
  3. xslt.js version 3.2 released
  4. xslt.js version 3.0 released XML XSLT now with jQuery plugin
  5. Forum Scanners - prevent forum abuse
  6. Automate JavaScript compression with YUI Compressor and /packer/

Navigation