简体   繁体   English

Java RMI远程对象强制转换/状态

[英]Java RMI Remote Object Cast / State

I have an RMI application, with details below. 我有一个RMI应用程序,下面有详细信息。 The server knows it is creating a DocumentProvider object, and passes a reference to that object to the client using Document interface. 服务器知道它正在创建DocumentProvider对象,并使用Document接口将对该对象的引用传递给客户端。 When the client passes the Document object to the server, the server can no longer cast this into DocumentProvider, though that's what the server originally created. 当客户端将Document对象传递给服务器时,服务器不能再将其强制转换为DocumentProvider,尽管这是服务器最初创建的。

Is there a way to do this kind of cast, ie the server knows it's creating a particular implementation of Document with more internal features (in this case, the InternalClass serverInfo), and wants to access those additional features in its implementation of the Document functions (in this case in the "doCast()" function). 有没有一种方法可以执行这种类型的转换,即服务器知道它正在创建具有更多内部功能的Document的特定实现(在这种情况下,就是InternalClass serverInfo),并且希望在其Document函数的实现中访问这些附加功能。 (在这种情况下,在“ doCast()”函数中)。

Another way of asking: Once the server creates a DocumentProvider object, is that object sitting on the server? 询问的另一种方式:服务器创建DocumentProvider对象后,该对象是否位于服务器上? And if so, how can I give the client a reference to it? 如果是这样,我如何为客户提供参考呢? the InternalClass serverInfo; InternalClass serverInfo; part is NOT serializable, and cannot be transferred to the client. 该部分不可序列化,并且无法传输到客户端。 (Somewhat like a file handle, etc., though I can keep it and manage it within the DocumentProvider class). (有点像文件句柄等,尽管我可以保留它并在DocumentProvider类中对其进行管理)。

//Interface
public interface Document extends extends java.rmi.Remote {
        public String doSomething()  throws java.rmi.RemoteException;
        public String doCast()  throws java.rmi.RemoteException;
}

//The server:
    public class DocumentProvider extends java.rmi.server.UnicastRemoteObject implements java.rmi.Remote, Document {
        InternalClass serverInfo;

        public String doSomething()  throws java.rmi.RemoteException {
            return "DocumentProvider doing something";
        }
        public String doCast(Document d)  throws java.rmi.RemoteException {
            DocumentProvider d2 = (DocumentProvider) d; // this cast fails, even when d was a DocumentProvider class
            // then access d.serverInfo, etc.
        }
    }

public interface DocumentFactory extends extends java.rmi.Remote {
        public Document createDocument() throws java.rmi.RemoteException;
}
public class DocumentProvider extends java.rmi.server.UnicastRemoteObject implements java.rmi.Remote, DocumentFactory {
    public Document createDocument() throws java.rmi.RemoteException {
        return new DocumentProvider();
    }
}

//Client app:
main()... {
    DocumentFactory dFactory = (lookup/resolve this; this works ok);
    Document dMain = dFactory.createDocument();
    dMain.doCast(dMain);  // this fails because
}

To elaborate, "InternalClass serverInfo" is a non-serializable 3rd party "handle", I must somehow keep its state in the server, so the main question is how to do that, while returning a reference to a remote object, allowing the client to "manipulate" that internal object through remote calls which are defined in the interface. 详细地说,“ InternalClass serverInfo”是不可序列化的第三方“句柄”,我必须以某种方式将其状态保留在服务器中,因此主要问题是如何做到这一点,同时返回对远程对象的引用,从而允许客户端通过接口中定义的远程调用“操纵”该内部对象。 (Another easier example: if serverInfo were a file handle, and I allow the client to do "seek" and "read" functions through the Document interface) (另一个更简单的示例:如果serverInfo是文件句柄,并且我允许客户端通过Document接口执行“搜索”和“读取”功能)

Remote objects are passed and returned as stubs which implement the same remote interface. 远程对象作为实现相同远程接口的存根传递并返回。 Casting to the actual remote object class therefore cannot possibly work, and trying to do it remotely makes even less sense. 因此,强制转换为实际的远程对象类可能无法工作,而尝试远程进行操作则毫无意义。 Just use the stub as an instance of the interface. 只需将存根用作接口的实例即可。 If that isn't sufficient there is something wrong with your design. 如果这还不够,则说明您的设计存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM