Topic
3 replies Latest Post - ‏2013-12-21T15:51:29Z by YJ3E_Fuzail_Sarang
rajesh272727
rajesh272727
2 Posts
ACCEPTED ANSWER

Pinned topic ClassCastException in Liberty profile while using EJB3.1 Lite

‏2013-11-18T05:02:07Z |

I have application ( EAR ) which has both WAR as well as EJB module. I'm trying to call LocalBean ( resides in ejb module ) from servlet ( resides in web application ) , I get cast exception. I have tried many option and I'm not sure what goes wrong in it. Having said, It does work when I have EJB in same web application and lookup using "java:module/TestEJB". I'm trying to migrate from Full profile to Liberty profile ( I'm using EJB3.1 Lite OSGI component ) with eclipse . If you look at the exception bot the class which trying to cast are in the same package ( one is generated by container and another in user define ). Please advice me. Thanks

# TestEJB

package com.example.app.specials.ejb;
 
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
 
 
@Stateless
@LocalBean
public class TestEJB {
 
public TestEJB()
{
 
}
 
 
public String getName(String str)
{
return "Hello my name is "+ str;
}
 
}
 

#Client class

 

package com.example.app.base.servlet;
 
import java.io.IOException;
 
import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.marriott.app.specials.ejb.TestEJB;
import com.marriott.app.specials.ejb.TestRemote;
 
/**
 * Servlet implementation class TestEJBServlet
 */
public class TestEJBServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestEJBServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
try
{
InitialContext ic = new InitialContext();
TestEJB       spec       = (TestEJB)ic.lookup("java:global/PresentationApp/app-service/TestEJB!"+TestEJB.class.getName());
System.out.println("Hiiiiiiiiiiiiiiiiiiiiii" + spec.getName(" Hello World "));
}
catch (Exception e)
{
e.printStackTrace();
}
 
 
}
 
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
 
}
 

#Exception

[err] java.lang.ClassCastException: com.exampleapp.specials.ejb.EJSLocalNSLTestEJB_805744f2 cannot be cast to com.example.app.specials.ejb.TestEJB

