I'm trying to register a custom URLStreamHandler to handle requests to Amazon S3 URLs in a generic fashion. The implementation of the handler looks pretty much like S3-URLStreamHandler (github). I did not put my Handler class into the sun.net.www.protocol.s3 package, but used the custom package com.github.dpr.protocol.s3. To make Java pickup this package, I provided the system property -Djava.protocol.handler.pkgs="com.github.dpr.protocol" following the documentation of the URL class. However, if I try to process a s3-URL like s3://my.bucket/some-awesome-file.txt, I get a MalformedURLException:
Caused by: java.net.MalformedURLException: unknown protocol: s3
at java.net.URL.<init>(URL.java:600)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at java.net.URI.toURL(URI.java:1089)
...
My application is a Spring based web application, that currently runs in tomcat, but should not be cluttered with any knowledge about the underlying application container.
I already debugged the respective code and found that my URLStreamHandler can't be initialized as the classloader used to load the class doesn't know it. This is the respective code from java.net.URL (jdk 1.8.0_92):
1174: try {
1175: cls = Class.forName(clsName);
1176: } catch (ClassNotFoundException e) {
1177: ClassLoader cl = ClassLoader.getSystemClassLoader();
1178: if (cl != null) {
1179: cls = cl.loadClass(clsName);
1180: }
1181: }
The class loader of the java.net.URL class (used by Class.forName) is null indicating the bootstrap class loader and the system class loader doesn't know my class. If I put a break point in there and try to load my handler class using the current thread's class loader, it works fine. That is my class is apparently there and is in the application's class path, but Java uses the "wrong" class loaders to lookup the handler.
I'm aware of this question on SOF, but I must not register a custom URLStreamHandlerFactory as tomcat registers it's own factory (org.apache.naming.resources.DirContextURLStreamHandlerFactory) on application start and there must only be one factory registered for a single JVM. Tomcat's DirContextURLStreamHandlerFactory allows to add user factories, that could be used to handle custom protocols, but I don't want to add a dependency to Tomcat in my application code, as the application should run in different containers.
Is there any way to register a handler for custom URLs in a container independent fashion?
UPDATE 2017-01-25:
I gave the different options @Nicolas Filotto proposed a try:
Option 1 - Set custom URLStreamHandlerFactory using reflection
This option works as expected. But by using reflection it introduces a tight dependency to the inner workings of the java.net.URL class. Luckily Oracle is not too eager with fixing convenience issues in the basic java classes - actually this related bug report is open for almost 14 years (great work Sun/Oracle) - and this can easily be unit tested.
Option 2 - Put handler JAR into {JAVA_HOME}/jre/lib/ext
This option works as well. But only adding the handler jar as system extension won't do the trick - of course. One will need to add all dependencies of the handler as well. As these classes are visible to all applications using this Java installation, this may lead to undesired effects due to different versions of the same library in the classpath.
Option 3 - Put handler JAR into {CATALINA_HOME}/lib
This doesn't work. According to Tomcat's classloader documentation resources put into this directory will be loaded using Tomcat's Common classloader. This classloader will not be used by java.net.URL to lookup the protocol handler.
Given these options I'll stay with the reflection variant. All of the options are not very nice, but at least the first one can easily be tested and does not require any deployment logic. However I adapted the code slightly to use the same lock object for synchronization as java.net.URL does:
public static void registerFactory() throws Exception {
final Field factoryField = URL.class.getDeclaredField("factory");
factoryField.setAccessible(true);
final Field lockField = URL.class.getDeclaredField("streamHandlerLock");
lockField.setAccessible(true);
// use same lock as in java.net.URL.setURLStreamHandlerFactory
synchronized (lockField.get(null)) {
final URLStreamHandlerFactory urlStreamHandlerFactory = (URLStreamHandlerFactory) factoryField.get(null);
// Reset the value to prevent Error due to a factory already defined
factoryField.set(null, null);
URL.setURLStreamHandlerFactory(new AmazonS3UrlStreamHandlerFactory(urlStreamHandlerFactory));
}
}