Is there a way to override system properties in Java?

I am getting a practical problem and the problem can be described as follows.

We are developing a component (say a plugin) to perform some task when an event is fired in an external CMS using the API they provide. They provided some jar libraries. So what we do is implement the interface they provide. Then the internal method is called when the event fires. (The CMS only creates one instance of the class the first time the event is fired, then it just executes the method with each event trigger)

The function can be summarized as follows:

import com.external.ProvidedInterface;


public class MonitorProgram implements ProvidedInterface{

   public void process(){
      //This method is called when an event is triggered in CMS
   }

}

      

In our class we use "javax.net.ssl.HttpsURLConnection" (JAVA 1.5). But HttpsURLConnection has moved to javax.net.ssl ​​from com.sun.net.ssl ​​for 1.4. But it looks like the CMS I'm talking about (we don't know what their implementation actually is) uses something like this

System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");

      

which results in a ClassCastException in our code.

I think my question is clear. In our case, we cannot set the VM parameters,

-Djava.protocol.handler.pkgs=

      

Also we cannot install it using

System.setProperty("")

      

because the VM instance is the same for the CMS and our program.

What can I do to fix this problem? And an idea or experience?

0


a source to share


3 answers


It is not clear to me.

Do you want to overwrite a system property? You can do it.

Overwrite System.property before calling external library method and when method returns you can set old System.property back



    final String propertyName = "Property";
    String oldProperty = System.getProperty(propertyName);
    System.setProperty(propertyName,"NEW_VALUE");
    monitorProgram.process();
    System.setProperty(propertyName,oldProperty);

      

Or do you want to prevent the called process from overwriting system.property? And why can't you manually set the system property?

+2


a source


I don't think you will be very successful with two pieces of code for using different properties.

However, in your own code, you can define your own URLStreamHandlerFactory. This will allow you to create javax.net.ssl.HttpsURLConnection from the url. While protocol handlers are not the easiest to understand, I think you can make them do the job.



See http://java.sun.com/developer/onlineTraining/protocolhandlers/

+1


a source


  • Find the violation class in the stack trace
  • Use jad or similar decompilation tool.
  • Correct property name
  • Compile the resulting file and replace the .class file in the CMS jar or place it in the location that was previously in the classpath.
  • Use ant to automate this process (well, compiling and building JARs, not decompiling)
  • When it works, make sure you save everything (original file, modified file, build file) so you can easily do it again.

While this may sound ridiculous or dangerous to solve the problem, it will work. Especially since your CMS provider doesn't seem to be actively developing their product.

0


a source







All Articles