我有什么方法可以覆盖 Java 中的系统属性吗?

发布于 2024-07-20 16:58:12 字数 999 浏览 3 评论 0原文

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

我们正在开发一个组件(例如插件),用于在外部 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

a√萤火虫的光℡ 2024-07-27 16:58:12

这对我来说并不清楚。

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

在调用外部库方法之前覆盖 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?
为什么不能手动设置系统属性?

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?

花心好男孩 2024-07-27 16:58:12

我认为您不会成功地让两段代码使用不同的属性。

但是,在您自己的代码中,您可以定义自己的 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/

撞了怀 2024-07-27 16:58:12
  1. 在堆栈跟踪中找到有问题的类
  2. 使用 jad 或类似工具对其进行反编译。
  3. 修复属性的名称
  4. 编译生成的文件并替换 CMS jar 中的 .class 文件或将其放入类路径中较早的位置。
  5. 使用 ant 自动执行此过程(是 JAR 的编译和构建;而不是反编译
  6. )有效,请确保将所有内容(原始文件、更改的文件、构建文件)保存在某处,以便您可以轻松地再次执行此操作。

虽然这听起来像是一种荒谬或危险的解决问题的方法,但它会起作用。 特别是因为您的 CMS 提供商似乎并没有积极开发其产品。

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文