You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by Murray Altheim <m....@open.ac.uk> on 2004/03/03 02:02:28 UTC

Registering the 'xmldb:' URL protocol in Java

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()?

Secondly, is the protocol "xmldb" or "xmldb:xindice-embed"? If not
the latter, what component of the URL is "xindice-embed" in the
URL scheme of things? I don't see this in the URL RFC.

Thanks!

Murray

......................................................................
Murray Altheim                    http://kmi.open.ac.uk/people/murray/
Knowledge Media Institute
The Open University, Milton Keynes, Bucks, MK7 6AA, UK               .

   "Ours is a dangerous time with two relatively new threats,
    both of them exacerbated by the Iraq invasion and this
    administration's policies. One is the threat of future
    terrorism by Osama and al Qaeda. The other is the threat
    to our freedoms and our constitutional republic. These
    are dangers that were never faced before in my lifetime."
                                              -- Daniel Ellsberg
    http://www.sfgate.com/cgi-bin/article.cgi?file=/chronicle/archive/2004/02/29/CMG3R50LHE5.DTL


Re: Registering the 'xmldb:' URL protocol in Java

Posted by Murray Altheim <m....@open.ac.uk>.
Upayavira wrote:
> Gianugo Rabellino wrote:
>>Murray Altheim wrote:
[...]
>>>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...
> 
> You could, instead, look into the Avalon Excalibur source resolver, 
> which is used by Cocoon. It gives you an abstraction that allows you to 
> do that. Also, with the XMLDBSource in the Cocoon XMLDB block, you've 
> effectively got a way of reading/writing to/from a XMLDB 'thing'.

Upayavira,

I'll take a look at that. I also found this:

   Java 1.2 Unleashed: Content and Protocol Handlers
   http://utenti.lycos.it/yanorel6/2/ch33.htm

which seems to provide a solution too.

Thanks,

Murray

......................................................................
Murray Altheim                    http://kmi.open.ac.uk/people/murray/
Knowledge Media Institute
The Open University, Milton Keynes, Bucks, MK7 6AA, UK               .

   "Ours is a dangerous time with two relatively new threats,
    both of them exacerbated by the Iraq invasion and this
    administration's policies. One is the threat of future
    terrorism by Osama and al Qaeda. The other is the threat
    to our freedoms and our constitutional republic. These
    are dangers that were never faced before in my lifetime."
                                              -- Daniel Ellsberg
    http://www.sfgate.com/cgi-bin/article.cgi?file=/chronicle/archive/2004/02/29/CMG3R50LHE5.DTL


Re: Registering the 'xmldb:' URL protocol in Java

Posted by Upayavira <uv...@upaya.co.uk>.
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...

You could, instead, look into the Avalon Excalibur source resolver, 
which is used by Cocoon. It gives you an abstraction that allows you to 
do that. Also, with the XMLDBSource in the Cocoon XMLDB block, you've 
effectively got a way of reading/writing to/from a XMLDB 'thing'.

Regards, Upayavira



Re: Registering the 'xmldb:' URL protocol in Java

Posted by Terry Rosenbaum <Te...@amicas.com>.
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,
>

Re: Registering the 'xmldb:' URL protocol in Java

Posted by Gianugo Rabellino <gi...@apache.org>.
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,

-- 
Gianugo Rabellino
Pro-netics s.r.l. -  http://www.pro-netics.com
Orixo, the XML business alliance - http://www.orixo.com
     (Blogging at: http://www.rabellino.it/blog/)

Re: Registering the 'xmldb:' URL protocol in Java

Posted by Murray Altheim <m....@open.ac.uk>.
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)

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).

What it sounds like is that one would extend the URLStreamHandler
(which is an abstract class) in some way to handle the new 'xmldb'
protocol, but this is where I fall flat on my face. I thought perhaps
somebody on this list had done this already...

Murray

......................................................................
Murray Altheim                    http://kmi.open.ac.uk/people/murray/
Knowledge Media Institute
The Open University, Milton Keynes, Bucks, MK7 6AA, UK               .

   "Ours is a dangerous time with two relatively new threats,
    both of them exacerbated by the Iraq invasion and this
    administration's policies. One is the threat of future
    terrorism by Osama and al Qaeda. The other is the threat
    to our freedoms and our constitutional republic. These
    are dangers that were never faced before in my lifetime."
                                              -- Daniel Ellsberg
    http://www.sfgate.com/cgi-bin/article.cgi?file=/chronicle/archive/2004/02/29/CMG3R50LHE5.DTL


Re: Registering the 'xmldb:' URL protocol in Java

Posted by Gianugo Rabellino <gi...@apache.org>.
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...

Ciao,


