• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

handle.getEJBObject cast ?

 
Dimiter Stoilov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I've got:
ObjectInputStream is = ... ;
Handle handle = (Handle)is.readObject();
Object ejbObj = handle.getEJBObject();
TheComponent component = ... ???

Question is how to cast the 'ejbObj' to desired Component interface?
[1]. Using reqular Java Cast
component = (TheComponent)ejbObj;
or
[2]. 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?


Thanx for your time guys,
Dimiter
(SCJP, SCWCD)
 
Giju George
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dimiter Stoilov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Giju,
plain clear.
 
Nitin Mehhta
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Handle means it must a remote bean. The regardless it is in the same heap or a different heap, narrow method must be used to cast a handle back to a remote component.
 
Naslai Shobha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is the link which describes the behaviour

http://www.coderanch.com/t/159630/java-EJB-SCBCD/certification/Shall-we-cast-EJBObject-getEJBHome
 
Dimiter Stoilov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys,

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.
 
Krishnan Y N
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Hope this helps clear the doubt

cheerz
krish
 
Dimiter Stoilov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic