java - 使用 rmi + 插件时需要做的特殊事情

标签 java plugins rmi

我有一个申请FooApplication (代码 foo.jar )带有插件 BarPlugin (代码 foo-bar-plugin.jar )。应用程序动态实例化插件的实例。效果很好。

FooApplication 有一些可通过 RMI 接口(interface)访问的功能 FooRemote 。除了一件事之外,这也很有效。 FooRemote有一个方法可以访问Remote由插件导出的对象,当我尝试将这些插件之一分发给 RMI 客户端时,我收到 java.rmi.UnmarshalException。

public interface FooRemote extends Remote
{
    /* other methods */
    public RemoteControl getPluginRemoteControl(int i) throws RemoteException;
}
/** just a named Remote object for debugging purposes */
public interface RemoteControl extends Remote
{
    public String getName() throws RemoteException;
}

我在 FooRemoteImpl 中所做的事情如下:

/* just a test object */
private static class RC0 extends UnicastRemoteObject implements RemoteControl 
{
    public RC0() throws RemoteException { super(); }
    @Override public String getName() throws RemoteException { return "RC0"; }
}

@Override public RemoteControl getPluginRemoteControl(int i) 
       throws RemoteException 
{
    int j = i;
    if (j <= 0)
        return new RC0();

    Collection<RemoteControl> rclist = this.model.getApplicationPluginRemotes();
    for (RemoteControl rc : rclist)
    {
        if (--j == 0)
            return rc;
    }
    return null;
}

当我打电话FooRemote.getPluginRemoteControl(0)时,它分发了我的虚拟类的一个实例 RC0 ,并且在客户端工作正常。当我调用FooRemote.getPluginRemoteControl(1)时,它尝试分发一个真正的插件 Remote ,但失败了:

??? Java exception occurred:
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
    java.lang.ClassNotFoundException: com.example.plugin.BarPluginRemoteControl (no security manager: RMI class loader disabled)

    at sun.rmi.server.UnicastRef.invoke(Unknown Source)

    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)

    at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)

    at $Proxy5.getPluginRemoteControl(Unknown Source)

Caused by: java.lang.ClassNotFoundException: com.example.plugin.BarPluginRemoteControl (no security manager: RMI class loader disabled)

[ more stuff deleted ] 

什么给出了?

最佳答案

您可能需要设置一个SecurityManager。您的客户端找不到 com.example.plugin.BarPluginRemoteControl 类,因为没有适当的安全管理器为其提供访问权限。

尝试以下操作:

在客户端代码中添加:

System.setSecurityManager(new java.rmi.RMISecurityManager());

创建一个名为 client.policy 的策略文件,其中包含:

grant{
    permission java.security.AllPermission;
};

将策略文件传递到客户端的启动命令中:

java -Djava.security.policy=client.policy  ... <main-class>...

关于java - 使用 rmi + 插件时需要做的特殊事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3823122/

相关文章:

javascript - 打印 javascript 阻止我的链接

java - 通过 SSL 的 RMI : Handshake failure

java - Ajax直接进入错误部分

java - 在 Java 递归函数中追踪数组的问题

java - 如何编辑扩展名为 .class 的文件?

java - Java RMI 回调表异常中没有此类对象

java - rmi java.lang.ClassNotFoundException : testrmi2. fileserverimpl_Stub

java - 相同正则表达式的模式/匹配器与 String.split()

java - Spring中引用传递的问题

android - 在 Android Studio 中查找重复代码 (Kotlin)