我有任何方法来覆盖Java中的系统属性? [英] Do I have any method to override System Properties in Java?

查看:176
本文介绍了我有任何方法来覆盖Java中的系统属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个实际问题,问题可以描述如下。



我们正在开发一个组件(说一个插件)是在外部CMS内使用他们提供的API触发的。他们提供了一些jar库,所以我们正在做的是实现他们提供的接口。然后当触发事件时调用内部方法。 (当第一个事件触发时,CMS只创建一个类的实例,然后它只是对每个事件触发器执行方法)



该函数可以总结如下,

  import com.external.ProvidedInterface; 


public class MonitorProgram implements ProvidedInterface {

public void process(){
//当CMS中触发事件时调用此方法
}

}



在我们的类中,我们使用javax .net.ssl.HttpsURLConnection(JAVA 1.5)。但HttpsURLConnection从com.sun.net.ssl迁移到javax.net.ssl 1.4。但是看来我指的是CMS(我们不知道它们的实现)使用像这样

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

导致我们的代码中出现ClassCastException。



我想我的问题很清楚。在我们的例子中,我们无法设置VM参数,

  -Djava.protocol.handler.pkgs = 



此外,我们也不能使用

  System.setProperty()

,因为VM实例与CMS和我们的程序相同。



我可以如何解决这个问题?

解决方案

这对我并不清楚。



您要覆盖系统属性吗?
可以这样做。



在调用外部库方法之前覆盖System.property,当方法返回时,可以将旧的System.property设置为

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

或者你想防止被调用的进程覆盖system.property?
为什么你不能手动设置系统属性?


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?

解决方案

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?

这篇关于我有任何方法来覆盖Java中的系统属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