Little help please,
I understand that when using remote references (stubs) to EJB's they must be narrowed in the following mannor:
InitialContext ctx = new InitialContext();
Object obj = ctx.lookup(SomeObject);
SpecificObject so = (SpecificObject) PortableRemoteObject.narrow(obj, SpecificObject.class);
However, do you also have to use PortableRemoteObject.narrow() when obtaining a datasource, or some other kind of object?
Thanks in advance!
-
PortableRemoteObject.narrow(); (8 messages)
- Posted by: Mr Singh
- Posted on: October 04 2004 04:56 EDT
Threaded Messages (8)
- PortableRemoteObject.narrow(); by Guy Pardon on October 04 2004 09:58 EDT
- PortableRemoteObject.narrow(); by Mr Singh on October 04 2004 10:18 EDT
- DataSourc by Guy Pardon on October 04 2004 11:22 EDT
- PortableRemoteObject.narrow(); by Mr Singh on October 04 2004 10:18 EDT
- Nope by Igor Shindin on October 05 2004 11:18 EDT
- narrow not for java: by Bernd Eckenfels on February 28 2005 13:11 EST
- PortableRemoteObject.narrow(); by Siva kumar on November 23 2005 03:48 EST
- does not work by Sai Giridhar on January 09 2006 06:49 EST
- does not work by Kanika Aggarwal on May 05 2006 05:44 EDT
-
PortableRemoteObject.narrow();[ Go to top ]
- Posted by: Guy Pardon
- Posted on: October 04 2004 09:58 EDT
- in response to Mr Singh
Hi,
The narrowing is from the CORBA world and got introduced in J2EE when they decided to adopt IIOP as the protocol for doing RMI. Consequently, you have to narrow only when you are dealing with IIOP, such as for remote bean handles.
DataSource objects are usually not accessed that way (intra-VM instead) and hence don't need narrowing.
Best,
Guy Pardon
JTA/J2EE Transactions Forum
TransactionsJTA download -
PortableRemoteObject.narrow();[ Go to top ]
- Posted by: Mr Singh
- Posted on: October 04 2004 10:18 EDT
- in response to Guy Pardon
Thanks Guy!
Just out of interest how are Datasources handled since to retrieve a datasource the same syntax is used ie.
InitialContext ctx = new InitialContext();
ctx.lookup("Datasource");
I'm assuming they don't use RMI? -
DataSourc[ Go to top ]
- Posted by: Guy Pardon
- Posted on: October 04 2004 11:22 EDT
- in response to Mr Singh
Hi,
Anything you lookup in JNDI is handled by a class-specific ObjectFactory that (loosely speaking) converts the name into an object of the class you lookup. What is actually bound inside JNDI is not really the Object that you get on lookup, but rather some reference data containing information about what class the object belongs to, and how to construct an 'equivalent' instance on lookup. Such an equivalent Object is the real result of the lookup.
So technically, the casting is all you need (like for the DataSource): it tells both the compiler and the runtime VM that the name you lookup is expected to be of the class you cast to. The JNDI and the class-specific ObjectFactory do the rest.
For pure Java, this mechanism is sufficient in most if not all cases.
For IIOP/CORBA (and dito bean handles), the case is more complex because the underlying CORBA runtime also needs a special treatment that can't be done by casting and the reference information alone. That is why you have to do the explicit call to narrow. It's really a special case as far as JNDI is concerned.
Guy -
Nope[ Go to top ]
- Posted by: Igor Shindin
- Posted on: October 05 2004 11:18 EDT
- in response to Mr Singh
However, do you also have to use PortableRemoteObject.narrow() when obtaining a datasource, or some other kind of object?Thanks in advance!
No. For database, you have to search database JNDI name and cast it to DataSource.
Datasource ds = (Datasource)ctx.lookup("myDB"); -
narrow not for java:[ Go to top ]
- Posted by: Bernd Eckenfels
- Posted on: February 28 2005 13:11 EST
- in response to Mr Singh
Hello,
generally everything you obtain from "java:" namespace does not need narrowing, since it is VM-local.
However, you might find links to remote ressources (typically under java:comp/env/), so it might be better you always narrow, unless you know you deal with VM local objects (like DataSources). I have not yet found a case where narrow hurts.
Greetings
Bernd -
PortableRemoteObject.narrow();[ Go to top ]
- Posted by: Siva kumar
- Posted on: November 23 2005 03:48 EST
- in response to Mr Singh
However, do you also have to use PortableRemoteObject.narrow() when obtaining a datasource, or some other kind of object?
Answer for the above question is NO.
You have to use narrow only when you are dealing with IIOP. -
does not work[ Go to top ]
- Posted by: Sai Giridhar
- Posted on: January 09 2006 06:49 EST
- in response to Mr Singh
this code is not working for me..
can anyone help me where i have did mistake.
pubserhome is the home for session bean and pubserremote is the remote interface for session bean.
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
properties.put(Context.PROVIDER_URL, "localhost:1099");
try{
Context jndiContext = new InitialContext(properties);
Object ref=jndiContext.lookup("pubserremote");
pubserhome home =(pubserhome) PortableRemoteObject.narrow( ref, pubserhome.class);
pubserremote searchbean=home.create();
in the above code i get the error as
incompatible types..
found:publisher.src.ejb.pubserhome
required:publisher.src.ejb.pubserremote
pubserhome searchbean=home.create();
^
and also in this line
pubserhome home =(pubserhome) PortableRemoteObject.narrow( ref, pubserhome.class);
thanks in advance -
does not work[ Go to top ]
- Posted by: Kanika Aggarwal
- Posted on: May 05 2006 05:44 EDT
- in response to Sai Giridhar
hi sai
pubserhome searchbean=home.create();
the above stmt is giving an error coz home.create() returns an object of remote type.
that is why u r getting this message:
found:publisher.src.ejb.pubserhome
required:publisher.src.ejb.pubserremote