why EJB's narrowed using PortableRemoteObject

Posted on 2004-11-17
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

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

    Author Comment

    by:chaitu chaitu
    i didnt getu;

    can u explain in detail
    LVL 35

    Expert Comment

    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:
    LVL 3

    Expert Comment


    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

    > 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.
    LVL 3

    Accepted Solution

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now