• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Problem about the PortableRemoteObject method

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In test method of EJB and I find the code:"InitialContext ctx=new InitialContext();
Object objRef = ctx.lookup("java:comp/env/ejb/Hello");
HelloHome
home=(HelloHome)javax.rmi.PortableRemoteObject.narrow(
objRef,HelloHome.class);"
The jdk help file says:"The narrow method takes an object reference or abstract interface type and attempts to narrow it to conform to the given interface.". I don't understand that the PortableRemoteObject.narrow method convert the objRef to HelloHome already But Why I must cast it to HelloHome interface explicitly again? I means wheather I can write this:"HelloHome
home=javax.rmi.PortableRemoteObject.narrow(objRef,HelloHome.class);" ?Help
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PortableRemoteObject.narrow() method signatures promises to return a java.lang.Object. According to the Java Language Specification, you must explicitly cast it into the correct class.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The signature of the PortableRemoteObject.narrow() method is:
public static Object narrow(Object narrowFrom, Class narrowTo)
The narrow method returns an Object which contains a HelloHome interface type. To assign it to your HelloHome "home" variable, you have to explicitly cast the returned Object to the HelloHome interface.
The narrow method is there only to allow interoperability with RMI-IIOP, which requires the explicit checking of types in the narrow method. (I think this might not actually be a cast conversion to HelloHome though.) If you look at the method, you can see it takes an Object parameter, and then returns the same Object again. But then you know it is safe to perform the Java cast afterwards.
Some application servers such as Weblogic don't require the narrow method, but they still recommend it for interoperability.
see http://edocs.bea.com/wle/rmi/rmiapi.htm
[ June 20, 2003: Message edited by: Dave Cronin ]
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.But why I must write(objRef,HelloHome.class) other than (objRef,HelloHome).Because I think HelloHome is a interface and it Shouldn't write to a class "HelloHome". Java why to regulate this?
 
Dave Cronin
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second parameter of the narrow() method is a java.lang.Class. java.lang.Class can represent either a class or an interface. Therefore, the method needs the code to obtain a Class from HelloHome. This is the same as using .
It has to do it this way so that the narrow method can work for any class or interface.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic