File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes PortableRemoteObject.narrow() explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "PortableRemoteObject.narrow() explain" Watch "PortableRemoteObject.narrow() explain" New topic

PortableRemoteObject.narrow() explain

bala subramanian

Joined: Dec 17, 2001
Posts: 13
What is the diff between the two code?
Properties prop=new Properties();
Context cxt=null;
cxt=new InitialContext(prop);
EmployeeHome home = (EmployeeHome)cxt.lookup("emp");
Employee emp = home.create();

Properties p = new Properties() ;
Context ctx = null ;
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory") ;
p.put(Context.PROVIDER_URL, url) ;
ctx = new InitialContext(p) ;
Object obj = ctx.lookup("utils.Sequence") ;
SequenceHome sHome = (SequenceHome) javax.rmi.PortableRemoteObject.narrow(obj, SequenceHome.class) ;
Sequence sRemote = (Sequence) sHome.create() ;
thanks in adv,
Steve Agarwal
Ranch Hand

Joined: Feb 02, 2003
Posts: 51
I am not a expert in EJB.
but narrow approach is mostly used for RMI IIOP type of remote intanciation. As looks to be the case in the 2nd code.
In the first case the JNDI might be getting the bean for local beans too.

SCJP1.4,SCWCD, SCBCD, SCEA part 1<br />"Its feels good to know the stuff in detail."
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Please dont cross post.

Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
I just noticed this in the second example:

The PortableRemoteObject.narrow tells you this is a Remote client view. So that's not a problem. If you do NOT see a narrow(), on the exam, you can say with absolute 100% certainty that you are seeing a LOCAL client view. Even though it is *possible* depending on your vendor, to NOT use a narrow() with a Remote view, that would not be according to the spec, and on the exam, you are to answer ONLY according to the spec.
But that's not the part that bothers me. What bothers me is that this code is this:
Sequence sRemote = (Sequence) sHome.create() ;
There should NEVER need to be a cast of the return from create(). Remember, create() is declared with the correct return type -- the type of the component interface -- so there should NEVER be a cast. Doesn't mean it will hurt you, but it should not be there, and is never needed. The ONLY things that need to be cast are things that are not returned with the correct declared return type. That means JNDI context lookup() and also getEJBObject() or getEJBHome() on the handle objects.
In addition, you need a narrow as well as a cast if those things-declared-without-the-specific-return-type are Remote stubs.
So, things which must be cast:
* result of JNDI lookup
* result of a call to a handle method
Things which must be narrowed:
* result of JNDI lookup if the client view is Remote (which means the thing coming back is a stub)
* ALL calls to a handle (because handles are ONLY for Remote client views)
* Any thing else that might return a stub, unless the method's return type is the Remote interface type.

Co-Author of Head First Design Patterns
Just a Jini girl living in a J2EE world.
I agree. Here's the link:
subject: PortableRemoteObject.narrow() explain
It's not a secret anymore!