4

I was tasked to write a custom ProxySelector implementation for our current OSGi project. In order to use authentication for proxies I also had to write a custom Authenticator.

Setting these custom implementations when the OSGi bundle starts works great. But OSGi bundles can be stopped/uninstalled which brings me to my question:

How do I unset my implementations and restore the default (JVM) implementations?

While the bundle starts I save the default implementation:

ProxySelector defaultSelector = ProxySelector.getDefault()
ProxySelector.setDefault(new MyProxySelector());

Sadly Authenticator does not have an getDefault() method.

When the bundle is stopping I want to unset ProxySelector and Authenticator like so:

ProxySelector.setDefault(defaultSelector); // see above
Authenticator.setDefault(null);

But this doesn't work. After the bundle stops I can't make any connections.

My only explanation is that setting the defaultSelector is only passing a reference. When my ProxySelector is destroyed these reference points nowhere thus causing the JVM to have no valid ProxySelector. The best solution I came up was to clone the selector. But ProxySelector does not have a clone() method. Casting the defaultSelector to some default implementation (sun.net.spi.DefaultProxySelector) might not be the best solution but could work. It could be a problem because it could override other custom implementations that have been set before.

So what is your best practice for this?

Jens
  • 20,533
  • 11
  • 60
  • 86

0 Answers0