Question is how to cast the 'ejbObj' to desired Component interface? . Using reqular Java Cast component = (TheComponent)ejbObj; or . Using PortableRemoteObject component = (TheComponent)PortableRemoteObject.narrow(ejbObj, TheComponent.class);
And another question. Do i always have to use PortableRemoteObject to cast TheComponent Interface and when can i use "reqular Java Cast" for it?
According to Spec 9.11.. The client code must use the javax.rmi.PortableRemoteObject.narrow(...)method to convert the result of the getEJBObject() method invoked on a handle to the entity bean�s remote interface type.
Thanks for posting this question and replying to it as I had attempted the same question in one of the simulation exams where I had also marked the same answer given By Giju. But my answer was marked wrong. Just to add my 2 cents to this ... I think I read it somewhere that whenever the method is not returning the exact component interface we need to use the narrow method.
I had this question in one of the Soft SCBCD Exams.
And answer i remember very well was 'Plain Java Cast'. But i rather belive in the Spec than those buggy simulators. :-) After all Handle is Remote. To be portable our Bean, we should narrow the handle.
Master EJB by Monson Haefel describes this in pretty good detail.. Given below is the excerpt from the book..
The narrow() method only needs to be used when a remote reference to an EJB home or EJB object is returned without a specific Remote interface type. This occurs in six circumstances:
1) When a remote EJB home reference is obtained using the javax.naming.Context.lookup() method:
2)When a remote EJB object reference is obtained using the javax.ejb.Handle.getEJBObject() method:
3)When a remote EJB home reference is obtained using the javax.ejb.HomeHandle.getEJBHome() method:
4) When a remote EJB home reference is obtained using the javax.ejb.EJBMetaData.getEJBHome() method:
5) When a remote EJB object reference is obtained from a collection returned by a remote home interface finder method:
6) When a wide remote EJB object type is returned from any business method. Here is a hypothetical example:
The PortableRemoteObject.narrow() method is not required when the remote type is specified in the method signature. This is true of the create() methods and find methods in remote home interfaces that return a single bean.