views:

228

answers:

3

Hi,

I have a question on using System Properties in Java. Some classes like Authenticator require that we set the system properties regarding Proxy settings and than verify whether the Proxy was valid or not.

My question is should I remove the Set Properties after I am done using it ?

There are other parts of programs that might be using these Properties, this change will autmatically impact thier functionality.

Is there a way, I can set Properties local to a Function (some wrapper class)?

What are the good practises for setting system properties and using them ?

+2  A: 

Things that use System.properties should have properties that have a global meaning to the running JVM, so that if, for example, you set a proxy, it should be the relevant proxy across that process.

So therefore there is no need to set them back. In fact, setting them back might make some APIs confused, as they may assume they get back the relevant value at all times, and didn't just cache it when they read it.

Of course if a given API isn't using them that way, then you might have issues, but that would really be an issue with a given API, more than a good practice issue with System properties.

In general, due to threading and synchronization issues, it is probably a good practice to set System properties only at the beginning of the JVM startup (either on the command line or in the main thread before starting other threads) with the expectation that the values remain unchanged for the remainder of the time running the JVM.

Yishai
I agree. My problem is that I want to add a Test Proxy button and someone might type a wrong one there :-( Also there is another Thread that uses this Proxy setting. So if I set the Property to a wrong value for testing, the thread which was originally using a different value will fail.
Geek
Sounds like an issue with the API. Can you elaborate on which system properties you are setting? There may be a workaround.
Yishai
A: 

If there is a chance that some other part of your program (or some other webapp in the container, etc) might be affected by "temporary" settings then it is a good idea to remove them.

Best practice would be to try an find some other way to do what you are trying to do. For example, consider creating your own protocol class that overrides a standard one in the area where it figures out what proxy to use.

If you cannot do that, try to structure your code so that the sequence:

  1. change the properties,
  2. do the operation,
  3. restore the properties,

is done in a mutex that respected by anything that might be affected by the properties you are changing. This may be a hard ask though ...

Stephen C
A: 

This doesn't answer your question about system properties in general, but regarding your specific problem with proxy settings properties, perhaps you can use a ProxySelector to isolate the Test Proxy you mention in the comments here?

You could create a subclass of ProxySelector that you utilize for the test. Make it such that it only applies the test settings when the test URI is attempted. This would isolate it from other requests.

This sort of global proxy setting inflexibility is what initially drove me to use HttpClient for HTTP needs instead of Sun's API.

Edit:

I'm not sure how I ever missed this method, but it is possible to get a URL connection and supply the proxy settings to that connection alone via java.net.Url.openConnection(Proxy) .

laz