File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

PortableRemoteObject.narrow() explain

bala subramanian
Greenhorn

Joined: Dec 17, 2001
Posts: 13
What is the diff between the two code?
Properties prop=new Properties();
Context cxt=null;
prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URL,"t3://localhost:80");
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,
Bala
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.
Steve


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: 8919

Please dont cross post.


Groovy
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,
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.
========================
cheers,
Kathy


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PortableRemoteObject.narrow() explain