• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inconvertible types attempting to cast ejb JNDI lookup to home interface

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic