Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

narrowing & casting with Handle.getEJBObject

 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page# 139 on the Head First EJB says the following:

Remember, you always have to cast and narrow a stub unless the method that returns it has the actual Remote interface as its declared return type.


The declared return type of the getEJBObject method of the Handle interface is itself "EJBObject". Then why do we need to narrow and cast while calling the getEJBObject method over the handle object?

Can someone please help me understand this?


Thanks,
Ankit
 
Micheal Jacob
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ankit,

When getEJBObject() is called on deserialized handle, the return type is EJBObject.

EJBObject is common interface extended by all bean provider's component remote interface.

Until you don't narrow it (because of RMI-IIOP) to particular component remote interface, you cannot call bean provider's business methods.

Hope it helps.

Micheal.
 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Michael.

I have still some more confusion. I tried serializing the handle and then deserializing and using it - without narrowing, just casting. Following the short code snippet for AdviceClient and AdviceClient2

AdviceClient


AdviceClient2


Note above that I have just used plain old casting, (no narrowing).

Now, I initially ran the AdviceClient. Then I undeployed the app and shutdown the j2ee server. Then after some time, I started the j2ee server back up, deployed the app, and after that I ran the AdviceClient2, and it worked.

Please help me understand the above.


Thanks,
Ankit
 
Micheal Jacob
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Which j2ee server you are using ?

EJB Spec (Page No 65) says, you should narrow it after deserializing the handle.

Your j2ee server may be helping you (in terms of reducing one line of code !!! by allowing you to use java casting), but your client code cannot be used as it is if you change your EJB Container.

Micheal.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must always assume that the server is using RMI-IIOP. So, it is necessary to narrow and cast. To do otherwise will cost you marks in the exam.
 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I was using the J2EE RI.

To clarify more on at what all times we should narrow and cast -- Is it that if the "declared" return type of a method is anything other than the actual remote component interface type (Advice in case of the Advice application from HFEJB) then you always have to narrow and cast - is this right?


Thanks,
Ankit
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I poated this a while ago.
http://www.coderanch.com/t/161190/java-EJB-SCBCD/certification/narrowing-results
 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That clarifies, Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic