IBM Support

ClassCastException when casting a remote EJB home or remote interface stub instance

Troubleshooting


Problem

You receive a ClassCastException when casting a remote Enterprise Java™Beans home or remote interface stub instance to its actual remote home interface or remote interface type. When resolving a remote EJB home reference or a local EJB home reference, an application programmer may "cleverly" attempt to narrow the result of a JNDI lookup using the EJBHome class rather than the exact home class (type) in order to generalize the task. Here is an example involving a remote Enterprise JavaBeans, MyBean and it's corresponding home, MyHome: [] InitialContext context = new InitialContext(); Object remoteObj = context.lookup("java:comp/env/ejb/MyHomeName"); EJBHome ejbHome = PortableRemoteObject.narrow(remoteObj, EJBHome.class): MyBean myBean = (MyBean)ejbHome.create(); ... []

Symptom

In this example, a ClassCastException indicating class MyBean_Stubxxx occurs when the application casts the result of myHome.create() to class MyBean. The explanation is provided below.

A ClassCastException occurs if myHome (an instance of EJBHome, above) is cast to its actual type, MyHome. The following example statements also result in a ClassCastException indicating the MyHome_Stubxxx class:

MyHome myHome = (MyHome)ejbHome;

Which is synonymous with:

MyHome myHome = (MyHome)PortableRemoteObject.narrow(object, EJBHome.class):

Because the code snippets above violate the Enterprise JavaBeans 2.0 specification with regard to narrowing remote EJB references, their expected behavior is indeterminate.

Cause

The problem is caused by the indeterminate behavior of the non-compliant narrow() invocation, to class EJBHome.class, rather than to the actual remote home interface class, MyHome.class. In IBM WebSphere Application Server, the result of the narrow() is an instance of MyHome_Stubxxx, that was loaded by the application module class loader of the server-side application hosting MyBean, not by the application module class loader of the current client-side application. The cast fails because the class loader that loaded the cast target class, MyHome, in the client-side application is different from the class loader which loaded class MyHome_Stubxxx (and its dependency classes, including MyHome.)

The class loaders are different because the narrow() operation did not translate the class loader of the cast source object, MyHome_Stubxxx, to that of the target type, MyHome. When the remote object reference, remoteObj, is narrowed to the correct class, MyHome.class, the narrow() operation correctly performs the class loader translation, and the cast exception does not occur.

In the case where an application narrows a remote reference to EJBHome and uses the resulting home reference to create instances of the specific Enterprise JavaBeans type, the Enterprise JavaBean stub instance returned by create() will also be loaded by the server-side application module class loader, and again, a ClassCastException exception indicating the remote stub class will occur when casting the stub instance to its remote interface class, which is loaded by the current client-side application module class loader.

Resolving The Problem

Narrow the remote Enterprise JavaBean home and interface object references to their specific class type, not to a superclass, such as EJBHome.class. The Enterprise JavaBeans 2.0 specification offers an example of resolving a specific session bean home interface:


Context initialContext = new InitialContext();
CartHome cartHome = (CartHome)javax.rmi.PortableRemoteObject.narrow(
initialContext.lookup(“java:comp/env/ejb/cart”), CartHome.class);

For complete details, refer to the "Client View" sections of the Enterprise JavaBeans 2.0 specification for the Sun J2EE Platform. [see URL, below]

[{"Product":{"code":"SSEQTP","label":"WebSphere Application Server"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"General","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF010","label":"HP-UX"},{"code":"PF016","label":"Linux"},{"code":"PF027","label":"Solaris"},{"code":"PF033","label":"Windows"}],"Version":"9.0;8.5.5;8.0;7.0","Edition":"Base;Network Deployment","Line of Business":{"code":"LOB45","label":"Automation"}},{"Product":{"code":"SSNVBF","label":"Runtimes for Java Technology"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Java SDK","Platform":[{"code":"","label":""}],"Version":"","Edition":"","Line of Business":{"code":"LOB36","label":"IBM Automation"}}]

Document Information

Modified date:
15 June 2018

UID

swg21192568