• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

PortableRemoteObject.narrow() explain

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please dont cross post.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic