Only one instance of the URLStreamHandlerFactory
implementation is registered in the Java URLStreamHandlerFactory
, so this implementation must be aware of all supported protocols.
By default, the Oracle / Sun behavior is not implemented this way, but directly in the java.net.URL
class. Thus, you cannot just enter the default implementation as a factory chain in RsrcURLStreamHandlerFactory
. The first part of the answer.
The java.net.URL
method getURLStreamHandler
loads the implementation for protocol X
according to the policy of naming its default class name as sun.net.www.protocol.X.Handler
If you look at jre/lib/rt.jar
, you will find:
sun/net/www/protocol/ftp/Handler.class sun/net/www/protocol/gopher/Handler.class sun/net/www/protocol/mailto/Handler.class sun/net/www/protocol/netdoc/Handler.class sun/net/www/protocol/http/Handler.class sun/net/www/protocol/jar/Handler.class sun/net/www/protocol/file/Handler.class
The list of base packages used to select the URLStreamHandler
protocol comes from the Java system property java.protocol.handler.pkgs
. I suggest you read the full java/net/URL.java
source code from the JDK src.zip
for details.
So, the right way to do this (regardless of what IBM / Eclipse did) is to leave the default mechanism in place and set for example -Djava.protocol.handler.pkgs="com.company.product.protocol"
on the command line ( if you have permission / accreditation for this). When implementing the URLStreamHandler
with the name com.company.product.protocol.rsrc.Handler
that uses the JarRsrcLoader
, you are doing the job.
An alternative is to write an implementation of URLStreamHandlerFactory
as a factory chain in RsrcURLStreamHandlerFactory
, inspired by the source code URL.getURLStreamHandler
. As an example, you can read this old JBoss code . It relies on the cache of internal URL
caches by preloading other known (or used) protocols before registering the factory. In my opinion, just ugly.
Warning : RsrcURLStreamHandler
replaced the original 180 lines of URLStreamHandler.parseURL
code URLStreamHandler.parseURL
its own 10-line version without calling super.parseURL
. Of course, this does not meet the URL concatenation specifications! Be careful, you may encounter an error depending on how such URLs are used.
Yves martin
source share