Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PortableRemoteObject.narrow() - why?  RSS feed

 
Ravi Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When obtaining a remote stub for a session/entity bean why must you use the PortableRemoteObject.narrow() method before the remote stub can be cast?
 
Nanda Kishore
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we execute a lookup to get the home interface of a bean, we normally use the lookup() method of the javax.naming.Context interface. This method will return an Object that needs to be casted to the home interface that we asked for. Unfortunately, this cannot be done using the normal casting [MyHome myHome = (MyHome)returnedObject]. The reason is related to CORBA.

For EJB, the communication between the server and the client is based on RMI (both remote and local interfaces, in fact, do implements the java.rmi.Remote interface). The underlying protocol that is used for the communication is IIOP, that is part of CORBA standards. So basically the communication protocol in EJB is Java RMI over IIOP.

IIOP has not been designed only for Java. It is for generic languages, and this means that there are some limitations. Some languages, in fact, do not have the concept of casting. Java RMI-IIOP provides a mechanism to narrow the Object received from our lookup, to the appropriate type. This is done through the javax.rmi.PortableRemoteObject class and, more specifically, using the narrow() method.

But when using EJB 2.0 Local Client API, we can do a direct/explicit cast from the looked up Object, to the interface type that we need.
 
Ravi Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the narrow() method actually do to the remote stub originally returned?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just a typecast in the normal condition where the object you're looking up is some kind of Java object. It is needed because, like Nanda mentioned, EJB must use RMI over IIOP. IIOP is used in CORBA, which is not Java specific, so you can't assume the reference that you get is always a Java object. If you got some other kind of reference, the narrow() method would give you a reference that you could use.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!