Monday, September 24, 2012

Apache Cordova (Phonegap) and the BarcodeScanner Plugin for Android

Preface
This documentation relates to following versions:
Cordova 2.0.0

For one of my projects I have to use the BarcodeScanner Plugin for Android. I had some problems getting it to work so I describe what had to be done in detail.

Create a new Android Project
First I created a new Android Project called Testbarcode according to the following documentation:

http://docs.phonegap.com/en/2.0.0/guide_getting-started_android_index.md.html#Getting%20Started%20with%20Android

Import the LibProject
Then I imported the LibProject as seperate project and turned it in to a Library Project in the Eclipse application properties.

Use the LibProject in the Testbarcode Project
I added the LibProject in to my Testbarcode project according to the description but there are some differences.

The difference is that in newer versions the file in res/xml is not called plugins.xml anymore but config.xml. So the following line:

<plugin name="BarcodeScanner" value="com.phonegap.plugins.barcodescanner.BarcodeScanner"/>

has to be added to config.xml and not plugins.xml. I received a class not found error.

https://github.com/phonegap/phonegap-plugins/tree/master/Android/BarcodeScanner

Simon Mac Donald has described this on his blog:
http://simonmacdonald.blogspot.de/2012/07/updates-to-plugins-for-phonegap-200.html

Using the index.html from Simon Mac Donald for testing
I have used the example index.html from Simon Mac Donalds Blog and changed the js from phonegap-1.2.0.js to cordova-2.0.0.js.

http://simonmacdonald.blogspot.ch/2011/12/installing-barcode-plugin-for-phonegap.html

That was it. I could scan barcodes.

Wednesday, August 29, 2012

Keeping "DRY" with Classtemplater

Since I have been starting with REST jax-rs and tests I always had to repeat coding DAOs and Tests. This was extremely unproductive. I have been looking in to Spring Roo and some Eclipse plugins.

Spring Roo was too large and complex and I would have had to re-create the whole project according to the Spring Roo and Maven guidelines. Also I didn't know if GWTP is supported. I didn't want to learn yet another Framework and start from scratch again even if Spring Roo looks very promising and seems to support the creation of GWT. But I just found it to be to complicated and less flexible as I wanted it to be. 

All the other Eclipse Plugins I have been looking at also didn't support all features that I wanted until I stumbled over a plugin called Classtemplater.

Classtemplater is using Apache Velocity which is a scripting language to generate customized configuration files. I have already worked with Velocity and got interested. And..... it is extremely easy and yet f*ing brilliant. Basically the whole code for your DAOs, Tests and and other Classes that rely on to the Entity Class (or any other class but the entity class is the most convenient one) can be generated with Velocity templates.

If you want to get started read the following documentation:
In Classtemplater you define a template that you create from a class. In my example I want to create a CardResourceTests.class based on the Card.class (the entity class that contains all the getter and setter methods and attributes). So you create the Card.class first. Then you create the template called for example resourceTestsCRUD.vm which you put in to a folder in the root of your project. Mine is called "templates".

Once you install the Eclipse Plugin (over Help -> Install New Software -> http://classtemplater.inepex.com) you can right click on to the entity class (in my example Card.class), choose "classtemplator" and click on to "Generate". This will open a menu where you can choose the corresponding .vm template (in my example resourceTestsCRUD.vm).

Classtemplater specific Velocity variables
Print Methods
If you want to get all the methods from a class you are using the .vm template with you can do following:
#foreach( $method in $class.methods )
${method.name} - ${method.returnType}
#end
 
This will print the method name and the return type of all methods.  
 
Print attributes
#foreach( $attr in $attrs )
${attr.name} - ${attr.type}
#end

This will print all the attributes and types of the attributes.

This is actually all you need. The rest is done with Velocity.

Example
In my example we'll be creating a Card.class entity with some attributes. We will use this entity class together with a resourceTestsCrud.vm Velocity template to create the code of the CardResourceTests.class.

Card.class:
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@PersistenceCapable(detachable="true")
public class Card {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extenstion(vendorName="datanucleus",  key="gae.encoded-pk", value="true")
    private String key;
    
    @Persistent
    private String name;
    
    @Persistent
    private Date creationDate;    
    
    @Persistent
    private Date modificationDate;
    
    public Card() {
        this.setName(null);
        this.setCreationDate(null);
        this.setModificationDate(null);
    }
    
    public Card(String name)
        this.setName(name);
        this.setCreationDate(new Date());
        this.setModificationDate(new Date());
    }

    public Long getKey() {
        return key;
    }

    public void setKey(Long key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getModificationDate() {
        return modificationDate;
    }

    public void setModificationDate(Date modificationDate) {
        this.modificationDate = modificationDate;
    }
}

resourceTestsCRUD.vm:
package ${package};

public class ${classname}ResourceTests {
    public void testCreate() throws Exception {
        Client client = Client.create();
       
        ${classname} ${classname.toLowerCase()} = new ${classname}();
#foreach($attr in $attrs)   
#if(${attr.name} != "key" && ${attr.name} != "creationDate" && ${attr.name} != "modificationDate")
        ${classname.toLowerCase()}.set${attr.name.substring(0,1).toUpperCase()}${attr.name.substring(1)} = setme(${attr.type});
#end
#end

        WebResource ${classname.toLowerCase()}Resource = client.resource("http://localhost:8888/api/${classname.toLowerCase()}");
        ClientResponse response = ${classname.toLowerCase()}Resource.type(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .post(ClientResponse.class, ${classname.toLowerCase()});
           
        ${classname} ${classname.toLowerCase()}Returned = (${classname})respone.getEntity(${classname}.class);
       
        assertEquals("Status", response.getStatus(), 200);
#foreach($attr in $attrs)   
#if(${attr.name} != "key" && ${attr.name} != "creationDate" && ${attr.name} != "modificationDate")
        assertEquals("${attr.name}", ${classname.toLowerCase()}.get${attr.name.substring(0,1).toUpperCase()}${attr.name.substring(1)}(), ${classname.toLowerCase()}Returned.get${attr.name.substring(0,1).toUpperCase()}${attr.name.substring(1)}());
#end
#end       
    }
}

Now we can right click on to the Card.class -> Choose "classtemplator" -> "Generate" and then choose the "resourceTestsCRUD.vm" template.

The code that appears in the preview that now could be saved as "CardResourceTests.class" looks as following:
package path.to.my.package;

public class CardResourceTests {
    public void testCreate() throws Exception {
        Client client = Client.create();
       
        Card card = new Card();
        card.setName = setme(String);

        WebResource cardResource = client.resource("http://localhost:8888/api/card");
        ClientResponse response = cardResource.type(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .post(ClientResponse.class, card);
           
        Card cardReturned = (Card)respone.getEntity(Card.class);
       
        assertEquals("Status", response.getStatus(), 200);
        assertEquals("name", card.getName(), cardReturned.getName());
    }
}

Sunday, August 26, 2012

AppEngine Datastore REST and Key in Entities

To begin with I have worked with AppEngine Keys but found out during testing that this won't work. The key can't be converted and returned with the jax-rs Response class as in general only Java types (int, string....) can be returned.

Example:
@Path("/card")
public class CardResource {
    CardDAO dao = new CardDAO();

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response create(Card card) {
        Card c = dao.create(card);
        if(c.equals(null)) {
            return Response.status(Status.BAD_REQUEST)
                    .type(MediaType.APPLICATION_JSON)
                    .entity("Create failed!")
                    .build();
        }
      
        return Response.status(Status.OK)
                .entity(c)
                .type(MediaType.APPLICATION_JSON)
                .build();
    }
}

The Response class will return the entity as a JSON object in this case. But the key is returned as "null" value when using the AppEngine Key class.

There are two solutions to this problem:

Solution 1
Work with Long instead of Key. This is fine if you want to use a number as key and don't have to mix it with data that could have an identical key.

Here a sample entity:
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@PersistenceCapable(detachable="true")
public class Card {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long key;
    
    @Persistent
    private String name;
    
    @Persistent
    private Date creationDate;    
    
    @Persistent
    private Date modificationDate;
    
    public Card() {
        this.setName(null);
        this.setCreationDate(null);
        this.setModificationDate(null);
    }
    
    public Card(String name)
        this.setName(name);
        this.setCreationDate(new Date());
        this.setModificationDate(new Date());
    }

    public Long getKey() {
        return key;
    }

    public void setKey(Long key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getModificationDate() {
        return modificationDate;
    }

    public void setModificationDate(Date modificationDate) {
        this.modificationDate = modificationDate;
    }
}

Solution2
Work with Encoded String as Key. This solution is the prefered one as the generated string is a random string that does not conflict with any other id of a different data set if you have to mix the data together with other data created elsewhere.

Here a sample entity:
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@PersistenceCapable(detachable="true")
public class Card {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extenstion(vendorName="datanucleus",  key="gae.encoded-pk", value="true")
    private String key;
    
    @Persistent
    private String name;
    
    @Persistent
    private Date creationDate;    
    
    @Persistent
    private Date modificationDate;
    
    public Card() {
        this.setName(null);
        this.setCreationDate(null);
        this.setModificationDate(null);
    }
    
    public Card(String name)
        this.setName(name);
        this.setCreationDate(new Date());
        this.setModificationDate(new Date());
    }

    public Long getKey() {
        return key;
    }

    public void setKey(Long key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getModificationDate() {
        return modificationDate;
    }

    public void setModificationDate(Date modificationDate) {
        this.modificationDate = modificationDate;
    }
}

Sunday, July 8, 2012

Unit Testing REST with Jersey and AppEngine

I have read many articles about Unit Testing REST and JSON including much examples. I have been guided to use Grizzly or another webserver to test if REST is working on the Jersey Website. They load and stop the webcontainer over the @Before and @After annotations. I have tried that using the Datastore with AppEngine but it didn't work. Reason was that the webservers are spawning multiple threads which is not allowed in the AppEngine Development environment. The problem is that the threaded webservers will not find the datastore that is created during tests. I also tried to use the Jersey Test Framework which has not played nicely together with AppEngine out of the same reasons.

I have created a post on Stackoverflow and got the answer, that first of all using a webserver is rather considered as an Integration Test not a Unit Test and that it makes sense in AppEngine to execute the method in the resources class directly for testing purposes. During my studies how to accomplish this I have noticed however that when I use the Response class I can't seem to get back an entity using the getEntity() method in my test class. When debugging I could see that the response contained the content of the object but I was unable to get the content using the getEntity() method from the Response class. So I made another post on Google Groups and got the answer that I could Mock the response.

I didn't try mocking yet and went directly to the integration testing by starting up the development environment because I wanted to see if it works first when I use the ClientResponse class to get the JSON response in to my Card object. And it worked. So my current suggestion on this is to start the webserver for the REST tests. I may try mocking once I have some time.

Here a create example:

CardResource class:
@Path("/card")
public class CardResource {
    CardDAO dao = new CardDAO();

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response create(Card card) {
        Card c = dao.create(card);
        if(c.equals(null)) {
            return Response.status(Status.BAD_REQUEST).entity("Create failed!").build();
        }
       
        return Response.status(Status.OK)
                .entity(c)
                .type(MediaType.APPLICATION_JSON)
                .build();
    }
}

CardResourceTests class (Junit):
public class CardResourceTests {   
    @Test
    public void testCreate() throws Exception {
        Client client = Client.create();
        WebResource webResource = client.resource("http://localhost:8888/api/card");
       
        Card c = new Card(1, "Cardname", "12345678", 1, 1,
                "cardname.jpg", new Date(), new Date());         
       
        ClientResponse s = webResource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, c);
       
        Card r = (Card)s.getEntity(Card.class);
       
        assertEquals("Result", r.getName(), "Cardname");
    }
}

Friday, June 8, 2012

Implementing Junit in to a GWT application

Download JUnit
Download latest junit.jar and copy it to the war/WEB-INF/lib directory.

Add Libraries to the Classpath
  • Right click on to the application in Eclipse
  • Choose Properties
  • Choose Java Build Path
  • Choose Libraries
  • Click on to Add Jar 
  • Select the junit.jar file
  • Click Ok to add them to the build path

Create a folder for tests
It is best practice to put tests in to a seperate directory outside the source folder but still inside the project folder. I created a different source folder called "tests" inside my project folder and added the same java package. If your java package inside the src folder is "com.yourcompany.yourapp", you can create the same java package in the tests folder. I use following java package path: "com.yourcompany.yourapp.junit" to put all my junit tests.

  • Right click on to your application folder within Eclipse
  • Choose New -> Source Folder
  • Name the folder tests
  • Then right click on to the tests source folder
  • Choose New -> Package
  • Name the package com.yourcompany.yourapp.junit
  • Click OK

That's it. You are ready to write tests.

Create a sample test case
Next we will write a simple test to see if everything is working.

  • Right click on to the com.yourcompany.yourapp.junit package
  • Choose New -> Class
  • Name the class MyFirstTest.java
  • Click OK

Add the following code to the MyFirstTest.java class:

MyFirstTest.java
package com.yourcompany.yourapp.junit;

import static org.junit.Assert.*;
import org.junit.Test;

public class MyFirstTest {
    @Test
    public void testAddition() {
        assertEquals(4, 2 + 2);
    }
}



Run the JUnit Test

  • Right click on to your application folder within Eclipse
  • Choose Run as -> JUnit Test

Eclipse should open a new window and display the results of the JUnit test.


Tuesday, May 29, 2012

Some Errors / Solutions down the way

appengine-web.xml does not contain a <threadsafe> element
Error
SEVERE: Received exception processing C:\Users\CA-DT\Documents\Eclipse Projects\loyaltycard\war\WEB-INF/appengine-web.xml
com.google.apphosting.utils.config.AppEngineConfigException: appengine-web.xml does not contain a <threadsafe> element.
See http://code.google.com/appengine/docs/java/config/appconfig.html#Using_Concurrent_Requests for more information.
You probably want to enable concurrent requests.

Description
http://code.google.com/appengine/docs/java/config/appconfig.html#Using_Concurrent_Requests

Solution
Add <threadsafe>true</threadsafe> to your war/WEB-INF/appengine-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

    <application></application>
    <version>1</version>
    <threadsafe>true</threadsafe>


WARNING: failed _ah_ServeBlobFilter: java.lang.ClassCastException
Error 
WARNING: failed _ah_ServeBlobFilter: java.lang.ClassCastException: com.google.appengine.tools.development.ApiProxyLocalImpl cannot be cast to com.google.appengine.tools.development.ApiProxyLocal

Description
  • appengine-api-labs 
  • appengine-api-stubs 
  • appengine-testing

The above mentioned files shouldn't be in the war/WEB-INF/lib directory. They are only thought as test libraries needed during Unit tests. 

Solution
  • Delete the files from the classpath.
  • Put the files in to another location such as c:\GWTTestLibs.
  • Add the files to the classpath as External Jars

The App Engine SDK 'C:\<workspace path>\<project name>\war' on the project's build path is not valid
Error
The App Engine SDK 'C:\<workspace path>\<project name>\war' on the project's build path is not valid

Description
There is a problem in the latest GAE plugin which prevents this from working. If you get an error like "The App Engine SDK 'C:\<workspace path>\<project name>\war' on the project's build path is not valid" proceed with following solution below.

Solution
  1. Open preferences -> Google -> App Engine and select the sdk version again
  2. Open preference -> Java Build Path -> Export Order. Change the Export order so that the App Engine SDK is ABOVE the "Web App Libraries" classpath container.

Implementing Jersey (JAX-RS) in to a GWT application

Goal
I want to build my GWT project using a Jersey (JAX-RS) backend to decouple the Client from the Server and to allow different kind of Clients (developed in all kind of languages) to connect to my application

This tutorial is based on Eclipse as it is the only client supported by GWT currently. I have read that there can be difficulties using Maven. Hence these instructions are following the manual (official) way how to add third party server side libraries.

Create a new GWTP Project
First thing you have to do is to create a new GWTP (GWT-Platform) project. Don't tick the creation of sample code.

Add Jersey (and dependent) Libraries
The following libraries have to be downloaded and added to the war/WEB-INF/lib directory:
It is important to take the same Versions for the jersey-core, jersey-server and jersey-json JARs. Otherwise you can get errors during execution.

Add Libraries to the Classpath
  • Right click on to the application in Eclipse
  • Choose Properties
  • Choose Java Build Path
  • Choose Libraries
  • Click on to Add Jar 
  • Select all the Jar files from above (jersey-core, jersey-server, jersey-json and asm)
  • Click Ok to add them to the build path

Create a new Resources Java Package
  • Right click on to the src folder within Eclipse
  • Choose New -> Package
  • Insert your Package name: com.yourcompany.yourapp.resources

Add necessary lines to web.xml
You need to add some lines to web.xml. You need to initialize some Jersey related stuff but what is especially important that you add the package path and the url-pattern.

When starting your GWT application in it's default configuration you should be able to execute a servlet over the path http://127.0.0.1:8888/api/<Resource>.

web.xml
    <!--
        The below lines are implementing Jersey (JAX-RS) in to the GWT application.
     -->
     <servlet>
        <servlet-name>Jersey</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
            <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
        </init-param>       
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.yourcompany.yourapp.resources</param-value>
        </init-param>        
    </servlet>
   
    <servlet-mapping>
        <servlet-name>Jersey</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

Create a Hello Resource
Right click on to the package com.yourcompany.yourapp.resources
Choose New -> Class
Name your Class HelloResource.java

HelloResource.java
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
        return "Hello Jersey";
    }
}

Once finished you should be able to run the application as Webapplication.
Right click on to your application
Choose Run As -> Google Web Application

Please note that the default URL will not work as there haven't been any GWT Presenters implemented. But you should now be able to open following URL: http://127.0.0.1:8888/api/hello.


Sunday, April 29, 2012

Getting Started With GWTP

I have basically no knowledge yet with GWT and GWTP. The first time when I peaked in to GWT the documentation still lacked and it was difficult to find information. Especially about using GWTP. On my second search I have found a really good tutorial of the GWT and GWTP basic features by Dani from Cloud3 Tech. Below you can find the course videos in order. The course is fantastic and gives a good overview on how to do things.

Lesson 1: Introduction
Lesson 2: First GWT Project
Lesson 3: Working with the Presenter
Lesson 4: Nested Presenters and Presenter Widgets
Lesson 5: Popup Dialogs
Lesson 6: Dependency Injection
Lesson 7: Events
Lesson 8: Client Place Manager
Lesson 9: Actions

The lessons however do not cover:
  • Complex Authentication and Authorization
  • File Upload
  • Using REST with a Jersey JAX-RS Server
  • Using Cassandra or AppEngine as backend
 This will still involve some studies.

Wednesday, April 18, 2012

Setting up a new GWT Web Project with Eclipse