[err] at com.example.app.base.servlet.TestEJBServlet.doGet(TestEJBServlet.java:46)
[err] at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
[err] at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
[err] at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1240)
[err] at [internal classes]
  • bkail
    bkail
    23 Posts
    ACCEPTED ANSWER

    Re: ClassCastException in Liberty profile while using EJB3.1 Lite

    ‏2013-11-18T14:51:05Z  in response to rajesh272727

    In the error scenario, are the EJB module and the .war module in the same .ear or separate?  If separate .ear, then I would recommend adding a local business interface, and then ensure the interface is in a JAR in a common library class loader.  This approach has not been formally tested on Liberty profile, but it has been tested on full profile and is known to work.

    If they the EJB module and .war are in the same application, then I am not sure why it is failing.  Can you print out the following?

    TestEJB spec = ...
    System.out.println("this CL=" + getClass().getClassLoader());

    System.out.println("TestEJB CL=" + TestEJB.class.getClassLoader());
    System.out.println("spec CL=" + spec.getClass().getClassLoader());

  • rajesh272727
    rajesh272727
    2 Posts
    ACCEPTED ANSWER

    Re: ClassCastException in Liberty profile while using EJB3.1 Lite

    ‏2013-11-18T18:02:41Z  in response to rajesh272727

    Thanks for quick reply, Appreciate it.

    Both ejb module and war are in same application. and below is the classloader log. I may not be able toget "spec" classloader as it is not intialized ( error while getting instance ) anyway spec is reference variable of TestEJB and i printed Test EJB classloder, both the classloader are same. I also provided server.xml info, just incase if you think there could be conflicts with other features i added.

     

    Classloader log

    ============

    this CL=com.ibm.ws.classloading.internal.ParentLastClassLoader@57ce984a

    TestEJB CL=com.ibm.ws.classloading.internal.ParentLastClassLoader@57ce984a

     

     

    server.xml

    ========

    <server description="new server">

     

    <logging consoleLogLevel="INFO" copySystemStreams="true" maxFiles="10"/>

     

    <!-- Enable features -->

    <featureManager>

    <feature>jsp-2.2</feature>

    <feature>jndi-1.0</feature>

    <feature>wasJmsClient-1.1</feature>

    <feature>localConnector-1.0</feature>

    <feature>ejbLite-3.1</feature>

    <feature>webCache-1.0</feature>

    <feature>restConnector-1.0</feature>

    <feature>osgi.jpa-1.0</feature>

    <feature>usr:CustomRegistryFeature</feature>

    <feature>usr:UserFeature</feature>

    <feature>jdbc-4.0</feature>

    <feature>ssl-1.0</feature>

    <feature>servlet-3.0</feature>

    </featureManager>

     

     

     

     

    <sslDefault sslRef="defaultSSLSettings"/>

    <ssl clientAuthenticationSupported="true" id="defaultSSLSettings" keyStoreRef="defaultKeyStore" trustStoreRef="defaultTrustStore"/>

    <keyStore id="defaultKeyStore" location="${server.config.dir}/resources/security/key.jks" password="{xor}MjwwMm5tbA==" type="JKS"/>

    <keyStore id="defaultTrustStore" location="${server.config.dir}/resources/security/trust.jks" password="{xor}MjwwMm5tbA==" type="JKS"/>

     

     

     

     

     

    <httpEndpoint host="localhost" httpPort="80" httpsPort="443" id="defaultHttpEndpoint">

    </httpEndpoint>

     

    <applicationMonitor updateTrigger="mbean"/>

    <library id="Custom_Registry">

    <fileset caseSensitive="false" dir="${wlp.user.dir}/shared" includes="*.*"/>

    </library>

    <library id="DB2Lib">

    <fileset caseSensitive="false" dir="C:/vignette/jdbc" includes="db2jcc4.jar, db2jcc_license_cisuz.jar, vgnjdbc.jar"/>

    </library>

    <customUserRegistry flexFile="${server.config.dir}/resources/security/flex.properties" groupsFile="${server.config.dir}/resources/security/groups.props" usersFile="${server.config.dir}/resources/security/users.props"/>

    <jdbcDriver id="JDBCDriver" javax.sql.ConnectionPoolDataSource="com.ibm.db2.jcc.DB2ConnectionPoolDataSource" javax.sql.DataSource="com.ibm.db2.jcc.DB2SimpleDataSource" libraryRef="DB2Lib"/>

    <dataSource beginTranForResultSetScrollingAPIs="false" id="ContentDB" jdbcDriverRef="JDBCDriver" jndiName="jdbc/Content" transactional="false" type="javax.sql.DataSource">

     

    <properties.db2.jcc currentSchema="VIGN71" databaseName="micmsprd" password="{xor}OzopbQk4MW4=" portNumber="60000" serverName="micmsprd.marriott.com" user="vgnapusr"/>

    <connectionManager minPoolSize="50"/>

    </dataSource>

     

    <dataSource beginTranForResultSetScrollingAPIs="false" beginTranForVendorAPIs="true" id="PropertyDb2" jdbcDriverRef="JDBCDriver" jndiName="jdbc/PropertyDb2" supplementalJDBCTrace="true" transactional="true" type="javax.sql.DataSource">

     

    <properties.db2.jcc currentSchema="VIGN71" databaseName="micmsprd" password="{xor}OzopbQk4MW4=" portNumber="60000" serverName="micmsprd.marriott.com" user="vgnapusr"/>

    <connectionManager minPoolSize="50"/>

    </dataSource>

     

    <enterpriseApplication id="PresentationApp" location="PresentationApp.ear" name="PresentationApp">

    <classloader commonLibraryRef="Custom_Registry, DB2Lib" delegation="parentLast">

    <commonLibrary>

    <folder dir="C:\snapviews\rkand316_App_Maintenance_dev_osgi\release\config\local"/>

    </commonLibrary>

    </classloader>

     

    </enterpriseApplication>

    </server>

     

    • YJ3E_Fuzail_Sarang
      YJ3E_Fuzail_Sarang
      1 Post
      ACCEPTED ANSWER

      Re: ClassCastException in Liberty profile while using EJB3.1 Lite

      ‏2013-12-21T15:51:29Z  in response to rajesh272727

      Having the same issue here except I have the ear and war running sepertely, would be good to know if this was resolved.