What is PortableRemoteObject.narrow() method and what is used for? I found somewhere that it is "CORBA compliant". Why?

Alessandro A. Garbagnati

When you execute a lookup to get the home interface of your bean, you normally use the lookup() method of the javax.naming.Context interface.
This method will return you an Object that needs to be casted to the home interface you've asked for. Unfortunately, this cannot be done using the normal/explicit casting [MyHome myHome = (MyHome)returnedObject].

As you have already found out, the reason is connected to CORBA. Why?
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 it is used for the communication is IIOP (I think 1.2), that is part of CORBA standards. It is normally used to describe this communication system using the Java RMI over IIOP.

IIOP has not been designed for Java, but 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 the Object you have received from from your lookup, to the appropriate type. This is done through the javax.rmi.PortableRemoteObject class and, more specifically, using the narrow() method.

Just a note: when you are using the new EJB 2.0 Local Client API, you should be able to do a direct/explicit cast from the looked up Object, to the interface you need.

About | Sitemap | Contact