Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB Local Home Interface ClassCastException!!

 
kenshin Lin
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all:
i encounter a problem that a ClassCastException occured when i lookup
a EJBLocalHome object, and then cast it. i can sure the object
of "lookup" is implemented by Container. Why this happened?
my code is:
============================================
Context ctx = new InitialContext();

Object obj = ctx.lookup("DBSessionLocal");
if(obj instanceof DBSessionLocalHome){
System.out.println("ok");
}else{
System.out.println("not ok");
}
=================================================
the output is "not ok"

and if(obj instanceof EJBLocalHome) is "true" , some one can tell me

why?
 
Sergiu Truta
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
Object ref = ctx.lookup(jndiName);
try
{
EJBHome value = (EJBHome)PortableRemoteObject.narrow(ref,
Class.forName(className));
......
}
where the "className" is the fully qualified name, like:
com.pack1.pack2.....packn.EJBHome
Enjoy
 
Srikanth Shenoy
author
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kenshin,
Sergiu is correct. You have to use PortableRemoteObject to narrow the JNDI returned object.
If you are still getting ClasscastException after following Sergiu's tip, then I presume your class packaging itself might be to blame.
This happens if one your class loaded by EAR class loader is trying to acess the EJB. For more details on this, please refer to the free chapters from "J2EE Project Survival Guide" available for public review at
Server Side. It covers Class Loading in J2EE in detail.
Thanks,
Srikanth Shenoy
Author
Struts Survival Guide - Basics to Best Practices
J2EE Project Survival Guide
 
kenshin Lin
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi :
but the object i lookup is "Local Home object" , i think that there is
no need to cast the object by using PortableRemoteObject.narrow()
ps: the clinet is in the same JVM
 
Ken Boyd
Ranch Hand
Posts: 329
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We still have to cast because the return type of lookup is Object, but we don't have to narrow it since it isn't a stub i.e. client is local. If you have Head First EJB refer to page 160.
Hope this help.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic