I have an application that detects some classes as being RMI exportable, and exports them. The exportable classes should implement an interface, that will be present on the RMI client to do the cast and make the stub usable. I then execute the following code (using CGLIB) to export a class :
public void bind(Object objectInstance) {
try {
Enhancer classEnhancer = new Enhancer();
classEnhancer.setSuperclass(objectInstance.getClass());
classEnhancer.setInterfaces(new Class[]{Remote.class});
classEnhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
return methodProxy.invokeSuper(obj, args);
}
});
Object proxy = classEnhancer.create();
Remote stub =
(java.rmi.Remote)UnicastRemoteObject.exportObject((Remote)proxy, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind("Object", stub);
System.out.println("object instance bound");
} catch (RemoteException e) {
System.out.print("error : " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
}
When I run the app and connect with an RMI browser I can see that the only interface implemented by the stub is java.rmi.Remote. The interface implemented by the original object is not implemented by the remote object. Is this really like this, or am I missing something ?
I am using JDK 1.8.0_31 on a Mac book pro running Yosemite
I was creating a class that implemented the java.mi.Remote interface, hoping that the stub would also implement it. Doesn't work that way. The remote object must implement at least an interface that extends remote.