Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

narrowing & casting with Handle.getEJBObject

 
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I poated this a while ago.
https://coderanch.com/t/161190/java-EJB-SCBCD/certification/narrowing-results
 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That clarifies, Thanks.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic