The basic problem here is that you need to export the proxy object itself, not the invocation handler. Otherwise the proxy object gets serialized to the Registry, instead of its stub, with the consequences we see.
So you need to make the following adjustments:
SecureRemoteInvocationHandler
doesn't need to extendUnicastRemoteObject
either directly or indirectly.- You need to add
Remote proxyStub = UnicastRemoteObject.exportObject(proxy, 0, csf, ssf);
beforer.bind()
inServerRMI,
wherecsf
andssf
are the socket factories. (I renamed them in my code.)
There are other improvements you can make:
public class SecurityInvocationHandler<T extends Remote>
for better type-safety, and similarly:
public static <T extends Remote> T newInstance(...)
You need to make the variable containing the result of LocateRegistry.createRegistry()
static so it doesn't get garbage-collected.
You need to get adjust all remote object contructors to call super()
with a port number, so you get dynamic stubs.
You won't get much further than this until you sort out what is required for the SSL handshake to complete. You need to define javax.net.ssl.keyStore/keyStorePassword
in the server, and javax.net.ssl.trustStore
in the client if you aren't using the default one (i.e. if the server has a self-signed certificate).
The reason it doesn't work your way is that your exported SecurityInvocationHandler
replaces itself with its stub during serialization, and that stub isn't an InvocationHandler,
because InvocationHandler
isn't a remote interface, so when the object gets deserialized it can't be reassembled, as there is no InvocationHandler
to store in the dynamic proxy, just this stub, which the dynamic proxy doesn't know from Adam.