Download and Install Eclipse
Official Site: http://www.eclipse.org/

You can download Eclipse from the Eclipse download page (currently Indigo):
http://www.eclipse.org/downloads/

I suggest that you download the Eclipse IDE for Java EE Developers. This version contains everything you need.

Copy Eclipse to Program Files Folder
Once the zipfile is downloaded open it and copy the contents (the eclipse directory) in to your C:\Windows\Program Files folder or your prefered location for the Eclipse binaries.

I usually then click on to the eclipse.exe binary and move it to my taskbar to have it available.

Once finished start Eclipse. The first time you start Eclipse it will ask you where to put your projects. I usually create a folder in Documents named Eclipse Projects.

Install Android SDK
Official Site:  http://developer.android.com/index.html

The GWT application is spitting out an error about missing dependencies when you want to use the Android GWT libraries. Therefore it is recommended to install the Android SDK first. If you don't want to do that you can simply untick the Android GWT libraries.

Download the Android SDK from following URL:
http://developer.android.com/sdk/index.html

I have downloaded the Windows Installer executed it and clicked through the menus.

As next step it is required to install the ADT plugin. The ADT (Android Development Tools) plugin is connecting the Eclipse editor with the SDK.

Click on to Help -> Install new Software

Paste following URL in to the Work with field:
https://dl-ssl.google.com/android/eclipse/
 
