Java – GWT 1.7 – Configuring a DataSource in Jetty (Hosted Mode)

gwtjavajettyjndi

I'm new to GWT (1.7) and tried to establish a connection to my MySQL database from the servlet. Because I got some errors, I googled them and found out, that I have to configure a DataSource in Jetty to get it working in Hosted Mode. I followed this tutorial:
Tutorial

I created my own JettyLauncher class as described and added the according parameter to the Run configuration. After that, I added this entry to my WEB-INF/web.xml:

    <resource-ref>      
     <description>MySQl Connection</description> 
        <res-ref-name>jdbc/skyline</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>  
    </resource-ref>

After that I created the jetty-env.xml with the following content:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure class="org.mortbay.jetty.webapp.WebAppContext">

 <New id="skyline" class="org.mortbay.jetty.plus.naming.Resource">
    <Arg>jdbc/skyline</Arg>
    <Arg>
     <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
                 <Set name="Url">jdbc:mysql://localhost:3306/skyline</Set>
                 <Set name="User">root</Set>
                 <Set name="Password">admin</Set>
     </New>
    </Arg>
   </New>

</Configure>

And when I now try to start the app in eclipse (Galileo, with GWT Plugin) I get the following error:

[WARN] Configuration problem at <resource-ref><description>MySQl Connection</description><res-ref-name>jdbc/skyline</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth></resource-ref>
java.lang.ClassCastException: org.mortbay.jetty.plus.naming.Resource cannot be cast to org.mortbay.jetty.plus.naming.NamingEntry
    at org.mortbay.jetty.plus.naming.NamingEntry.lookupNamingEntry(NamingEntry.java:211)
    at org.mortbay.jetty.plus.naming.NamingEntry.bindToENC(NamingEntry.java:104)
    at org.mortbay.jetty.plus.webapp.Configuration.bindResourceRef(Configuration.java:73)
    at org.mortbay.jetty.plus.webapp.AbstractConfiguration.initResourceRef(AbstractConfiguration.java:262)
    at org.mortbay.jetty.plus.webapp.AbstractConfiguration.initWebXmlElement(AbstractConfiguration.java:161)
    at org.mortbay.jetty.webapp.WebXmlConfiguration.initialize(WebXmlConfiguration.java:289)
    at org.mortbay.jetty.plus.webapp.AbstractConfiguration.initialize(AbstractConfiguration.java:133)
    at org.mortbay.jetty.webapp.WebXmlConfiguration.configure(WebXmlConfiguration.java:222)
    at org.mortbay.jetty.plus.webapp.AbstractConfiguration.configure(AbstractConfiguration.java:113)
    at org.mortbay.jetty.webapp.WebXmlConfiguration.configureWebApp(WebXmlConfiguration.java:180)
    at org.mortbay.jetty.plus.webapp.AbstractConfiguration.configureWebApp(AbstractConfiguration.java:96)
    at org.mortbay.jetty.plus.webapp.Configuration.configureWebApp(Configuration.java:124)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1217)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
    at skyline.frontend.server.helper.CustomJettyLauncher$WebAppContextWithReload.doStart(CustomJettyLauncher.java:412)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:222)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at skyline.frontend.server.helper.CustomJettyLauncher.start(CustomJettyLauncher.java:464)
    at com.google.gwt.dev.HostedMode.doStartUpServer(HostedMode.java:365)
    at com.google.gwt.dev.HostedModeBase.startUp(HostedModeBase.java:590)
    at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:397)
    at com.google.gwt.dev.HostedMode.main(HostedMode.java:232)

I use the jetty-naming-6.1.11.jar and the jetty-plus-6.1.11.jar. Both are in the WEB-INF/lib directory. I already tried a newer version of both libraries with the same result.

Could anybode tell me, where I went wrong?

Best Solution

If you're getting ClassCastExceptions where you should not (like here, as org.mortbay.jetty.plus.naming.Resource extends org.mortbay.jetty.plus.naming.NamingEntry), you have a classpath problem, meaning there are two versions of org.mortbay.jetty.plus.naming.Resource loaded from two different JAR files by different class loaders flying around in your Jetty instance.

I don't know about GWT, but you would not usually have to include any Jetty JARs in your application's WEB-INF/lib folder, as they are present in Jetty anyway.

Related Question