March 9, 2013

Getting code samples from Essential GWT book to work

I bought a book called 'Essential GWT' by Federico Kereki but did not receive a CD for the samples mentioned in the Essential GWT book. I downloaded the samples from here: http://www.informit.com/content/images/9780321705143/downloads/0321705149_Code.zip. Unfortunately, there was not much documentation about the samples or how to compile them. Here, I've noted some information which others find it useful. Note that you need to have GWT plugin for Eclipse installed.

After unzipping, there were these folders:

bookphp  mvpproject  stdserialize

The mvpproject seems to contains the bulk of samples that the book discusses. The source for the samples are contained in the folder 'src' and 'war' contains some web deployment info. To compile and deploy the samples:

1. Create a GWT Web Application Project. Call it 'mvpproject' and set the package name to be 'com.fkereki.mvpproject'. If these are not right, you can an error when you try to deploy saying that *.gwt.xml is not found.

2. Copy the source code that you downloaded to your project through Eclipse:

        BOOK_SOURCE/mvpproject/src/com/fkereki/mvpproject/*
        BOOK_SOURCE/mvpproject/src/com/fkereki/mvpproject/client/*
        BOOK_SOURCE/mvpproject/src/com/fkereki/mvpproject/server/*

4. The project requires the following external jars:

       gwt-ajaxloader.jar
       commons-fileupload-1.2.2.jar
       commons-lang3-3.1.jar           
       mysql-connector-java-5.1.23-bin.jar

5. Modify web.xml and appengine-web.xml in your project as follows:
    a. uncomment <sessions-enabled>true</sessions-enabled> in appengine- web.xml
    b. web.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!-- Servlets -->

    <servlet>
        <servlet-name>loginServlet</servlet-name>
        <servlet-class>com.fkereki.mvpproject.server.LoginServiceImpl</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>loginServlet</servlet-name>
        <url-pattern>/mvpproject/login</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>worldServlet</servlet-name>
        <servlet-class>com.fkereki.mvpproject.server.WorldServiceImpl</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>worldServlet</servlet-name>
        <url-pattern>/mvpproject/world</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>xhrProxyServlet</servlet-name>
        <servlet-class>com.fkereki.mvpproject.server.XhrProxyImpl</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>xhrProxyServlet</servlet-name>
        <url-pattern>/mvpproject/xhrproxy</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>fileProcess</servlet-name>
        <servlet-class>com.fkereki.mvpproject.server.FileProcess</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>fileProcess</servlet-name>
        <url-pattern>/mvpproject/fileprocess</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>fileProduce</servlet-name>
        <servlet-class>com.fkereki.mvpproject.server.FileProduce</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>fileProduce</servlet-name>
        <url-pattern>/mvpproject/fileproduce</url-pattern>
    </servlet-mapping>

  <!-- Default page to serve -->
  <welcome-file-list>
    <welcome-file>Mvpproject.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>SystemServiceServlet</servlet-name>
    <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
    <init-param>
      <param-name>services</param-name>
      <param-value/>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>SystemServiceServlet</servlet-name>
    <url-pattern>/_ah/spi/*</url-pattern>
  </servlet-mapping>

</web-app>

5. You need md5-min.js for some password hashing. It is in the downloaded folder (/BOOK_SOURCE/mvpproject/war/md5-min.js). Copy it to your war folder.

6. Modify your Mvpproject.html as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- The HTML 4.01 Transitional DOCTYPE declaration-->
<!-- above set at the top of the file will set     -->
<!-- the browser's rendering engine into           -->
<!-- "Quirks Mode". Replacing this declaration     -->
<!-- with a "Standards Mode" doctype is supported, -->
<!-- but may lead to some differences in layout.   -->

<html>
<head>
<!-- Note: charset=UTF-8 is required for international applications -->

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--                                                               -->
<!-- Consider inlining CSS to reduce the number of requested files -->
<!--                                                               -->
<link type="text/css" rel="stylesheet" href="Mvpproject.css">

<!--                                           -->
<!-- Any title is fine                         -->
<!--                                           -->
<title>MVP Project</title>

<!--                                           -->
<!-- This script loads your compiled module.   -->
<!-- If you add any GWT meta tags, they must   -->
<!-- be added before this line.                -->
<!--                                           -->
<script type="text/javascript" language="javascript" src="mvpproject/mvpproject.nocache.js"></script>
<!-- 
<script type="text/javascript" language="javascript" src="http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=aasJBXDV34FUcgc.WTwpYWlbhWme9l1xJtF8juZYjwZOTXEwawx5lbzAIKJtgII-"></script>
-->
<script type="text/javascript" language="javascript" src="md5-min.js"></script>
</head>

<!--                                           -->
<!-- The body can have arbitrary html, or      -->
<!-- you can leave the body empty if you want  -->
<!-- to create a completely dynamic UI.        -->
<!--                                           -->
<body>

<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1'
    style="position: absolute; width: 0; height: 0; border: 0"></iframe>

<!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
<noscript>
<div
    style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled in order for this
application to display correctly.</div>
</noscript>
<div id="loading" style="top: 50%; left: 50%;"><img
    src="./ajax-loader.gif" /></div>
</body>
</html>


7. Since I used the new Apache Commans lang (version 3.x) which is different the author's, I modified /src/com/fkereki/mvpproject/server/Security.java to import org.apache.commons.lang3.RandomStringUtils rather than import org.apache.commons.lang.RandomStringUtils.

8. I also had to comment the File IO code in FileProcess.java to avoid this error:

java.io.FileOutputStream is not supported by Google App Engine's Java runtime environment

Et voila, your web application should start without errors (note I did do GWT compile first).