Nicozan-Blog

Knowledge Seeking

GWT- Time to Refactor means Async Interfaces Issue April 7, 2011

Filed under: GWT,Java — nicozan @ 4:43 pm
Tags: , , , ,

Introduction

When you first create a GWT App, the project is divided ,usually, into three packages: client,shared and server. In this post we’ll consider the next packages names:
-com.wordpress.nicozan.client
-com.wordpress.nicozan.shared
-com.wordpress.nicozan.server

Refactoring Services

Let’s consider that in the package com.wordpress.nicozan.client we have the interface bankService and we have too the interface bankServiceAsync. This Async-interface help us to communicate with the server side of our application. The server side is in the package com.wordpress.nicozan.server and contains an implementation named bankServiceImpl.
Up to now everything seems nice and simple, but if your application starts to grow and improve, you’ll probably have many services. This means that your client package we’ll have several service interfaces mixed with your widgets and utility classes.
So to avoid having everything mixed up, you decide to create the next packages:
-com.wordpress.nicozan.client.util
-com.wordpress.nicozan.client.widgets
-com.wordpress.nicozan.client.services

This post will focus in the services package. Inside this package we’ll put the service interfaces that are related to the Async interfaces.
At the moment we compile (mvn clean install ), an error will appear. The error will say that the Async interfaces cannot be found. This problem is because by default the gwt-maven-plugin generates the Async interfaces from the service interfaces located in the client package, and it doesn’t look inside the client sub-packages. The way to solve this problem is by adding 2 tags to the plugin configuration inside the pom.xml:

...
<!-- GWT Maven Plugin -->
 <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>gwt-maven-plugin</artifactId>
       <version>2.1.0-1</version>
           <executions>
                 <execution>
                       <goals>
                             <goal>compile</goal>
                             <goal>test</goal>
                             <goal>i18n</goal>
                             <goal>generateAsync</goal>
                       </goals>
                 </execution>
           </executions>
       <!-- Plugin configuration. There are many available options, see
         gwt-maven-plugin documentation at codehaus.org -->
      <configuration>
         <runTarget>MyWebApp.html</runTarget>
         <hostedWebapp>${webappDirectory}</hostedWebapp>
          <i18nMessagesBundle>com.wordpress.nicozan.client.Messages</i18nMessagesBundle>  
         <servicePattern>**/client/services/*Service.java</servicePattern>
         <inplace>true</inplace>
       </configuration>
 </plugin>
...

In the code snippet above, the two important tags are servicePattern and inplace. In the first one, we specify a relative path to where service interfaces are. The second tag, specifies that the generated Async interface have to be located in the same package as its analog service interface.