tags:

views:

298

answers:

2

Our application defines a URL scheme whose resolution is context-sensitive: The content of the URL depends on the state of an in-progress database transaction. As such, to retrieve the content I cannot, for example, open a new database connection. However, I can't see a way to get at external context from the URLStreamHandler instance.

The documentation is a bit misleading, too: I checked at the java Protocol Handler site and it indicated (in the Implementing URLStreamHandlerFactory section) that the URL class would accept a URLStreamHandlerFactory instance, but the 1.5 JDK indicates that URL accepts only a URLStreamHandler when building a URL.

So, my question is, what is the best way to have context-sensitive URL resolution? Is there any better way than to have a ThreadLocal class variable on my URLStreamHandler implementation, that is set before the calls?

+1  A: 

See URL.setURLStreamHandlerFactory

This is a static method and:

Sets an application's URLStreamHandlerFactory. This method can be called at most once in a given Java Virtual Machine.

Ken Gentle
A: 

It turns out that for our purposes, at least, the thread-local context object works best.

Chris R