I am not clear what this portableRemoteObject.narrow() thing is and when do we use it, and when we don't and is it always reqd. or we give it just for the purpose of standardisation?
If anybody can drop a hint it will be great.
PortableRemoteObject.narrow() is used whenever you have a reference to a remote object that you need to cast to a more specific class or interface, such as an EJB Home returned from a JNDI lookup:
Object obj = initialContext.lookup(...); // lookup EJB Home JNDI name
MyEJBHome home = (MyEJBHome)PortableRemoteObject.narrow(obj, MyEJBHome.class);
You don't need to use it when working with an EJB through a local interface, though:
MyLocalEJBHome localHome = (MyLocalEJBHome)initialContext.lookup(...); // no narrow() needed to cast
Hope this helps,
It's for compatibility with CORBA standard.
The EJB spec requires the RMI-IIOP to conform to the CORBA standard, CORBA standard supports multiple languages some of which does not support direct casting so in order to be compliant whenever you have to cast a remote reference you need to use the PortableRemoteObject.narrow method to do that. one of the cases where you use it is HomeReference Lookup from JNDI context as the return type of context.lookup is an Object so in order to cast it to the correct Home class you need to use the PortableRemoteObject.narrow, however if you call create on this home reference you dont need to use narrow as the create methods return type is the RemoteInterface so no casting is needed. Also when you lookup the local interfaces you are in the same JVM and can use the casting capabilities of java language.