我有什么方法可以覆盖 Java 中的系统属性吗?
我遇到一个实际问题,该问题可以描述如下。
我们正在开发一个组件(例如插件),用于在外部 CMS 内使用外部 CMS 提供的 API 触发事件时执行某些任务。 他们提供了一些jar库,所以我们要做的就是实现他们提供的接口。 然后当事件被触发时调用内部方法。 (当第一个事件触发时,CMS 仅创建一个类实例,然后它只执行每个事件触发的方法)
该功能可以总结如下,
import com.external.ProvidedInterface;
public class MonitorProgram implements ProvidedInterface{
public void process(){
//This method is called when an event is triggered in CMS
}
}
在我们的类中,我们使用“javax.net.ssl.HttpsURLConnection” (Java 1.5)。 但是 HttpsURLConnection 在 1.4 版本中从 com.sun.net.ssl 迁移到了 javax.net.ssl。 但我提到的 CMS(我们实际上并不知道他们的实现)似乎使用了类似的东西,
System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
导致我们的代码中出现 ClassCastException。
我想我的问题很清楚。 在我们的例子中,我们无法设置VM参数,
-Djava.protocol.handler.pkgs=
也无法使用它来设置它,
System.setProperty("")
因为CMS和我们的程序的VM实例是相同的。
我该怎么做才能解决这个问题? 还有想法或经验?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我来说并不清楚。
您想覆盖系统属性吗?
你可以这样做。
在调用外部库方法之前覆盖 System.property,当该方法返回时,您可以设置旧的 System.property
或者您想防止被调用的进程覆盖 system.property?
为什么不能手动设置系统属性?
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
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?
我认为您不会成功地让两段代码使用不同的属性。
但是,在您自己的代码中,您可以定义自己的 URLStreamHandlerFactory。 这样做将允许您从 URL 创建 javax.net.ssl.HttpsURLConnection。 虽然协议处理程序并不是最容易弄清楚的事情,但我认为您可以让它们完成这项工作。
请参阅 http://java.sun.com/developer/onlineTraining/protocolhandlers/
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/
虽然这听起来像是一种荒谬或危险的解决问题的方法,但它会起作用。 特别是因为您的 CMS 提供商似乎并没有积极开发其产品。
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.