0

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

dlsa
  • 3,402
  • 2
  • 17
  • 17
  • That's the only interface you've told your Enhancer to implement. Why are you surprised? – user207421 Aug 05 '15 at 22:45
  • Correct. But I tried to include another interface in the array, and the stub didn't implement it. Seems that the interface really must extend java.mi.Remote, and its not sufficient for the remote object to implement it. – dlsa Aug 07 '15 at 16:16
  • @disa That's exactly what it says in the RMI Specification. The stub will implement the same remote interfaces as the remote object. Can't understand why you're surprised. – user207421 Aug 09 '15 at 09:47
  • I misunderstood the spec – dlsa Aug 10 '15 at 10:48

1 Answers1

0

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.

dlsa
  • 3,402
  • 2
  • 17
  • 17