Skip to Content
author's profile photo Former Member
Former Member

ClassCastException using EJB from a service

Hi all,

I developed a DeployService and currently I'm getting a ClassCastException while accessing the a EJB.

I put the lookup within a delegate which is also be used from a webDynpro application. With the WebDynpro App everything work properly, but if I call the delegate from the service a SAPClassCastException I thrown, when I cast the object to the home interface.

The coding is as follows:

Properties props = new Properties();

props.put(Context.PROVIDER_URL,"localhost:50004");

props.put(Context.INITIAL_CONTEXT_FACTORY,

"com.sap.engine.services.jndi.InitialContextFactoryImpl");

context = new InitialContext(props);

Object obj = context.lookup(jndiName);

EJBHome ejb = EJBHomeFactory.getInstance().lookup(jndiName, ComponentHome.class);

ComponentHome componentHome = (ComponentHome)ejb;

Thanks a lot for the help.

Falk

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Feb 23, 2005 at 05:42 AM

    Hi Falk,

    Hey just tell me that what is the EJBHome and ComponentHome and EJBHomeFactory in your coding.....

    You can try the following code for the same.

    Object obj =(Object) jndicontext.lookup("<Your JNDI name>");

    TestEJBHome home = (TestEJBHome) javax.rmi.PortableRemoteObject.narrow(

    obj,

    TestEJBHome.class);

    TestEJB hello = home.create();

    Here, TestEJBHome is your Home interface of the EJB and TestEJB is your remote interface. Now you can use hello object to call your business methods of EJB.

    Regards,

    Bhavik

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Bhavik,

      the EJBHome is the interface which extends the Remoteinterface of the EJB. ComponentHome is the remote interface itself. The EJBHomeFactory provides the following tasks:

      - caching the Interfaces remote and local

      - retrieving interfaces if they are not available yet by using the context lookup and narrowing them.(all is described in service locator pattern of sun)

      The current problem I have is that while processing the narrow I will retrive the ClassCastException. This means that I already have the implementation you described and it doesn't work.

      Regards

      Falk

  • author's profile photo Former Member
    Former Member
    Posted on Feb 23, 2005 at 09:55 AM

    Hi Falk,

    Try to narrow object using javax.rmi.PortableRemoteObject.narrow() function.

    Check what error it will give and let me know.

    Regards,

    Bhavik

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Svetoslav Manolov

      Hi Svetoslav,

      I changed the statement to new InitialContext().

      Furthermore I tried the lookup path ejbContexts ejbCosNaming and localejb. With localejbs it seems that the object returned is correct but the narrow will fail with an ClassCastException.

      The point I dont understand: Why does it work with WD and why does it fail running the same coding from a j2ee service. Is there some additional which I have to implent for the service which will be done for me using WD?

      Regards

      Falk

  • author's profile photo Former Member
    Former Member
    Posted on Feb 23, 2005 at 03:41 PM

    Hi,

    The problem you have (ClassCastException) can be connected with having the same jar with home, remote and implementation of EJB-s in different applications, which has different classloaders.

    Try to have one jar and define reference between service and application that have EJB-s.

    In yours service you can have only interfaces (home,remote).

    Also you can not have client jars for EJB-s in server or application classpath, this also makes ClassCastException.

    Client jars are only for remote clients.

    I hope it will be useful.

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Falk,

      the lookup from localejbs context returns local ejb objects. So it is natural that the narrow throws ClassCastException. You have to lookup with the jndi name of the bean - either pre-defined in the ejb-j2ee-engine.xml, or <application-name>/<ejb-name> by default.

      If you succeed with the same look up name from WD and fail from a service, then this is probably a class loader problem and Milosz's advice is quite helpful.

      Regards,

      Svetoslav

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.