Click on to the Add button

Enter  ADT Plugin as name and click Ok

Tick Developer Tools and click Next


You see some details click Next


Accept the Terms and click Finish


In the middle of the installation you'll be warned that the plugin is not signed. This is fine. Just click Ok

You will be asked to restart Eclipse. Click the Restart now button


When finished Eclipse will restart and after the restart you'll have to choose which SDK to take. I just took the newest one. Click Next.

I don't want to send Usage Statistics to Google. So I disabled it. Click Finish.


Click Install to install all the libraries.


Install Google AppEngine & GWT & SDKs
Official Site AppEngine: https://developers.google.com/appengine/
Official Site GWT: https://developers.google.com/web-toolkit/

Next step is to install Google AppEngine. If you haven't done so sign up to AppEngine first using the following link:
https://developers.google.com/appengine/

Download the Google AppEngine SDK
Download the Google AppEngine SDK and unzip it on to your hard drive. Doesn't matter where. Usually I keep all my Java libraries like JRE and JDK in a folder called Java within the Program Files folder. This is also the location to where I put the AppEngine SDK.

Now it's time to install the AppEngine & GWT plugins. For this it's the same procedure again as above.

Click on to Help -> Install new Software
Paste following URL in to the Work with field:
http://dl.google.com/eclipse/plugin/3.7

Click on to the Add button

Enter  GWT as name and click Ok

Tick all the libraries and click Next


You see some information. Click Next


Accept the Terms and click Finish


In the middle of the installation you'll be warned that the plugin is not signed. This is fine. Just click Ok


You will be asked to restart Eclipse. Click the Restart now button


Once Eclipse has restarted you can register with your AppEngine (Gmail account)


Once you have signed in successfully this part is completed and we can move on.

Install GWTP (gwt-platform)
Official site: http://code.google.com/p/gwt-platform/

A very nice addon to GWT is the GWTP plugin. This will help you to create Model-View-Presenter based code. It basically sets up all the wiring between classes for you.

