Isn't this what the property system InitialContext is set up to use is for? What I'm getting at is, is there some reason not to use the built in mechanisms in InitialContext to get the right InitialContext for well, the right context?
we have similar code and the simplest way is to refactor your class so that the InitialContext is injected upon construction. I.e., pass it in the constructor.
The client determines then how to create the context.
a) Either you call your utility class from a Session Bean: use new InitialContext()
b) From some remote client: Have a jndi.properties file or use new InitialContext(props)