Reputation: 572
I have an RMI application, with details below. The server knows it is creating a DocumentProvider object, and passes a reference to that object to the client using Document interface. 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.
Is there a way to do this kind of cast, i.e. 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).
Another way of asking: Once the server creates a DocumentProvider object, is that object sitting on the server? And if so, how can I give the client a reference to it? the 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).
//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. (Another easier example: if serverInfo were a file handle, and I allow the client to do "seek" and "read" functions through the Document interface)
Upvotes: 1
Views: 2232
Reputation: 310850
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.
Upvotes: 1