Add a thread context setup and JNDI lookup to the batch environment.

I'm submitting this as a PR so it can be reviewed before it's merged.