Background

Java's URL implementation provides the facility to set a URLStreamHandlerFactory :

        URLStreamHandlerFactory myFactory = new MyCustomStreamHandlerFactory();
        URL.setURLStreamHandlerFactory(myFactory);
      

However the setURLStreamHandlerFactory method can only be called once for a Java Virtual Machine . While this may be adequate for standalone applications this can be a problem where multiple applications run within the same JVM, such as in a web application environment.

Overview

JNet uses a technique to circumvent this restriction and allow URL stream handler factories to be registered based on the current execution context , so it's possible to have different handlers for different applications running in the same JVM. It also provides the facility to overcome the limitation of only being able to call the URL.setURLStreamHandlerFactory() once.

Installer

Installer provides the facility to set the URLStreamHandlerFactory, even if already set by another application.

          URLStreamHandlerFactory myFactory = new MyCustomStreamHandlerFactory();
          Installer.setURLStreamHandlerFactory(myFactory);
        

If a URLStreamHandlerFactory has alreay been set AND the new factory is a ParentAwareURLStreamHandlerFactory , then the old factory is set as the parent URLStreamHandlerFactory.

Dynamic URLStreamHandlerFactory

DynamicURLStreamHandlerFactory provides the facility for different applications to use different URLStreamHandlerFactory implementations by storing delegate URLStreamHandlerFactory implementations in a thread local variable.

This allows the URL handler factory to be changed dynamically at runtime through the push(URLStreamHandlerFactory) and pop() methods.

          // At startup, set the DynamicURLStreamHandlerFactory
          URLStreamHandlerFactory dynamicFactory = new DynamicURLStreamHandlerFactory();
          Installer.setURLStreamHandlerFactory(dynamicFactory);
          ....

          // Set a custom URLStreamHandlerFactory for the current context
          URLStreamHandlerFactory myFactory = new MyCustomStreamHandlerFactory();
          DynamicURLStreamHandlerFactory.push(myFactory);
          ...