Click on to Help -> Install new Software
Paste following URL in to the Work with field:
http://plugin.gwt-platform.googlecode.com/hg/update

Click on to the Add button

Enter GWTP as name and click Ok

Tick all the libraries and click Next


You see some information. Click Next


Accept the Terms and click Finish


In the middle of the installation you'll be warned that the plugin is not signed. This is fine. Just click Ok


You will be asked to restart Eclipse. Click the Restart now button 


There is a very nice Video to get you started with GWTP:

Some nice Eclipse Shortcuts
Ctrl + Shift + R - This will open a search panel. Search for classes to open them all in Eclipse.

Which Editor to use for GWT

Eclipse
Google is suggesting to use Eclipse as editor. Google has written Plugins for Eclipse which makes it more easy to use GWT. Also there are other plugins such as the GWTP plugin that automatically adds Model/View/Presenter based classes and dependencies. Please note that the GWT Designer can only be used when you are not using UiBinder and want to use code to create the GUI. GWT Designer currently isn't supporting UiBinder yet.

So if you look for a more advanced solution and one that will always be supported by Google it is recommended (also by me) to use Eclipse.


Netbeans
Netbeans is a very advanced really easy to use editor. I prefer to work with Netbeans as it is more intuitive then Eclipse. It is a petty that the GWT and AppEngine plugins are far not as mature as the Eclipse plugins. But for those who want to walk further with Netbeans I have written a basic tutorial how to use the plugins in Netbeans 7.1.1.

Setting up a new GWT Maven Web Project with Netbeans

Download and install Netbeans
You can download Netbeans from Netbeans site.

Install the GWT4NB Plugin
You can download the GWT4NB Plugin from GWT4NB download page.
  • Once downloaded open Netbeans, click on to Tools -> Plugins
  • Click on to the Downloads tab and then on to Add Plugins
  • Choose the folder where you have downloaded the plugin in to and click on to Open


  • You'll see that the plugin has been recogniced and that you can install it now.
  • Click on to the Install button


You will be asked to agree to the terms of use and you'll see a message that the plugin isn't signed. This is fine. You can just proceed. After that the plugin should be installed. You should be able to see it when you click on to the Installed tab.

Install the Google AppEngine Plugins
Currently Kenai only provide plugins for Netbeans 6.9. It is however possible to install those also with Netbeans 7.1 and 7.1.1.

You can find the plugins for the 6.9 Version by clicking on the below URL:
http://kenai.com/downloads/nbappengine/NetBeans69/

I assume that you are familiar with the Plugin install process. You can install the plugins the same way as the GWT plugin.

I have installed the following 3 plugins:
Download the Google AppEngine SDK
Download the Google AppEngine SDK and unzip it on to your hard drive. Doesn't matter where. Usually I keep all my Java libraries like JRE and JDK in a folder called Java within the Program Files folder. This is also the location to where I put the AppEngine SDK.

Adding the Google AppEngine Server
Next step is to add the Google AppEngine Server to Netbeans.
  • Click on to Tools -> Servers 
  • Then click on to Add Server
  • Choose Google App Engine and click Next 



  • Now choose the Google AppEngine SDK folder you just unzipped and click Next

  • I changed the port from 8080 to 8090 because the Glassfish server is using 8080.
  • Then click Finish
Now you should see your Google AppEngine Server in the list of servers.


You should now be able to start coding.

Sunday, April 15, 2012

How to start a new Java Web Project

My background
I'm currently working as external consultant for a bank in which I consult customers how to integrate their services on the J2EE platform. I'm not a fully fledged developer but have a bright knowledge about large scale datacenters and hosting platforms from a technical/architectural and also from a management perspective. I have also developed some small apps in the past to keep my technical knowledge up to date. Lately while reading some docs I discovered that much has changed in the java world outside the large/slow new technology adopting industries.
Also the way of using database queries seems to change moving away from traditional join patterns which keeps all data seperated combining them to one query through joins to optimized query patterns that can deliver the data from one "table" with the compromise having redundant data in different "table" like structures. In Cassandra for example these structures are called Colomn, ColumnFamilies and SuperColumns.
I have been thinking about how I would start a new fully modern Java web project from scratch and which technologies would fit in todays demands and best practices. And that's where my journey through the new technology jungle started.
Requirements to my new web project
My requirements to a new application:
  • I want to use the new modern based like databases such as Cassandra or AppEngine to study the new kind of data modelling methods that allows me to grow to whatever data volume and replication is needed based on a master/master database concept.
  • I want to have an interactive Ajax based frontend that can respond as a web page and also as a mobile app giving the user a rich and easy to use interface.
  • I want to use Java as I'll need Java background for my future activities as a consultant 
