Sunday, December 11, 2011

The Grails ConfigurationHolder is smarter than you might think.


The following seemed like a perfectly reasonable approach for dealing with getting configuration properties using a ConfigurationHolder. I wanted to get the configured accessKey without having to worry about pesky null pointer exceptions.

String accessKey = ConfigurationHolder.config?.aws?.accessKey

I was not positive if the deployment config groovy file was getting updated and wanted to make sure that the app didn’t blow up during bootstrapping itself. I assumed that dotting into closures that did not exist in the config would fail. Wrong, wrong, wrong. It conveniently returns empty closures. My craftiness ended up resolving to a string containing ‘{}’.

I am thinking that the moral of the story is that “Your problems are not beautiful an unique snowflakes”…  You should always assume (at least with Grails) that someone has dealt with the same issue your are facing and you just need to look at the problem differently… don’t get crafty!

Update...

Not so fast… the convenient default maps for non-existent closures in the configuration holder are present in integration tests but NOT in plain old unit tests.