3 Replies Latest reply: Feb 16, 2012 2:42 PM by Pierre Guillemot RSS

ClassCastException

Pierre Guillemot Newbie

Hello there,

 

i am new to JBoss AS, i am currently using JBoss AS6.0 final, MySQL and Netbeans 7.1.

I finally managed to configure the MySqlDS.xml and persistence.xml, i can see in the logs that the PU is read and all, the database is accessed, everything is working.

 

In order to try my datasource access in created a simple servlet :

@WebServlet(name = "Test", urlPatterns = {"/Test"})

public class Test extends HttpServlet {

 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        try {

            /*

             * TODO output your page here. You may use following sample code.

             */

            out.println("<html>");

            out.println("<head>");

            out.println("<title>Servlet Test</title>");           

            out.println("</head>");

            out.println("<body>");

            out.println("<h1>Servlet Test at " + request.getContextPath() + "</h1>");

            out.println("</body>");

            out.println("</html>");

           

            Context context;

            try {

                context = new InitialContext();

                DataSource data = (DataSource)context.lookup("java:/MySqlDs");

            } catch (NamingException ex) {

                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);

            }

           

        } finally {           

            out.close();

        }

    }

 

Except that in have this error :

 

23:01:32,412 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/YouFoodInc].[Test]] "Servlet.service()" pour la servlet Test a généré une exception: java.lang.ClassCastException: org.jboss.jpa.injection.InjectedEntityManagerFactory cannot be cast to javax.sql.DataSource
    at Test.processRequest(Test.java:57)    at Test.doGet(Test.java:80)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
    at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67) [:6.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
    at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
    at java.lang.Thread.run(Unknown Source) [:1.6.0_27]

 

I added the mysql-connector-java-5.1.13-bin.jar inside the serverd/default/lib. And I have not added anything else inside the lib folder.

 

I already did this kind of thing using GlassFish 3.0, and i never had any issues.

 

If you need any other informations, please tell me. Thank you for your attention,

 

Regards,

 

Pierre.

  • 1. Re: ClassCastException
    Wolf-Dieter Fink Master

    Please attach the MySqlDS.xml and persistence.xml files.

     

    What you want to do after you lookup DS in your code? Do you have entity beans or try to access database via plain SQL?

  • 2. Re: ClassCastException
    Pierre Guillemot Newbie

    Here is the persistence.xml :

     

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
      <persistence-unit name="YouFoodIncPU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/MySqlDS</jta-data-source>
        <properties>
                <property name="hibernate.hbm2ddl.auto" value="update"/>
                <property name="hibernate.archive.autodetection" value="class" />
                <property name="hibernate.show_sql" value="false" />
                <property name="hibernate.format_sql" value="true" />
                <property name="use_sql_comments" value="true" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
                <property name="jboss.entity.manager.factory.jndi.name" value="java:/MySqlDs" />
        </properties>
      </persistence-unit>
    </persistence>
    

     

    And the MySqlDs.xml with obviously the password modified :

     

    <datasources>
      <local-tx-datasource>
        <jndi-name>MySqlDS</jndi-name>
        <connection-url>jdbc:mysql://localhost:3306/youfoodincdb</connection-url>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <user-name>root</user-name>
        <password>password</password>
        <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
        <!-- should only be used on drivers after 3.22.1 with "ping" support
        <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
        -->
        <!-- sql to call when connection is created
        <new-connection-sql>some arbitrary sql</new-connection-sql>
          -->
        <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
        <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
          -->
    
        <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
        <metadata>
           <type-mapping>mySQL</type-mapping>
        </metadata>
      </local-tx-datasource>
    </datasources>
    

     

    I can access the (empty) database through mysql console.

     

    I just want to check my connection with my datasource, i still haven't coded any entities or daos. I want to make it work first.

     

    Thanks for your help.

  • 3. Re: ClassCastException
    Pierre Guillemot Newbie

    It's me again, i am still facing the same issue while trying to implement a secure domain for my application :

     

    20:34:55,853 TRACE [org.jboss.security.SecurityRolesAssociation] Setting threadlocal:{}
    20:34:55,863 DEBUG [org.jboss.security.integration.JNDIBasedSecurityManagement] Creating SDC for domain=youfoodinc_domain
    20:34:55,864 TRACE [org.jboss.security.plugins.JaasSecurityManager] Constructing
    20:34:55,864 DEBUG [org.jboss.security.plugins.auth.JaasSecurityManagerBase.youfoodinc_domain] CallbackHandler: org.jboss.security.auth.callback.JBossCallbackHandler@174e5edb
    20:34:55,864 DEBUG [org.jboss.security.plugins.auth.JaasSecurityManagerBase.youfoodinc_domain] CachePolicy set to: org.jboss.security.auth.AuthenticationTimedCachePolicy@6edc77c4
    20:34:55,995 DEBUG [org.jboss.security.integration.JNDIBasedSecurityManagement] setCachePolicy, c=org.jboss.security.auth.AuthenticationTimedCachePolicy@6edc77c4
    20:34:55,997 TRACE [org.jboss.security.plugins.auth.JaasSecurityManagerBase.youfoodinc_domain] Begin isValid, principal:test@test.fr, cache info: null
    20:34:55,997 TRACE [org.jboss.security.plugins.auth.JaasSecurityManagerBase.youfoodinc_domain] defaultLogin, principal=test@test.fr
    20:34:55,998 TRACE [org.jboss.security.auth.login.XMLLoginConfigImpl] Begin getAppConfigurationEntry(youfoodinc_domain), size=12
    20:34:56,050 TRACE [org.jboss.security.auth.login.XMLLoginConfigImpl] End getAppConfigurationEntry(youfoodinc_domain), authInfo=AppConfigurationEntry[]:
    [0]
    LoginModule Class: org.jboss.security.auth.spi.DatabaseServerLoginModule
    ControlFlag: LoginModuleControlFlag : required
    Options:
    name=principalsQuery, value=SELECT password FROM _user WHERE email='test@test.fr'
    name=dsJndiName, value=java:/MySqlDs
    name=rolesQuery, value=SELECT _role, 'Roles' FROM _user WHERE email='test@test.fr'
    
    20:34:56,058 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] initialize
    20:34:56,099 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] Security domain: youfoodinc_domain
    20:34:56,099 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] DatabaseServerLoginModule, dsJndiName=java:/MySqlDs
    20:34:56,099 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] principalsQuery=SELECT password FROM _user WHERE email='test@test.fr'
    20:34:56,100 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] rolesQuery=SELECT _role, 'Roles' FROM _user WHERE email='test@test.fr'
    20:34:56,149 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] suspendResume=true
    20:34:56,151 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] login
    20:34:56,152 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] suspendAnyTransaction
    20:34:56,153 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] resumeAnyTransaction
    20:34:56,154 TRACE [org.jboss.security.auth.spi.DatabaseServerLoginModule] abort
    20:34:56,155 TRACE [org.jboss.security.plugins.auth.JaasSecurityManagerBase.youfoodinc_domain] Login failure: javax.security.auth.login.LoginException: java.lang.ClassCastException: org.jboss.jpa.injection.InjectedEntityManagerFactory cannot be cast to javax.sql.DataSource
        at org.jboss.security.auth.spi.DatabaseServerLoginModule.getUsersPassword(DatabaseServerLoginModule.java:172)
        at org.jboss.security.auth.spi.UsernamePasswordLoginModule.login(UsernamePasswordLoginModule.java:245)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at javax.security.auth.login.LoginContext.invoke(LoginContext.java:769)
        at javax.security.auth.login.LoginContext.access$000(LoginContext.java:186)
        at javax.security.auth.login.LoginContext$4.run(LoginContext.java:683)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:680)
        at javax.security.auth.login.LoginContext.login(LoginContext.java:579)
        at org.jboss.security.plugins.auth.JaasSecurityManagerBase.defaultLogin(JaasSecurityManagerBase.java:553)
        at org.jboss.security.plugins.auth.JaasSecurityManagerBase.authenticate(JaasSecurityManagerBase.java:487)
        at org.jboss.security.plugins.auth.JaasSecurityManagerBase.isValid(JaasSecurityManagerBase.java:365)
        at org.jboss.security.plugins.JaasSecurityManager.isValid(JaasSecurityManager.java:160)
        at org.jboss.web.tomcat.security.JBossWebRealm.authenticate(JBossWebRealm.java:399)
        at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:256)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:485)
        at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285)
        at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
        at java.lang.Thread.run(Thread.java:662)
    
        at javax.security.auth.login.LoginContext.invoke(LoginContext.java:872) [:1.6.0_31]
        at javax.security.auth.login.LoginContext.access$000(LoginContext.java:186) [:1.6.0_31]
        at javax.security.auth.login.LoginContext$4.run(LoginContext.java:683) [:1.6.0_31]
        at java.security.AccessController.doPrivileged(Native Method) [:1.6.0_31]
        at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:680) [:1.6.0_31]
        at javax.security.auth.login.LoginContext.login(LoginContext.java:579) [:1.6.0_31]
        at org.jboss.security.plugins.auth.JaasSecurityManagerBase.defaultLogin(JaasSecurityManagerBase.java:553) [:3.0.0.CR2]
        at org.jboss.security.plugins.auth.JaasSecurityManagerBase.authenticate(JaasSecurityManagerBase.java:487) [:3.0.0.CR2]
        at org.jboss.security.plugins.auth.JaasSecurityManagerBase.isValid(JaasSecurityManagerBase.java:365) [:3.0.0.CR2]
        at org.jboss.security.plugins.JaasSecurityManager.isValid(JaasSecurityManager.java:160) [:6.0.0.Final]
        at org.jboss.web.tomcat.security.JBossWebRealm.authenticate(JBossWebRealm.java:399) [:6.0.0.Final]
        at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:256) [:6.0.0.Final]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:485) [:6.0.0.Final]
        at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
        at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
        at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
        at java.lang.Thread.run(Thread.java:662) [:1.6.0_31]
    
    20:34:56,200 TRACE [org.jboss.security.plugins.auth.JaasSecurityManagerBase.youfoodinc_domain] End isValid, false
    

     

    I stil have the same Persistence Unit and Datasource.

    I modified the login-conf.xml to include :

     

    <!--YouFood inc-->
        <application-policy name="youfoodinc_domain">  
            <authentication>  
                <login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag = "required">  
                    <module-option name="dsJndiName">java:/MySqlDs</module-option> 
                    <module-option name="principalsQuery">SELECT password FROM _user WHERE email='test@test.fr'</module-option>  
                    <module-option name="rolesQuery">SELECT _role, 'Roles' FROM _user WHERE email='test@test.fr'</module-option> 
                </login-module> 
            </authentication>
        </application-policy>
    

     

    I modified jboss-web.xml :

    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-web>
      <context-root>/YouFoodInc</context-root>
      <security-domain>java:/jaas/youfoodinc_domain</security-domain>
    </jboss-web>
    

    As well as web.xml, which is a bit long, i will put it if someone' asking for.

     

    It's a very weird error, I have no idea why it happened. Seems like it's using another jar ? I haven't changed my AS's jar (except for the mysql driver inside lib).

     

    Any idea anyone ??