Object obj = ctx.lookup(SomeObject);
SpecificObject so = (SpecificObject) PortableRemoteObject.narrow(obj, SpecificObject.class);
Thanks in advance!
Datasource ds = (Datasource)ctx.lookup("myDB");
DataSource objects are usually not accessed that way (intra-VM instead) and hence don't need narrowing.
InitialContext ctx = new InitialContext();
ctx.lookup("Datasource");
I'm assuming they don't use RMI?
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.
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.
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
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