In praise of Apache libraries
Posted by Glenn Proctor on March 9, 2012. Glenn is Head of Software at Eagle.
I've been doing some Java development recently and I've found the various Apache libraries to be invaluable. They provide implementations of a vast range of useful functionality, are very easy to integrate with your own code, and benefit from the open source Apache licence. A list of the libraries I've been using in my current, relatively small project is below. Of course I could have implemented most of this myself, but not having to do so has been a huge time-saver, and has enabled me to spend my time working on the actual interesting part of the code rather than – for example – fussing with the details of a home-grown configuration mechanism.
This is probably the biggest of the tools I've used in this project – it provides a set of core components and a complete HTTP client implementation, ready to use. Even a relatively involved task such as executing an HTTP POST request, with several parameters, through an authenticating HTTP proxy server, and dealing with the results (whether expected or not) is achievable in a few lines of code.
As the name suggests, this library provides a set of methods for dealing with IO. It streamlines the process, in comparison with having to deal with Java's flexible but somewhat unwieldy (and seemingly ever-expanding) set of Streams, Readers, Writers etc.
This provides a huge array of classes for manipulating Java core types. I've been making use of StringUtils, which, in the words of the user guide, provides "oodles and oodles of functions which tweak, transform, squeeze and cuddle java.lang.Strings" – I couldn't have put it better myself! Having done a lot of Perl programming, I miss some of Perl's string manipulation tools, even things as simple as a join method for strings. StringUtil provides this, and much more.
This is the library that prompted me to write this post. My application needs a flexible configuration method, allowing settings to be specified on the command line, in a properties file, or as HTTP parameters in a servlet. I was resigning myself to coding most of this from scratch – hardly rocket science but quite a mundane task – when I came across the Commons Configuration library which allows me to do everything I needed in about five lines of code! A real time-saver. It also provides the opportunity to do more complex things – for example automatic reloading of state if a configuration file changes on disk.
This provides encoders and decoders for things like Base64, URLs etc. I'm using it to generate MD5 hashes, which becomes an almost-trivial one-line task.
Last but not least is Apache Log4j, which is perhaps the most widely-used of the various Java logging libraries. This has been around for years, and was incorporated into Apache relatively recently. For me it strikes just the right balance between simplicity and flexibility.
Of course I've only scratched the surface of the functionality of these libraries, and there are many others I've not mentioned at all. I hope that next time you find yourself needing to implement something in Java, you'll take a moment to see if there's an Apache library that already does it – chances are there probably will be, and you'll save yourself a lot of time!Tweet