In the very last line I'm writing to the deployment metadata that is already in the deployment unit.
Yeah, but deployment metadata != deployment unit. ;-)
"adds some metadata to the deployment unit."
... anway, never mind, just being picky. :-)
So I need a callback in the OSGiBundleState that calls me back at the right time so I can initialize the state based on what's in the OSGiMetaData. I couldn't find the right callback for that. Maybe we need to somehow get the OSGiStartLevelMetaDataDeployer to run a bit earlier as well to achieve this, I'm not sure...
I see bundle getting installed in OSGiBundleStateInstallDeployer.
I guess your deployer just needs to be the next in line?