cancel
Showing results for 
Search instead for 
Did you mean: 

InitialContext lookup of EJB, ClassCastException

Former Member
0 Kudos

Hello,

i am trying to look up a EJB in my REST Service. Both deployed on the same server. The problem is that i am getting a Object of Type Proxy from the lookup method and can't cast it to the needed Bean Class. Any ideas?

Here the code:

//This returns the Proxy Object?!

Object obj = new

InitialContext().lookup("ejb:/mycompany.de/reboard~report~cs/REMOTE/com.sap.bpem.facade.api._simplified.adapted.BPMFacadeBeanSimplifiedEjb/com.sap.bpem.facade.api._simplified.adapted.BPMFacadeBeanSimplifiedRemote");

//Class cast excpetion here!

BPMFacadeBeanSimplifiedRemote myBean = (BPMFacadeBeanSimplifiedRemote) PortableRemoteObject.narrow(obj, BPMFacadeBeanSimplifiedRemote.class);

Thanks for help!

Accepted Solutions (1)

Accepted Solutions (1)

rolf_paulsen
Active Participant
0 Kudos

Hello Dimitri,

probably your EJB and your REST Service are deployed on the same SAP NetWeaver Java, but in different EAR files.

Due to somewhat strance classloading and "narrow()" implementation in SAP NetWeaver Java, you have to define a dependency from one EAR file to the other in the META-INF/application-j2ee-engine.xml of one of the two EAR files.

The content should look like this in the referencing enterprise application

<?xml version="1.0" encoding="UTF-8"?>

<application-j2ee-engine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="application-j2ee-engine.xsd">

  <reference reference-type="hard">

    <reference-target provider-name="sap.com" target-type="application">the-referenced-enterprise-application</reference-target>

  </reference>

</application-j2ee-engine>

(replace "the-referenced-enterprise-application" and probably "sap.com").

(If you are using EAR DCs, this is done automatically if you define a Runtime Dependeny in Component Properties)

The result is that the referencing enterprise applicationis sharing class loader with the referenced enterprise application.

Without this deployment descriptor, both enterprise applicatoin have class BPMFacadeBeanSimplifiedRemote in different, isolated class loaders, and narrow is implemented as a noop

In real remote clients outside the JVM of NetWeaver Java, this is cured by PortableRemoteObject.narrow() which instantiates a real proxy object. Inside SAP NW Java, narrow() does never return a Remote proxy, even if you look up the remote object.

Regards

Rolf

Former Member
0 Kudos

Thank you Rolf, this helped me a lot!

Answers (0)