Set ProxySelector and Authenticator back by default

I was tasked with writing a custom ProxySelector for our current OSGi project. To use authentication for proxies, I also had to write a custom Authenticator .

Setting up these custom implementations when running the OSGi package works great. But OSGi packages can be stopped or removed, which brings me to my question:

How to disable my implementations and restore the default implementation (JVM)?

Until the start starts, I keep the default implementation:

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

Unfortunately, the authenticator does not have a getDefault() method.

When the bundle stops, I want to disable ProxySelector and Authenticator as follows:

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

But that does not work. After stopping the package, I can’t connect.

My only explanation is that setting defaultSelector only passes the link. When my ProxySelector is destroyed, these anchor points nowhere cause the JVM to not have a valid ProxySelector. The best solution I came up with is to clone the selector. But ProxySelector does not have a clone() method. defaultSelector in some default implementation (sun.net.spi.DefaultProxySelector) may not be the best solution, but it may work. This can be a problem as it can override other custom implementations that were installed earlier.

So what is your best practice for this?

+4
source share

All Articles