-- 
Gianugo Rabellino
Pro-netics s.r.l. -  http://www.pro-netics.com
Orixo, the XML business alliance - http://www.orixo.com
     (Blogging at: http://www.rabellino.it/blog/)

Re: Registering the 'xmldb:' URL protocol in Java

Posted by Murray Altheim <m....@open.ac.uk>.
Murray Altheim wrote:
> [I note a mistake in that I'm trying to register the xmldb URL scheme,
> not protocol. Low on caffeine this evening...]

Another mistake. I really do need some coffee, or maybe just go to bed...

In URLs, the initial token preceding the ':' is called the protocol
and is used to determine the protocol for file transfer. In URIs
that token is called the scheme, and is used to indicate the schema
of the rest of the URI's characters, i.e., how they are to be parsed
and interpreted.

One thing I might mention is that beyond having *functional* URLs
for xmldb, it would allow me to use a ton of existing code without
trying to rewrite it to use both URLs and URIs.

Murray

......................................................................
Murray Altheim                    http://kmi.open.ac.uk/people/murray/
Knowledge Media Institute
The Open University, Milton Keynes, Bucks, MK7 6AA, UK               .

   "Ours is a dangerous time with two relatively new threats,
    both of them exacerbated by the Iraq invasion and this
    administration's policies. One is the threat of future
    terrorism by Osama and al Qaeda. The other is the threat
    to our freedoms and our constitutional republic. These
    are dangers that were never faced before in my lifetime."
                                              -- Daniel Ellsberg
    http://www.sfgate.com/cgi-bin/article.cgi?file=/chronicle/archive/2004/02/29/CMG3R50LHE5.DTL


Re: Registering the 'xmldb:' URL protocol in Java

Posted by Murray Altheim <m....@open.ac.uk>.
[I note a mistake in that I'm trying to register the xmldb URL scheme,
not protocol. Low on caffeine this evening...]

Mario Cormier wrote:
> Try java.net.URI instead of java.net.URL.  Basically you can think of a 
> URI as a URL of an unknown type.
> 
> Hope this helps!

Mario,

The difference between the two is more than just that URIs don't come
with a set of defined schemes, they also don't perform any of the
functions that URLs (in Java) do, such as provide a resolution service.
The Javadocs for each hilight the difference:

   "[A] URI instance is little more than a structured string that
    supports the syntactic, scheme-independent operations of comparison,
    normalization, resolution, and relativization."

whereas:

   "[A] URL is a structured string that supports the syntactic operation
    of resolution as well as the network I/O operations of looking up
    the host and opening a connection to the specified resource."

If there's a way to register the xmldb: scheme in Java, then the
creation of an xmldb URL would enable the openConnection() method
providing access to the Xindice resource. Also, the openStream()
method would provide an InputStream for reading the Xindice-based
resource. This would fully "web-enable" Xindice in the Java sense.

So I am asking for the moon.  :-)

> 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()?
>>
>>Secondly, is the protocol "xmldb" or "xmldb:xindice-embed"? If not
>>the latter, what component of the URL is "xindice-embed" in the
>>URL scheme of things? I don't see this in the URL RFC.
>>
>>Thanks!

Murray

......................................................................
Murray Altheim                    http://kmi.open.ac.uk/people/murray/
Knowledge Media Institute
The Open University, Milton Keynes, Bucks, MK7 6AA, UK               .

   "Ours is a dangerous time with two relatively new threats,
    both of them exacerbated by the Iraq invasion and this
    administration's policies. One is the threat of future
    terrorism by Osama and al Qaeda. The other is the threat
    to our freedoms and our constitutional republic. These
    are dangers that were never faced before in my lifetime."
                                              -- Daniel Ellsberg
    http://www.sfgate.com/cgi-bin/article.cgi?file=/chronicle/archive/2004/02/29/CMG3R50LHE5.DTL


Re: Registering the 'xmldb:' URL protocol in Java

Posted by Mario Cormier <mc...@miranda.com>.
Try java.net.URI instead of java.net.URL.  Basically you can think of a 
URI as a URL of an unknown type.

Hope this helps!

Mario

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()?
>
> Secondly, is the protocol "xmldb" or "xmldb:xindice-embed"? If not
> the latter, what component of the URL is "xindice-embed" in the
> URL scheme of things? I don't see this in the URL RFC.
>
> Thanks!
>
> Murray
>
> ......................................................................
> Murray Altheim                    http://kmi.open.ac.uk/people/murray/
> Knowledge Media Institute
> The Open University, Milton Keynes, Bucks, MK7 6AA, UK               .
>
>   "Ours is a dangerous time with two relatively new threats,
>    both of them exacerbated by the Iraq invasion and this
>    administration's policies. One is the threat of future
>    terrorism by Osama and al Qaeda. The other is the threat
>    to our freedoms and our constitutional republic. These
>    are dangers that were never faced before in my lifetime."
>                                              -- Daniel Ellsberg
>    
> http://www.sfgate.com/cgi-bin/article.cgi?file=/chronicle/archive/2004/02/29/CMG3R50LHE5.DTL
>
>