This section is the "black magic" that @ahgittin referred to. I'll see if the code can be simplified, and at least add some comments about what it's doing.

But I'd like it if this didn't block merging. @Nakomis 's https://github.com/brooklyncentral/brooklyn/pull/1182 contains these commits (which ideally it shouldn't - we should review+merge here first, and then rebase https://github.com/brooklyncentral/brooklyn/pull/1182). And another branch I'm working on for adding persistence to CLI needs these changes as well.