I happened to be looking at an interesting article on the subject of protocol handlers the other day.
You might be interested:
/Tutorials & Code Camps/ /A New Era for Java Protocol Handlers/
http://java.sun.com/developer/onlineTraining/protocolhandlers/
-Terry
Gianugo Rabellino wrote:
Murray Altheim wrote:
Gianugo Rabellino wrote:
Murray Altheim wrote:
I thought perhaps if I needed an answer to this question, others might as well. I also figure that someone on this list has the answer to this one at their fingertips...
For those programming in Java, the java.net.URL class is ubiquitous. But creating a URL with 'xmldb' such as
xmldb:xindice-embed:///db/documentation/overview
will result in
[java] java.net.MalformedURLException: unknown protocol: xmldb [java] at java.net.URL.<init>(URL.java:586) [java] at java.net.URL.<init>(URL.java:476) [...]
which never feels all that much like a warm puppy or a cold beer.
So the question is: how does one register a new protocol in Java so that you can create xmldb URLs and obtain a valid String when doing getProtocol()?
URLStreamHandlerFactory is your friend and foe. It's your friend since it allows you to add a custom protocol to Java, it's your foe since only exactly ONE additional protocol per JVM is allowed. I think that the double colon issue can be somehow solves, but this limitation is quite hard to digest...
Gianugo,
In reading over the javadocs for this, I didn't get that impression at all. The docs for URL do state that
setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
allows only one call per JVM, but that's for setting the factory, not the protocol. The factory itself seems to be able to receive any number of registrations using
createURLStreamHandler(String protocol)
Oh yes, indeed. But the point is that it isn't safe at all since you might encounter an environment where someone has already set such factory (that's the case for a few app servers around) *or* you might screw up apps willing to do that after your call. Sorry, I wasn't clear enough on that point, but the whole URL thing in Java is plain crap.
so I *think* this isn't an issue. What I've been googling around for is how to create the URLStreamHandler for a custom protocol, since the one for 'xmldb' doesn't exist (and may be the reason the xmldb URLs throw an exception upon creation).
I did it once and found it painful. Not for the code itself but for the underlying architecture around which sucks big time. I would definitely consider Upavayira offer of the Excalibur datasource component instead, since it will also buy you for free a (writeable) URL access to XIndice.
Ciao,
