Context ic = new InitialContext();
Object o = ic.lookup("EJB");
AdviceHome home = (AdviceHome) PortableRemoteObject.narrow(o, AdviceHome.class);
Above is the code that use Narrow Cast. In my book, they explain that use narrow cast to assume the server is using IIOP (although the server just really use RMI).
And this book still says:
client code won’t be vendor-independent! In other words, you won’t
have a portable app if you don’t use narrow, because redeploying the
bean on a server that does use IIOP will break the clients
I have two points that I don’t understand:
1) IIOP protocol is bigger than plain odd RMI protocol: so, we have casted an small object to big object, right ?
2) I don’t really understand about why cliend code will break so much. Why the server that does use IIOP will break the code at client in this case ? Because the client just calls some methods that have defined in EJBObject Interface. And I cannot imagine how client code will depend on what protocol the server use.
Please help me with clear explanation and simple example.
Thanks 🙂
The
narrowmethod is analogous to a downcast. What the documentation says is don’t use downcast, even if that will always work when doing plain RMI. So the meaning of the wordnarrowis in type-theoretical sense — narrowing down to the specific type from the general typePortableRemoteObject.