Based on these requirements the below possible candidates of frontends/backends have emerged.
    Frontends/Backends - Possible Candidates
    I have been looking at following possible candidates:
    • JSF / J2EE Beans / Cassandra
    • backbone.js / Jersey REST / Cassandra
    • GWT / Jersey REST / Cassandra or AppEngine
    JSF / J2EE Beans / Cassandra
    JSF (Java Server Faces) is currently the standard used in J2EE Java development. It is a very nice and simple MVC (Model View Controller) framework which I personally really like. JSF has all the comforts of a server side language and if you know the concepts of MVC it'll take no time to build a very nice looking frontend. The integration with Managed Beans is also very simple and straight forward. There are also very good Ajax libraries out there. I have been looking at the amazing simple Primefaces. Also JSF is extremely well documented with examples to find everywhere.
    This has been my first choice because of the low learning curve coming from a background in Ruby on Rails.
    I have started to write a small JSF application and wanted to integrate Cassandra as my backend database. So next question was which driver to take. For Java I have discovered following potential candidates:
    • Hector (currently the most active community and advanced client)
    • Kundera (a full JPA integration and also very active community)
    • CQL that aims to get the new standard which will support JDBC in future (Support is currently only given for Datastax enterprise customers also when writing to the community mailing list. As I am no enterprise user this solution was not yet usable for me as I want to learn how to do things by myself.
    Cassandra is currently stable and used by large companies around the world but still very young and there are many different individuals writing different kind of drivers which still lack different kind of features. But it's getting there. A big lack with all these drivers is the documentation. When I talk about documentation I don't mean the Javadocs. Of course this is available. But the real world examples explaining how to do things in mid- to large size projects are fully missing. This is what I loved in the past about Ruby on Rails. You could always rely on Railscasts. But now I'm bound to Java and that's what I want to use. And at least the people in the mailing lists of Hector and Kundera are very responsive and helpful.
    I have managed to get the Kundera JPA running very quickly which perfectly integrated with Managed Beans. I have however been pointed out by the people from the Hector mailing list that this will eat much payload and is therefore not recommended for a lightweight environment. However I think for J2EE extrimists this is the way to go.
    Pros
    • Fully J2EE compatible with Kundera JPA integration
    • J2EE standards can be used
    • Only Java knowledge is required 
    • Well documented
    • SEO is taken care of
    Cons
    • High Payload
    • Can only be used with Java (Serialization J2EE) when not using REST
    On the other hand I got the advice that one should fully decouple the frontend from the backend, best using REST/JSON. So for Java nearly all of the people in the Hector mailing list pointed out that it's best practice currently using Jersey (JAX-RS). I was also introduced to new JavaScript based frontends like backbone.js that would enable me to write fully independent frontends to the backend which makes sense as this method enables us to build any kind of client applications. This way regardless if you use a JavaFX, Flash, Javascript or another type of frontend it will be able to store and load information over REST. This brought me to the next adventure with backbone.js.
    backbone.js / Jersey REST / Cassandra
    With the background from the JSF journey I have been taking a look at backbone.js (used for MVC based Javascript) that integrates with underscore.js (for additional functionality) and jQuery. I was asking myself how I would structure large applications and found require.js and following article. I followed the article and got a fully working example (without Jersey). I have also read following article regarding jQuery integration and Jersey. I however never got the example working properly and have also not been able to integrate backbone.js and require.js to work with this example. Reason is that I have only basic knowledge about JavaScript development.
    Debugging in general worked fine with FireBug but I got to the point where only the HTML was displayed and there were no JavaScript errors at all (how would I debug without seeing any errors?). I spent a lot (at least for me too much) time to debug problems.
    My personal impression is that backbone.js is a really well thought JavaScript variant of a MVC application which includes all the necessary pieces but with all the dependencies like underscore.js, require.js, jQuery.js makes it difficult to maintain for somebody without really deep JavaScript knowledge. Also jQuery and require.js are developed independently which could lead to compliance issues. It would be nice to see one common well documented JavaScript framework arise out of all these different scripts which is well structured in to the same direction with common best practices and possibilites to update to new versions with everything integrated to it. I think this could give it a big boost. Especially with "PHP" or "Ruby on Rails" like documentation and samples.
    Pros
    • A very light weight and flexible and fast way to code rich interfaces
    • Practicaly no limits as it is pure JavaScript
    • Can be used for any kind of mobile and web based apps
    • Well integrates with REST/JSON based server side apps
    • Partly well documented
    Cons
    • Harder to debug problems as it depends on individual libraries
    • Much knowledge in JavaScript is required
    • Different browsers may need to be handled individually within the JavaScript code
    • SEO has to be handled over Snapshots
    Because of the issues I had I was not at the end of my studies and some other nights brought me to GWT (Google Web Toolkit) which is pure Java that will be converted in to JavaScript. It is one framework not depending on many libraries. It supports different permutations for all browsers and it's Java. So the learning curve is high to learn the usage of the framework but lower then learning JavaScript.
    GWT / Jersey REST / Cassandra or AppEngine
    So my conclusion (which is a personal one based on the conclusions above) is that I want to use GWT with a Jersey REST backend and Cassandra or AppEngine database.
    One of the big problems when having the data stored on supplier platforms such as Google is that the suppliers usually don't provide much support moving data away in case a supplier change is necessary. I however have to say that much time/resources can be saved if a supplier is managing the whole infrastructure with data replication.
    The good thing with REST interfaces is that the data could actually be exported over REST and inserted in to a new database such as Cassandra. With this in mind I'm not in a big worry anymore about storing my data to AppEngine. The good thing about AppEngine is that my data will automatically be replicated to other datacentres and I don't need to concentrate on Servers/Database maintenance and replication. So with this in mind the REST interfaces have to be structured for the "worst case scenario" and a plan has to be made accordingly how to move the application and data to a provider independent infrastructure.
    From a GWT side as GWT is generating JavaScript it will also run on any other Google independent platform.
    Pros
    • One framework to handle JavaScript and all different browsers
    • Only Java knowledge is required
    • The permutation to different browser is done automatically
    Cons
    • It takes some time to learn the framework
    • Using AppEngine is provider dependent
    • SEO has to be handled over Snapshots 
    • JSNI may need to be used for additional non-existing JavaScript functionality
    • Java knowledge is required
    My technology stack in the end... Spring & jQuery & MongoDB

    I have learnt some important things:
    • Don't get yourself confused with all these hyped technologies. Depending on mature frameworks is usually the best.
    • GWT is great and new and for sure a very capable framework to write web applications. But when it comes to search engine indexing optimization and other stuff it is still very complicated. If you want to create a standard website and don't expect hundred thousands of users in the beginning you don't really need it.
    • Spring is well known for it's bean configuration over DI. Combined with Spring Data that is responsible for database access, Spring Templates to minimize coding and the support also for certain NoSQL Databases it is a powerful backend.
    • Spring MVC is also a very nice and also very robust and stable framework that has full REST support since Version 3.
    • I have tried Spring Roo but decided to not use it. First of all the REST integration didn't work properly with the current version 3.2.1 and as soon you have certain specialities that are not covered by Roo it'll get difficult to maintain code using Roo.
    • Spring would also support integrating GWT or the AppEngine Datastore if required and AppEngine is supporting Spring.
    • By layering the application in to Service, Repository, Domain Architecture it is not too difficult to change the database backend.
    • MongoDB is supported by Spring and not only contains support for small data records but also for files or large data using their GridFS. All data can be distributed over several servers. This makes MongoDB my favorite choice for persistence.
    The question marks with PaaS and IaaS services
    • As a customer of such a service I would like to have cost transparency and control. This however is not possible as most services are calculated on a per hour and usage bases and one would have to run many tests to actually compare if the costs are really cheaper to a platform that is maintained by yourself. I have seen that it takes much time to actually figure out the real costs on a monthly bases and the exponential growth.
    • With Google for example the price of Frontend, Discount and Backend instances is very intransparent. While reading the mailing lists there were also much people complaining that their costs are higher then expected while using the platform in production. For Google of course it's the customers fault because the customers could have developed their application in a different way that is optimized for AppEngine.
    • With Rackspace and Amazone the costs are more transparent when looking the the managed Cloud Database offers but the costs are also comparable high against running infrastructure by yourself.
    • I think it makes sense to evaluate those services properly when enough money is around and a product is already successful. For other products or startups I would still recommend using a single or two servers with backup capabilities depending of course on the demand of the customers.
    To learn Spring and how to connect to MongoDB there are only two books that you need:
    For MongoDB there is enough documentation on their website. For their documentation they earn defenatly an award. Just look at all the cumbersome and whimpy documentations of other tools.