why EJB's narrowed using PortableRemoteObject

Posted on 2004-11-17
Medium Priority
Last Modified: 2013-11-24
why EJB's narrowed in the following mannor:

InitialContext ctx = new InitialContext();
Object obj = ctx.lookup(SomeObject);
SpecificObject so = (SpecificObject) PortableRemoteObject.narrow(obj, SpecificObject.class);
Question by:chaitu chaitu
LVL 35

Expert Comment

ID: 12612220
Because EJB must also support CORBA reference objects that cannot be cast by Java native casting.
LVL 20

Author Comment

by:chaitu chaitu
ID: 12612238
i didnt getu;

can u explain in detail
LVL 35

Expert Comment

ID: 12612257
EJB obejcts can be transported by using RMI over IIOP protocol (RMI/IIOP). The RMI is used to transport Java object where Interent Inter-ORB Protocol is used to transport CORBA objects. IIOP is a generic protocol and does not have the concept of casting as it is in Java. Therefoer you will need to cast the references you receive over IIOP somehow. That's why you use the PortableRemoteObject.narror() method. For more information you might want to take a look here: http://www.jguru.com/faq/view.jsp?EID=734137
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 12612355

we can either do a context.lookup or a PortableRemoteObject.narrow.
In which situations would u use one over the other.  Which is the best solution to use?

Let's start to clarify understanding EJB.

We can access EJB in two ways. One is by local interface and another is by remote interface. The different between local and remote interface is listed below

The benefit of remote interface is that it allow you to invoke a method on EJB over the network by using RMI(Remote Method Invocation). Sometime the invoked EJB is deployed on the same machine with the invoking EJB. So it does not make sense to call it over RMI which will have overhead in calling. To optimize this, Local interface allow us to access to an EJB by bypassing RMI. But we should keep in mind that local interface only work when EJB is located in the same application so it disallows load-balancing and failover feather.

Ok. back to your question.
We use PortableRemoteObject.narrow() when we write a client program(java application or another ejb) to access local objects(local interface is a subinterface of EJBLocalObject)

Here is an example for local interface.

HelloWorldLocalHome home =
HelloWorldLocal hello = home.create();

And Here is an example for remote interface.

HelloWorldHome home =
  (HelloWorldHome)PortableRemoteObject.narrow(ctx.lookup("HelloWorld"), HelloWorldHome.class);
HelloWorld hello = home.create();

you will have clear idea...

LVL 35

Expert Comment

ID: 12612362
> We use PortableRemoteObject.narrow() when we write a client program(java
> application or another ejb) to access local objects(local interface is
> a subinterface of EJBLocalObject)

You mean *remote* objects I guess.

Accepted Solution

drjustin earned 200 total points
ID: 12626718
After looking up our remote object, we do a very strange operation: javax.rmi.PortableRemote Object.narrow(). This is a static method on an RMI-IIOP class called PortableRemoteObject. This method casts the generic object that we looked up via JNDI to our RMI-IIOP interface type. This narrow() operation is required whenever you lookup an RMI-IIOP object via JNDI. Why do we need it, and why don’t we just cast it using a regular Java cast? The short answer is CORBA interoperability.

When you receive an object using Java RMI, you can simply cast it into the desired object using a Java cast. This is possible because RMI
automatically downloads the appropriate stub for the object you’re dealing with. CORBA, however, does not have a mechanism for  automatic stub downloading.

When using RMI-IIOP, you cannot simply cast an object you receive over the network, because the stub class file may not exist on the
client machine. RMI-IIOP does not allow downloadable class files because CORBA does not mandate support for this feature. Thus, you must perform a safe cast by explicitly calling the javax.rmi.PortableRemoteObject.narrow() method. This static method converts an object into the desired remote interface type for you. You pass narrow() the object you desire to cast, and the class to which you wish to cast it. The narrow() method returns the resultant object or throws an exception if the narrow is not possible, perhaps because the class does not exist.

In the end, a compromise was reached. J2EE server vendors are allowed to use other protocols besides IIOP. However, they need to support IIOP in case interoperability is required with CORBA systems. That is the irony of the phrase RMI-IIOP —it does not necessarily mean usage of the IIOP protocol. Rather, it means standardizing on the PortableRemoteObject.narrow() method.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question