Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inconvertible types attempting to cast ejb JNDI lookup to home interface

 
John Davis
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I receive the following when trying to compile a servlet (a servlet which attempts to obtain a home interface via a JNDI lookup)
C:\ASAservletCode\ASAdonations.java:61: inconvertible types
found : java.lang.Object
required: RagHome
RagHomehome= (RagHome) context.lookup( "Rag" );
^
1 error
Finished
The code in the servlet is:
Properties env = new Properties();

env.put( "java.naming.factory.initial",
"desisoft.ejb.client.JRMPFactory" );

env.put( "desisoft.ejb.nameServer1",
"g2h:2050" );

Context context = new InitialContext( env );

RagHomehome= (RagHome) context.lookup( "Rag" );
The compiler obviously knows about RagHome. The context.lookup method returns a Java.lang.object
I am attempting a cast of object to RagHome.
What am I doing wrong?
 
Shin Hashitani
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,
Try this:
...
import javax.rmi.PortableRemoteObject;
...
java.lang.Object obj = Objectcontext.lookup( "Rag" );
RagHome home = (RagHome) PortableRemoteObject.narrow(obj, RagHome.class);

It is required to support RMI-IIOP for remote access in EJB. Since CORBA supports a lot of languages, and some of them don't support casting, you need to explicitly narrow the object coming back from lookup().
When you use lookup() and the reference is remote, you always need narrow(). Or, if you need to get remote reference by using Handle, HomeHandle,or EJBMetaData, you need narrow().
There might be other situations that you need narrow() method, but I am not sure.
I hope this helps.

Shin Hashitani
 
Shin Hashitani
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another situation:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=11&t=002370
When getting a reference contained in Collection or Enumeration resulting from a finder method, you need narrow().
 
Joe McGuire
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While in development you may get an inconvertible types error, such as the code:

Returns the error:
ClientTest.java:30: inconvertible types
found : java.lang.Object
required: CabinHome
CabinHome home = (CabinHome) PortableRemoteObject.narrow(obj, CabinHome.class);
^

I have found that explicitly importing the Home interface resolves the issue:

--------------------
Joe McGuire
Sun Certified Java™ 2 Programmer, BEA WLS Certified Developer
[ March 07, 2002: Message edited by: Joe McGuire ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic