我有什麼方法可以覆蓋 Java 中的系統屬性嗎? (Do I have any method to override System Properties in Java?)


問題描述

我有什麼方法可以覆蓋 Java 中的系統屬性嗎? (Do I have any method to override System Properties in Java?)

I am getting a practical issue and the issue can be dascribed as follows.

We are developing a component (Say a plugin) to do some task when an event is triggered within an external CMS using the API provided by them. They have provided some jar libraries, So what we are doing is implementing an Interface provided by them. Then an internal method is called when an event is triggered. (The CMS is creating only one instance of class when the first event triggers, 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
   }

}

Within our class we are using "javax.net.ssl.HttpsURLConnection" (JAVA 1.5). But HttpsURLConnection migrated to javax.net.ssl from com.sun.net.ssl for 1.4. But it seems the CMS I am referring to (We dont know their implementation actually) uses something like this

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

leading to a ClassCastException in our code.

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

-Djava.protocol.handler.pkgs=

Also we cant set it back using,

System.setProperty("")

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

What can I do for get this problem resolved? And idea or experiences?


參考解法

方法 1:

This is not clear for me.

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

Overwrite the System.property before calling the external library method and when the method returns you can set the 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, that the called process overwrites the system.property? And why you can not set the system property by hand?

方法 2:

I don't think you are going to have much success getting two pieces of code to use different properties.  

In your own code however, you can define your own URLStreamHandlerFactory.  Doing this will allow you to create a javax.net.ssl.HttpsURLConnection from a URL.  While protocol handlers aren't the easiest thing to figure out, I think you can get them to do the job.

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

方法 3:

  1. Find the offending class in the stack trace
  2. Use jad or a similar tool to decompile it.
  3. Fix the name of the property
  4. Compile the resulting file and either replace the .class file in the CMS's jar or put it into a place which is earlier in the classpath.
  5. Use ant to automate this process (well, the compile and build of the JAR; not the decompiling)
  6. When it works, make sure you save everything (original file, changed file, build file) somewhere so you can easily do it again.

While this may sound like a ridiculous or dangerous way to fix the issue, it will work. Especially since your CMS provider doesn't seem to develop his product actively.

(by Chathuranga ChandrasekaraMarkus LausbergbrianeggeAaron Digulla)

參考文件

  1. Do I have any method to override System Properties in Java? (CC BY-SA 3.0/4.0)

#runtime #java #jvm #casting






相關問題

通過java運行的執行程序似乎超時? (Executed programs run through java seem to time out?)

Kesalahan NullPointerException dalam kode Java saya. (NullPointerException errors in my Java code.)

Hentikan proses anak ketika proses induk berhenti (Stop child process when parent process stops)

Атрымаць шлях выканання кансольнага скрыпта Python (Get the runpath of a python console-script)

選擇不包含主要類型 - 錯誤 (Selection does not contain main type - error)

在活動之間使用意圖錯誤 (using intents between activities error)

在運行時使用 Spring 在表中創建新字段 (Create new field into a table with Spring at runtime)

如何添加運行時超時以防止 java.net.SocketTimeoutException? (How do I add a runtime timeout to prevent java.net.SocketTimeoutException?)

我有什麼方法可以覆蓋 Java 中的系統屬性嗎? (Do I have any method to override System Properties in Java?)

如何計算此代碼的時間複雜度? (How can I compute the time complexity of this code?)

運行 .exe 文件生成 bt jar2exe 軟件時出現 java runtime environment not found 錯誤 (Getting java runtime enviroment not found error while running .exe file produced bt jar2exe software)

在運行時在 VBA 中顯示所有變量及其值 (Show all variables and their values in VBA during runtime)







留言討論