5
votes

I have a GWT that needs to access some static resources. I don't want to place these resources in the actual /src/main/webapp directory (note: using Maven and corresponding layout) because they're not part of the app, but part of the data.

My problem is not how to give the application access to these resources at deployment time; that should be easy to do via Tomcat configuration. Rather the problem is how to give my app access to some test data during development. I have some test static files in a directory outside my GWT app tree, and would like to configure GWT's dev mode Jetty server to access those resources. That is, I want to be able to say mvn gwt:run (or run or debug the app in dev mode from Eclipse), and have the server serve those static resources.

I know about (and use) the dev mode -noserver option. However, since I'm modifying the server-side code a lot, along with the client code, it's not practical to redeploy the server every time the server code changes.

So far what I've been trying is to create a jetty-web.xml file in my WEB-INF directory, and add a new context so the server will serve my resources. Here are my failed jetty-web.xml attempts:

<Configure id="parentHandler" class="org.mortbay.jetty.handler.ContextHandler">
    <Set name="contextPath">/static_resources</Set>
    <Set name="resourceBase">/real/filesystem/path/to/static/resources/</Set>
    <Set name="handler">
    <New class="org.mortbay.jetty.handler.ResourceHandler">
        <Set name="cacheControl">max-age=3600,public</Set>
    </New>
    </Set>
</Configure>

If I put that in jetty-web.xml, then when I start dev mode, Jetty does serve my external resources, but doesn't serve my GWT app.

On the other hand, if I do this...

<Configure id="parentHandler" class="org.mortbay.jetty.handler.ContextHandler">
    <New id="newHandler" class="org.mortbay.jetty.handler.ContextHandler" >
        <Set name="contextPath">/static_resources</Set>
        <Set name="resourceBase">/real/filesystem/path/to/static/resources/</Set>
        <Set name="handler">
            <New class="org.mortbay.jetty.handler.ResourceHandler">
                <Set name="cacheControl">max-age=3600,public</Set>
            </New>
        </Set>
    </New>
    <Get id="server" name="server">
        <Call name="setHandler">
            <Arg><Ref id="newHandler" /></Arg>
        </Call>
    </Get>
    <Ref id="newHandler">
        <Set name="server"><Ref id="server" /></Set>
    </Ref>
</Configure>

...Jetty serves neither my static stuff nor my GWT app.

Any suggestions? What am I doing wrong?

(Note: GWT dev mode uses version 6 [or 6.something] of Jetty, and sets it up programmatically, so as far as I can see, no other Jetty config files are read in.)

Many thanks!

2
FWIW, when I need to customize the server, I don't bother with the hosted mode jetty instance, but set up a different server and use -noserver to prevent jetty from starting.Colin Alworth
Right, I do use that option sometimes. But that means packaging and deploying the .war in the other server every time the server code changes, no? And then setting up debugging on the other server, too? I mean, I can imagine it's possble and that there are ways to automatize it, but it doesn't seem like the most direct option.Andrew
My general approach there (comments since it doesnt actually fix the issue) is to run the other server out of my ide. Using mvn war:exploded copies all the important bits to the right dir, while still letting me debug as normal. Main difference is that there are two debug sessions going, and two buttons to click to start things instead of one, though i need to restart gwt dev mode more than the server.Colin Alworth
Thanks! I'll definitely try that if I can't it working via Jetty. I suppose an approach like yours, or some maven-based solution, might be cleaner, since it wouldn't depend on the details of GWT internals (like which servlet container dev mode uses). A patch or feature request to GWT for a cleaner way to configure the dev mode server might be in order, too, I guess.Andrew

2 Answers

0
votes

The approach I like to use is to write a simple Servlet which can stream any file, similar to: http://www.exampledepot.com/egs/javax.servlet/GetImage.html, and map it in my web.xml to certain URL patterns.

Then I can easily make the base directory for the static files configurable via one of my configuration files. I can also do arbitrary stuff like mixing several directories together, or getting the file contents from a DB, ...

I prefer this approach, because it works with all servlet containers in any environment.

-1
votes

My working one:

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

<Configure class="org.mortbay.jetty.webapp.WebAppContext">
        <New id="sampleDS" class="org.mortbay.jetty.plus.naming.Resource">            
            <Arg>java:jboss/datasources/sampleDS</Arg>
            <Arg>
                 <New class="oracle.jdbc.pool.OracleConnectionPoolDataSource">
                 <Set name="URL">jdbc:oracle:thin:@127.0.0.1:1521:ORCL</Set>                 
                 <Set name="User">user</Set>
                 <Set name="Password">pass</Set>
                 </New>
            </Arg>
        </New>
</Configure>
  1. Put it under WEB-INF
  2. Download [jetty-naming-6.1.12.jar][1] and [jetty-plus-6.1.12.jar][2] and put them under `WEB-INF\lib'

Restart eclipse and run again should do the trick.